📄 dbconfig.java
字号:
* @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * an algorithm with the name <code>name</code> does not exist or * the algorithm is scheduled to be deleted. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the algorithm or he should belong to the * admin group. * @param name The name of the algorithm. * @param newGroup The name of the new group. */ public synchronized void changeAlgorithmGroup(String user, String name, String newGroup) throws DBConfigException { // check arguments for validity if (user == null || name == null || newGroup == null) throw new DBConfigException("Invalid changeAlgorithmGroup() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID aID = getAid(name); // if algorithm is scheduled to be deleted, throw an exception if (((Algorithm)algorithms.get(aID.index)).toBeDeleted) throw new DBConfigException("This algorithm is scheduled to be deleted, it can no longer be used or modified."); ID gID = getGid(newGroup); // check if uid is the admin try { check_belong_admin(uID); } catch (DBConfigException e) { // uid is not in admin group // check if it is the owner of the algorithm // look for aid in the list of algorithms // if uid is not the owner of aid a DBConfigException // is thrown check_owner_algorithm(uID, aID); } // if this point is reached uid is in admin or // uid is the owner of the algorithm aid // change the group of the algorithm ((Algorithm)algorithms.get(aID.index)).gid = gID.id; // save DBConfig object to the disk saveDBConfig(); } /** * Increment an algorithm's usage count. * * @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * an algorithm with the name <code>name</code> does not exist or * the algorithm is scheduled to be deleted. * @param user The initiator of the operation. The <code>user</code> * should belong to the admin group, or he should be the owner of * the algorithm or he should belong to the group of the algorithm. * @param name The name of the algorithm. */ public synchronized void markAlgorithmInUse(String user, String name) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid markAlgorithmInUse() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID aID = getAid(name); // if algorithm is scheduled to be deleted, throw an exception if (((Algorithm)algorithms.get(aID.index)).toBeDeleted) throw new DBConfigException("The algorithm is scheduled to be deleted and cannot be used."); // get the gid of the algorithm // aid is guaranteed to exist therefore gid is // guaranteed to exist int gid = getGidOfAlgorithm(aID); // check if uid is the admin try { check_belong_admin(uID); } catch(DBConfigException e) { // uid is not in admin group // check if it is the owner of the algorithm // look for aid in the list of algorithms // if uid is not the owner of aid a DBConfigException // is thrown try { check_owner_algorithm(uID, aID); } catch (DBConfigException e1) { // uid is not the owner of aid // check if it belongs to gid int j; for (j = 0; j < usersGroups.size(); j++) if (((UserGroup)usersGroups.get(j)).uid == uID.id && ((UserGroup)usersGroups.get(j)).gid == gid) { // uid belongs to gid // mark algorithm in use // aid is guaranteed to exist ! ((Algorithm)algorithms.get(aID.index)).countUsage++; // save DBConfig object to the disk saveDBConfig(); return; } // uid does not belong to gid if (j == usersGroups.size()) throw new DBConfigException("No permission to use the algorithm."); } } // if this point is reached uid is in admin or // uid is the owner of the algorithm aid // mark algorithm in use // aid is guaranteed to exist ! ((Algorithm)algorithms.get(aID.index)).countUsage++; // save DBConfig object to the disk saveDBConfig(); } /** * Decrement an algorithm's usage count. * * @exception DBConfigException The operation can not be completed, * an algorithm with the name <code>name</code> does not exist. * @param name The name of the algorithm. */ public synchronized void unmarkAlgorithmInUse(String name) throws DBConfigException { // check arguments for validity if (name == null) throw new DBConfigException("Invalid unmarkAlgorithmInUse() parameters!"); // if the algorithm does not exist // a DBConfigException is thrown ID aID = getAid(name); int count; count = --((Algorithm)algorithms.get(aID.index)).countUsage; // save DBConfig object to the disk saveDBConfig(); if (count < 0) throw new DBConfigException("DBConfig internal error (unmarkAlgorithmInUse)."); // if the algorithm was scheduled to be deleted // and the countUsage reaches 0, delete the algorithm if (count == 0 && ((Algorithm)algorithms.get(aID.index)).toBeDeleted == true) { // delete the algorithm algorithms.remove(aID.index); // save DBConfig object to the disk saveDBConfig(); try { File file_to_delete = new File(name + ".jar"); if(file_to_delete.delete() == false) System.err.println("Could not delete " + name + ".jar"); } catch (Throwable e) { throw new DBConfigException("Some errors occured: " + e); } } } /** * Returns the use count for an algorithm. * * @exception DBConfigException The operation can not be completed, * an algorithm with the name <code>name</code> does not exist. * @param name The name of the algorithm. * @return The use count for the algorithm. */ public synchronized int getAlgorithmUseCount(String name) throws DBConfigException { // check arguments for validity if (name == null) throw new DBConfigException("Invalid getAlgorithmUseCount() parameters!"); // if the algorithm does not exist // a DBConfigException is thrown ID aID = getAid(name); return ((Algorithm)algorithms.get(aID.index)).countUsage; } // in the following database related methods, variable name refers to // database name. /** * Adds a new database. The <code>user</code> will be the owner * of the database. * * @exception DBConfigException the operation can not be completed, * a user with the name <code>user</code> does not exist or * a group with the name <code>group</code> does not exist or * the databse name <code>name</code> is already in use. * @param user The owner of the new database. This user should have * permissions to add new databases in the system. * @param name The name of the database. * @param group The group to which the database belongs. */ public synchronized void addDatabase(String user, String name, String group) throws DBConfigException { // check arguments for validity if (user == null || name == null || group == null) throw new DBConfigException("Invalid addDatabase() parameters!"); // throws a DBConfigException if the user or group do not exist ID uID = getUid(user); // initiator of the operation ID gID = getGid(group); // the group to which the database belongs // checks if the user has permissions to add a new database // throws a DBConfigException if not check_perm_databases(uID); // check if a database with name already exists try { getDbid(name); } catch (DBConfigException e) { // the name for this database is not already in use // add the new database and return databases.add(new Database(getNextDBID(), name, uID.id, gID.id)); // save DBConfig object to the disk saveDBConfig(); return; } // if this point is reached, no exception was thrown in the try above // this name of the database is already in use // can not complete the operation throw new DBConfigException("The database name " + name + " is already in use."); } /** * Deletes a database. * * @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * a database with the name <code>name</code> does not exist. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the database or he should belong to the * admin group. * @param name The name of the database to be deleted. */ public synchronized void deleteDatabase(String user, String name) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid deleteDatabase() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID dbID = getDbid(name); if (dbID.id < FIRST_FREE_DBID) throw new DBConfigException("Cannot remove " + name); // check if uid is the admin try { check_belong_admin(uID); } catch (DBConfigException e) { // uid is not in admin group // check if it is the owner of the database // look for aid in the list of databases // if uid is not the owner of dbid a DBConfigException // is thrown check_owner_database(uID, dbID); } // if this point is reached uid is in admin or // uid is the owner of the database dbid // if database is used just mark it for deletion if (((Database)databases.get(dbID.index)).countUsage > 0) ((Database)databases.get(dbID.index)).toBeDeleted = true; // otherwise remove the database from the list else { // find the directory of the database String dirName = getDirDatabase(name); // then remove the database! databases.remove(dbID.index); // save DBConfig object to the disk saveDBConfig(); try { File dir = new File(dirName); String dbdataName = dirName + "/dbdata" + dbID.id; String dbcacheName = dirName + "/dbcache" + dbID.id; File dbdata = new File(dbdataName); File dbcache = new File(dbcacheName); if (dbdata.exists()) { if (dbdata.delete() == false) System.err.println("Could not delete " + dbdataName); } else System.err.println(dbdataName + "does not exist!"); if (dbcache.exists()) { if(dbcache.delete() == false) System.err.println("Could not delete " + dbcacheName); } else System.err.println(dbcacheName + " does not exist!"); if(dir.delete() == false) System.err.println("Could not delete " + dirName); } catch (Throwable e) { throw new DBConfigException("Some errors occured: " + e); } } } /** * Changes the group of the database. * * @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * a database with the name <code>name</code> does not exist or * the database is scheduled to be deleted. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the database or he should belong to the * admin group. * @param name The name of the database. * @param newGroup The name of the new group. */ public synchronized void changeDatabaseGroup(String user, String name, String newGroup) throws DBConfigException { // check arguments for validity if (user == null || name == null || newGroup == null) throw new DBConfigException("Invalid changeDatabaseGroup() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID dbID = getDbid(name); // if database is scheduled to be deleted // throw an exaception if (((Database)databases.get(dbID.index)).toBeDeleted) throw new DBConfigException("This database is scheduled to be deleted, it can no longer be used or modified."); ID gID = getGid(newGroup); // check if uid is the admin try { check_belong_admin(uID); } catch (DBConfigException e) { // uid is not in admin group // check if it is the owner of the database // look for dbid in the list of databases // if uid is not the owner of dbid a DBConfigException // is thrown check_owner_database(uID, dbID); } // if this point is reached uid is in admin or // uid is the owner of the database dbid // change the group of the database ((Database)databases.get(dbID.index)).gid = gID.id; // save DBConfig object to the disk saveDBConfig(); } /** * Sets the database cache status. * * @exception DBConfigException The operation can not be completed, * a database with the name <code>name</code> does not exist. * @param name The name of the database. * @param status The new status. Status can have the following values * CACHE_NOT_CREATED = cache not created * CACHE_INITIAL_CREATION =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -