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 import java.util.Map;
24
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.context.Context;
31 import org.apache.avalon.framework.context.ContextException;
32 import org.apache.avalon.framework.context.Contextualizable;
33 import org.apache.avalon.framework.logger.LogEnabled;
34 import org.apache.avalon.framework.logger.Logger;
35 import org.apache.avalon.framework.thread.ThreadSafe;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.torque.Database;
38 import org.apache.torque.TorqueInstance;
39
40
41
42
43
44
45
46
47
48 public class TorqueComponent
49 extends TorqueInstance
50 implements Torque,
51 LogEnabled,
52 Configurable,
53 Initializable,
54 Contextualizable,
55 Disposable,
56 ThreadSafe
57 {
58
59 private String appRoot = null;
60
61
62 private Logger logger = null;
63
64
65 private String configFile = null;
66
67
68
69
70
71
72
73
74
75
76
77
78 public void enableLogging(Logger aLogger)
79 {
80 this.logger = aLogger;
81 }
82
83
84
85
86 public Logger getLogger()
87 {
88 return logger;
89 }
90
91
92
93
94
95 public void configure(Configuration configuration)
96 throws ConfigurationException
97 {
98 getLogger().debug("configure(" + configuration + ")");
99
100 String configurationFile
101 = configuration.getChild("configfile").getValue();
102
103 if (StringUtils.isNotEmpty(appRoot))
104 {
105 if (configurationFile.startsWith("/"))
106 {
107 configurationFile = configurationFile.substring(1);
108 getLogger().debug("Config File changes to "
109 + configurationFile);
110 }
111
112 StringBuffer sb = new StringBuffer();
113 sb.append(appRoot);
114 sb.append(File.separator);
115 sb.append(configurationFile);
116
117 configurationFile = sb.toString();
118 }
119
120 getLogger().debug("Config File is " + configurationFile);
121
122 this.configFile = configurationFile;
123 }
124
125
126
127
128 public void contextualize(Context context)
129 throws ContextException
130 {
131
132 try
133 {
134 appRoot = ((File) context.get("urn:avalon:home")).getAbsolutePath();
135 }
136 catch (ContextException ce)
137 {
138 appRoot = null;
139 }
140
141 if (appRoot == null)
142 {
143
144 appRoot = (String) context.get("componentAppRoot");
145 }
146
147 if (StringUtils.isNotEmpty(appRoot))
148 {
149 if (appRoot.endsWith("/"))
150 {
151 appRoot = appRoot.substring(0, appRoot.length() - 1);
152 getLogger().debug("Application Root changed to " + appRoot);
153 }
154 }
155 }
156
157
158
159
160 public void initialize()
161 throws Exception
162 {
163 getLogger().debug("initialize()");
164
165 TorqueInstance instance = org.apache.torque.Torque.getInstance();
166
167
168 Map<String, Database> databases = null;
169 if (instance.isInit())
170 {
171 databases = instance.getDatabases();
172 for (Database otherDatabase : databases.values())
173 {
174 getDatabaseMap(otherDatabase.getName()).copyFrom(
175 otherDatabase.getDatabaseMap());
176 }
177 }
178
179
180 org.apache.torque.Torque.setInstance(this);
181
182 init(configFile);
183
184
185 if (instance.isInit())
186 {
187 for (Database otherDatabase : databases.values())
188 {
189 Database database = getDatabase(otherDatabase.getName());
190 if (otherDatabase.getIdBroker() != null)
191 {
192 database.createAndRegisterIdBroker();
193 }
194 }
195 }
196 }
197
198
199
200
201 public void dispose()
202 {
203 getLogger().debug("dispose()");
204 try
205 {
206 shutdown();
207 }
208 catch (Exception e)
209 {
210 getLogger().error("Error while stopping Torque", e);
211 }
212 }
213 }