use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class ExampleTrigger method afterCreateDocument.
@Override
public void afterCreateDocument(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
if (document.getFileURI().toString().contains("copied")) {
LOG.info("Prevent recreation of document {}", document.getFileURI().toString());
return;
}
LOG.info("Created document {}", document.getDocumentURI());
final byte[] data = "<a>dummy data</a>".getBytes();
final XmldbURI newDocumentURI = XmldbURI.create(document.getFileURI().toString() + "-copied.xml");
try (final Collection collection = broker.openCollection(document.getCollection().getURI(), Lock.LockMode.WRITE_LOCK)) {
// Stream into database
broker.storeDocument(txn, newDocumentURI, new StringInputSource(data), MimeType.XML_TYPE, collection);
} catch (Exception e) {
LOG.error(e);
throw new TriggerException(e);
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class Deployment method storeBinaryResources.
private void storeBinaryResources(final DBBroker broker, final Txn transaction, final Path directory, final Collection targetCollection, final Optional<RequestedPerms> requestedPerms, final List<String> errors) throws IOException, EXistException, PermissionDeniedException, LockException, TriggerException {
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
for (final Path entry : stream) {
if (!Files.isDirectory(entry)) {
final XmldbURI name = XmldbURI.create(FileUtils.fileName(entry));
try {
final Permission permission = PermissionFactory.getDefaultResourcePermission(broker.getBrokerPool().getSecurityManager());
setPermissions(broker, requestedPerms, false, MimeType.BINARY_TYPE, permission);
storeBinary(broker, transaction, targetCollection, entry, MimeType.BINARY_TYPE, name, permission);
} catch (final Exception e) {
LOG.error(e.getMessage(), e);
errors.add(e.getMessage());
}
}
}
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class Deployment method uninstall.
/**
* Delete the target collection of the package. If there's no repo.xml descriptor,
* target will be null.
*
* @param pkg
* @param target
* @throws PackageException
*/
private void uninstall(final DBBroker broker, final Txn transaction, final Package pkg, final Optional<ElementImpl> target) throws PackageException {
// determine target collection
final Optional<String> targetPath = target.map(ElementImpl::getStringValue).filter(s -> !s.isEmpty());
final XmldbURI targetCollection = targetPath.map(s -> XmldbURI.create(getTargetCollection(broker, s))).orElseGet(() -> getTargetFallback(pkg));
try {
Collection collection = broker.getOrCreateCollection(transaction, targetCollection);
if (collection != null) {
broker.removeCollection(transaction, collection);
}
if (target != null) {
final XmldbURI configCollection = XmldbURI.CONFIG_COLLECTION_URI.append(targetCollection);
collection = broker.getOrCreateCollection(transaction, configCollection);
if (collection != null) {
broker.removeCollection(transaction, collection);
}
}
} catch (final PermissionDeniedException | IOException | TriggerException e) {
LOG.error("Exception occurred while removing package.", e);
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class PluginsManagerImpl method start.
@Override
public void start(final DBBroker broker, final Txn transaction) throws EXistException {
boolean interrupted = false;
try {
try {
collection = broker.getCollection(COLLETION_URI);
if (collection == null) {
collection = broker.getOrCreateCollection(transaction, COLLETION_URI);
if (collection == null) {
return;
}
// if db corrupted it can lead to unrunnable issue
// throw new ConfigurationException("Collection '/db/system/plugins' can't be created.");
collection.setPermissions(broker, Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM);
broker.saveCollection(transaction, collection);
}
} catch (final TriggerException | PermissionDeniedException | IOException | LockException e) {
LOG.warn("Loading PluginsManager configuration failed: {}", e.getMessage());
}
final Configuration _config_ = Configurator.parse(this, broker, collection, CONFIG_FILE_URI);
configuration = Configurator.configure(this, _config_);
// load plugins by META-INF/services/
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
for (final Class<? extends Plug> pluginClazz : listServices(Plug.class)) {
try {
final MethodHandle methodHandle = lookup.findConstructor(pluginClazz, methodType(void.class, PluginsManager.class));
final Function<PluginsManager, Plug> ctor = (Function<PluginsManager, Plug>) LambdaMetafactory.metafactory(lookup, "apply", methodType(Function.class), methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
final Plug plgn = ctor.apply(this);
jacks.put(pluginClazz.getName(), plgn);
} catch (final Throwable e) {
if (e instanceof InterruptedException) {
// NOTE: must set interrupted flag
interrupted = true;
}
LOG.error(e);
}
}
} catch (final Throwable e) {
LOG.error(e);
}
for (final Plug jack : jacks.values()) {
jack.start(broker, transaction);
}
} finally {
if (interrupted) {
// NOTE: must set interrupted flag
Thread.currentThread().interrupt();
}
}
}
use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.
the class InTxnLocalCollectionManagementService method createCollection.
@Override
public Collection createCollection(final XmldbURI name, final Date created) throws XMLDBException {
final XmldbURI collName = resolve(name);
withDb((broker, transaction) -> {
try {
final org.exist.collections.Collection coll = broker.getOrCreateCollection(transaction, collName);
if (created != null) {
coll.setCreated(created.getTime());
}
broker.saveCollection(transaction, coll);
return null;
} catch (final TriggerException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
return new InTxnLocalCollection(user, brokerPool, collection, collName);
}
Aggregations