001 package org.apache.fulcrum.yaafi.framework.context;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023
024 import org.apache.avalon.framework.context.Context;
025 import org.apache.avalon.framework.context.ContextException;
026 import org.apache.avalon.framework.context.DefaultContext;
027 import org.apache.fulcrum.yaafi.framework.constant.AvalonFortressConstants;
028 import org.apache.fulcrum.yaafi.framework.constant.AvalonMerlinConstants;
029 import org.apache.fulcrum.yaafi.framework.constant.AvalonPhoenixConstants;
030 import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
031 import org.apache.fulcrum.yaafi.framework.util.Validate;
032
033 /**
034 * Helper for converting Avalon Context of Fortress and Phoenix
035 * container to a standard Merlin context.
036 *
037 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
038 */
039
040 public class AvalonToYaafiContextMapper
041 {
042 /** The directory for storing temporary files */
043 private File tempRootDir;
044
045 /** Our default context */
046 private DefaultContext defaultContext;
047
048 /** our defaul class loader */
049 private ClassLoader classLoader;
050
051 /**
052 * Constructor
053 *
054 * @param tempRootDir current temp directory
055 * @param context the existing context
056 */
057 public AvalonToYaafiContextMapper(
058 File tempRootDir,
059 Context context,
060 ClassLoader classLoader)
061 {
062 Validate.notNull( tempRootDir, "tempRootDir" );
063 Validate.notNull( context, "context" );
064 Validate.notNull( classLoader, "classLoader" );
065
066 this.tempRootDir = tempRootDir;
067 this.classLoader = classLoader;
068
069 // here we have to create a new DefaultContext since
070 // it contains service specific entries
071
072 this.defaultContext = new DefaultContext( context );
073 }
074
075 /**
076 * Map a Avalon context to the YAAFI (Merlin) incarnation whereas
077 * the following containers are supported
078 * <ul>
079 * <li>merlin</li>
080 * <li>fortress</li>
081 * <li>phoenix</li>
082 * </ul>
083 *
084 * @param context the Avalon context to map
085 * @param from Avalon container identifier
086 * @return the mapped Avalon context
087 * @throws ContextException Accessing the context failed
088 */
089 public Context mapFrom( Context context, String from )
090 throws ContextException
091 {
092 Validate.notNull( context, "context" );
093 Validate.notEmpty( from, "from" );
094
095 if( AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(from) )
096 {
097 return mapFromPhoenix(context);
098
099 }
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 }