1 package org.apache.fulcrum.yaafi.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.activity.Disposable;
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.component.Component;
27 import org.apache.avalon.framework.component.ComponentException;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.logger.ConsoleLogger;
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
32 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
33 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
34
35
36 /**
37 * This is a simple YAAFI based container that can be used in unit test
38 * of the fulcrum components.
39 *
40 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
41 */
42 public class Container extends AbstractLogEnabled implements Initializable, Disposable
43 {
44 /** The YAAFI configuration */
45 private ServiceContainerConfiguration config;
46
47 /** Component manager */
48 private ServiceContainer manager;
49
50 /**
51 * Constructor
52 */
53 public Container()
54 {
55
56 this.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
57 this.config = new ServiceContainerConfiguration();
58 }
59
60 /**
61 * Starts up the container and initializes it.
62 *
63 * @param configFileName Name of the component configuration file
64 * @param roleFileName Name of the role configuration file
65 */
66 public void startup(
67 String configFileName,
68 String roleFileName,
69 String parametersFileName )
70 {
71 getLogger().debug("Starting container...");
72
73 this.config.setComponentConfigurationLocation( configFileName );
74 this.config.setComponentRolesLocation( roleFileName );
75 this.config.setParametersLocation( parametersFileName );
76 this.config.setLogger( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) );
77
78 File configFile = new File(configFileName);
79
80 if (!configFile.exists())
81 {
82 throw new RuntimeException(
83 "Could not initialize the container because the config file could not be found:" + configFile);
84 }
85
86 try
87 {
88 initialize();
89 getLogger().info("YaffiContainer ready.");
90 }
91 catch (Exception e)
92 {
93 getLogger().error("Could not initialize the container", e);
94 throw new RuntimeException("Could not initialize the container");
95 }
96 }
97
98
99
100
101
102 /**
103 * Initializes the container
104 *
105 * @throws Exception generic exception
106 */
107 public void initialize() throws Exception
108 {
109 this.manager = ServiceContainerFactory.create(
110 this.config
111 );
112 }
113
114 /**
115 * Disposes of the container and releases resources
116 */
117 public void dispose()
118 {
119 getLogger().debug("Disposing of container...");
120 this.manager.dispose();
121 getLogger().info("YaffiContainer has been disposed.");
122 }
123
124 /**
125 * Returns an instance of the named component
126 *
127 * @param roleName Name of the role the component fills.
128 * @throws ComponentException generic exception
129 */
130 public Object lookup(String roleName) throws ComponentException
131 {
132 try
133 {
134 return this.manager.lookup(roleName);
135 }
136 catch( Exception e )
137 {
138 String msg = "Failed to lookup role " + roleName;
139 throw new ComponentException(roleName,msg,e);
140 }
141 }
142
143 /**
144 * Releases the component implementing the Component interface. This
145 * interface is depracted but still around in Fulcrum
146 *
147 * @param component
148 */
149 public void release(Component component)
150 {
151 this.manager.release(component);
152 }
153
154 /**
155 * Releases the component
156 *
157 * @param component
158 */
159 public void release(Object component)
160 {
161
162 this.manager.release(component);
163 }
164
165 /**
166 * Decommision the service
167 * @param name the name of the service
168 */
169 protected void decommision( String name )
170 throws ServiceException, Exception
171 {
172 if( this.manager != null )
173 {
174 this.manager.decommision( name );
175 }
176 }
177 }