View Javadoc
1   package org.apache.fulcrum.yaafi.framework.context;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.avalon.framework.context.Context;
25  import org.apache.avalon.framework.context.ContextException;
26  import org.apache.avalon.framework.context.DefaultContext;
27  import org.apache.fulcrum.yaafi.framework.constant.AvalonFortressConstants;
28  import org.apache.fulcrum.yaafi.framework.constant.AvalonMerlinConstants;
29  import org.apache.fulcrum.yaafi.framework.constant.AvalonPhoenixConstants;
30  import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
31  import org.apache.fulcrum.yaafi.framework.util.Validate;
32  
33  /**
34   * Helper for converting Avalon Context of Fortress and Phoenix
35   * container to a standard Merlin context.
36   *
37   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
38   */
39  
40  public class AvalonToYaafiContextMapper
41  {
42      /** The directory for storing temporary files */
43      private File tempRootDir;
44  
45      /** Our default context */
46      private DefaultContext defaultContext;
47  
48      /** our defaul class loader */
49      private ClassLoader classLoader;
50  
51      /**
52       * Constructor
53       *
54       * @param tempRootDir current temp directory
55       * @param context the existing context
56       * @param classLoader the classloader
57       */
58      public AvalonToYaafiContextMapper(
59          File tempRootDir,
60          Context context,
61          ClassLoader classLoader)
62      {
63          Validate.notNull( tempRootDir, "tempRootDir" );
64          Validate.notNull( context, "context" );
65          Validate.notNull( classLoader, "classLoader" );
66  
67          this.tempRootDir = tempRootDir;
68          this.classLoader = classLoader;
69  
70          // here we have to create a new DefaultContext since
71          // it contains service specific entries
72  
73          this.defaultContext = new DefaultContext( context );
74      }
75  
76      /**
77       * Map a Avalon context to the YAAFI (Merlin) incarnation whereas
78       * the following containers are supported
79       * <ul>
80       *   <li>merlin</li>
81       *   <li>fortress</li>
82       *   <li>phoenix</li>
83       * </ul>
84       *
85       * @param context the Avalon context to map
86       * @param from Avalon container identifier
87       * @return the mapped Avalon context
88       * @throws ContextException Accessing the context failed
89       */
90      public Context mapFrom( Context context, String from )
91          throws ContextException
92      {
93          Validate.notNull( context, "context" );
94          Validate.notEmpty( from, "from" );
95  
96          if( AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(from) )
97          {
98              return mapFromPhoenix(context);
99  
100         }
101         else if( AvalonFortressConstants.AVALON_CONTAINER_FORTESS.equals(from) )
102         {
103             return mapFromFortress(context);
104 
105         }
106         else if( AvalonMerlinConstants.AVALON_CONTAINER_MERLIN.equals(from) )
107         {
108             return mapFromMerlin(context);
109         }
110         else if( AvalonYaafiConstants.AVALON_CONTAINER_YAAFI.equals(from) )
111         {
112             return mapFromMerlin(context);
113         }
114         else
115         {
116             String msg = "Don't know the following container type : " + from;
117             throw new IllegalArgumentException(msg);
118         }
119     }
120 
121     /**
122      * Map a Avalon Phoenix context to the YAAFI (Merlin) incarnation
123      *
124      * @param context the Avalon context to map
125      * @return the mapped Avalon context
126      * @throws ContextException Accessing the context failed
127      */
128     private Context mapFromPhoenix(Context context)
129         throws ContextException
130     {
131         DefaultContext result = this.getDefaultContext();
132 
133         String urnAvalonName = AvalonYaafiConstants.AVALON_CONTAINER_YAAFI;
134         String urnAvalonPartition = (String) context.get( AvalonPhoenixConstants.PHOENIX_APP_NAME );
135         File urnAvalonHome = (File) context.get( AvalonPhoenixConstants.PHOENIX_APP_HOME );
136         File urnAvalonTemp = this.getTempRootDir();
137 
138         // add the Merlin specific parameters
139 
140         result.put( AvalonYaafiConstants.URN_AVALON_NAME, urnAvalonName );
141         result.put( AvalonYaafiConstants.URN_AVALON_PARTITION, urnAvalonPartition );
142         result.put( AvalonYaafiConstants.URN_AVALON_HOME, urnAvalonHome );
143         result.put( AvalonYaafiConstants.URN_AVALON_TEMP, urnAvalonTemp );
144         result.put( AvalonYaafiConstants.URN_AVALON_CLASSLOADER, this.getClassLoader() );
145 
146         // add the deprecated ECM parameter
147 
148         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
149 
150         // add the Fortress specific parameters
151 
152         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_ID,urnAvalonPartition);
153         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_LOGGER,urnAvalonName);
154         result.put(AvalonFortressConstants.FORTRESS_CONTEXT_ROOT,urnAvalonHome);
155         result.put(AvalonFortressConstants.FORTRESS_IMPL_WORKDIR,urnAvalonTemp);
156 
157         return result;
158     }
159 
160     /**
161      * Map a Avalon Fortress context to the YAAFI (Merlin) incarnation
162      *
163      * @param context the Avalon context to map
164      * @return the mapped Avalon context
165      * @throws ContextException Accessing the context failed
166      */
167     private Context mapFromFortress(Context context)
168         throws ContextException
169     {
170         DefaultContext result = this.getDefaultContext();
171 
172         String urnAvalonPartition = (String) context.get( AvalonFortressConstants.FORTRESS_COMPONENT_ID );
173         File urnAvalonHome = (File) context.get( AvalonFortressConstants.FORTRESS_CONTEXT_ROOT );
174         File urnAvalonTemp = (File) context.get( AvalonFortressConstants.FORTRESS_IMPL_WORKDIR );
175 
176         // add the Merlin specific parameters
177 
178         result.put( AvalonYaafiConstants.URN_AVALON_NAME, AvalonYaafiConstants.AVALON_CONTAINER_YAAFI );
179         result.put( AvalonYaafiConstants.URN_AVALON_PARTITION, urnAvalonPartition );
180         result.put( AvalonYaafiConstants.URN_AVALON_HOME, urnAvalonHome );
181         result.put( AvalonYaafiConstants.URN_AVALON_TEMP, urnAvalonTemp );
182         result.put( AvalonYaafiConstants.URN_AVALON_CLASSLOADER, this.getClassLoader() );
183 
184         // add the deprecated ECM parameter
185 
186         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
187 
188 
189         return result;
190     }
191 
192     /**
193      * Map a Avalon Merlin context to the YAAFI (Merlin) incarnation
194      *
195      * @param context the Avalon context to map
196      * @return the mapped Avalon context
197      * @throws ContextException Accessing the context failed
198      */
199     private Context mapFromMerlin(Context context)
200         throws ContextException
201     {
202         DefaultContext result = this.getDefaultContext();
203 
204         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
205         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
206         File urnAvalonTemp = (File) context.get(AvalonYaafiConstants.URN_AVALON_TEMP);
207         String urnAvalonName = (String) (String) context.get(AvalonYaafiConstants.URN_AVALON_NAME);
208 
209         // add the Fortress specific parameters
210 
211         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_ID,urnAvalonPartition);
212         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_LOGGER,urnAvalonName);
213         result.put(AvalonFortressConstants.FORTRESS_CONTEXT_ROOT,urnAvalonHome);
214         result.put(AvalonFortressConstants.FORTRESS_IMPL_WORKDIR,urnAvalonTemp);
215 
216         // add the deprecated ECM parameter
217 
218         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
219 
220         return result;
221 
222     }
223 
224     /**
225      * @return Returns the classLoader.
226      */
227     private ClassLoader getClassLoader()
228     {
229         return this.classLoader;
230     }
231 
232     /**
233      * @return Returns the defaultContext.
234      */
235     private DefaultContext getDefaultContext()
236     {
237         return this.defaultContext;
238     }
239 
240     /**
241      * @return Returns the tempRootDir.
242      */
243     private File getTempRootDir()
244     {
245         return this.tempRootDir;
246     }
247 }