| 1 | |
package org.apache.ojb.broker.ant; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import java.io.FileInputStream; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.Properties; |
| 23 | |
|
| 24 | |
import org.apache.ddlutils.io.DatabaseIO; |
| 25 | |
import org.apache.ddlutils.model.Database; |
| 26 | |
import org.apache.ddlutils.task.DatabaseTaskBase; |
| 27 | |
import org.apache.ojb.broker.PBKey; |
| 28 | |
import org.apache.ojb.broker.metadata.DescriptorRepository; |
| 29 | |
import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; |
| 30 | |
import org.apache.ojb.broker.metadata.MetadataManager; |
| 31 | |
import org.apache.ojb.broker.metadata.RepositoryPersistor; |
| 32 | |
import org.apache.tools.ant.AntClassLoader; |
| 33 | |
import org.apache.tools.ant.BuildException; |
| 34 | |
import org.apache.tools.ant.DirectoryScanner; |
| 35 | |
import org.apache.tools.ant.Project; |
| 36 | |
import org.apache.tools.ant.types.FileSet; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class RepositoryDataTask extends DatabaseTaskBase |
| 44 | |
{ |
| 45 | |
|
| 46 | |
private ArrayList _commands = new ArrayList(); |
| 47 | |
|
| 48 | |
private String _jcdAlias; |
| 49 | |
|
| 50 | |
private File _ojbPropertiesFile; |
| 51 | |
|
| 52 | |
private File _repositoryFile; |
| 53 | |
|
| 54 | |
private File _singleSchemaFile = null; |
| 55 | |
|
| 56 | |
private ArrayList _fileSets = new ArrayList(); |
| 57 | |
|
| 58 | |
private boolean _useInternalDtd = true; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void setJcdAlias(String alias) |
| 67 | |
{ |
| 68 | |
_jcdAlias = alias; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public String getJcdAlias() |
| 77 | |
{ |
| 78 | |
return _jcdAlias; |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public File getOjbPropertiesFile() |
| 87 | |
{ |
| 88 | |
return _ojbPropertiesFile; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public void setOjbPropertiesFile(File ojbPropertiesFile) |
| 96 | |
{ |
| 97 | |
_ojbPropertiesFile = ojbPropertiesFile; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public File getRepositoryFile() |
| 106 | |
{ |
| 107 | |
return _repositoryFile; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public void setRepositoryFile(File file) |
| 116 | |
{ |
| 117 | |
_repositoryFile = file; |
| 118 | |
} |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public void setUseInternalDtd(boolean useInternalDtd) |
| 126 | |
{ |
| 127 | |
_useInternalDtd = useInternalDtd; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public void addConfiguredFileset(FileSet fileset) |
| 136 | |
{ |
| 137 | |
_fileSets.add(fileset); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void setSchemaFile(File schemaFile) |
| 146 | |
{ |
| 147 | |
_singleSchemaFile = schemaFile; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public void addWriteDtdToFile(WriteDtdToFileCommand command) |
| 156 | |
{ |
| 157 | |
_commands.add(command); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public void addWriteDataToDatabase(WriteDataToDatabaseCommand command) |
| 166 | |
{ |
| 167 | |
_commands.add(command); |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public void addWriteDataSqlToFile(WriteDataSqlToFileCommand command) |
| 176 | |
{ |
| 177 | |
_commands.add(command); |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
protected Database readModel() |
| 184 | |
{ |
| 185 | |
DatabaseIO reader = new DatabaseIO(); |
| 186 | |
Database model = null; |
| 187 | |
|
| 188 | |
reader.setUseInternalDtd(_useInternalDtd); |
| 189 | |
if ((_singleSchemaFile != null) && !_fileSets.isEmpty()) |
| 190 | |
{ |
| 191 | |
throw new BuildException("Please use either the schemafile attribute or the sub fileset element, but not both"); |
| 192 | |
} |
| 193 | |
if (_singleSchemaFile != null) |
| 194 | |
{ |
| 195 | |
model = readSingleSchemaFile(reader, _singleSchemaFile); |
| 196 | |
} |
| 197 | |
else |
| 198 | |
{ |
| 199 | |
for (Iterator it = _fileSets.iterator(); it.hasNext();) |
| 200 | |
{ |
| 201 | |
FileSet fileSet = (FileSet)it.next(); |
| 202 | |
File fileSetDir = fileSet.getDir(getProject()); |
| 203 | |
DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject()); |
| 204 | |
String[] files = scanner.getIncludedFiles(); |
| 205 | |
|
| 206 | |
for (int idx = 0; (files != null) && (idx < files.length); idx++) |
| 207 | |
{ |
| 208 | |
Database curModel = readSingleSchemaFile(reader, new File(fileSetDir, files[idx])); |
| 209 | |
|
| 210 | |
if (model == null) |
| 211 | |
{ |
| 212 | |
model = curModel; |
| 213 | |
} |
| 214 | |
else if (curModel != null) |
| 215 | |
{ |
| 216 | |
try |
| 217 | |
{ |
| 218 | |
model.mergeWith(curModel); |
| 219 | |
} |
| 220 | |
catch (IllegalArgumentException ex) |
| 221 | |
{ |
| 222 | |
throw new BuildException("Could not merge with schema from file "+files[idx]+": "+ex.getLocalizedMessage(), ex); |
| 223 | |
} |
| 224 | |
} |
| 225 | |
} |
| 226 | |
} |
| 227 | |
} |
| 228 | |
return model; |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
private Database readSingleSchemaFile(DatabaseIO reader, File schemaFile) |
| 239 | |
{ |
| 240 | |
Database model = null; |
| 241 | |
|
| 242 | |
if (!schemaFile.isFile()) |
| 243 | |
{ |
| 244 | |
log("Path "+schemaFile.getAbsolutePath()+" does not denote a schema file", Project.MSG_ERR); |
| 245 | |
} |
| 246 | |
else if (!schemaFile.canRead()) |
| 247 | |
{ |
| 248 | |
log("Could not read schema file "+schemaFile.getAbsolutePath(), Project.MSG_ERR); |
| 249 | |
} |
| 250 | |
else |
| 251 | |
{ |
| 252 | |
try |
| 253 | |
{ |
| 254 | |
model = reader.read(schemaFile); |
| 255 | |
log("Read schema file "+schemaFile.getAbsolutePath(), Project.MSG_INFO); |
| 256 | |
} |
| 257 | |
catch (Exception ex) |
| 258 | |
{ |
| 259 | |
throw new BuildException("Could not read schema file "+schemaFile.getAbsolutePath()+": "+ex.getLocalizedMessage(), ex); |
| 260 | |
} |
| 261 | |
} |
| 262 | |
return model; |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
private MetadataManager initOJB() |
| 271 | |
{ |
| 272 | |
try |
| 273 | |
{ |
| 274 | |
if (_ojbPropertiesFile == null) |
| 275 | |
{ |
| 276 | |
_ojbPropertiesFile = new File("OJB.properties"); |
| 277 | |
if (!_ojbPropertiesFile.exists()) |
| 278 | |
{ |
| 279 | |
throw new BuildException("Could not find OJB.properties, please specify it via the ojbpropertiesfile attribute"); |
| 280 | |
} |
| 281 | |
} |
| 282 | |
else |
| 283 | |
{ |
| 284 | |
if (!_ojbPropertiesFile.exists()) |
| 285 | |
{ |
| 286 | |
throw new BuildException("Could not load the specified OJB properties file "+_ojbPropertiesFile); |
| 287 | |
} |
| 288 | |
log("Using properties file "+_ojbPropertiesFile.getAbsolutePath(), Project.MSG_INFO); |
| 289 | |
System.setProperty("OJB.properties", _ojbPropertiesFile.getAbsolutePath()); |
| 290 | |
} |
| 291 | |
|
| 292 | |
MetadataManager metadataManager = MetadataManager.getInstance(); |
| 293 | |
RepositoryPersistor persistor = new RepositoryPersistor(); |
| 294 | |
|
| 295 | |
if (_repositoryFile != null) |
| 296 | |
{ |
| 297 | |
if (!_repositoryFile.exists()) |
| 298 | |
{ |
| 299 | |
throw new BuildException("Could not load the specified repository file "+_repositoryFile); |
| 300 | |
} |
| 301 | |
log("Loading repository file "+_repositoryFile.getAbsolutePath(), Project.MSG_INFO); |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
metadataManager.mergeConnectionRepository(persistor.readConnectionRepository(_repositoryFile.getAbsolutePath())); |
| 306 | |
metadataManager.mergeDescriptorRepository(persistor.readDescriptorRepository(_repositoryFile.getAbsolutePath())); |
| 307 | |
} |
| 308 | |
else if (metadataManager.connectionRepository().getAllDescriptor().isEmpty() && |
| 309 | |
metadataManager.getGlobalRepository().getDescriptorTable().isEmpty()) |
| 310 | |
{ |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
Properties props = new Properties(); |
| 315 | |
|
| 316 | |
props.load(new FileInputStream(_ojbPropertiesFile)); |
| 317 | |
|
| 318 | |
String repositoryPath = props.getProperty("repositoryFile", "repository.xml"); |
| 319 | |
File repositoryFile = new File(repositoryPath); |
| 320 | |
|
| 321 | |
if (!repositoryFile.exists()) |
| 322 | |
{ |
| 323 | |
repositoryFile = new File(_ojbPropertiesFile.getParentFile(), repositoryPath); |
| 324 | |
} |
| 325 | |
metadataManager.mergeConnectionRepository(persistor.readConnectionRepository(repositoryFile.getAbsolutePath())); |
| 326 | |
metadataManager.mergeDescriptorRepository(persistor.readDescriptorRepository(repositoryFile.getAbsolutePath())); |
| 327 | |
} |
| 328 | |
|
| 329 | |
if (metadataManager.getDefaultPBKey() == null) |
| 330 | |
{ |
| 331 | |
for (Iterator it = metadataManager.connectionRepository().getAllDescriptor().iterator(); it.hasNext();) |
| 332 | |
{ |
| 333 | |
JdbcConnectionDescriptor descriptor = (JdbcConnectionDescriptor)it.next(); |
| 334 | |
|
| 335 | |
if (descriptor.isDefaultConnection()) |
| 336 | |
{ |
| 337 | |
metadataManager.setDefaultPBKey(new PBKey(descriptor.getJcdAlias(), descriptor.getUserName(), descriptor.getPassWord())); |
| 338 | |
break; |
| 339 | |
} |
| 340 | |
} |
| 341 | |
} |
| 342 | |
return metadataManager; |
| 343 | |
} |
| 344 | |
catch (Exception ex) |
| 345 | |
{ |
| 346 | |
if (ex instanceof BuildException) |
| 347 | |
{ |
| 348 | |
throw (BuildException)ex; |
| 349 | |
} |
| 350 | |
else |
| 351 | |
{ |
| 352 | |
throw new BuildException(ex); |
| 353 | |
} |
| 354 | |
} |
| 355 | |
} |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
public void execute() throws BuildException |
| 361 | |
{ |
| 362 | |
if (_commands.isEmpty()) |
| 363 | |
{ |
| 364 | |
log("No sub tasks specified, so there is nothing to do.", Project.MSG_INFO); |
| 365 | |
return; |
| 366 | |
} |
| 367 | |
|
| 368 | |
ClassLoader sysClassLoader = Thread.currentThread().getContextClassLoader(); |
| 369 | |
AntClassLoader newClassLoader = new AntClassLoader(getClass().getClassLoader(), true); |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
Thread.currentThread().setContextClassLoader(newClassLoader); |
| 374 | |
|
| 375 | |
try |
| 376 | |
{ |
| 377 | |
MetadataManager manager = initOJB(); |
| 378 | |
Database dbModel = readModel(); |
| 379 | |
DescriptorRepository objModel = manager.getGlobalRepository(); |
| 380 | |
|
| 381 | |
if (dbModel == null) |
| 382 | |
{ |
| 383 | |
throw new BuildException("No database model specified"); |
| 384 | |
} |
| 385 | |
for (Iterator it = _commands.iterator(); it.hasNext();) |
| 386 | |
{ |
| 387 | |
Command cmd = (Command)it.next(); |
| 388 | |
|
| 389 | |
cmd.setPlatform(getPlatform()); |
| 390 | |
cmd.execute(this, dbModel, objModel); |
| 391 | |
} |
| 392 | |
} |
| 393 | |
finally |
| 394 | |
{ |
| 395 | |
|
| 396 | |
Thread.currentThread().setContextClassLoader(sysClassLoader); |
| 397 | |
} |
| 398 | |
} |
| 399 | |
} |