KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > factory > InternalGeneralFactory


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.factory;
25
26 import org.objectweb.jalisto.se.api.JalistoProperties;
27 import org.objectweb.jalisto.se.api.Session;
28 import org.objectweb.jalisto.se.api.internal.*;
29 import org.objectweb.jalisto.se.api.internal.multi.LockTable;
30 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess;
31 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess;
32 import org.objectweb.jalisto.se.exception.JalistoException;
33 import org.objectweb.jalisto.se.impl.server.LogicalPageAccessImpl;
34 import org.objectweb.jalisto.se.impl.server.InternalPhysicalFileAccessImpl;
35 import org.objectweb.jalisto.se.impl.server.*;
36 import org.objectweb.jalisto.se.impl.lock.AccessController;
37 import org.objectweb.jalisto.se.impl.lock.LockTableImpl;
38 import org.objectweb.jalisto.se.impl.mono.OidTableMonoImpl;
39 import org.objectweb.jalisto.se.impl.mono.SessionMonoImpl;
40 import org.objectweb.jalisto.se.impl.multi.InTransactionBaseImageMultiImpl;
41 import org.objectweb.jalisto.se.impl.multi.OidTableMultiImpl;
42 import org.objectweb.jalisto.se.impl.multi.SessionMultiImpl;
43 import org.objectweb.jalisto.se.impl.readonly.*;
44
45 import java.util.Hashtable JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 public class InternalGeneralFactory extends InternalFactoryImpl {
50
51     protected Map JavaDoc accessController;
52     protected Map JavaDoc lockManagers;
53     protected Map JavaDoc sessions;
54
55     public static InternalFactory getInstance() {
56         if (instance == null) {
57             instance = new InternalGeneralFactory();
58             instance.init();
59         }
60         return instance;
61     }
62
63     /**
64      * ********************************************************************************************
65      */

66
67     public void init() {
68         super.init();
69         accessController = new Hashtable JavaDoc();
70         lockManagers = new Hashtable JavaDoc();
71         sessions = new Hashtable JavaDoc();
72     }
73
74     public synchronized AccessController getAccessController(JalistoProperties properties) {
75         String JavaDoc path = properties.getDbFileFullName();
76         if (!accessController.containsKey(path)) {
77             AccessController controller = new AccessController();
78             accessController.put(path, controller);
79         }
80         return (AccessController) accessController.get(path);
81     }
82
83     public synchronized Session getSession(JalistoProperties properties) {
84         Session session;
85         if (properties.isMonoImplementation()) {
86             String JavaDoc name = properties.getName();
87             if (!sessions.containsKey(name)) {
88                 session = new SessionMonoImpl(properties);
89                 sessions.put(name, session);
90             } else {
91                 session = (Session) sessions.get(name);
92             }
93             if (session.currentTransaction().isActive()) {
94                 System.out.println("WARNING : get the mono session already active");
95             }
96         } else if (properties.isReadOnlyImplementation()) {
97             session = new SessionReadOnlyImpl(properties);
98         } else {
99             session = new SessionMultiImpl(properties);
100             sessions.put(session.getInternalSession().getSessionId(), session);
101         }
102         return session;
103     }
104
105     public synchronized OidTable getOidTable(JalistoProperties properties) {
106         String JavaDoc path = properties.getDbFileFullName();
107         if (!oidTables.containsKey(path)) {
108             OidTable oidTable;
109             InternalPhysicalFileAccess physicalAccess = getInternalPhysicalAccess(properties);
110             if (properties.isMonoImplementation()) {
111                 oidTable = OidTableMonoImpl.getAnOidTable(physicalAccess, properties);
112             } else if (properties.isReadOnlyImplementation()) {
113                 oidTable = OidTableReadOnlyImpl.getAnOidTable(physicalAccess, properties);
114             } else {
115                 oidTable = OidTableMultiImpl.getAnOidTable(physicalAccess, properties);
116             }
117             oidTables.put(path, oidTable);
118         }
119         return (OidTable) oidTables.get(path);
120     }
121
122     public synchronized InTransactionBaseImage getAInTransactionBaseImage(
123             PluggablePhysicalFileAccess physicalAccess, JalistoProperties properties) {
124         if (properties.isMonoImplementation() || properties.isReadOnlyImplementation()) {
125             return InTransactionBaseImageImpl.getInTransactionBaseImageInstance(physicalAccess, properties);
126         } else {
127             return InTransactionBaseImageMultiImpl.getInTransactionBaseImageInstance(physicalAccess, properties);
128         }
129     }
130
131     public synchronized LogicalSystemPageAccess getLogicalAccess(JalistoProperties properties) {
132         String JavaDoc path = properties.getDbFileFullName();
133         if (!logicals.containsKey(path)) {
134             LogicalSystemPageAccess logical;
135             if (properties.isReadOnlyImplementation()) {
136                 logical = new LogicalPageAccessReadOnlyImpl(properties);
137             } else {
138                 logical = new LogicalPageAccessImpl(properties);
139             }
140             logicals.put(path, logical);
141         }
142         return (LogicalSystemPageAccess) logicals.get(path);
143     }
144
145     public synchronized InternalPhysicalFileAccess getInternalPhysicalAccess(JalistoProperties properties) {
146         String JavaDoc path = properties.getDbFileFullName();
147         if (!physicals.containsKey(path)) {
148             InternalPhysicalFileAccess physical;
149             if (properties.isReadOnlyImplementation()) {
150                 physical = new InternalPhysicalFileAccessReadOnlyImpl(properties);
151             } else {
152                 physical = new InternalPhysicalFileAccessImpl(properties);
153             }
154             physicals.put(path, physical);
155         }
156         return (InternalPhysicalFileAccess) physicals.get(path);
157     }
158
159
160     public synchronized LockTable getLockTable(JalistoProperties properties) {
161         String JavaDoc path = properties.getDbFileFullName();
162         if (!lockManagers.containsKey(path)) {
163             lockManagers.put(path, new LockTableImpl());
164         }
165         return (LockTable) lockManagers.get(path);
166     }
167
168     public Object JavaDoc getSessionById(Object JavaDoc sessionId) {
169         return sessions.get(sessionId);
170     }
171
172     public void cleanFactory() {
173         Iterator JavaDoc sessionsIt = sessions.values().iterator();
174         while (sessionsIt.hasNext()) {
175             SessionInternal session = (SessionInternal) sessionsIt.next();
176             if (session.currentTransaction().isActive()) {
177                 throw new JalistoException("cannot clean factory : session " + session.getSessionId() + " is active");
178             }
179             if (session.isOpen()) {
180                 throw new JalistoException("cannot clean factory : session " + session.getSessionId() + " is open");
181             }
182         }
183
184         accessController.clear();
185         lockManagers.clear();
186         sessions.clear();
187         super.cleanFactory();
188     }
189
190 }
191
Popular Tags