View Javadoc
1   package org.apache.fulcrum.testcontainer;
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.component.Component;
25  import org.apache.avalon.framework.component.ComponentException;
26  import org.apache.avalon.framework.logger.AbstractLogEnabled;
27  import org.apache.avalon.framework.logger.ConsoleLogger;
28  import org.apache.fulcrum.testcontainer.avalon.logger.Log4J2Logger;
29  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
30  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
31  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
32  import org.apache.logging.log4j.Level;
33  import org.apache.logging.log4j.LogManager;
34  import org.apache.logging.log4j.core.config.Configurator;
35  
36  /**
37   * This is a simple YAAFI based container that can be used in unit test
38   * of the fulcrum components.
39   *
40   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
41   */
42  public class YAAFIContainer extends AbstractLogEnabled implements Container
43  {
44      /** The YAAFI configuration */
45      private ServiceContainerConfiguration config;
46  
47      /** Component manager */
48      private ServiceContainer manager;
49  
50      /** The log level for the ConsoleLogger */
51      //private int logLevel = ConsoleLogger.LEVEL_DEBUG;
52      private Level logLevel = Level.DEBUG;
53      
54      org.apache.logging.log4j.Logger logger;
55  
56      /**
57       * Constructor.
58       */
59      public YAAFIContainer()
60      {
61          //this.enableLogging( new ConsoleLogger( logLevel ) );
62          logger = LogManager.getLogger( "avalon" );
63          Configurator.setLevel( "avalon",logLevel );
64          this.enableLogging( new Log4J2Logger( logger ) );
65          this.config = new ServiceContainerConfiguration();
66      }
67  
68      /**
69       * Constructor.
70       *
71       * @param logLevel the log level to be used: {@link ConsoleLogger} LEVEL_*.
72       */
73      public YAAFIContainer(int logLevel)
74      {
75          logger = LogManager.getLogger( "avalon" );
76          if (logLevel == ConsoleLogger.LEVEL_DEBUG) {
77              this.logLevel = Level.DEBUG;
78          }  else if (logLevel == ConsoleLogger.LEVEL_DEBUG) {
79                  this.logLevel = Level.DEBUG;
80          }  else if (logLevel == ConsoleLogger.LEVEL_INFO) {
81              this.logLevel = Level.INFO;
82          }  else if (logLevel == ConsoleLogger.LEVEL_WARN) {
83              this.logLevel = Level.WARN;
84          }  else if (logLevel == ConsoleLogger.LEVEL_ERROR) {
85              this.logLevel = Level.ERROR;
86          }  else if (logLevel == ConsoleLogger.LEVEL_FATAL) {
87              this.logLevel = Level.FATAL;
88          }  else if (logLevel == ConsoleLogger.LEVEL_DISABLED) {
89              this.logLevel = Level.OFF;
90          } else {
91              this.logLevel = Level.INFO;
92          }
93          Configurator.setLevel( "avalon", this.logLevel );
94          this.enableLogging( new Log4J2Logger( logger ) );
95          this.config = new ServiceContainerConfiguration();
96      }
97  
98      /**
99       * 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 }