use of org.apache.asterix.external.library.ExternalLibrary in project asterixdb by apache.
the class ExternalLibraryUtils method installLibraryIfNeeded.
/**
* Each element of a library is installed as part of a transaction. Any
* failure in installing an element does not effect installation of other
* libraries.
*/
protected static void installLibraryIfNeeded(String dataverse, final File libraryDir, Map<String, List<String>> uninstalledLibs) throws Exception {
String libraryName = libraryDir.getName().trim();
List<String> uninstalledLibsInDv = uninstalledLibs.get(dataverse);
// was this library just un-installed?
boolean wasUninstalled = uninstalledLibsInDv != null && uninstalledLibsInDv.contains(libraryName);
MetadataTransactionContext mdTxnCtx = null;
try {
mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
Library libraryInMetadata = MetadataManager.INSTANCE.getLibrary(mdTxnCtx, dataverse, libraryName);
if (libraryInMetadata != null && !wasUninstalled) {
// exists in metadata and was not un-installed, we return.
// Another place which shows that our metadata transactions are broken
// (we didn't call commit before!!!)
MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
return;
}
// Add library
MetadataManager.INSTANCE.addLibrary(mdTxnCtx, new Library(dataverse, libraryName));
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Added library " + libraryName + " to Metadata");
}
// Get the descriptor
String[] libraryDescriptors = libraryDir.list((dir, name) -> name.endsWith(".xml"));
if (libraryDescriptors == null) {
throw new IOException("Unable to list files in directory " + libraryDir);
}
if (libraryDescriptors.length == 0) {
// should be fine. library was installed but its content was not added to metadata
MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
return;
} else if (libraryDescriptors.length > 1) {
throw new IllegalStateException("More than 1 library descriptors defined");
}
ExternalLibrary library = getLibrary(new File(libraryDir + File.separator + libraryDescriptors[0]));
// Get the dataverse
Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
if (dv == null) {
MetadataManager.INSTANCE.addDataverse(mdTxnCtx, new Dataverse(dataverse, NonTaggedDataFormat.NON_TAGGED_DATA_FORMAT, MetadataUtil.PENDING_NO_OP));
}
// Add functions
if (library.getLibraryFunctions() != null) {
for (LibraryFunction function : library.getLibraryFunctions().getLibraryFunction()) {
String[] fargs = function.getArguments().trim().split(",");
List<String> args = new ArrayList<>();
for (String arg : fargs) {
args.add(arg);
}
Function f = new Function(dataverse, libraryName + "#" + function.getName().trim(), args.size(), args, function.getReturnType().trim(), function.getDefinition().trim(), library.getLanguage().trim(), function.getFunctionType().trim(), 0);
MetadataManager.INSTANCE.addFunction(mdTxnCtx, f);
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Installed function: " + libraryName + "#" + function.getName().trim());
}
}
}
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Installed functions in library :" + libraryName);
}
// Add adapters
if (library.getLibraryAdapters() != null) {
for (LibraryAdapter adapter : library.getLibraryAdapters().getLibraryAdapter()) {
String adapterFactoryClass = adapter.getFactoryClass().trim();
String adapterName = libraryName + "#" + adapter.getName().trim();
AdapterIdentifier aid = new AdapterIdentifier(dataverse, adapterName);
DatasourceAdapter dsa = new DatasourceAdapter(aid, adapterFactoryClass, IDataSourceAdapter.AdapterType.EXTERNAL);
MetadataManager.INSTANCE.addAdapter(mdTxnCtx, dsa);
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Installed adapter: " + adapterName);
}
}
}
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Installed adapters in library :" + libraryName);
}
MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
} catch (Exception e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Exception in installing library " + libraryName, e);
}
MetadataManager.INSTANCE.abortTransaction(mdTxnCtx);
}
}
use of org.apache.asterix.external.library.ExternalLibrary in project asterixdb by apache.
the class ExternalLibraryUtils method getLibrary.
/**
* Get the library from the xml file
*
* @param libraryXMLPath
* @return
* @throws Exception
*/
private static ExternalLibrary getLibrary(File libraryXMLPath) throws Exception {
JAXBContext configCtx = JAXBContext.newInstance(ExternalLibrary.class);
Unmarshaller unmarshaller = configCtx.createUnmarshaller();
ExternalLibrary library = (ExternalLibrary) unmarshaller.unmarshal(libraryXMLPath);
return library;
}
Aggregations