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       */
57      public AvalonToYaafiContextMapper(
58          File tempRootDir,
59          Context context,
60          ClassLoader classLoader)
61      {
62          Validate.notNull( tempRootDir, "tempRootDir" );
63          Validate.notNull( context, "context" );
64          Validate.notNull( classLoader, "classLoader" );
65  
66          this.tempRootDir = tempRootDir;
67          this.classLoader = classLoader;
68  
69          // here we have to create a new DefaultContext since
70          // it contains service specific entries
71  
72          this.defaultContext = new DefaultContext( context );
73      }
74  
75      /**
76       * Map a Avalon context to the YAAFI (Merlin) incarnation whereas
77       * the following containers are supported
78       * <ul>
79       *   <li>merlin</li>
80       *   <li>fortress</li>
81       *   <li>phoenix</li>
82       * </ul>
83       *
84       * @param context the Avalon context to map
85       * @param from Avalon container identifier
86       * @return the mapped Avalon context
87       * @throws ContextException Accessing the context failed
88       */
89      public Context mapFrom( Context context, String from )
90          throws ContextException
91      {
92          Validate.notNull( context, "context" );
93          Validate.notEmpty( from, "from" );
94  
95          if( AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(from) )
96          {
97              return mapFromPhoenix(context);
98  
99          }
100         else if( AvalonFortressConstants.AVALON_CONTAINER_FORTESS.equals(from) )
101         {
102             return mapFromFortress(context);
103 
104         }
105         else if( AvalonMerlinConstants.AVALON_CONTAINER_MERLIN.equals(from) )
106         {
107             return mapFromMerlin(context);
108         }
109         else if( AvalonYaafiConstants.AVALON_CONTAINER_YAAFI.equals(from) )
110         {
111             return mapFromMerlin(context);
112         }
113         else
114         {
115             String msg = "Don't know the following container type : " + from;
116             throw new IllegalArgumentException(msg);
117         }
118     }
119 
120     /**
121      * Map a Avalon Phoenix context to the YAAFI (Merlin) incarnation
122      *
123      * @param context the Avalon context to map
124      * @return the mapped Avalon context
125      * @throws ContextException Accessing the context failed
126      */
127     private Context mapFromPhoenix(Context context)
128         throws ContextException
129     {
130         DefaultContext result = this.getDefaultContext();
131 
132         String urnAvalonName = AvalonYaafiConstants.AVALON_CONTAINER_YAAFI;
133         String urnAvalonPartition = (String) context.get( AvalonPhoenixConstants.PHOENIX_APP_NAME );
134         File urnAvalonHome = (File) context.get( AvalonPhoenixConstants.PHOENIX_APP_HOME );
135         File urnAvalonTemp = this.getTempRootDir();
136 
137         // add the Merlin specific parameters
138 
139         result.put( AvalonYaafiConstants.URN_AVALON_NAME, urnAvalonName );
140         result.put( AvalonYaafiConstants.URN_AVALON_PARTITION, urnAvalonPartition );
141         result.put( AvalonYaafiConstants.URN_AVALON_HOME, urnAvalonHome );
142         result.put( AvalonYaafiConstants.URN_AVALON_TEMP, urnAvalonTemp );
143         result.put( AvalonYaafiConstants.URN_AVALON_CLASSLOADER, this.getClassLoader() );
144 
145         // add the deprecated ECM parameter
146 
147         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
148 
149         // add the Fortress specific parameters
150 
151         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_ID,urnAvalonPartition);
152         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_LOGGER,urnAvalonName);
153         result.put(AvalonFortressConstants.FORTRESS_CONTEXT_ROOT,urnAvalonHome);
154         result.put(AvalonFortressConstants.FORTRESS_IMPL_WORKDIR,urnAvalonTemp);
155 
156         return result;
157     }
158 
159     /**
160      * Map a Avalon Fortress context to the YAAFI (Merlin) incarnation
161      *
162      * @param context the Avalon context to map
163      * @return the mapped Avalon context
164      * @throws ContextException Accessing the context failed
165      */
166     private Context mapFromFortress(Context context)
167         throws ContextException
168     {
169         DefaultContext result = this.getDefaultContext();
170 
171         String urnAvalonPartition = (String) context.get( AvalonFortressConstants.FORTRESS_COMPONENT_ID );
172         File urnAvalonHome = (File) context.get( AvalonFortressConstants.FORTRESS_CONTEXT_ROOT );
173         File urnAvalonTemp = (File) context.get( AvalonFortressConstants.FORTRESS_IMPL_WORKDIR );
174 
175         // add the Merlin specific parameters
176 
177         result.put( AvalonYaafiConstants.URN_AVALON_NAME, AvalonYaafiConstants.AVALON_CONTAINER_YAAFI );
178         result.put( AvalonYaafiConstants.URN_AVALON_PARTITION, urnAvalonPartition );
179         result.put( AvalonYaafiConstants.URN_AVALON_HOME, urnAvalonHome );
180         result.put( AvalonYaafiConstants.URN_AVALON_TEMP, urnAvalonTemp );
181         result.put( AvalonYaafiConstants.URN_AVALON_CLASSLOADER, this.getClassLoader() );
182 
183         // add the deprecated ECM parameter
184 
185         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
186 
187 
188         return result;
189     }
190 
191     /**
192      * Map a Avalon Merlin context to the YAAFI (Merlin) incarnation
193      *
194      * @param context the Avalon context to map
195      * @return the mapped Avalon context
196      * @throws ContextException Accessing the context failed
197      */
198     private Context mapFromMerlin(Context context)
199         throws ContextException
200     {
201         DefaultContext result = this.getDefaultContext();
202 
203         String urnAvalonPartition = (String) context.get(AvalonYaafiConstants.URN_AVALON_PARTITION);
204         File urnAvalonHome = (File) context.get(AvalonYaafiConstants.URN_AVALON_HOME);
205         File urnAvalonTemp = (File) context.get(AvalonYaafiConstants.URN_AVALON_TEMP);
206         String urnAvalonName = (String) (String) context.get(AvalonYaafiConstants.URN_AVALON_NAME);
207 
208         // add the Fortress specific parameters
209 
210         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_ID,urnAvalonPartition);
211         result.put(AvalonFortressConstants.FORTRESS_COMPONENT_LOGGER,urnAvalonName);
212         result.put(AvalonFortressConstants.FORTRESS_CONTEXT_ROOT,urnAvalonHome);
213         result.put(AvalonFortressConstants.FORTRESS_IMPL_WORKDIR,urnAvalonTemp);
214 
215         // add the deprecated ECM parameter
216 
217         result.put(AvalonYaafiConstants.COMPONENT_APP_ROOT, urnAvalonHome.getAbsolutePath());
218 
219         return result;
220 
221     }
222 
223     /**
224      * @return Returns the classLoader.
225      */
226     private ClassLoader getClassLoader()
227     {
228         return this.classLoader;
229     }
230 
231     /**
232      * @return Returns the defaultContext.
233      */
234     private DefaultContext getDefaultContext()
235     {
236         return this.defaultContext;
237     }
238 
239     /**
240      * @return Returns the tempRootDir.
241      */
242     private File getTempRootDir()
243     {
244         return this.tempRootDir;
245     }
246 }