use of io.cdap.cdap.common.lang.DirectoryClassLoader in project cdap by caskdata.
the class DatasetTypeManager method addModule.
/**
* Add datasets module in a namespace
*
* @param datasetModuleId the {@link DatasetModuleId} to add
* @param className module class
* @param jarLocation location of the module jar
* @param force if true, an update will be allowed even if there are conflicts with other modules, or if
* removal of a type would break other modules' dependencies.
*/
public void addModule(final DatasetModuleId datasetModuleId, final String className, final Location jarLocation, final boolean force) throws DatasetModuleConflictException {
LOG.debug("adding module: {}, className: {}, jarLocation: {}", datasetModuleId, className, jarLocation == null ? "[local]" : jarLocation);
try {
TransactionRunners.run(transactionRunner, context -> {
final DatasetTypeTable datasetTypeTable = DatasetTypeTable.create(context);
final DatasetInstanceTable datasetInstanceTable = new DatasetInstanceTable(context);
// 1. get existing module with all its types
DatasetModuleMeta existing = datasetTypeTable.getModule(datasetModuleId);
DependencyTrackingRegistry reg;
// 2. unpack jar and create class loader
ClassLoaderFolder classLoaderFolder = null;
DirectoryClassLoader cl = null;
try {
// NOTE: if jarLocation is null, we assume that this is a system module, ie. always present in classpath
if (jarLocation != null) {
classLoaderFolder = BundleJarUtil.prepareClassLoaderFolder(jarLocation, () -> Files.createTempDirectory(Files.createDirectories(systemTempPath), datasetModuleId.getEntityName()).toFile());
cl = new DirectoryClassLoader(classLoaderFolder.getDir(), cConf.get(Constants.AppFabric.PROGRAM_EXTRA_CLASSPATH), FilterClassLoader.create(getClass().getClassLoader()), "lib");
}
reg = new DependencyTrackingRegistry(datasetModuleId, datasetTypeTable, cl, force);
// 3. register the new module while tracking dependencies.
// this will fail if a type exists in a different module
DatasetDefinitionRegistries.register(className, cl, reg);
} catch (TypeConflictException e) {
// type conflict from the registry, we want to throw that as is
throw e;
} catch (Exception e) {
LOG.error("Could not instantiate instance of dataset module class {} for module {} using jarLocation {}", className, datasetModuleId, jarLocation);
throw Throwables.propagate(e);
} finally {
// Close the ProgramClassLoader
Closeables.closeQuietly(cl);
Closeables.closeQuietly(classLoaderFolder);
}
// 4. determine whether any type were removed from the module, and whether any other modules depend on them
if (existing != null) {
Set<String> removedTypes = new HashSet<>(existing.getTypes());
removedTypes.removeAll(reg.getTypes());
// TODO (CDAP-6294): track dependencies at the type level
if (!force && !removedTypes.isEmpty() && !existing.getUsedByModules().isEmpty()) {
throw new DatasetModuleConflictException(String.format("Cannot update module '%s' to remove types %s: Modules %s may depend on it. Delete them first", datasetModuleId, removedTypes, existing.getUsedByModules()));
}
Collection<DatasetSpecification> instances = datasetInstanceTable.getByTypes(datasetModuleId.getParent(), removedTypes);
if (!instances.isEmpty()) {
throw new DatasetModuleConflictException(String.format("Attempt to remove dataset types %s from module '%s' that have existing instances: %s. " + "Delete them first.", removedTypes, datasetModuleId, instances.stream().map(input -> input.getName() + ":" + input.getType()).collect(Collectors.joining(", "))));
}
}
// NOTE: we use set to avoid duplicated dependencies
// NOTE: we use LinkedHashSet to preserve order in which dependencies must be loaded
Set<String> moduleDependencies = new LinkedHashSet<String>();
for (DatasetTypeId usedType : reg.getUsedTypes()) {
DatasetModuleMeta usedModule = datasetTypeTable.getModuleByType(usedType);
if (usedModule == null) {
throw new IllegalStateException(String.format("Found a null used module for type %s for while adding module %s", usedType, datasetModuleId));
}
// adding all used types and the module itself, in this very order to keep the order of loading modules
// for instantiating a type
moduleDependencies.addAll(usedModule.getUsesModules());
boolean added = moduleDependencies.add(usedModule.getName());
if (added) {
// also adding this module as a dependent for all modules it uses
usedModule.addUsedByModule(datasetModuleId.getEntityName());
datasetTypeTable.writeModule(usedType.getParent(), usedModule);
}
}
URI jarURI = jarLocation == null ? null : jarLocation.toURI();
DatasetModuleMeta moduleMeta = existing == null ? new DatasetModuleMeta(datasetModuleId.getEntityName(), className, jarURI, reg.getTypes(), Lists.newArrayList(moduleDependencies)) : new DatasetModuleMeta(datasetModuleId.getEntityName(), className, jarURI, reg.getTypes(), Lists.newArrayList(moduleDependencies), Lists.newArrayList(existing.getUsedByModules()));
datasetTypeTable.writeModule(datasetModuleId.getParent(), moduleMeta);
});
} catch (RuntimeException e) {
for (Throwable cause : Throwables.getCausalChain(e)) {
if (cause instanceof DatasetModuleConflictException) {
throw (DatasetModuleConflictException) cause;
} else if (cause instanceof TypeConflictException) {
throw new DatasetModuleConflictException(cause.getMessage(), cause);
}
}
throw Throwables.propagate(e);
} catch (Exception e) {
LOG.error("Operation failed", e);
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.common.lang.DirectoryClassLoader in project cdap by caskdata.
the class AbstractArtifactManager method createClassLoader.
@Override
public CloseableClassLoader createClassLoader(@Nullable String namespace, ArtifactInfo artifactInfo, @Nullable ClassLoader parentClassLoader) throws IOException, UnauthorizedException {
ClassLoaderFolder folder = BundleJarUtil.prepareClassLoaderFolder(getArtifactLocation(artifactInfo, namespace), () -> DirUtils.createTempDir(tmpDir));
DirectoryClassLoader directoryClassLoader = new DirectoryClassLoader(folder.getDir(), parentClassLoader == null ? bootstrapClassLoader : parentClassLoader, "lib");
return new CloseableClassLoader(directoryClassLoader, new ClassLoaderCleanup(directoryClassLoader, folder));
}
use of io.cdap.cdap.common.lang.DirectoryClassLoader in project cdap by caskdata.
the class ArtifactClassLoaderFactory method createClassLoader.
/**
* Creates a multi level classloader where each location in the specified iterator corresponds to a classloader whose
* parent is built from the location behind it.
*
* @param artifactLocations the locations of the artifact to create the classloader from
* @return a closeable classloader based off the specified artifacts; on closing the returned {@link ClassLoader},
* all temporary resources created for the classloader will be removed
* @see #createClassLoader(File)
*/
CloseableClassLoader createClassLoader(Iterator<Location> artifactLocations, EntityImpersonator entityImpersonator) {
if (!artifactLocations.hasNext()) {
throw new IllegalArgumentException("Cannot create a classloader without an artifact.");
}
Location artifactLocation = artifactLocations.next();
if (!artifactLocations.hasNext()) {
return createClassLoader(artifactLocation, entityImpersonator);
}
try {
ClassLoaderFolder classLoaderFolder = entityImpersonator.impersonate(() -> BundleJarUtil.prepareClassLoaderFolder(artifactLocation, () -> DirUtils.createTempDir(tmpDir)));
CloseableClassLoader parentClassLoader = createClassLoader(artifactLocations, entityImpersonator);
return new CloseableClassLoader(new DirectoryClassLoader(classLoaderFolder.getDir(), parentClassLoader, "lib"), () -> {
Closeables.closeQuietly(parentClassLoader);
Closeables.closeQuietly(classLoaderFolder);
});
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.common.lang.DirectoryClassLoader in project cdap by caskdata.
the class PostgreSqlStorageProvider method loadJDBCDriver.
private static void loadJDBCDriver(CConfiguration cConf, String storageImpl) {
String driverExtensionPath = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_DIRECTORY);
String driverName = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_DRIVER_NAME);
if (driverExtensionPath == null || driverName == null) {
throw new IllegalArgumentException("The JDBC driver directory and driver name must be specified.");
}
File driverExtensionDir = new File(driverExtensionPath, storageImpl);
if (!driverExtensionDir.exists()) {
throw new IllegalArgumentException("The JDBC driver driver " + driverExtensionDir + " does not exist.");
}
// Create a separate classloader for the JDBC driver, which doesn't have any CDAP dependencies in it.
ClassLoader driverClassLoader = new DirectoryClassLoader(driverExtensionDir, null);
try {
Driver driver = (Driver) Class.forName(driverName, true, driverClassLoader).newInstance();
// wrap the driver class and register it ourselves since the driver manager will not use driver from other
// classloader
JDBCDriverShim driverShim = new JDBCDriverShim(driver);
DriverManager.registerDriver(driverShim);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
throw Throwables.propagate(e);
}
LOG.info("Successfully loaded {} from {}", driverName, driverExtensionPath);
}
Aggregations