KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > Sessions


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.config.*;
24 import com.db4o.ext.*;
25 import com.db4o.foundation.*;
26 import com.db4o.inside.*;
27
28 class Sessions extends Collection4{
29     // FIXME: aggregate Collection4 instead of extending it
30

31     void forEach(Visitor4 visitor){
32         synchronized(Global4.lock){
33             Iterator4 i = iterator();
34             while(i.moveNext()){
35                 visitor.visit(i.current());
36             }
37         }
38     }
39
40     ObjectContainer open(Configuration config,String JavaDoc databaseFileName) {
41         
42         synchronized(Global4.lock){
43             ObjectContainer oc = null;
44             Session newSession = new Session(databaseFileName);
45     
46             Session oldSession = (Session) get(newSession);
47             if (oldSession != null) {
48                 oc = oldSession.subSequentOpen();
49                 if (oc == null) {
50                     remove(oldSession);
51                 }
52                 return oc;
53             }
54             
55             if (Deploy.debug) {
56                 System.out.println("db4o Debug is ON");
57                 if (!Deploy.flush) {
58                     System.out.println("Debug option set NOT to flush file.");
59                 }
60                 try{
61                     oc = new YapRandomAccessFile(config,newSession);
62                 }catch(Exception JavaDoc e){
63                     e.printStackTrace();
64                 }
65             } else {
66                 try {
67                     oc = new YapRandomAccessFile(config,newSession);
68                 } catch (DatabaseFileLockedException e) {
69                     throw e;
70                 } catch (ObjectNotStorableException e) {
71                     throw e;
72                 } catch (Throwable JavaDoc t) {
73                     Messages.logErr(Db4o.i_config, 4, databaseFileName, t);
74                     return null;
75                 }
76             }
77             newSession.i_stream = (YapStream) oc;
78             add(newSession);
79             Platform4.postOpen(oc);
80             Messages.logMsg(Db4o.i_config, 5, databaseFileName);
81             return oc;
82         }
83     }
84     
85     public Object JavaDoc remove(Object JavaDoc obj){
86         synchronized(Global4.lock){
87             return super.remove(obj);
88         }
89     }
90     
91 }
92
Popular Tags