1 package org.apache.turbine.services.component;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Iterator;
20 import javax.servlet.ServletConfig;
21
22 import org.apache.commons.configuration.BaseConfiguration;
23 import org.apache.commons.configuration.Configuration;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.stratum.component.ComponentLoader;
27 import org.apache.stratum.lifecycle.Disposable;
28 import org.apache.turbine.Turbine;
29 import org.apache.turbine.services.InitializationException;
30 import org.apache.turbine.services.TurbineBaseService;
31
32 /***
33 * An implementation of ComponentService which loads all the
34 * components given in the TurbineResources.properties File
35 *
36 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37 * @version $Id: TurbineComponentService.java 264148 2005-08-29 14:21:04Z henning $
38 * @deprecated torque is now loaded using the AvalonComponentService
39 */
40 public class TurbineComponentService
41 extends TurbineBaseService
42 implements ComponentService
43 {
44
45 /*** Logging */
46 private static Log log = LogFactory.getLog(TurbineComponentService.class);
47
48 /*** Extension used for Configuration files. */
49 private static String CONFIG = "config";
50
51 /*** Name tag used in Configurations */
52 private static String NAME = "name";
53
54 /*** Prefix used by the Component Loader */
55 private static String COMPONENT = "component";
56
57 /*** List of Components that was initialized */
58 private Object[] components = null;
59
60 /***
61 * Load all configured components and initialize them. This is
62 * a zero parameter variant which queries the Turbine Servlet
63 * for its config.
64 *
65 * @throws InitializationException Something went wrong in the init
66 * stage
67 */
68 public void init()
69 throws InitializationException
70 {
71 ServletConfig config = Turbine.getTurbineServletConfig();
72 Configuration loaderConf = new BaseConfiguration();
73
74 String[] names = getConfiguration().getStringArray(NAME);
75
76 log.warn("The ComponentService is deprecated!");
77
78 for (int i = 0; i < names.length; i++)
79 {
80 String key = names[i];
81
82 loaderConf.addProperty(COMPONENT + "." + NAME, key);
83
84 String subProperty = COMPONENT + "." + key;
85 Configuration subConf = getConfiguration().subset(key);
86
87 for (Iterator it = subConf.getKeys(); it.hasNext();)
88 {
89 String subKey = (String) it.next();
90 Object subVal = subConf.getProperty(subKey);
91
92 if (subKey.equals(CONFIG))
93 {
94 log.debug("Fixing up " + subVal);
95 String newPath =
96 config.getServletContext().getRealPath((String) subVal);
97
98 if (newPath == null)
99 {
100 throw new InitializationException("Could not translate path " + subVal);
101 }
102
103 subVal = newPath;
104 log.debug("Now: " + subVal);
105 }
106
107 loaderConf.addProperty(subProperty + "." + subKey,
108 subVal);
109 }
110
111 log.info("Added " + key + " as a component");
112 }
113
114 try
115 {
116 ComponentLoader cl = new ComponentLoader(loaderConf);
117 components = cl.load();
118 setInit(true);
119 }
120 catch (Exception e)
121 {
122 log.error("Component Service failed: ", e);
123 throw new InitializationException("ComponentService failed: ", e);
124 }
125 }
126
127 /***
128 * Inits the service using servlet parameters to obtain path to the
129 * configuration file. Change relatives paths.
130 *
131 * @param config The ServletConfiguration from Turbine
132 *
133 * @throws InitializationException Something went wrong when starting up.
134 * @deprecated use init() instead.
135 */
136 public void init(ServletConfig config)
137 throws InitializationException
138 {
139 init();
140 }
141
142 /***
143 * Shuts the Component Service down, calls dispose on the components that
144 * implement this interface
145 *
146 */
147
148 public void shutdown()
149 {
150 if (components != null)
151 {
152 for (int i = 0; i < components.length; i++)
153 {
154 if (components[i] instanceof Disposable)
155 {
156 log.debug("Disposing a " + components[i].getClass().getName() + " object");
157 ((Disposable) components[i]).dispose();
158 }
159 else
160 {
161 log.debug("Not disposing " + components[i].getClass().getName() + ", not a Disposable Object");
162 }
163 }
164 }
165 setInit(false);
166 }
167 }