001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.objects;
025:
026: import org.myoodb.*;
027: import org.myoodb.core.*;
028:
029: public class AdministratorManagerDbImpl extends
030: org.myoodb.collectable.CollectableDbImpl implements
031: AdministratorManager {
032: public static String ADMINISTRATOR_USERNAME = "admin";
033: public static String ADMINISTRATOR_PASSWORD = "admin";
034:
035: private static boolean isAdministrator(String name) {
036: return name.equals(ADMINISTRATOR_USERNAME);
037: }
038:
039: public AdministratorManagerDbImpl() {
040: }
041:
042: private User internalGetUser(String username) {
043: User user = null;
044:
045: try {
046: user = MyOodbManager.getTheManager().getUserManager()
047: .getUser(username);
048: } catch (Exception e) {
049: throw new org.myoodb.exception.PermissionException(
050: "Get User", e);
051: }
052:
053: return user;
054: }
055:
056: private synchronized void internalCreateUser(String username,
057: String password) {
058: if (isAdministrator(invokedBy()) == false) {
059: throw new org.myoodb.exception.PermissionException(
060: "Only the Administrator can create users");
061: } else if (isAdministrator(username) == true) {
062: throw new org.myoodb.exception.PermissionException(
063: "Administrator already exists");
064: }
065:
066: try {
067: MyOodbManager.getTheManager().getUserManager().newUser(
068: username, password);
069: } catch (Exception e) {
070: throw new org.myoodb.exception.PermissionException(
071: "Create User", e);
072: }
073: }
074:
075: private synchronized void internalDeleteUser(String username) {
076: if (isAdministrator(invokedBy()) == false) {
077: throw new org.myoodb.exception.PermissionException(
078: "Only the Administrator can delete users");
079: } else if (isAdministrator(username) == true) {
080: throw new org.myoodb.exception.PermissionException(
081: "Administrator user cannot be deleted");
082: }
083:
084: try {
085: MyOodbManager.getTheManager().getUserManager().removeUser(
086: username);
087: } catch (Exception e) {
088: throw new org.myoodb.exception.PermissionException(
089: "Delete User", e);
090: }
091: }
092:
093: public boolean internalBreakUserLocks(String username) {
094: boolean success = true;
095: User self = internalGetUser(username);
096:
097: java.util.Iterator iter = MyOodbManager.getTheManager()
098: .getTransactionManager().getTransactions().iterator();
099: while (iter.hasNext()) {
100: AbstractTransaction tx = (AbstractTransaction) iter.next();
101:
102: if (tx.getStatus() < AbstractTransaction.STATUS_COMMITING) {
103: User owner = tx.getOwner();
104:
105: if ((owner == null) || (owner.equals(self) == false)) {
106: continue;
107: }
108:
109: try {
110: tx.abort();
111:
112: MyOodbManager.getTheManager()
113: .getTransactionManager()
114: .notifyWaitingTransactions();
115: } catch (Exception e) {
116: success = false;
117: } finally {
118: MyOodbManager.getTheManager()
119: .getTransactionManager()
120: .notifyWaitingTransactions();
121: }
122:
123: try {
124: MyOodbManager.getTheManager()
125: .getTransactionManager().deleteTransaction(
126: tx.getClient().getThread(), tx);
127: } catch (Exception e) {
128: success = false;
129: }
130: }
131: }
132:
133: return success;
134: }
135:
136: private boolean internalIsUserPassword(String username,
137: String password) {
138: User user = null;
139:
140: try {
141: user = MyOodbManager.getTheManager().getUserManager()
142: .getUser(username);
143: } catch (Exception e) {
144: throw new org.myoodb.exception.PermissionException(
145: "Is User Password", e);
146: }
147:
148: return user.getPassword().equals(password);
149: }
150:
151: public void createUser(String username, String password) {
152: if (isExplicitLock() == true) {
153: throw new org.myoodb.exception.PermissionException(
154: "Cannot preform operation within a transaction");
155: }
156:
157: internalCreateUser(username, password);
158: }
159:
160: public void deleteUser(String username) {
161: if (isExplicitLock() == true) {
162: throw new org.myoodb.exception.PermissionException(
163: "Cannot preform operation within a transaction");
164: }
165:
166: internalDeleteUser(username);
167: }
168:
169: public boolean breakUserLocks(String username) {
170: if (isExplicitLock() == true) {
171: throw new org.myoodb.exception.PermissionException(
172: "Cannot preform operation within a transaction");
173: }
174:
175: return internalBreakUserLocks(username);
176: }
177:
178: public boolean isUserPassword(String username, String password) {
179: return internalIsUserPassword(username, password);
180: }
181:
182: public void verifyDatabase() {
183: try {
184: org.myoodb.core.MyOodbManager.getTheManager()
185: .getStoreManager().verifyDatabase();
186: } catch (RuntimeException e) {
187: throw e;
188: } catch (Exception e) {
189: e.printStackTrace();
190: }
191: }
192:
193: public boolean isVerifyingDatabase() {
194: return org.myoodb.core.MyOodbManager.getTheManager()
195: .getStoreManager().isVerifyingDatabase();
196: }
197:
198: public void defragDatabase() {
199: try {
200: org.myoodb.core.MyOodbManager.getTheManager()
201: .getStoreManager().defragDatabase();
202: } catch (RuntimeException e) {
203: throw e;
204: } catch (Exception e) {
205: e.printStackTrace();
206: }
207: }
208:
209: public boolean isDefraggingDatabase() {
210: return org.myoodb.core.MyOodbManager.getTheManager()
211: .getStoreManager().isDefraggingDatabase();
212: }
213:
214: public void backupDatabase(String location) {
215: try {
216: org.myoodb.core.MyOodbManager.getTheManager()
217: .getStoreManager().backupDatabase(location);
218: } catch (RuntimeException e) {
219: throw e;
220: } catch (Exception e) {
221: e.printStackTrace();
222: }
223: }
224:
225: public boolean isBackingUpDatabase() {
226: return org.myoodb.core.MyOodbManager.getTheManager()
227: .getStoreManager().isBackingUpDatabase();
228: }
229:
230: public void restoreDatabase(String location) {
231: try {
232: org.myoodb.core.MyOodbManager.getTheManager()
233: .getStoreManager().restoreDatabase(location);
234: } catch (RuntimeException e) {
235: throw e;
236: } catch (Exception e) {
237: e.printStackTrace();
238: }
239: }
240:
241: public boolean isRestoringDatabase() {
242: return org.myoodb.core.MyOodbManager.getTheManager()
243: .getStoreManager().isRestoringDatabase();
244: }
245: }
|