1 package org.apache.torque.avalon;
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.Initializable;
25 import org.apache.avalon.framework.activity.Startable;
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.context.Context;
30 import org.apache.avalon.framework.context.ContextException;
31 import org.apache.avalon.framework.context.Contextualizable;
32 import org.apache.avalon.framework.logger.LogEnabled;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.avalon.framework.thread.ThreadSafe;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.torque.TorqueInstance;
37
38 /***
39 * Avalon component for Torque.
40 *
41 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
42 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
44 * @version $Id: TorqueComponent.java 476550 2006-11-18 16:08:37Z tfischer $
45 */
46 public class TorqueComponent
47 extends TorqueInstance
48 implements Torque,
49 LogEnabled,
50 Configurable,
51 Initializable,
52 Contextualizable,
53 Startable,
54 ThreadSafe
55 {
56 /*** The Avalon Application Root */
57 private String appRoot = null;
58
59 /*** The Avalon Logger */
60 private Logger logger = null;
61
62 /*** The configuration file name. */
63 private String configFile = null;
64
65
66 /***
67 * Creates a new instance. Default constructor used by Avalon.
68 */
69 public TorqueComponent()
70 {
71 super();
72
73
74 org.apache.torque.Torque.setInstance(this);
75 }
76
77
78
79
80
81
82
83
84
85 /***
86 * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
87 */
88 public void enableLogging(Logger aLogger)
89 {
90 this.logger = aLogger;
91 }
92
93 /***
94 * Convenience method to provide the Avalon logger the way AbstractLogEnabled does.
95 */
96 public Logger getLogger()
97 {
98 return logger;
99 }
100
101 /***
102 * @see
103 * org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
104 */
105 public void configure(Configuration configuration)
106 throws ConfigurationException
107 {
108 getLogger().debug("configure(" + configuration + ")");
109
110 String configurationFile
111 = configuration.getChild("configfile").getValue();
112
113 if (StringUtils.isNotEmpty(appRoot))
114 {
115 if (configurationFile.startsWith("/"))
116 {
117 configurationFile = configurationFile.substring(1);
118 getLogger().debug("Config File changes to "
119 + configurationFile);
120 }
121
122 StringBuffer sb = new StringBuffer();
123 sb.append(appRoot);
124 sb.append(File.separator);
125 sb.append(configurationFile);
126
127 configurationFile = sb.toString();
128 }
129
130 getLogger().debug("Config File is " + configurationFile);
131
132 this.configFile = configurationFile;
133 }
134
135 /***
136 * @see org.apache.avalon.framework.context.Contextualizable
137 */
138 public void contextualize(Context context)
139 throws ContextException
140 {
141
142 try
143 {
144 appRoot = ((File) context.get("urn:avalon:home")).getAbsolutePath();
145 }
146 catch (ContextException ce)
147 {
148 appRoot = null;
149 }
150
151 if (appRoot == null)
152 {
153
154 appRoot = (String) context.get("componentAppRoot");
155 }
156
157 if (StringUtils.isNotEmpty(appRoot))
158 {
159 if (appRoot.endsWith("/"))
160 {
161 appRoot = appRoot.substring(0, appRoot.length() - 1);
162 getLogger().debug("Application Root changed to " + appRoot);
163 }
164 }
165 }
166
167 /***
168 * @see org.apache.avalon.framework.activity.Initializable#initialize()
169 */
170 public void initialize()
171 throws Exception
172 {
173 getLogger().debug("initialize()");
174 init(configFile);
175 }
176
177 /***
178 * @see org.apache.avalon.framework.activity.Startable#start()
179 */
180 public void start()
181 {
182 getLogger().debug("start()");
183 }
184
185 /***
186 * @see org.apache.avalon.framework.activity.Startable#stop()
187 */
188 public void stop()
189 {
190 getLogger().debug("stop()");
191 try
192 {
193 shutdown();
194 }
195 catch (Exception e)
196 {
197 getLogger().error("Error while stopping Torque", e);
198 }
199 }
200 }