001package org.apache.fulcrum.testcontainer;
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.component.Component;
025import org.apache.avalon.framework.component.ComponentException;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.avalon.framework.logger.ConsoleLogger;
028import org.apache.fulcrum.testcontainer.avalon.logger.Log4J2Logger;
029import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
030import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
031import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
032import org.apache.logging.log4j.Level;
033import org.apache.logging.log4j.LogManager;
034import org.apache.logging.log4j.core.config.Configurator;
035
036/**
037 * This is a simple YAAFI based container that can be used in unit test
038 * of the fulcrum components.
039 *
040 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
041 */
042public class YAAFIContainer extends AbstractLogEnabled implements Container
043{
044    /** The YAAFI configuration */
045    private ServiceContainerConfiguration config;
046
047    /** Component manager */
048    private ServiceContainer manager;
049
050    /** The log level for the ConsoleLogger */
051    //private int logLevel = ConsoleLogger.LEVEL_DEBUG;
052    private Level logLevel = Level.DEBUG;
053    
054    org.apache.logging.log4j.Logger logger;
055
056    /**
057     * Constructor.
058     */
059    public YAAFIContainer()
060    {
061        //this.enableLogging( new ConsoleLogger( logLevel ) );
062        logger = LogManager.getLogger( "avalon" );
063        Configurator.setLevel( "avalon",logLevel );
064        this.enableLogging( new Log4J2Logger( logger ) );
065        this.config = new ServiceContainerConfiguration();
066    }
067
068    /**
069     * Constructor.
070     *
071     * @param logLevel the log level to be used: {@link ConsoleLogger} LEVEL_*.
072     */
073    public YAAFIContainer(int logLevel)
074    {
075        logger = LogManager.getLogger( "avalon" );
076        if (logLevel == ConsoleLogger.LEVEL_DEBUG) {
077            this.logLevel = Level.DEBUG;
078        }  else if (logLevel == ConsoleLogger.LEVEL_DEBUG) {
079                this.logLevel = Level.DEBUG;
080        }  else if (logLevel == ConsoleLogger.LEVEL_INFO) {
081            this.logLevel = Level.INFO;
082        }  else if (logLevel == ConsoleLogger.LEVEL_WARN) {
083            this.logLevel = Level.WARN;
084        }  else if (logLevel == ConsoleLogger.LEVEL_ERROR) {
085            this.logLevel = Level.ERROR;
086        }  else if (logLevel == ConsoleLogger.LEVEL_FATAL) {
087            this.logLevel = Level.FATAL;
088        }  else if (logLevel == ConsoleLogger.LEVEL_DISABLED) {
089            this.logLevel = Level.OFF;
090        } else {
091            this.logLevel = Level.INFO;
092        }
093        Configurator.setLevel( "avalon", this.logLevel );
094        this.enableLogging( new Log4J2Logger( logger ) );
095        this.config = new ServiceContainerConfiguration();
096    }
097
098    /**
099     * Starts up the container and initializes it.
100     *
101     * @param configFileName Name of the component configuration file
102     * @param roleFileName Name of the role configuration file
103     */
104    public void startup(
105        String configFileName,
106        String roleFileName,
107        String parametersFileName )
108    {
109        getLogger().debug("Starting YAAFI container... ");
110        getLogger().debug( "with logger: " + getLogger().getClass().getName());
111
112        this.config.setComponentConfigurationLocation( configFileName );
113        this.config.setComponentRolesLocation( roleFileName );
114        this.config.setParametersLocation( parametersFileName );
115        this.config.setLogger( new Log4J2Logger( logger ) );
116
117        File configFile = new File(configFileName);
118
119        if (!configFile.exists())
120        {
121            throw new RuntimeException(
122                "Could not initialize the container because the config file could not be found:" + configFile);
123        }
124
125        try
126        {
127            initialize();
128            getLogger().info("YAFFI Container ready.");
129        }
130        catch (Exception e)
131        {
132            getLogger().error("Could not initialize the container", e);
133            throw new RuntimeException("Could not initialize the container");
134        }
135    }
136
137    // -------------------------------------------------------------
138    // Avalon lifecycle interfaces
139    // -------------------------------------------------------------
140
141    /**
142     * Initializes the container.
143     *
144     * @throws Exception generic exception
145     */
146    public void initialize() throws Exception
147    {
148        this.manager = ServiceContainerFactory.create(
149            this.config
150            );
151    }
152
153    /**
154     * Disposes of the container and releases resources.
155     */
156    public void dispose()
157    {
158        getLogger().debug("Disposing of container...");
159        if( this.manager != null )
160        {
161            this.manager.dispose();
162        }
163        getLogger().info("YAFFI Container has been disposed.");
164    }
165
166    /**
167     * Returns an instance of the named component.
168     *
169     * @param roleName Name of the role the component fills.
170     * @throws ComponentException generic exception
171     */
172    public Object lookup(String roleName) throws ComponentException
173    {
174        try
175        {
176            return this.manager.lookup(roleName);
177        }
178        catch( Exception e )
179        {
180            String msg = "Failed to lookup role " + roleName;
181            throw new ComponentException(roleName,msg,e);
182        }
183    }
184
185    /**
186     * Releases the component implementing the Component interface. This
187     * interface is deprecated but still around in Fulcrum
188     *
189     * @param component instance of the component to release
190     */
191    public void release(Component component)
192    {
193        this.manager.release(component);
194    }
195
196    /**
197     * Releases the component.
198     *
199     * @param component component to be released
200     */
201    public void release(Object component)
202    {
203        this.manager.release(component);
204    }
205}