KickJava   Java API By Example, From Geeks To Geeks.

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


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.InternalPhysicalFileAccess;
30 import org.objectweb.jalisto.se.exception.JalistoException;
31 import org.objectweb.jalisto.se.impl.server.InternalPhysicalFileAccessImpl;
32 import org.objectweb.jalisto.se.impl.mono.OidTableMonoImpl;
33 import org.objectweb.jalisto.se.impl.mono.SessionMonoImpl;
34
35 import java.util.Hashtable JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38
39 public class InternalMonoFactory extends InternalFactoryImpl {
40
41     protected Map JavaDoc sessions;
42
43     public static InternalFactory getInstance() {
44         if (instance == null) {
45             instance = new InternalMonoFactory();
46             instance.init();
47         }
48         return instance;
49     }
50
51     public void init() {
52         super.init();
53         sessions = new Hashtable JavaDoc();
54     }
55
56     public synchronized Session getSession(JalistoProperties properties) {
57         Session session;
58         if (properties.isMonoImplementation()) {
59             String JavaDoc name = properties.getName();
60             if (!sessions.containsKey(name)) {
61                 session = new SessionMonoImpl(properties);
62                 sessions.put(name, session);
63             } else {
64                 session = (Session) sessions.get(name);
65             }
66             if (session.currentTransaction().isActive()) {
67                 System.out.println("WARNING : get the mono session already active");
68             }
69         } else {
70             throw new JalistoException("try to get no mono object implementation with mono factory");
71         }
72         return session;
73     }
74
75     public synchronized OidTable getOidTable(JalistoProperties properties) {
76         String JavaDoc path = properties.getDbFileFullName();
77         if (!oidTables.containsKey(path)) {
78             OidTable oidTable;
79             InternalPhysicalFileAccess physicalAccess = getInternalPhysicalAccess(properties);
80             if (properties.isMonoImplementation()) {
81                 oidTable = OidTableMonoImpl.getAnOidTable(physicalAccess, properties);
82             } else {
83                 throw new JalistoException("try to get no mono object implementation with mono factory");
84             }
85             oidTables.put(path, oidTable);
86         }
87         return (OidTable) oidTables.get(path);
88     }
89
90     public synchronized InternalPhysicalFileAccess getInternalPhysicalAccess(JalistoProperties properties) {
91         String JavaDoc path = properties.getDbFileFullName();
92         if (!physicals.containsKey(path)) {
93             physicals.put(path, new InternalPhysicalFileAccessImpl(properties));
94         }
95         return (InternalPhysicalFileAccess) physicals.get(path);
96     }
97
98     public Object JavaDoc getSessionById(Object JavaDoc sessionId) {
99         return sessions.get(sessionId);
100     }
101
102     public void cleanFactory() {
103         Iterator JavaDoc sessionsIt = sessions.values().iterator();
104         while (sessionsIt.hasNext()) {
105             SessionInternal session = (SessionInternal) sessionsIt.next();
106             if (session.currentTransaction().isActive()) {
107                 throw new JalistoException("cannot clean factory : session " + session.getSessionId() + " is active");
108             }
109             if (session.isOpen()) {
110                 throw new JalistoException("cannot clean factory : session " + session.getSessionId() + " is open");
111             }
112         }
113         sessions.clear();
114         super.cleanFactory();
115     }
116
117 }
118
Popular Tags