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 a YAAFI context to a different container
35   *
36   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
37   */
38  
39  public class YaafiToAvalonContextMapper
40  {
41      /** the name of the component for which we create the context */
42      private String urnAvalonName;
43  
44      /** the classloader of the component */
45      private ClassLoader urnAvalonClassLoader;
46  
47      /**
48       * Constructor
49       *
50       * @param urnAvalonName the name of the component for which we create the context
51       * @param urnAvalonClassLoader the classloader of the component
52       */
53      public YaafiToAvalonContextMapper( String urnAvalonName, ClassLoader urnAvalonClassLoader )
54      {
55          Validate.notEmpty( urnAvalonName, "urnAvalonName" );
56          Validate.notNull( urnAvalonClassLoader, "urnAvalonClassLoader" );
57  
58          this.urnAvalonName = urnAvalonName;
59          this.urnAvalonClassLoader = urnAvalonClassLoader;
60      }
61  
62      /**
63       * @return Returns the urnAvalonClassLoader.
64       */
65      public ClassLoader getUrnAvalonClassLoader()
66      {
67          return urnAvalonClassLoader;
68      }
69  
70      /**
71       * @return Returns the urnAvalonName.
72       */
73      public String getUrnAvalonName()
74      {
75          return urnAvalonName;
76      }
77  
78      /**
79       * Map a YAAFI (Merlin) context to a different incarnation
80       *
81       * @param context the context to be mapped
82       * @param to the target Avalon container
83       * @return the mapped context
84       * @throws ContextException accessing the context failed
85       */
86      public DefaultContext mapTo( Context context, String to )
87          throws ContextException
88      {
89          Validate.notNull( context, "context" );
90          Validate.notEmpty( to, "to" );
91  
92          if( AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(to) )
93          {
94              return mapToPhoenix(context);
95  
96          }
97          else if( AvalonFortressConstants.AVALON_CONTAINER_FORTESS.equals(to) )
98          {
99              return mapToFortress(context);
100 
101         }
102         else if( AvalonMerlinConstants.AVALON_CONTAINER_MERLIN.equals(to) )
103         {
104             return mapToMerlin(context);
105         }
106         else if( AvalonYaafiConstants.AVALON_CONTAINER_YAAFI.equals(to) )
107         {
108             return mapToYaafi(context);
109         }
110         else
111         {
112             String msg = "Don't know the following container type : " + to;
113             throw new IllegalArgumentException(msg);
114         }
115     }
116 
117     /**
118      * Map to a Phoenix context
119      *
120      * @param context the original context
121      * @return the mapped context
122      * @throws ContextException accessing the context failed
123      */
124     private DefaultContext mapToPhoenix( Context context )
125         throws ContextException
126     {
127         DefaultContext result = createDefaultContext(context);
128 
129         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
130         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
131         String urnAvalonName = this.getUrnAvalonName();
132 
133         result.put(AvalonPhoenixConstants.PHOENIX_APP_NAME,urnAvalonPartition);
134         result.put(AvalonPhoenixConstants.PHOENIX_APP_HOME,urnAvalonHome);
135         result.put(AvalonPhoenixConstants.PHOENIX_BLOCK_NAME,urnAvalonName);
136 
137         return result;
138     }
139 
140     /**
141      * Map to a Fortress context
142      *
143      * @param context the original context
144      * @return the mapped context
145      * @throws ContextException accessing the context failed
146      */
147     private DefaultContext mapToFortress( Context context )
148         throws ContextException
149     {
150         DefaultContext result = createDefaultContext(context);
151 
152         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
153         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
154         File urnAvalonTemp = (File) context.get(AvalonYaafiConstants.URN_AVALON_TEMP);
155         String urnAvalonName = this.getUrnAvalonName();
156 
157         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_ID,urnAvalonPartition);
158         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_LOGGER,urnAvalonName);
159         result.put(AvalonFortressConstants.FORTRESS_CONTEXT_ROOT,urnAvalonHome);
160         result.put(AvalonFortressConstants.FORTRESS_IMPL_WORKDIR,urnAvalonTemp);
161 
162         return result;
163     }
164 
165     /**
166      * Map to a Merlin context. Actually there is nothing to do but
167      * we do the full monty to ensure that context mannipulation wirks.
168      *
169      * @param context the original context
170      * @return the mapped context
171      * @throws ContextException accessing the context failed
172      */
173     private DefaultContext mapToMerlin( Context context )
174         throws ContextException
175     {
176         DefaultContext result = createDefaultContext(context);
177 
178         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
179         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
180         File urnAvalonTemp = (File) context.get(AvalonYaafiConstants.URN_AVALON_TEMP);
181         String urnAvalonName = this.getUrnAvalonName();
182         ClassLoader urnAvalonClossLoader = this.getUrnAvalonClassLoader();
183 
184         result.put(AvalonMerlinConstants.URN_AVALON_PARTITION,urnAvalonPartition);
185         result.put(AvalonMerlinConstants.URN_AVALON_NAME,urnAvalonName);
186         result.put(AvalonMerlinConstants.URN_AVALON_HOME,urnAvalonHome);
187         result.put(AvalonMerlinConstants.URN_AVALON_TEMP,urnAvalonTemp);
188         result.put(AvalonMerlinConstants.URN_AVALON_CLASSLOADER,urnAvalonClossLoader);
189 
190         return result;
191     }
192 
193     /**
194      * Map to a YAAFI context.
195      *
196      * @param context the original context
197      * @return the mapped context
198      * @throws ContextException accessing the context failed
199      */
200     private DefaultContext mapToYaafi( Context context )
201         throws ContextException
202     {
203         DefaultContext result = createDefaultContext(context);
204 
205         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
206         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
207         File urnAvalonTemp = (File) context.get(AvalonYaafiConstants.URN_AVALON_TEMP);
208         String urnAvalonName = this.getUrnAvalonName();
209         ClassLoader urnAvalonClossLoader = this.getUrnAvalonClassLoader();
210 
211         result.put(AvalonYaafiConstants.URN_AVALON_PARTITION,urnAvalonPartition);
212         result.put(AvalonYaafiConstants.URN_AVALON_NAME,urnAvalonName);
213         result.put(AvalonYaafiConstants.URN_AVALON_HOME,urnAvalonHome);
214         result.put(AvalonYaafiConstants.URN_AVALON_TEMP,urnAvalonTemp);
215         result.put(AvalonYaafiConstants.URN_AVALON_CLASSLOADER,urnAvalonClossLoader);
216         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT,urnAvalonHome.getAbsolutePath());
217 
218         return result;
219     }
220 
221     /**
222      * Create a context to work with
223      */
224     private DefaultContext createDefaultContext(Context context)
225     {
226         return new DefaultContext(context);
227     }
228 }