String applicationVersion = getVersion();
DatabaseParams params = new DatabaseParams( databaseType.defaultParams );
params.setUrl( jdbcUrl );
DefaultPlexusContainer container = new DefaultPlexusContainer();
initializeWagon( container );
List<Artifact> artifacts = new ArrayList<Artifact>();
artifacts.addAll(
downloadArtifact( container, params.getGroupId(), params.getArtifactId(), params.getVersion() ) );
artifacts.addAll(
downloadArtifact( container, "org.apache.continuum", managementArtifactId, applicationVersion ) );
artifacts.addAll( downloadArtifact( container, "jpox", "jpox", databaseFormat.getJpoxVersion() ) );
List<File> jars = new ArrayList<File>();
// Little hack to make it work more nicely in the IDE
List<String> exclusions = new ArrayList<String>();
URLClassLoader cp = (URLClassLoader) DataManagementCli.class.getClassLoader();
for ( URL url : cp.getURLs() )
{
String urlEF = url.toExternalForm();
if ( urlEF.endsWith( "target/classes/" ) )
{
int idEndIdx = urlEF.length() - 16;
String id = urlEF.substring( urlEF.lastIndexOf( '/', idEndIdx - 1 ) + 1, idEndIdx );
// continuum-legacy included because the IDE doesn't do the proper assembly of enhanced classes and JDO metadata
if ( !"data-management-api".equals( id ) && !"data-management-cli".equals( id ) &&
!"continuum-legacy".equals( id ) && !"continuum-model".equals( id ) &&
!"redback-legacy".equals( id ) )
{
exclusions.add( "org.apache.continuum:" + id );
jars.add( new File( url.getPath() ) );
}
}
// Sometimes finds its way into the IDE. Make sure it is loaded in the extra classloader too
if ( urlEF.contains( "jpox-enhancer" ) )
{
jars.add( new File( url.getPath() ) );
}
}
ArtifactFilter filter = new ExcludesArtifactFilter( exclusions );
for ( Artifact a : artifacts )
{
if ( "jpox".equals( a.getGroupId() ) && "jpox".equals( a.getArtifactId() ) )
{
if ( a.getVersion().equals( databaseFormat.getJpoxVersion() ) )
{
jars.add( a.getFile() );
}
}
else if ( filter.include( a ) )
{
jars.add( a.getFile() );
}
}
ClassRealm realm = container.createComponentRealm( "app", jars );
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( realm );
ClassRealm oldRealm = container.setLookupRealm( realm );
DatabaseFactoryConfigurator configurator = (DatabaseFactoryConfigurator) container.lookup(
DatabaseFactoryConfigurator.class.getName(), configRoleHint, realm );
configurator.configure( params );
DataManagementTool manager =
(DataManagementTool) container.lookup( DataManagementTool.class.getName(), toolRoleHint, realm );
if ( mode == OperationMode.EXPORT )
{
manager.backupDatabase( directory );
}
else if ( mode == OperationMode.IMPORT )
{
manager.eraseDatabase();
manager.restoreDatabase( directory );
}
container.setLookupRealm( oldRealm );
Thread.currentThread().setContextClassLoader( oldLoader );
}