View Javadoc
1   package org.apache.fulcrum.testcontainer;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.io.File;
21  
22  import org.apache.avalon.excalibur.component.DefaultRoleManager;
23  import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
24  import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
25  import org.apache.avalon.excalibur.logger.LoggerManager;
26  import org.apache.avalon.framework.component.Component;
27  import org.apache.avalon.framework.component.ComponentException;
28  import org.apache.avalon.framework.configuration.Configuration;
29  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
30  import org.apache.avalon.framework.context.DefaultContext;
31  import org.apache.avalon.framework.logger.AbstractLogEnabled;
32  /**
33   * This is a simple ECM based container that can be used in unit test
34   * of the fulcrum components.
35   *
36   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
37   * @version $Id$
38   */
39  public class ECMContainer extends AbstractLogEnabled implements Container
40  {
41  
42  
43      /** Component manager */
44      private ExcaliburComponentManager manager = new ExcaliburComponentManager();
45      /** Configurqation file */
46      private String configFileName;
47      /** Role file name */
48      private String roleFileName;
49      /** LogManager for logging */
50      private LoggerManager lm = new Log4JLoggerManager();
51      /**
52       * Constructor
53       */
54      public ECMContainer()
55      {
56          this.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum.testcontainer.Container"));
57      }
58      /**
59       * Starts up the container and initializes it.
60       *
61       * @param configFileName Name of the component configuration file
62       * @param roleFileName Name of the role configuration file
63       */
64      public void startup(String configFileName, String roleFileName,String parametersFileName)
65      {
66          getLogger().debug("Starting container...");
67          getLogger().debug( "with logger: " + getLogger().getClass().getSimpleName());
68          this.configFileName = configFileName;
69          this.roleFileName = roleFileName;
70          File configFile = new File(configFileName);
71          if (!configFile.exists())
72          {
73              throw new RuntimeException(
74                  "Could not initialize the container because the config file could not be found:" + configFile);
75          }
76          try
77          {
78              initialize();
79              getLogger().info("Container ready.");
80          }
81          catch (Exception e)
82          {
83              getLogger().error("Could not initialize the container", e);
84              throw new RuntimeException("Could not initialize the container");
85          }
86      }
87      // -------------------------------------------------------------
88      // Avalon lifecycle interfaces
89      // -------------------------------------------------------------
90      /**
91       * Initializes the container
92       *
93       * @throws Exception generic exception
94       */
95      public void initialize() throws Exception
96      {
97          boolean useRoles = true;
98          File roleFile = new File(roleFileName+"");
99          if (!roleFile.exists())
100         {
101             useRoles = false;
102             getLogger().info("Not using separate 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     /**
150      * Releases the component
151      *
152      * @param component instance of the component to release
153      */
154     public void release(Component component)
155     {
156         this.manager.release(component);
157     }
158 
159     public void release(Object component)
160     {
161         this.manager.release((Component)component);
162     }
163 }