KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > admin > AdministratorManagerDbImpl


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
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 JavaDoc ADMINISTRATOR_USERNAME = "admin";
32     public static String JavaDoc ADMINISTRATOR_PASSWORD = "admin";
33
34     private static boolean isAdministrator(String JavaDoc name)
35     {
36         return name.equals(ADMINISTRATOR_USERNAME);
37     }
38
39     public AdministratorManagerDbImpl()
40     {
41     }
42
43     private User internalGetUser(String JavaDoc username)
44     {
45         User user = null;
46     
47         try
48         {
49             user = MyOodbManager.getTheManager().getUserManager().getUser(username);
50         }
51         catch (Exception JavaDoc 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 JavaDoc username, String JavaDoc 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 JavaDoc e)
75         {
76            throw new org.myoodb.exception.PermissionException("Create User", e);
77         }
78     }
79
80     private synchronized void internalDeleteUser(String JavaDoc 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 JavaDoc e)
96         {
97             throw new org.myoodb.exception.PermissionException("Delete User", e);
98         }
99     }
100
101     public boolean internalBreakUserLocks(String JavaDoc username)
102     {
103         boolean failed = false;
104         User self = internalGetUser(username);
105
106         java.util.Iterator JavaDoc 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 JavaDoc e)
127                 {
128                     failed = true;
129                 }
130             }
131         }
132
133         return failed;
134     }
135
136     private boolean internalIsUserPassword(String JavaDoc username, String JavaDoc password)
137     {
138         User user = null;
139
140         try
141         {
142             user = MyOodbManager.getTheManager().getUserManager().getUser(username);
143         }
144         catch (Exception JavaDoc 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 JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc username, String JavaDoc 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 JavaDoc e)
194         {
195             throw e;
196         }
197         catch (Exception JavaDoc 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 JavaDoc e)
215         {
216             throw e;
217         }
218         catch (Exception JavaDoc 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 JavaDoc location)
230     {
231         try
232         {
233             org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().backupDatabase(location);
234         }
235         catch (RuntimeException JavaDoc e)
236         {
237             throw e;
238         }
239         catch (Exception JavaDoc 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 JavaDoc location)
251     {
252         try
253         {
254             org.myoodb.core.MyOodbManager.getTheManager().getStoreManager().restoreDatabase(location);
255         }
256         catch (RuntimeException JavaDoc e)
257         {
258             throw e;
259         }
260         catch (Exception JavaDoc 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