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())));
}
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;
}
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;
}
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();
}
}
Aggregations