View Javadoc

1   package org.apache.fulcrum.testcontainer;
2   /*
3    * Copyright 2004 The Apache Software Foundation.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License")
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  import java.io.File;
18  
19  import org.apache.avalon.framework.activity.Disposable;
20  import org.apache.avalon.framework.activity.Initializable;
21  import org.apache.avalon.framework.component.Component;
22  import org.apache.avalon.framework.component.ComponentException;
23  import org.apache.avalon.framework.logger.AbstractLogEnabled;
24  import org.apache.avalon.framework.logger.ConsoleLogger;
25  import org.apache.avalon.framework.service.ServiceException;
26  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
27  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
28  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
29  
30  
31  
32  /**
33   * This is a simple YAAFI based container that can be used in unit test
34   * of the fulcrum components.
35   *
36   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
37   */
38  public class YAAFIContainer extends AbstractLogEnabled implements Container
39  {
40      /** The YAAFI configuration */
41      private ServiceContainerConfiguration config;
42  
43      /** Component manager */
44      private ServiceContainer manager;
45  
46      /**
47       * Constructor
48       */
49      public YAAFIContainer()
50      {
51          // org.apache.log4j.BasicConfigurator.configure();
52          this.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
53          this.config = new ServiceContainerConfiguration();
54      }
55  
56      /**
57       * Starts up the container and initializes it.
58       *
59       * @param configFileName Name of the component configuration file
60       * @param roleFileName Name of the role configuration file
61       */
62      public void startup(
63          String configFileName,
64          String roleFileName,
65          String parametersFileName )
66      {
67          getLogger().debug("Starting container...");
68  
69          this.config.setComponentConfigurationLocation( configFileName );
70          this.config.setComponentRolesLocation( roleFileName );
71          this.config.setParametersLocation( parametersFileName );
72          this.config.setLogger( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
73  
74          File configFile = new File(configFileName);
75  
76          if (!configFile.exists())
77          {
78              throw new RuntimeException(
79                  "Could not initialize the container because the config file could not be found:" + configFile);
80          }
81  
82          try
83          {
84              initialize();
85              getLogger().info("YaffiContainer ready.");
86          }
87          catch (Exception e)
88          {
89              getLogger().error("Could not initialize the container", e);
90              throw new RuntimeException("Could not initialize the container");
91          }
92      }
93  
94      // -------------------------------------------------------------
95      // Avalon lifecycle interfaces
96      // -------------------------------------------------------------
97  
98      /**
99       * Initializes the container
100      *
101      * @throws Exception generic exception
102      */
103     public void initialize() throws Exception
104     {
105         this.manager = ServiceContainerFactory.create(
106             this.config
107             );
108     }
109 
110     /**
111      * Disposes of the container and releases resources
112      */
113     public void dispose()
114     {
115         getLogger().debug("Disposing of container...");
116         if( this.manager != null )
117         {
118             this.manager.dispose();
119         }
120         getLogger().info("YaffiContainer has been disposed.");
121     }
122 
123     /**
124      * Returns an instance of the named component
125      *
126      * @param roleName Name of the role the component fills.
127      * @throws ComponentException generic exception
128      */
129     public Object lookup(String roleName) throws ComponentException
130     {
131         try
132         {
133             return this.manager.lookup(roleName);
134         }
135         catch( Exception e )
136         {
137             String msg = "Failed to lookup role " + roleName;
138             throw new ComponentException(roleName,msg,e);
139         }
140     }
141 
142     /**
143      * Releases the component implementing the Component interface. This
144      * interface is depracted but still around in Fulcrum
145      *
146      * @param component
147      */
148     public void release(Component component)
149     {
150         this.manager.release(component);
151     }
152 
153     /**
154      * Releases the component
155      *
156      * @param component
157      */
158     public void release(Object component)
159     {
160         this.manager.release(component);
161     }
162 }