Search in sources :

Example 1 with ODatabaseLifecycleListener

use of com.orientechnologies.orient.core.db.ODatabaseLifecycleListener in project wicket-orientdb by OrienteerBAP.

the class OrientDbWebApplication method init.

@Override
protected void init() {
    super.init();
    Orient.instance().registerThreadDatabaseFactory(new DefaultODatabaseThreadLocalFactory(this));
    Orient.instance().addDbLifecycleListener(new ODatabaseLifecycleListener() {

        private ORecordHook createHook(Class<? extends ORecordHook> clazz, ODatabaseInternal iDatabase) {
            if (!(iDatabase instanceof ODatabaseDocument))
                return null;
            try {
                return (ORecordHook) clazz.getConstructor(ODatabaseDocument.class).newInstance(iDatabase);
            } catch (Exception e) {
                try {
                    return (ORecordHook) clazz.newInstance();
                } catch (Exception e1) {
                    throw new IllegalStateException("Can't initialize hook " + clazz.getName(), e);
                }
            }
        }

        @Override
        public void onOpen(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
        }

        @Override
        public void onCreate(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
            // Fix for "feature" appeared in OrientDB 2.1.1
            // Issue: https://github.com/orientechnologies/orientdb/issues/4906
            fixOrientDBRights(iDatabase);
        }

        public void registerHooks(ODatabaseInternal iDatabase) {
            Set<ORecordHook> hooks = iDatabase.getHooks().keySet();
            List<Class<? extends ORecordHook>> hooksToRegister = new ArrayList<Class<? extends ORecordHook>>(getOrientDbSettings().getORecordHooks());
            for (ORecordHook hook : hooks) {
                if (hooksToRegister.contains(hook.getClass()))
                    hooksToRegister.remove(hook.getClass());
            }
            for (Class<? extends ORecordHook> oRecordHookClass : hooksToRegister) {
                ORecordHook hook = createHook(oRecordHookClass, iDatabase);
                if (hook != null) {
                    if (hook instanceof IHookPosition) {
                        iDatabase.registerHook(hook, ((IHookPosition) hook).getPosition());
                    } else {
                        iDatabase.registerHook(hook);
                    }
                }
            }
        }

        @Override
        public void onClose(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        @Override
        public void onDrop(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        public PRIORITY getPriority() {
            return PRIORITY.REGULAR;
        }

        @Override
        public void onCreateClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onDropClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onLocalNodeConfigurationRequest(ODocument arg0) {
        /*NOP*/
        }
    });
    getRequestCycleListeners().add(newTransactionRequestCycleListener());
    getRequestCycleListeners().add(new OrientDefaultExceptionsHandlingListener());
    getSecuritySettings().setAuthorizationStrategy(new WicketOrientDbAuthorizationStrategy(this));
    getApplicationListeners().add(new IApplicationListener() {

        @Override
        public void onAfterInitialized(Application application) {
            Orient.instance().startup();
            Orient.instance().removeShutdownHook();
        }

        @Override
        public void onBeforeDestroyed(Application application) {
            Orient.instance().shutdown();
        }
    });
    getAjaxRequestTargetListeners().add(new FixFormEncTypeListener());
    // workaround to support changing system users passwords in web interface
    getOrientDbSettings().getORecordHooks().add(OUserCatchPasswordHook.class);
    PropertyResolver.setLocator(this, new ODocumentPropertyLocator(new PropertyResolver.CachingPropertyLocator(new PropertyResolver.DefaultPropertyLocator())));
}
Also used : Set(java.util.Set) ODocumentPropertyLocator(ru.ydn.wicket.wicketorientdb.utils.ODocumentPropertyLocator) IHookPosition(ru.ydn.wicket.wicketorientdb.components.IHookPosition) PropertyResolver(org.apache.wicket.core.util.lang.PropertyResolver) FixFormEncTypeListener(ru.ydn.wicket.wicketorientdb.utils.FixFormEncTypeListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ArrayList(java.util.ArrayList) List(java.util.List) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) InvocationTargetException(java.lang.reflect.InvocationTargetException) ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) WicketOrientDbAuthorizationStrategy(ru.ydn.wicket.wicketorientdb.security.WicketOrientDbAuthorizationStrategy) IApplicationListener(org.apache.wicket.IApplicationListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) AuthenticatedWebApplication(org.apache.wicket.authroles.authentication.AuthenticatedWebApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 2 with ODatabaseLifecycleListener

use of com.orientechnologies.orient.core.db.ODatabaseLifecycleListener in project orientdb by orientechnologies.

the class OSchemaShared method doCreateClass.

private OClass doCreateClass(final String className, final int clusters, final int retry, OClass... superClasses) {
    OClass result;
    final ODatabaseDocumentInternal db = getDatabase();
    final OStorage storage = db.getStorage();
    StringBuilder cmd = null;
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
    if (superClasses != null)
        OClassImpl.checkParametersConflict(Arrays.asList(superClasses));
    acquireSchemaWriteLock();
    try {
        final String key = className.toLowerCase(Locale.ENGLISH);
        if (classes.containsKey(key) && retry == 0)
            throw new OSchemaException("Class '" + className + "' already exists in current database");
        cmd = new StringBuilder("create class ");
        // if (getDatabase().getStorage().getConfiguration().isStrictSql())
        // cmd.append('`');
        cmd.append(className);
        // if (getDatabase().getStorage().getConfiguration().isStrictSql())
        // cmd.append('`');
        List<OClass> superClassesList = new ArrayList<OClass>();
        if (superClasses != null && superClasses.length > 0) {
            boolean first = true;
            for (OClass superClass : superClasses) {
                // Filtering for null
                if (superClass != null) {
                    if (first)
                        cmd.append(" extends ");
                    else
                        cmd.append(", ");
                    cmd.append(superClass.getName());
                    first = false;
                    superClassesList.add(superClass);
                }
            }
        }
        if (clusters == 0)
            cmd.append(" abstract");
        else {
            cmd.append(" clusters ");
            cmd.append(clusters);
        }
        if (executeThroughDistributedStorage()) {
            final int[] clusterIds = createClusters(className, clusters);
            createClassInternal(className, clusterIds, superClassesList);
            final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
            OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
            commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
            final Object res = db.command(commandSQL).execute();
        } else if (storage instanceof OStorageProxy) {
            db.command(new OCommandSQL(cmd.toString())).execute();
            reload();
        } else {
            final int[] clusterIds = createClusters(className, clusters);
            createClassInternal(className, clusterIds, superClassesList);
        }
        result = classes.get(className.toLowerCase(Locale.ENGLISH));
        // WAKE UP DB LIFECYCLE LISTENER
        for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result);
    } catch (ClusterIdsAreEmptyException e) {
        throw OException.wrapException(new OSchemaException("Cannot create class '" + className + "'"), e);
    } finally {
        releaseSchemaWriteLock();
    }
    return result;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoshardedStorage(com.orientechnologies.orient.core.storage.OAutoshardedStorage) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)

Example 3 with ODatabaseLifecycleListener

use of com.orientechnologies.orient.core.db.ODatabaseLifecycleListener in project orientdb by orientechnologies.

the class OSchemaShared method doCreateClass.

private OClass doCreateClass(final String className, int[] clusterIds, int retry, OClass... superClasses) throws ClusterIdsAreEmptyException {
    OClass result;
    final ODatabaseDocumentInternal db = getDatabase();
    final OStorage storage = db.getStorage();
    StringBuilder cmd = null;
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
    if (superClasses != null)
        OClassImpl.checkParametersConflict(Arrays.asList(superClasses));
    acquireSchemaWriteLock();
    try {
        final String key = className.toLowerCase(Locale.ENGLISH);
        if (classes.containsKey(key) && retry == 0)
            throw new OSchemaException("Class '" + className + "' already exists in current database");
        if (!executeThroughDistributedStorage())
            checkClustersAreAbsent(clusterIds);
        if (clusterIds == null || clusterIds.length == 0) {
            clusterIds = createClusters(className, getDatabase().getStorage().getConfiguration().getMinimumClusters());
        }
        cmd = new StringBuilder("create class ");
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        cmd.append(className);
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        List<OClass> superClassesList = new ArrayList<OClass>();
        if (superClasses != null && superClasses.length > 0) {
            boolean first = true;
            for (OClass superClass : superClasses) {
                // Filtering for null
                if (superClass != null) {
                    if (first)
                        cmd.append(" extends ");
                    else
                        cmd.append(", ");
                    cmd.append('`').append(superClass.getName()).append('`');
                    first = false;
                    superClassesList.add(superClass);
                }
            }
        }
        if (clusterIds != null) {
            if (clusterIds.length == 1 && clusterIds[0] == -1)
                cmd.append(" abstract");
            else {
                cmd.append(" cluster ");
                for (int i = 0; i < clusterIds.length; ++i) {
                    if (i > 0)
                        cmd.append(',');
                    else
                        cmd.append(' ');
                    cmd.append(clusterIds[i]);
                }
            }
        }
        if (executeThroughDistributedStorage()) {
            createClassInternal(className, clusterIds, superClassesList);
            final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
            OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
            commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
            final Object res = db.command(commandSQL).execute();
        } else if (storage instanceof OStorageProxy) {
            db.command(new OCommandSQL(cmd.toString())).execute();
            reload();
        } else
            createClassInternal(className, clusterIds, superClassesList);
        result = classes.get(className.toLowerCase(Locale.ENGLISH));
        // WAKE UP DB LIFECYCLE LISTENER
        for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result);
    } finally {
        releaseSchemaWriteLock();
    }
    return result;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoshardedStorage(com.orientechnologies.orient.core.storage.OAutoshardedStorage) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)

Example 4 with ODatabaseLifecycleListener

use of com.orientechnologies.orient.core.db.ODatabaseLifecycleListener in project orientdb by orientechnologies.

the class OSchemaShared method dropClassInternal.

private void dropClassInternal(final String className) {
    acquireSchemaWriteLock();
    try {
        if (getDatabase().getTransaction().isActive())
            throw new IllegalStateException("Cannot drop a class inside a transaction");
        if (className == null)
            throw new IllegalArgumentException("Class name is null");
        getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE);
        final String key = className.toLowerCase(Locale.ENGLISH);
        final OClass cls = classes.get(key);
        if (cls == null)
            throw new OSchemaException("Class '" + className + "' was not found in current database");
        if (!cls.getSubclasses().isEmpty())
            throw new OSchemaException("Class '" + className + "' cannot be dropped because it has sub classes " + cls.getSubclasses() + ". Remove the dependencies before trying to drop it again");
        checkEmbedded(getDatabase().getStorage());
        for (OClass superClass : cls.getSuperClasses()) {
            // REMOVE DEPENDENCY FROM SUPERCLASS
            ((OClassImpl) superClass).removeBaseClassInternal(cls);
        }
        for (int id : cls.getClusterIds()) {
            if (id != -1)
                deleteCluster(getDatabase(), id);
        }
        dropClassIndexes(cls);
        classes.remove(key);
        if (cls.getShortName() != null)
            // REMOVE THE ALIAS TOO
            classes.remove(cls.getShortName().toLowerCase(Locale.ENGLISH));
        removeClusterClassMap(cls);
        // WAKE UP DB LIFECYCLE LISTENER
        for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onDropClass(getDatabase(), cls);
    } finally {
        releaseSchemaWriteLock();
    }
}
Also used : ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Aggregations

ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)4 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)2 OStorage (com.orientechnologies.orient.core.storage.OStorage)2 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)2 ODatabaseInternal (com.orientechnologies.orient.core.db.ODatabaseInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Application (org.apache.wicket.Application)1 IApplicationListener (org.apache.wicket.IApplicationListener)1 AuthenticatedWebApplication (org.apache.wicket.authroles.authentication.AuthenticatedWebApplication)1 PropertyResolver (org.apache.wicket.core.util.lang.PropertyResolver)1