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
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
38
39
40
41
42 public class YAAFIContainer extends AbstractLogEnabled implements Container
43 {
44
45 private ServiceContainerConfiguration config;
46
47
48 private ServiceContainer manager;
49
50
51
52 private Level logLevel = Level.DEBUG;
53
54 org.apache.logging.log4j.Logger logger;
55
56
57
58
59 public YAAFIContainer()
60 {
61
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
70
71
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
100
101
102
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
139
140
141
142
143
144
145
146 public void initialize() throws Exception
147 {
148 this.manager = ServiceContainerFactory.create(
149 this.config
150 );
151 }
152
153
154
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
168
169
170
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
187
188
189
190
191 public void release(Component component)
192 {
193 this.manager.release(component);
194 }
195
196
197
198
199
200
201 public void release(Object component)
202 {
203 this.manager.release(component);
204 }
205 }