1 package org.apache.fulcrum.testcontainer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
37
38
39 public class ECMContainer extends AbstractLogEnabled implements Container
40 {
41
42
43
44 private ExcaliburComponentManager manager = new ExcaliburComponentManager();
45
46 private String configFileName;
47
48 private String roleFileName;
49
50 private LoggerManager lm = new Log4JLoggerManager();
51
52
53
54 public ECMContainer()
55 {
56 this.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum.testcontainer.Container"));
57 }
58
59
60
61
62
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
89
90
91
92
93
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
105 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
106 Configuration sysConfig = builder.buildFromFile(configFileName);
107 if (useRoles)
108 {
109 Configuration roleConfig = builder.buildFromFile(roleFileName);
110
111 DefaultRoleManager roles = new DefaultRoleManager();
112 roles.enableLogging(lm.getLoggerForCategory("org.apache.fulcrum"));
113 roles.configure(roleConfig);
114 this.manager.setRoleManager(roles);
115 }
116
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
127 this.manager.initialize();
128 }
129
130
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
140
141
142
143
144 public Object lookup(String roleName) throws ComponentException
145 {
146 return this.manager.lookup(roleName);
147 }
148
149
150
151
152
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 }