📄 dbconfig.java
字号:
else id = ++globalInfo.nextDBID; } } // returns the user uid for the user with the name specified as // argument and the index in the users list, // throws a DBConfigNonexistentUser exception if the user with that name // does not exist private ID getUid(String user) throws DBConfigNonexistentUser { for (int i = 0; i < users.size(); i++) if (((User)users.get(i)).name.equals(user)) return new ID(((User)users.get(i)).uid, i); throw new DBConfigNonexistentUser("Nonexistent user (" + user + ")."); } // returns the group gid associated with the group whose name is // specified as argument and the index in the groups list, // throws a DBConfigNonexistentGroup exception if the group // with that name does not exist private ID getGid(String group) throws DBConfigNonexistentGroup { for (int i = 0; i < groups.size(); i++) if (((Group)groups.get(i)).name.equals(group)) return new ID(((Group)groups.get(i)).gid, i); throw new DBConfigNonexistentGroup("Nonexistent group ( " + group + " )."); } // returns the algorithm aid of the algorithm whose name is // specified as argument and the index in the algorithms list, // throws a DBConfigNonexistentAlgorithm exception if the // algorithm with that name does not exist private ID getAid(String algorithm) throws DBConfigNonexistentAlgorithm { for (int i = 0; i < algorithms.size(); i++) if (((Algorithm)algorithms.get(i)).name.equals(algorithm)) return new ID(((Algorithm)algorithms.get(i)).aid, i); throw new DBConfigNonexistentAlgorithm("Nonexistent algorithm ( " + algorithm + " )."); } // returns the database dbid of the database whose name is // specified as argument and the index in the databases list, // throws a DBConfigNonexistentDatabase exception if the // database with that name does not exist private ID getDbid(String database) throws DBConfigNonexistentDatabase { for (int i = 0; i < databases.size(); i++) if (((Database)databases.get(i)).name.equals(database)) return new ID(((Database)databases.get(i)).dbid, i); throw new DBConfigNonexistentDatabase("Nonexistent database ( " + database+ " )."); } // checks if the user having uid uID.id is the owner of the group // having gID.id, throws a DBConfigException if it is not private void check_owner_group(ID uID, ID gID) throws DBConfigException { // check if uID.id is the owner of gID.id if (((Group)groups.get(gID.index)).uid != uID.id) throw new DBConfigException("User is not the owner of the group."); } // checks if the user having uid uID.id is the owner of the algorithm // having aid aID.id, throws a DBConfigException if it is not private void check_owner_algorithm(ID uID, ID aID) throws DBConfigException { // check if uid is the owner of aid if (((Algorithm)algorithms.get(aID.index)).uid != uID.id) throw new DBConfigException("User is not the owner of the algorithm."); } // checks if the user having uid uID.id is the owner of the database // having dbid dbID.id, throws a DBConfigException if it is not private void check_owner_database(ID uID, ID dbID) throws DBConfigException { // check if uid is the owner of dbid if (((Database)databases.get(dbID.index)).uid != uID.id) throw new DBConfigException("User is not the owner of the database"); } // checks if uid uID.id belongs to the "admin" group // throws a DBConfigException if not private void check_belong_admin(ID uID) throws DBConfigException { for (int i = 0; i < usersGroups.size(); i++) if (((UserGroup)usersGroups.get(i)).uid == uID.id && ((UserGroup)usersGroups.get(i)).gid == ADMIN_GID) return; throw new DBConfigException("User does not belong to the admin group."); } // checks if uid uID.id has permissions to add new groups // throws a DBConfigException if not private void check_perm_groups(ID uID) throws DBConfigException { // if the uid has permissions to add new groups return if ((((User)users.get(uID.index)).permissions & ADD_NEW_GROUPS) == ADD_NEW_GROUPS) return; throw new DBConfigException("User has no permission to add new groups."); } // checks if uid uID.id has permissions to add new algorithms // throws a DBConfigException if not private void check_perm_algorithms(ID uID) throws DBConfigException { // if the uid has permissions to add new algorithms return if ((((User)users.get(uID.index)).permissions & ADD_NEW_ALGORITHMS) == ADD_NEW_ALGORITHMS) return; throw new DBConfigException("User has no permission to add new algorithms."); } // checks if uid uID.id has permissions to add new databases // throws a DBConfigException if not private void check_perm_databases(ID uID) throws DBConfigException { // if the uid has permissions to add new databases return if ((((User)users.get(uID.index)).permissions & ADD_NEW_DATABASES) == ADD_NEW_DATABASES) return; throw new DBConfigException("User has no permission to add new databases."); } // delete the user with the ID uID // deletes the user from the users list // deletes all the user - group associations // all the resources with the owner user will be // inherited by the user ADMIN_UID private synchronized void deleteUser(ID uID) { int i; // remove user from users list users.remove(uID.index); // remove all uid - gid associations from usersGroups list for (i = 0; i < usersGroups.size(); i++) if (((UserGroup)usersGroups.get(i)).uid == uID.id) { usersGroups.remove(i); //stay on the same position i--; } // all the groups with the owner uid will have the owner ADMIN_UID for (i = 0; i < groups.size(); i++) if ( ((Group)groups.get(i)).uid == uID.id) ((Group)groups.get(i)).uid = ADMIN_UID; // all the algorithms with the owner uid will have the owner ADMIN_UID for (i = 0; i < algorithms.size(); i++) if(((Algorithm)algorithms.get(i)).uid == uID.id) ((Algorithm)algorithms.get(i)).uid = ADMIN_UID; // all the databases with the owner uid will have the owner ADMIN_UID for (i = 0; i < databases.size(); i++) if(((Database)databases.get(i)).uid == uID.id) ((Database)databases.get(i)).uid = ADMIN_UID; } // returns the gid of the database with dbid dbID.id // dbID should be a valid id private int getGidOfDatabase(ID dbID) { return ((Database)databases.get(dbID.index)).gid; } // returns the gid of the algorithm with aid aID.id // aid should be a valid id private int getGidOfAlgorithm(ID aID) { return ((Algorithm)algorithms.get(aID.index)).gid; } // eliminates duplicates from the Vector v // leaving only distinct values in v private void eliminateDuplicates(Vector v) { Hashtable ht = new Hashtable(); for(int i = 0; i < v.size(); i++) ht.put(v.get(i), ""); v.clear(); Enumeration keys = ht.keys(); while (keys.hasMoreElements()) v.add(keys.nextElement()); } /** * DBConfig is a Singleton, and this is the method that * allows you to get a reference to it. * * @return Returns the unique instance of this class. * If a serialized version of the instance exists in the file * DBConfig.CONFIG_FILE this version is returned, otherwise * a new instance is created and returned. * @exception DBConfigException The operation can not be completed. */ public static synchronized DBConfig getDBConfig() throws DBConfigException { // if the dbconfig reference is not null, return it if (dbconfig != null) return dbconfig; try { File file = new File(CONFIG_FILE); if (file.exists()) { // a serialized version of this object exists // deserialize the object ObjectInputStream input = new ObjectInputStream(new FileInputStream(file)); dbconfig = (DBConfig)input.readObject(); input.close(); } else dbconfig = new DBConfig(); // return the reference return dbconfig; } catch (Exception e) { throw new DBConfigException("File corrupted " + CONFIG_FILE + "."); } } /** * Serializes the unique instance of this class in the file * DBConfig.CONFIG_FILE * * @exception DBConfigException The operation can not be completed. */ public static synchronized void saveDBConfig() throws DBConfigException { // the dbconfig should not be null // since this function should be called after getDBConfig if (dbconfig == null) throw new DBConfigException("Internal error - nothing to serialize."); try { ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(CONFIG_FILE)); output.writeObject(dbconfig); output.close(); } catch (Exception e) { System.out.println(e); throw new DBConfigException("Error writing " + CONFIG_FILE + "."); } } /** * Sends a DBConfig object to the stream received as * argument. * * @param out The stream on which the serialization of the DBConfig will * be put. * @exception DBConfigException The operation can not be completed. * @exception IOException */ public static synchronized void sendUpdate(ObjectOutputStream out) throws DBConfigException, IOException { // create a brand new DBConfig object that will be filled with // the values of THE DBconfig object, with the exception of the // passwords (we don't want to send them to clients) DBConfig db_to_send = new DBConfig(); if (dbconfig == null) throw new DBConfigException("Internal error, null DBConfig reference."); // set the GlobalInfo reference db_to_send.globalInfo = dbconfig.globalInfo; // remove all users from db_to_send db_to_send.users.clear(); // add clones of all the users from dbconfig for (int i = 0; i < dbconfig.users.size(); i++) try { db_to_send.users.add(((User)dbconfig.users.get(i)).clone()); } catch (CloneNotSupportedException e) { throw new DBConfigException("Internal error, cannot clone a User!"); } // delete all the passwords for (int i = 0; i < db_to_send.users.size(); i++) ((User)(db_to_send.users.get(i))).password = ""; // set all the groups from dbconfig db_to_send.groups = dbconfig.groups; // set all the algorithms from dbconfig db_to_send.algorithms = dbconfig.algorithms; // set all the databases from dbconfig db_to_send.databases = dbconfig.databases; // set all the usersGroups from dbconfig db_to_send.usersGroups = dbconfig.usersGroups; // first reset the stream since it likes to cache references // and we don't want that to happen out.reset(); // write the new dbconfig object on the output stream out.writeObject(db_to_send); out.flush(); } /** * Reads the DBConfig from the input stream received as argument. * * @param in The stream from which the DBConfig is read. * @exception OptionalDataException * @exception ClassNotFoundException * @exception IOException */ public static synchronized void updateFrom(ObjectInputStream in) throws OptionalDataException, ClassNotFoundException, IOException { dbconfig = (DBConfig)in.readObject(); } // the first parameter of all the following methods identifies the // user that makes the call such that the system can check what is // he actually allowed to do. // in the following group related methods, variable name refers to // group name. /** * Adds a new group and specifies what users belong to it. * The <code>user</code> will be the owner of the group. * The owner of the group belongs also to the group. The owner can be * included or not in the list of users specified as the third argument, * it will be added anyway to the group. * * @exception DBConfigException The operation can not be completed. * @param user The owner of the new group. This user should have * permissions to add new groups in the system. * @param name The name of the new group. * @param users The list of users that belong to the new group. */ public synchronized void addGroup(String user, String name, Vector users) throws DBConfigException { // check arguments for validity if (user == null || name == null || users == null) throw new DBConfigException("Invalid addGroup() parameters!"); try { for (int i = 0; i < users.size(); i++) { String u = (String)users.get(i); if (u == null) throw new DBConfigException("Invalid addGroup() parameters!"); } } catch (ClassCastException e) { throw new DBConfigException("Invalid addGroup() parameters!"); } // if the user does not exist // a DBConfigNonexistentUser is thrown ID uID = getUid(user); // check if uid has permissions to add new groups // throws a DBConfigException if not check_perm_groups(uID); // check if there is already a group with same name try { getGid(name); } catch (DBConfigNonexistentGroup e) { // the name of this group is not already in use int gid; // add the new group to the groups list groups.add(new Group(gid = getNextGID(), name, uID.id)); // check if owner is in the list of users boolean in_list = false; for (int i = 0; i < users.size(); i++) if (users.get(i).equals(user)) in_list = true; // add the owner - group association // if owner is not in the list of users if (!in_list) usersGroups.add(new UserGroup(uID.id, gid)); // eliminate possible duplicates of users eliminateDuplicates(users); // add the user - group associations // for all users in the list boolean all = true; for (int i = 0; i < users.size(); i++) { try { // throws a DBConfigNonexistentUser if the user does not exist uID = getUid((String)users.get(i)); usersGroups.add(new UserGroup(uID.id, gid)); } catch(DBConfigNonexistentUser e1) { all = false; } } // save DBConfig object to the disk saveDBConfig(); if (all) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -