use of org.exist.config.Configuration in project exist by eXist-db.
the class Constructor method load.
/**
* Create new java object by mapping instructions.
* @param newClazz object
* @param instance to load
* @param conf configuration
* @return new java object by mapping instructions.
*/
public static Object load(final NewClass newClazz, final Configurable instance, final Configuration conf) {
final String url = newClazz.mapper();
if (url == null) {
Configurator.LOG.error("Field must have 'ConfigurationFieldClassMask' annotation or registered mapping instruction for class '{}' [{}], skip instance creation.", newClazz.name(), conf.getName());
return null;
}
try (final InputStream is = instance.getClass().getClassLoader().getResourceAsStream(url)) {
if (is == null) {
Configurator.LOG.error("Registered mapping instruction for class '{}' missing resource '{}', skip instance creation.", newClazz.name(), url);
return null;
}
final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
try {
final XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
Object obj = null;
final Deque<Object> objs = new ArrayDeque<>();
final Deque<CallMethod> instructions = new ArrayDeque<>();
int eventType;
while (reader.hasNext()) {
eventType = reader.next();
switch(eventType) {
case XMLEvent.START_ELEMENT:
String localName = reader.getLocalName();
if ("class".equals(localName)) {
if (!"name".equals(reader.getAttributeLocalName(0))) {
Configurator.LOG.error("class element first attribute must be 'name', skip instance creation.");
return null;
}
final String clazzName = reader.getAttributeValue(0);
final Class<?> clazz = Class.forName(clazzName);
final MethodHandles.Lookup lookup = MethodHandles.lookup();
final MethodHandle methodHandle = lookup.findConstructor(clazz, methodType(void.class));
final Supplier<Object> constructor = (Supplier<Object>) LambdaMetafactory.metafactory(lookup, "get", methodType(Supplier.class), methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
final Object newInstance = constructor.get();
if (obj == null) {
obj = newInstance;
}
objs.push(newInstance);
if (!instructions.isEmpty()) {
instructions.peek().setValue(newInstance);
}
} else if ("callMethod".equals(localName)) {
Configuration _conf_ = conf;
if (!instructions.isEmpty()) {
_conf_ = instructions.peek().getConfiguration();
}
final CallMethod call = new CallMethod(objs.peek(), _conf_);
for (int i = 0; i < reader.getAttributeCount(); i++) {
call.set(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
}
instructions.push(call);
}
break;
case XMLEvent.END_ELEMENT:
localName = reader.getLocalName();
if ("class".equals(localName)) {
objs.pop();
} else if ("callMethod".equals(localName)) {
final CallMethod call = instructions.pop();
call.eval();
}
break;
}
}
configurations.put(obj, conf);
return obj;
} catch (final Throwable e) {
if (e instanceof InterruptedException) {
// NOTE: must set interrupted flag
Thread.currentThread().interrupt();
}
Configurator.LOG.error(e);
}
return null;
} catch (final IOException e) {
Configurator.LOG.error("Registered mapping instruction for class '{}' missing resource '{}', skip instance creation.", newClazz.name(), url);
return null;
}
}
use of org.exist.config.Configuration in project exist by eXist-db.
the class SecurityManagerImpl method processParameterBeforeSave.
@Override
public void processParameterBeforeSave(final DBBroker broker, final DocumentImpl document) {
XmldbURI uri = document.getCollection().getURI();
final boolean isRemoved = uri.endsWith(SecurityManager.REMOVED_COLLECTION_URI);
if (isRemoved) {
uri = uri.removeLastSegment();
}
final boolean isAccount = uri.endsWith(SecurityManager.ACCOUNTS_COLLECTION_URI);
final boolean isGroup = uri.endsWith(SecurityManager.GROUPS_COLLECTION_URI);
if (isAccount || isGroup) {
// uri = uri.removeLastSegment();
// String realmId = uri.lastSegment().toString();
// AbstractRealm realm = (AbstractRealm)findRealmForRealmId(realmId);
final Configuration conf = Configurator.parse(broker.getBrokerPool(), document);
saving.put(document.getURI(), conf.getPropertyInteger("id"));
}
}
use of org.exist.config.Configuration in project exist by eXist-db.
the class AbstractPrincipal method setCollection.
public final void setCollection(DBBroker broker, Collection collection) throws ConfigurationException {
if (collection != null) {
Configurator.unregister(configuration);
final Configuration _config_ = Configurator.parse(this, broker, collection, XmldbURI.create(name + ".xml"));
configuration = Configurator.configure(this, _config_);
}
}
use of org.exist.config.Configuration in project exist by eXist-db.
the class AbstractRealm method loadGroupsFromRealmStorage.
private void loadGroupsFromRealmStorage(final DBBroker broker) throws ConfigurationException, PermissionDeniedException, LockException {
if (collectionGroups != null && collectionGroups.getDocumentCount(broker) > 0) {
final AbstractRealm r = this;
for (final Iterator<DocumentImpl> i = collectionGroups.iterator(broker); i.hasNext(); ) {
final DocumentImpl doc = i.next();
final Configuration conf = Configurator.parse(broker.getBrokerPool(), doc);
final String name = conf.getProperty("name");
groupsByName.writeE(principalDb -> {
if (name != null && !principalDb.containsKey(name)) {
// Group group = instantiateGroup(this, conf);
final GroupImpl group = new GroupImpl(r, conf);
getSecurityManager().registerGroup(group);
principalDb.put(group.getName(), group);
// set collection
if (group.getId() > 0) {
group.setCollection(broker, collectionGroups);
}
}
});
}
}
}
use of org.exist.config.Configuration in project exist by eXist-db.
the class AbstractRealm method loadAccountsFromRealmStorage.
private void loadAccountsFromRealmStorage(final DBBroker broker) throws ConfigurationException, PermissionDeniedException, LockException {
// load accounts information
if (collectionAccounts != null && collectionAccounts.getDocumentCount(broker) > 0) {
final AbstractRealm r = this;
for (final Iterator<DocumentImpl> i = collectionAccounts.iterator(broker); i.hasNext(); ) {
final DocumentImpl doc = i.next();
final Configuration conf = Configurator.parse(broker.getBrokerPool(), doc);
final String name = conf.getProperty("name");
usersByName.writeE(principalDb -> {
if (name != null && !principalDb.containsKey(name)) {
// A account = instantiateAccount(this, conf);
final Account account;
try {
account = new AccountImpl(r, conf);
// ensure that the account has at least a primary group
if (account.getGroups().length == 0) {
try {
account.setPrimaryGroup(getGroup(SecurityManager.UNKNOWN_GROUP));
LOG.warn("Account '{}' has no groups, but every account must have at least 1 group. Assigned group: " + SecurityManager.UNKNOWN_GROUP, account.getName());
} catch (final PermissionDeniedException e) {
throw new ConfigurationException("Account has no group, unable to default to " + SecurityManager.UNKNOWN_GROUP + ": " + e.getMessage(), e);
}
}
} catch (Throwable e) {
LOG.error("Account object can't be built from '{}'", doc.getFileURI(), e);
return;
}
getSecurityManager().registerAccount(account);
principalDb.put(account.getName(), account);
// set collection
if (account.getId() > 0) {
((AbstractPrincipal) account).setCollection(broker, collectionAccounts);
// ensure that the account has at least a primary group
if (account.getGroups().length == 0) {
try {
account.setPrimaryGroup(getGroup(SecurityManager.UNKNOWN_GROUP));
LOG.warn("Account '{}' has no groups, but every account must have at least 1 group. Assigned group: " + SecurityManager.UNKNOWN_GROUP, account.getName());
} catch (final PermissionDeniedException e) {
throw new ConfigurationException("Account has no group, unable to default to " + SecurityManager.UNKNOWN_GROUP + ": " + e.getMessage(), e);
}
}
}
}
});
}
}
}
Aggregations