Search in sources :

Example 11 with Configuration

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;
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Configuration(org.exist.config.Configuration) InputStream(java.io.InputStream) IOException(java.io.IOException) MethodHandles(java.lang.invoke.MethodHandles) Supplier(java.util.function.Supplier) XMLInputFactory(javax.xml.stream.XMLInputFactory) MethodHandle(java.lang.invoke.MethodHandle)

Example 12 with Configuration

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"));
    }
}
Also used : Configuration(org.exist.config.Configuration) XmldbURI(org.exist.xmldb.XmldbURI)

Example 13 with Configuration

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_);
    }
}
Also used : Configuration(org.exist.config.Configuration)

Example 14 with Configuration

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);
                    }
                }
            });
        }
    }
}
Also used : Configuration(org.exist.config.Configuration) GroupImpl(org.exist.security.internal.GroupImpl) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 15 with Configuration

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);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : Configuration(org.exist.config.Configuration) ConfigurationException(org.exist.config.ConfigurationException) AccountImpl(org.exist.security.internal.AccountImpl) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

Configuration (org.exist.config.Configuration)16 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 InputStream (java.io.InputStream)4 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 Database (org.exist.Database)3 ConfigurationException (org.exist.config.ConfigurationException)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 AccountImpl (org.exist.security.internal.AccountImpl)3 Test (org.junit.Test)3 MethodHandle (java.lang.invoke.MethodHandle)2 MethodHandles (java.lang.invoke.MethodHandles)2 EXistException (org.exist.EXistException)2 Collection (org.exist.collections.Collection)2 AbstractRealm (org.exist.security.AbstractRealm)2 AuthenticationException (org.exist.security.AuthenticationException)2 Realm (org.exist.security.realm.Realm)2 BrokerPoolServiceException (org.exist.storage.BrokerPoolServiceException)2 DBBroker (org.exist.storage.DBBroker)2 AtomicLazyVal (com.evolvedbinary.j8fu.lazy.AtomicLazyVal)1 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1