1 package org.myoodb.admin; 25 26 import org.myoodb.*; 27 import org.myoodb.core.*; 28 29 public class AdministratorManagerDbImpl extends org.myoodb.collectable.CollectableDbImpl implements AdministratorManager 30 { 31 public static String ADMINISTRATOR_USERNAME = "admin"; 32 public static String ADMINISTRATOR_PASSWORD = "admin"; 33 34 private static boolean isAdministrator(String name) 35 { 36 return name.equals(ADMINISTRATOR_USERNAME); 37 } 38 39 public AdministratorManagerDbImpl() 40 { 41 } 42 43 private User internalGetUser(String username) 44 { 45 User user = null; 46 47 try 48 { 49 user = MyOodbManager.getTheManager().getUserManager().getUser(username); 50 } 51 catch (Exception e) 52 { 53 throw new org.myoodb.exception.PermissionException("Get User", e); 54 } 55 56 return user; 57 } 58 59 private synchronized void internalCreateUser(String username, String password) 60 { 61 if (isAdministrator(invokedBy()) == false) 62 { 63 throw new org.myoodb.exception.PermissionException("Only the Administrator can create users"); 64 } 65 else if (isAdministrator(username) == true) 66 { 67 throw new org.myoodb.exception.PermissionException("Administrator already exists"); 68 } 69 70 try 71 { 72 MyOodbManager.getTheManager().getUserManager().newUser(username, password); 73 } 74 catch (Exception e) 75 { 76 throw new org.myoodb.exception.PermissionException("Create User", e); 77 } 78 } 79 80 private synchronized void internalDeleteUser(String username) 81 { 82 if (isAdministrator(invokedBy()) == false) 83 { 84 throw new org.myoodb.exception.PermissionException("Only the Administrator can delete users"); 85 } 86 else if (isAdministrator(username) == true) 87 { 88 throw new org.myoodb.exception.PermissionException("Administrator user cannot be deleted"); 89 } 90 91 try 92 { 93 MyOodbManager.getTheManager().getUserManager().removeUser(username); 94 } 95 catch (Exception e) 96 { 97 throw new org.myoodb.exception.PermissionException("Delete User", e); 98 } 99 } 100 101 public boolean internalBreakUserLocks(String username) 102 { 103 boolean failed = false; 104 User self = internalGetUser(username); 105 106 java.util.Iterator txIter = MyOodbManager.getTheManager().getTransactionManager().getTransactions().iterator(); 107 while (txIter.hasNext()) 108 { 109 AbstractTransaction tx = (AbstractTransaction) txIter.next(); 110 111 if (tx.getStatus() < AbstractTransaction.STATUS_COMMITING) 112 { 113 User owner = tx.getOwner(); 114 115 if ((owner == null) || (owner.equals(self) == false)) 116 { 117 continue; 118 } 119 120 try 121 { 122 tx.abort(); 123 124 MyOodbManager.getTheManager().getTransactionManager().notifyWaitingTransactions(); 125 } 126 catch (Exception e) 127 { 128 failed = true; 129 } 130 } 131 } 132 133 return failed; 134 } 135 136 private boolean internalIsUserPassword(String username, String password) 137 { 138 User user = null; 139 140 try 141 { 142 user = MyOodbManager.getTheManager().getUserManager().getUser(username); 143 } 144 catch (Exception e) 145 { 146 throw new org.myoodb.exception.PermissionException("Is User Password", e); 147 } 148 149 return user.getPassword().equals(password); 150 } 151 152 public void createUser(String username, String password) 153 { 154 if (isExplicitLock() == true) 155 { 156 throw new org.myoodb.exception.PermissionException("Cannot preform operation within a transaction"); 157 } 158 159 internalCreateUser(username, password); 160 } 161 162 public void deleteUser(String username) 163 { 164 if (isExplicitLock() == true) 165 { 166 throw new org.myoodb.exception.PermissionException("Cannot preform operation within a transaction"); 167 } 168 169 internalDeleteUser(username); 170 } 171 172 public boolean breakUserLocks(String username) 173 { 174 if (isExplicitLock() == true) 175 { 176 throw new org.myoodb.exception.PermissionException("Cannot preform operation within a transaction"); 177 } 178 179 return internalBreakUserLocks(username); 180 } 181 182 public boolean isUserPassword(String username, String password) 183 { 184 return internalIsUserPassword(username, password); 185 } 186 187 public void verifyDatabase() 188 { 189 try 190 { 191 org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().verifyDatabase(); 192 } 193 catch (RuntimeException e) 194 { 195 throw e; 196 } 197 catch (Exception e) 198 { 199 e.printStackTrace(); 200 } 201 } 202 203 public boolean isVerifyingDatabase() 204 { 205 return org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().isVerifyingDatabase(); 206 } 207 208 public void defragDatabase() 209 { 210 try 211 { 212 org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().defragDatabase(); 213 } 214 catch (RuntimeException e) 215 { 216 throw e; 217 } 218 catch (Exception e) 219 { 220 e.printStackTrace(); 221 } 222 } 223 224 public boolean isDefraggingDatabase() 225 { 226 return org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().isDefraggingDatabase(); 227 } 228 229 public void backupDatabase(String location) 230 { 231 try 232 { 233 org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().backupDatabase(location); 234 } 235 catch (RuntimeException e) 236 { 237 throw e; 238 } 239 catch (Exception e) 240 { 241 e.printStackTrace(); 242 } 243 } 244 245 public boolean isBackingUpDatabase() 246 { 247 return org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().isBackingUpDatabase(); 248 } 249 250 public void restoreDatabase(String location) 251 { 252 try 253 { 254 org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().restoreDatabase(location); 255 } 256 catch (RuntimeException e) 257 { 258 throw e; 259 } 260 catch (Exception e) 261 { 262 e.printStackTrace(); 263 } 264 } 265 266 public boolean isRestoringDatabase() 267 { 268 return org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().isRestoringDatabase(); 269 } 270 } 271 | Popular Tags |