001    package org.apache.fulcrum.testcontainer;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    import java.io.File;
021    
022    import org.apache.avalon.excalibur.component.DefaultRoleManager;
023    import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
024    import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
025    import org.apache.avalon.excalibur.logger.LoggerManager;
026    import org.apache.avalon.framework.component.Component;
027    import org.apache.avalon.framework.component.ComponentException;
028    import org.apache.avalon.framework.configuration.Configuration;
029    import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
030    import org.apache.avalon.framework.context.DefaultContext;
031    import org.apache.avalon.framework.logger.AbstractLogEnabled;
032    /**
033     * This is a simple ECM based container that can be used in unit test
034     * of the fulcrum components.
035     *
036     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
037     * @version $Id: ECMContainer.java 535465 2007-05-05 06:58:06Z tv $
038     */
039    public class ECMContainer extends AbstractLogEnabled implements Container
040    {
041    
042    
043        /** Component manager */
044        private ExcaliburComponentManager manager = new ExcaliburComponentManager();
045        /** Configurqation file */
046        private String configFileName;
047        /** Role file name */
048        private String roleFileName;
049        /** LogManager for logging */
050        private LoggerManager lm = new Log4JLoggerManager();
051        /**
052         * Constructor
053         */
054        public ECMContainer()
055        {
056            org.apache.log4j.BasicConfigurator.configure();
057            this.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum.testcontainer.Container"));
058        }
059        /**
060         * Starts up the container and initializes it.
061         *
062         * @param configFileName Name of the component configuration file
063         * @param roleFileName Name of the role configuration file
064         */
065        public void startup(String configFileName, String roleFileName,String parametersFileName)
066        {
067            getLogger().debug("Starting container...");
068            this.configFileName = configFileName;
069            this.roleFileName = roleFileName;
070            File configFile = new File(configFileName);
071            if (!configFile.exists())
072            {
073                throw new RuntimeException(
074                    "Could not initialize the container because the config file could not be found:" + configFile);
075            }
076            try
077            {
078                initialize();
079                getLogger().info("Container ready.");
080            }
081            catch (Exception e)
082            {
083                getLogger().error("Could not initialize the container", e);
084                throw new RuntimeException("Could not initialize the container");
085            }
086        }
087        // -------------------------------------------------------------
088        // Avalon lifecycle interfaces
089        // -------------------------------------------------------------
090        /**
091         * Initializes the container
092         *
093         * @throws Exception generic exception
094         */
095        public void initialize() throws Exception
096        {
097            boolean useRoles = true;
098            File roleFile = new File(roleFileName+"");
099            if (!roleFile.exists())
100            {
101                useRoles = false;
102                getLogger().info("Not using seperate roles file");
103            }
104            // process configuration files
105            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
106            Configuration sysConfig = builder.buildFromFile(configFileName);
107            if (useRoles)
108            {
109                Configuration roleConfig = builder.buildFromFile(roleFileName);
110                // Setup the RoleManager
111                DefaultRoleManager roles = new DefaultRoleManager();
112                roles.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum"));
113                roles.configure(roleConfig);
114                            this.manager.setRoleManager(roles);
115            }
116            // Setup ECM
117            this.manager.setLoggerManager(lm);
118            this.manager.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum"));
119            DefaultContext context = new DefaultContext();
120            String absolutePath = new File("").getAbsolutePath();
121            context.put(COMPONENT_APP_ROOT, absolutePath);
122            context.put(URN_AVALON_HOME, absolutePath);
123            this.manager.contextualize(context);
124    
125            this.manager.configure(sysConfig);
126            // Init ECM!!!!
127            this.manager.initialize();
128        }
129        /**
130         * Disposes of the container and releases resources
131         */
132        public void dispose()
133        {
134            getLogger().debug("Disposing of container...");
135            this.manager.dispose();
136            getLogger().info("Container has been disposed.");
137        }
138        /**
139         * Returns an instance of the named component
140         *
141         * @param roleName Name of the role the component fills.
142         * @throws ComponentException generic exception
143         */
144        public Object lookup(String roleName) throws ComponentException
145        {
146            return this.manager.lookup(roleName);
147        }
148        /**
149         * Releases the component
150         *
151         * @param component
152         */
153        public void release(Component component)
154        {
155            this.manager.release(component);
156        }
157    
158        public void release(Object component)
159        {
160            this.manager.release((Component)component);
161        }
162    }