KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > MyOodbManager


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.core;
25
26 import java.io.*;
27 import java.util.*;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.Constructor JavaDoc;
30
31 import org.myoodb.*;
32
33 public final class MyOodbManager
34 {
35     public static String JavaDoc ROOT_DIR = "roots";
36     public static String JavaDoc OBJECT_DIR = "objs";
37     public final static String JavaDoc VERSION = "@version@";
38     public final static String JavaDoc ID_FILE = "id.properties";
39     public final static String JavaDoc ID_FILE_TMP = "id.properties.tm";
40     public final static String JavaDoc USER_FILE = "user.properties";
41     public final static String JavaDoc USER_FILE_TMP = "user.properties.tm";
42
43     public final static int ACCEPT_THREAD_PRIORITY = Thread.NORM_PRIORITY + 2;
44     public final static int SERVER_THREAD_PRIORITY = Thread.NORM_PRIORITY + 2;
45     public final static int TRANSACTION_THREAD_PRIORITY = Thread.NORM_PRIORITY;
46
47     private static MyOodbManager THE_MANAGER;
48
49     private int m_port;
50     private int m_sslPort;
51     private Database m_database;
52     private File m_databaseDirectory;
53     private Properties m_userProperties;
54     private Properties m_identifierProperties;
55
56     private ArrayList m_managers;
57     private UserManager m_userManager;
58     private ClassManager m_classManager;
59     private WorkManager m_workManager;
60     private WorkManager m_sslWorkManager;
61     private AbstractStorage m_storeManager;
62     private IdentifierManager m_identifierManager;
63     private TransactionManager m_transactionManager;
64
65     private boolean m_shuttingdownFlag = false;
66
67     static protected java.lang.reflect.Method JavaDoc m_loginCallback = null;
68     static protected java.lang.reflect.Method JavaDoc m_logoutCallback = null;
69     static protected java.lang.reflect.Method JavaDoc m_preObjectMethodCallback = null;
70     static protected java.lang.reflect.Method JavaDoc m_postObjectMethodCallback = null;
71     static protected java.lang.reflect.Method JavaDoc m_remoteCommandRequestCallback = null;
72
73     //
74
// method callback signature (org.myoodb.core.DatabaseClient client)
75
//
76
public static void setLoginCallback(Class JavaDoc classType, String JavaDoc method) throws Exception JavaDoc
77     {
78         if (classType != null)
79         {
80             Method JavaDoc[] methods = classType.getMethods();
81             for (int i = 0; i < methods.length; i++)
82             {
83                 if (methods[i].getName().equals(method))
84                 {
85                     m_loginCallback = methods[i];
86                     break;
87                 }
88             }
89
90             if (m_loginCallback == null)
91             {
92                 throw new org.myoodb.exception.InternalException("(MyOodbManager) did not find method to callback: " + method);
93             }
94         }
95         else
96         {
97             m_loginCallback = null;
98         }
99     }
100
101     //
102
// method callback signature (org.myoodb.core.DatabaseClient client)
103
//
104
public static void setLogoutCallback(Class JavaDoc classType, String JavaDoc method) throws Exception JavaDoc
105     {
106         if (classType != null)
107         {
108             Method JavaDoc[] methods = classType.getMethods();
109             for (int i = 0; i < methods.length; i++)
110             {
111                 if (methods[i].getName().equals(method))
112                 {
113                     m_logoutCallback = methods[i];
114                     break;
115                 }
116             }
117
118             if (m_logoutCallback == null)
119             {
120                 throw new org.myoodb.exception.InternalException("(MyOodbManager) did not find method to callback: " + method);
121             }
122         }
123         else
124         {
125             m_logoutCallback = null;
126         }
127     }
128
129     //
130
// method callback signature (org.myoodb.MyOodbLocal self, String methodName, Object[] args)
131
//
132
public static void setPreObjectMethodCallback(Class JavaDoc classType, String JavaDoc method) throws Exception JavaDoc
133     {
134         if (classType != null)
135         {
136             Method JavaDoc[] methods = classType.getMethods();
137             for (int i = 0; i < methods.length; i++)
138             {
139                 if (methods[i].getName().equals(method))
140                 {
141                     m_preObjectMethodCallback = methods[i];
142                     break;
143                 }
144             }
145
146             if (m_preObjectMethodCallback == null)
147             {
148                 throw new org.myoodb.exception.InternalException("(MyOodbManager) did not find method to callback: " + method);
149             }
150         }
151         else
152         {
153             m_preObjectMethodCallback = null;
154         }
155     }
156
157     //
158
// method callback signature (org.myoodb.MyOodbLocal self, String methodName, Object[] args)
159
//
160
public static void setPostObjectMethodCallback(Class JavaDoc classType, String JavaDoc method) throws Exception JavaDoc
161     {
162         if (classType != null)
163         {
164             Method JavaDoc[] methods = classType.getMethods();
165             for (int i = 0; i < methods.length; i++)
166             {
167                 if (methods[i].getName().equals(method))
168                 {
169                     m_postObjectMethodCallback = methods[i];
170                     break;
171                 }
172             }
173
174             if (m_postObjectMethodCallback == null)
175             {
176                 throw new org.myoodb.exception.InternalException("(MyOodbManager) did not find method to callback: " + method);
177             }
178         }
179         else
180         {
181             m_postObjectMethodCallback = null;
182         }
183     }
184
185     //
186
// method callback signature (org.myoodb.core.command.AbstractCommand command, org.myoodb.core.User user)
187
//
188
public static void setRemoteCommandRequestCallback(Class JavaDoc classType, String JavaDoc method) throws Exception JavaDoc
189     {
190         if (classType != null)
191         {
192             Method JavaDoc[] methods = classType.getMethods();
193             for (int i = 0; i < methods.length; i++)
194             {
195                 if (methods[i].getName().equals(method))
196                 {
197                     m_remoteCommandRequestCallback = methods[i];
198                     break;
199                 }
200             }
201
202             if (m_remoteCommandRequestCallback == null)
203             {
204                 throw new org.myoodb.exception.InternalException("(MyOodbManager) did not find method to callback: " + method);
205             }
206         }
207         else
208         {
209             m_remoteCommandRequestCallback = null;
210         }
211     }
212
213     private void checkJavaVersion() throws Exception JavaDoc
214     {
215         String JavaDoc javaVersion = System.getProperty("java.version");
216         StringTokenizer tokenizer = new StringTokenizer(javaVersion, ".");
217
218         int majorVersion = 0;
219         if (tokenizer.hasMoreTokens())
220         {
221             majorVersion = Integer.parseInt(tokenizer.nextToken());
222         }
223         int minorVersion = 0;
224         if (tokenizer.hasMoreTokens())
225         {
226             minorVersion = Integer.parseInt(tokenizer.nextToken());
227         }
228
229         if ((majorVersion == 1) && (minorVersion < 5))
230         {
231             throw new Exception JavaDoc("Java version 1.5 or higher is required to run MyOODB");
232         }
233     }
234
235     public MyOodbManager(String JavaDoc dirName, int port, int sslPort) throws Exception JavaDoc
236     {
237         checkJavaVersion();
238
239         if (THE_MANAGER != null)
240         {
241             throw new Exception JavaDoc("MyOodbManager already initialized for this JVM");
242         }
243
244         try
245         {
246             THE_MANAGER = this;
247             m_port = port;
248             m_sslPort = sslPort;
249
250             m_databaseDirectory = new File(dirName + File.separator);
251             if (m_databaseDirectory.isDirectory() == false)
252             {
253                 throw new Exception JavaDoc("No database found at '" + m_databaseDirectory + "'.");
254             }
255
256             initProperties();
257
258             WorkManager.LOGGER.info("MyOODB version @version@");
259             WorkManager.LOGGER.info("Copyright (C) 2003-@year@ The MyOODB Project");
260             WorkManager.LOGGER.info("Initializing...");
261
262             m_database = new Database();
263
264             // gather managers
265
m_managers = new ArrayList();
266
267             // Identifier Generator Init
268
m_identifierManager = new IdentifierManager();
269             m_managers.add(m_identifierManager);
270             m_identifierManager.startup();
271
272             // User Manager Init
273
m_userManager = new UserManager();
274             m_managers.add(m_userManager);
275             m_userManager.startup();
276
277             // Class Manager Init
278
m_classManager = new ClassManager();
279             m_managers.add(m_classManager);
280             m_classManager.startup();
281
282             // AbstractTransaction Manager Init
283
m_transactionManager = new TransactionManager();
284             m_managers.add(m_transactionManager);
285             m_transactionManager.startup();
286
287             // Store Manager Init
288
Class JavaDoc storeClass = m_classManager.getClass("org.myoodb.core.storage.ObjectStore");
289             if (storeClass == null)
290             {
291                 WorkManager.LOGGER.error("Store not found: org.myoodb.core.storage.ObjectStore");
292                 System.exit(1);
293             }
294             Constructor JavaDoc constructor = storeClass.getConstructor(new Class JavaDoc[]{});
295             m_storeManager = (AbstractStorage) constructor.newInstance(new Object JavaDoc[]{});
296             m_managers.add(m_storeManager);
297             m_storeManager.startup();
298
299             WorkManager.LOGGER.info("Initialization Complete!");
300
301         }
302         catch (Exception JavaDoc e)
303         {
304             THE_MANAGER = null;
305
306             WorkManager.LOGGER.error("Unable to initialize server: ", e);
307
308             throw e;
309         }
310     }
311
312     protected void initProperties() throws Exception JavaDoc
313     {
314         FileInputStream propIn = new FileInputStream(new File(m_databaseDirectory, USER_FILE));
315
316         try
317         {
318             m_userProperties = new Properties();
319             m_userProperties.load(propIn);
320         }
321         finally
322         {
323             propIn.close();
324         }
325
326         propIn = new FileInputStream(new File(m_databaseDirectory, ID_FILE));
327
328         try
329         {
330             m_identifierProperties = new Properties();
331             m_identifierProperties.load(propIn);
332         }
333         finally
334         {
335             propIn.close();
336         }
337     }
338
339     protected synchronized void storeUserProperties()
340     {
341         try
342         {
343             File propFileTemp = new File(m_databaseDirectory, USER_FILE_TMP);
344             OutputStream propOut = new BufferedOutputStream(new FileOutputStream(propFileTemp));
345
346             try
347             {
348                 m_userProperties.store(propOut, "User Property File.\n");
349                 propOut.close();
350
351                 File propFile = new File(m_databaseDirectory, USER_FILE);
352                 if (propFile.exists())
353                 {
354                     FileHelper.delete(propFile);
355                 }
356
357                 if (propFileTemp.renameTo(propFile) == false)
358                 {
359                     throw new IOException("Unable to rename temp property: " + USER_FILE);
360                 }
361
362             }
363             catch (Exception JavaDoc e)
364             {
365                 propOut.close();
366
367                 throw e;
368             }
369         }
370         catch (Exception JavaDoc e)
371         {
372             fatalError(this, "Unable to store user property file.", e);
373         }
374     }
375
376     protected synchronized void storeIdentifierProperties()
377     {
378         try
379         {
380             File propFileTemp = new File(m_databaseDirectory, ID_FILE_TMP);
381             OutputStream propOut = new BufferedOutputStream(new FileOutputStream(propFileTemp));
382
383             try
384             {
385                 m_identifierProperties.store(propOut, "Identifier Property File.\n");
386                 propOut.close();
387
388                 File propFile = new File(m_databaseDirectory, ID_FILE);
389                 if (propFile.exists())
390                 {
391                     FileHelper.delete(propFile);
392                 }
393
394                 if (propFileTemp.renameTo(propFile) == false)
395                 {
396                     throw new IOException("Unable to rename temp property: " + ID_FILE);
397                 }
398
399             }
400             catch (Exception JavaDoc e)
401             {
402                 propOut.close();
403
404                 throw e;
405             }
406         }
407         catch (Exception JavaDoc e)
408         {
409             fatalError(this, "Unable to store identifier property file.", e);
410         }
411     }
412
413     public void startup() throws Exception JavaDoc
414     {
415         try
416         {
417             if (getPortNumber() != -1)
418             {
419                 m_workManager = new WorkManager(getPortNumber(), false);
420                 m_workManager.startup();
421                 m_workManager.accept();
422
423                 WorkManager.LOGGER.info("Database open for business");
424             }
425
426             if (getSslPortNumber() != -1)
427             {
428                 m_sslWorkManager = new WorkManager(getSslPortNumber(), true);
429                 m_sslWorkManager.startup();
430                 m_sslWorkManager.accept();
431
432                 WorkManager.LOGGER.info("Database open for ssl business");
433             }
434         }
435         catch (Exception JavaDoc e)
436         {
437             WorkManager.LOGGER.error("Database startup (" + getPortNumber() + "/" + getSslPortNumber() + ") error : ", e);
438
439             throw e;
440         }
441     }
442
443     public void shutdown()
444     {
445         m_shuttingdownFlag = true;
446
447         try
448         {
449             WorkManager.LOGGER.info("shutdown...");
450
451             if (m_workManager != null)
452             {
453                 m_workManager.shutdown();
454                 m_workManager = null;
455             }
456
457             if (m_sslWorkManager != null)
458             {
459                 m_sslWorkManager.shutdown();
460                 m_sslWorkManager = null;
461             }
462
463             if (m_transactionManager != null)
464             {
465                 m_transactionManager.shutdown();
466                 m_transactionManager = null;
467             }
468
469             if (m_storeManager != null)
470             {
471                 m_storeManager.shutdown();
472                 m_storeManager = null;
473             }
474
475             if (m_userManager != null)
476             {
477                 m_userManager.shutdown();
478                 m_userManager = null;
479             }
480
481             if (m_classManager != null)
482             {
483                 m_classManager.shutdown();
484                 m_classManager = null;
485             }
486
487             if (m_identifierManager != null)
488             {
489                 m_identifierManager.shutdown();
490                 m_identifierManager = null;
491             }
492
493             m_managers = null;
494
495             WorkManager.LOGGER.info("Halted.");
496         }
497         catch (Exception JavaDoc e)
498         {
499             fatalError(null, "Manager.shutdown(): " + e.toString(), e);
500         }
501
502         THE_MANAGER = null;
503     }
504
505     public void fatalError(Object JavaDoc sender, String JavaDoc msg, Exception JavaDoc e)
506     {
507         if (e == null)
508         {
509             WorkManager.LOGGER.error(msg);
510         }
511         else
512         {
513             WorkManager.LOGGER.error(msg, e);
514         }
515
516         if ((THE_MANAGER != null) && (THE_MANAGER.getShuttingdownFlag() == false))
517         {
518             THE_MANAGER.shutdown();
519         }
520
521         System.exit(1);
522     }
523
524     public static MyOodbManager getTheManager()
525     {
526         return THE_MANAGER;
527     }
528
529     public int getPortNumber()
530     {
531         return m_port;
532     }
533
534     public int getSslPortNumber()
535     {
536         return m_sslPort;
537     }
538
539     public AbstractDatabase getDatabase()
540     {
541         return m_database;
542     }
543
544     public String JavaDoc getDatabaseName()
545     {
546         return m_databaseDirectory.getName();
547     }
548
549     public String JavaDoc getDatabaseDirectory()
550     {
551         return m_databaseDirectory.getAbsolutePath() + File.separator;
552     }
553
554     public Properties getIdentifierProperties()
555     {
556         return m_identifierProperties;
557     }
558
559     public Properties getUserProperties()
560     {
561         return m_userProperties;
562     }
563
564     public UserManager getUserManager()
565     {
566         return m_userManager;
567     }
568
569     public WorkManager getWorkManager()
570     {
571         return m_workManager;
572     }
573
574     public WorkManager getSslWorkManager()
575     {
576         return m_sslWorkManager;
577     }
578
579     public ClassManager getClassManager()
580     {
581         return m_classManager;
582     }
583
584     public AbstractStorage getStoreManager()
585     {
586         return m_storeManager;
587     }
588
589     public IdentifierManager getIdentifierManager()
590     {
591         return m_identifierManager;
592     }
593
594     public TransactionManager getTransactionManager()
595     {
596         return m_transactionManager;
597     }
598
599     public java.lang.reflect.Method JavaDoc getLoginCallback()
600     {
601         return m_loginCallback;
602     }
603
604     public java.lang.reflect.Method JavaDoc getLogoutCallback()
605     {
606         return m_logoutCallback;
607     }
608
609     public java.lang.reflect.Method JavaDoc getPreObjectMethodCallback()
610     {
611         return m_preObjectMethodCallback;
612     }
613
614     public java.lang.reflect.Method JavaDoc getPostObjectMethodCallback()
615     {
616         return m_postObjectMethodCallback;
617     }
618
619     public boolean getShuttingdownFlag()
620     {
621         return m_shuttingdownFlag;
622     }
623 }
624
Popular Tags