1 package org.apache.fulcrum.testcontainer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.File;
18
19 import org.apache.avalon.excalibur.component.DefaultRoleManager;
20 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
21 import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
22 import org.apache.avalon.excalibur.logger.LoggerManager;
23 import org.apache.avalon.framework.component.Component;
24 import org.apache.avalon.framework.component.ComponentException;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
27 import org.apache.avalon.framework.context.DefaultContext;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 /**
30 * This is a simple ECM based container that can be used in unit test
31 * of the fulcrum components.
32 *
33 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34 * @version $Id: ECMContainer.java 223140 2004-11-01 13:29:25Z epugh $
35 */
36 public class ECMContainer extends AbstractLogEnabled implements Container
37 {
38
39
40 /** Component manager */
41 private ExcaliburComponentManager manager = new ExcaliburComponentManager();
42 /** Configurqation file */
43 private String configFileName;
44 /** Role file name */
45 private String roleFileName;
46 /** LogManager for logging */
47 private LoggerManager lm = new Log4JLoggerManager();
48 /**
49 * Constructor
50 */
51 public ECMContainer()
52 {
53 org.apache.log4j.BasicConfigurator.configure();
54 this.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum.testcontainer.Container"));
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(String configFileName, String roleFileName,String parametersFileName)
63 {
64 getLogger().debug("Starting container...");
65 this.configFileName = configFileName;
66 this.roleFileName = roleFileName;
67 File configFile = new File(configFileName);
68 if (!configFile.exists())
69 {
70 throw new RuntimeException(
71 "Could not initialize the container because the config file could not be found:" + configFile);
72 }
73 try
74 {
75 initialize();
76 getLogger().info("Container ready.");
77 }
78 catch (Exception e)
79 {
80 getLogger().error("Could not initialize the container", e);
81 throw new RuntimeException("Could not initialize the container");
82 }
83 }
84
85
86
87 /**
88 * Initializes the container
89 *
90 * @throws Exception generic exception
91 */
92 public void initialize() throws Exception
93 {
94 boolean useRoles = true;
95 File roleFile = new File(roleFileName+"");
96 if (!roleFile.exists())
97 {
98 useRoles = false;
99 getLogger().info("Not using seperate roles file");
100 }
101
102 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
103 Configuration sysConfig = builder.buildFromFile(configFileName);
104 if (useRoles)
105 {
106 Configuration roleConfig = builder.buildFromFile(roleFileName);
107
108 DefaultRoleManager roles = new DefaultRoleManager();
109 roles.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum"));
110 roles.configure(roleConfig);
111 this.manager.setRoleManager(roles);
112 }
113
114 this.manager.setLoggerManager(lm);
115 this.manager.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum"));
116 DefaultContext context = new DefaultContext();
117 String absolutePath = new File("").getAbsolutePath();
118 context.put(COMPONENT_APP_ROOT, absolutePath);
119 context.put(URN_AVALON_HOME, absolutePath);
120 this.manager.contextualize(context);
121
122 this.manager.configure(sysConfig);
123
124 this.manager.initialize();
125 }
126 /**
127 * Disposes of the container and releases resources
128 */
129 public void dispose()
130 {
131 getLogger().debug("Disposing of container...");
132 this.manager.dispose();
133 getLogger().info("Container has been disposed.");
134 }
135 /**
136 * Returns an instance of the named component
137 *
138 * @param roleName Name of the role the component fills.
139 * @throws ComponentException generic exception
140 */
141 public Object lookup(String roleName) throws ComponentException
142 {
143 return this.manager.lookup(roleName);
144 }
145 /**
146 * Releases the component
147 *
148 * @param component
149 */
150 public void release(Component component)
151 {
152 this.manager.release(component);
153 }
154
155 public void release(Object component)
156 {
157 this.manager.release((Component)component);
158 }
159 }