001package 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
022import java.io.File;
023
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.DefaultContext;
027import org.apache.fulcrum.yaafi.framework.constant.AvalonFortressConstants;
028import org.apache.fulcrum.yaafi.framework.constant.AvalonMerlinConstants;
029import org.apache.fulcrum.yaafi.framework.constant.AvalonPhoenixConstants;
030import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
031import org.apache.fulcrum.yaafi.framework.util.Validate;
032
033/**
034 * Helper for converting a YAAFI context to a different container
035 *
036 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
037 */
038
039public class YaafiToAvalonContextMapper
040{
041    /** the name of the component for which we create the context */
042    private String urnAvalonName;
043
044    /** the classloader of the component */
045    private ClassLoader urnAvalonClassLoader;
046
047    /**
048     * Constructor
049     *
050     * @param urnAvalonName the name of the component for which we create the context
051     * @param urnAvalonClassLoader the classloader of the component
052     */
053    public YaafiToAvalonContextMapper( String urnAvalonName, ClassLoader urnAvalonClassLoader )
054    {
055        Validate.notEmpty( urnAvalonName, "urnAvalonName" );
056        Validate.notNull( urnAvalonClassLoader, "urnAvalonClassLoader" );
057
058        this.urnAvalonName = urnAvalonName;
059        this.urnAvalonClassLoader = urnAvalonClassLoader;
060    }
061
062    /**
063     * @return Returns the urnAvalonClassLoader.
064     */
065    public ClassLoader getUrnAvalonClassLoader()
066    {
067        return urnAvalonClassLoader;
068    }
069
070    /**
071     * @return Returns the urnAvalonName.
072     */
073    public String getUrnAvalonName()
074    {
075        return urnAvalonName;
076    }
077
078    /**
079     * Map a YAAFI (Merlin) context to a different incarnation
080     *
081     * @param context the context to be mapped
082     * @param to the target Avalon container
083     * @return the mapped context
084     * @throws ContextException accessing the context failed
085     */
086    public DefaultContext mapTo( Context context, String to )
087        throws ContextException
088    {
089        Validate.notNull( context, "context" );
090        Validate.notEmpty( to, "to" );
091
092        if( AvalonPhoenixConstants.AVALON_CONTAINER_PHOENIX.equals(to) )
093        {
094            return mapToPhoenix(context);
095
096        }
097        else if( AvalonFortressConstants.AVALON_CONTAINER_FORTESS.equals(to) )
098        {
099            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}