1 | |
package org.apache.ojb.broker.util.dbhandling; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.StringTokenizer; |
22 | |
|
23 | |
import org.apache.ojb.broker.PBKey; |
24 | |
import org.apache.ojb.broker.PersistenceBrokerFactory; |
25 | |
import org.apache.ojb.broker.util.ClassHelper; |
26 | |
import org.apache.ojb.broker.metadata.ConnectionRepository; |
27 | |
import org.apache.ojb.broker.metadata.MetadataManager; |
28 | |
import org.apache.tools.ant.*; |
29 | |
import org.apache.tools.ant.types.FileSet; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class DBHandlingTask extends Task |
37 | |
{ |
38 | |
|
39 | |
private static final String HANDLING_TORQUE = "torque"; |
40 | |
|
41 | |
private static final String COMMAND_CREATE = "create"; |
42 | |
private static final String COMMAND_INIT = "init"; |
43 | |
|
44 | |
|
45 | |
private String _handling = HANDLING_TORQUE; |
46 | |
|
47 | |
private String _propertiesFile = null; |
48 | |
|
49 | |
private String _jcdAlias = null; |
50 | |
|
51 | |
private String _workDir = null; |
52 | |
|
53 | |
private ArrayList _fileSets = new ArrayList(); |
54 | |
|
55 | |
private String _commands = ""; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void setHandling(String name) |
63 | |
{ |
64 | |
_handling = (name == null ? HANDLING_TORQUE : name.toLowerCase()); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public String getHandling() |
73 | |
{ |
74 | |
return _handling; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void setPropertiesFile(String path) |
83 | |
{ |
84 | |
_propertiesFile = path; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public String getPropertiesFile() |
93 | |
{ |
94 | |
return _propertiesFile; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void setJcdAlias(String alias) |
103 | |
{ |
104 | |
_jcdAlias = alias; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public String getJcdAlias() |
113 | |
{ |
114 | |
return _jcdAlias; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setWorkDir(String dir) |
123 | |
{ |
124 | |
_workDir = dir; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public String getWorkDir() |
133 | |
{ |
134 | |
return _workDir; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void addFileset(FileSet fileset) |
143 | |
{ |
144 | |
_fileSets.add(fileset); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void setCommands(String listOfCommands) |
153 | |
{ |
154 | |
_commands = listOfCommands; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public String getCommands() |
163 | |
{ |
164 | |
return _commands; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void execute() throws BuildException |
171 | |
{ |
172 | |
if ((_commands == null) || (_commands.length() == 0)) |
173 | |
{ |
174 | |
return; |
175 | |
} |
176 | |
|
177 | |
DBHandling handling = createDBHandling(); |
178 | |
|
179 | |
try |
180 | |
{ |
181 | |
if ((_workDir != null) && (_workDir.length() > 0)) |
182 | |
{ |
183 | |
handling.setWorkDir(_workDir); |
184 | |
System.setProperty("user.dir", _workDir); |
185 | |
} |
186 | |
for (Iterator it = _fileSets.iterator(); it.hasNext();) |
187 | |
{ |
188 | |
addIncludes(handling, (FileSet)it.next()); |
189 | |
} |
190 | |
|
191 | |
if ((_propertiesFile != null) && (_propertiesFile.length() > 0)) |
192 | |
{ |
193 | |
System.setProperty("OJB.properties", _propertiesFile); |
194 | |
} |
195 | |
|
196 | |
ConnectionRepository connRep = MetadataManager.getInstance().connectionRepository(); |
197 | |
PBKey pbKey = null; |
198 | |
|
199 | |
if ((_jcdAlias == null) || (_jcdAlias.length() == 0)) |
200 | |
{ |
201 | |
pbKey = PersistenceBrokerFactory.getDefaultKey(); |
202 | |
} |
203 | |
else |
204 | |
{ |
205 | |
pbKey = connRep.getStandardPBKeyForJcdAlias(_jcdAlias); |
206 | |
if (pbKey == null) |
207 | |
{ |
208 | |
throw new BuildException("Undefined jcdAlias "+_jcdAlias); |
209 | |
} |
210 | |
} |
211 | |
handling.setConnection(connRep.getDescriptor(pbKey)); |
212 | |
|
213 | |
String command; |
214 | |
|
215 | |
for (StringTokenizer tokenizer = new StringTokenizer(_commands, ","); tokenizer.hasMoreTokens();) |
216 | |
{ |
217 | |
command = tokenizer.nextToken().toLowerCase().trim(); |
218 | |
if (COMMAND_CREATE.equals(command)) |
219 | |
{ |
220 | |
handling.createDB(); |
221 | |
} |
222 | |
else if (COMMAND_INIT.equals(command)) |
223 | |
{ |
224 | |
handling.initDB(); |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | |
throw new BuildException("Unknown command "+command); |
229 | |
} |
230 | |
} |
231 | |
} |
232 | |
catch (Exception ex) |
233 | |
{ |
234 | |
throw new BuildException(ex); |
235 | |
} |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
private DBHandling createDBHandling() throws BuildException |
245 | |
{ |
246 | |
if ((_handling == null) || (_handling.length() == 0)) |
247 | |
{ |
248 | |
throw new BuildException("No handling specified"); |
249 | |
} |
250 | |
try |
251 | |
{ |
252 | |
String className = "org.apache.ojb.broker.platforms."+ |
253 | |
Character.toTitleCase(_handling.charAt(0))+_handling.substring(1)+ |
254 | |
"DBHandling"; |
255 | |
Class handlingClass = ClassHelper.getClass(className); |
256 | |
|
257 | |
return (DBHandling)handlingClass.newInstance(); |
258 | |
} |
259 | |
catch (Exception ex) |
260 | |
{ |
261 | |
throw new BuildException("Invalid handling '"+_handling+"' specified"); |
262 | |
} |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
private void addIncludes(DBHandling handling, FileSet fileSet) throws BuildException |
272 | |
{ |
273 | |
DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject()); |
274 | |
String[] files = scanner.getIncludedFiles(); |
275 | |
StringBuffer includes = new StringBuffer(); |
276 | |
|
277 | |
for (int idx = 0; idx < files.length; idx++) |
278 | |
{ |
279 | |
if (idx > 0) |
280 | |
{ |
281 | |
includes.append(","); |
282 | |
} |
283 | |
includes.append(files[idx]); |
284 | |
} |
285 | |
try |
286 | |
{ |
287 | |
handling.addDBDefinitionFiles(fileSet.getDir(getProject()).getAbsolutePath(), includes.toString()); |
288 | |
} |
289 | |
catch (IOException ex) |
290 | |
{ |
291 | |
throw new BuildException(ex); |
292 | |
} |
293 | |
} |
294 | |
} |