001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.core;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.URI;
024: import java.util.WeakHashMap;
025: import com.mobixess.jodb.core.io.rmi.JODBIOBaseProxy;
026: import com.mobixess.jodb.soda.api.ObjectContainer;
027:
028: /**
029: * @author Mobixess
030: * Factory class to launch JODB engine.
031: * <br>
032: * Static methods can be used to obtain database session containers.
033: */
034:
035: public class JODB {
036:
037: private static WeakHashMap<URI, JODBSessionContainer> _sessionsCache = new WeakHashMap<URI, JODBSessionContainer>();
038:
039: public final static ObjectContainer open(String file)
040: throws IOException {
041: return open(new File(file));
042: };
043:
044: /**
045: * Opens an {@link ObjectContainer ObjectContainer} on the specified database file for local use.
046: * <br><br>Subsequent calls with the same database file name will return the same
047: * {@link ObjectContainer ObjectContainer} object.<br><br>
048: * Every call to <code>openFile()</code> requires a corresponding
049: * {@link ObjectContainer#close ObjectContainer.close}.<br><br>
050: * Database files can only be accessed for readwrite access from one process
051: * (one Java VM) at one time.
052: * <br><br>
053: *
054: * @param file
055: * @return {@link ObjectContainer} session container
056: * @throws IOException
057: */
058: public final static ObjectContainer open(File file)
059: throws IOException {
060: synchronized (_sessionsCache) {
061: JODBSessionContainer result = _sessionsCache.get(file);
062: if (result == null) {
063: result = new JODBSessionContainer(file);
064: _sessionsCache.put(result.getDbIdentificator(), result);
065: }
066: result.incOpenCounter();
067: return result;
068: }
069: };
070:
071: /*package*/static void removeSessionFromCache(URI id) {
072: synchronized (_sessionsCache) {
073: _sessionsCache.remove(id);
074: }
075: }
076:
077: /**
078: * Opens an {@link JODBServer JODBServer} on the specified database file.
079: * <br>
080: *
081: * @param databaseFileName the database file
082: * @throws IOException
083: *
084: */
085: public static final JODBServer openServer(File file)
086: throws IOException {
087: return openServer(file, null);
088: }
089:
090: /**
091: * Opens an {@link JODBServer JODBServer} on the specified database file.
092: * <br>
093: *
094: * @param databaseFileName the full path to the database file
095: * @throws IOException
096: *
097: */
098: public static final JODBServer openServer(String databaseFileName)
099: throws IOException {
100: return openServer(databaseFileName, null);
101: }
102:
103: /**
104: * Opens an {@link JODBServer JODBServer} with specified name
105: * on the specified database file.
106: * <br>
107: * The serverName serves as server identity when multiple
108: * servers (representing different db files) run on the same host.
109: * <br>
110: *
111: *
112: * @param file the database file
113: * @param serverName the server name
114: * @throws IOException
115: *
116: */
117: public static final JODBServer openServer(File file,
118: String serverName) throws IOException {
119: JODBSessionContainer sessionContainer = (JODBSessionContainer) open(file);
120: JODBServer server;
121: try {
122: server = new JODBServer(sessionContainer, serverName);
123: } catch (Exception e) {
124: throw new JodbIOException(e);
125: }
126: return server;
127: }
128:
129: /**
130: * Opens an {@link JODBServer JODBServer} with specified name
131: * on the specified database file.
132: * <br>
133: * The serverName serves as server identity when multiple
134: * servers (representing different db files) run on the same host.
135: * <br>
136: *
137: *
138: * @param databaseFileName the full path to the database file
139: * @param serverName the server name
140: * @throws IOException
141: *
142: */
143: public static final JODBServer openServer(String databaseFileName,
144: String serverName) throws IOException {
145: return openServer(new File(databaseFileName), serverName);
146: }
147:
148: /**
149: * Opens an {@link ObjectContainer ObjectContainer}
150: * client and connects it to the specified server
151: * <br><br>
152: *
153: * The <tt>mode</tt> argument specifies the access mode
154: * in which the container is to be opened. The permitted values and their
155: * meanings are:
156: *
157: * <blockquote><table summary="Access mode permitted values and meanings">
158: * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr>
159: * <tr><td valign="top"><tt>"r"</tt></td>
160: * <td> Open for reading only. Invoking any of the <tt>set()</tt>
161: * methods of the resulting object will cause an {@link
162: * java.io.IOException} to be thrown. </td></tr>
163: * <tr><td valign="top"><tt>"rw"</tt></td>
164: * <td> Open for reading and writing.</td></tr>
165: * </table></blockquote>
166: *
167: * @param hostName the host name of server (should comply with {@link URI} name specification)
168: * @param serverName optional server name (required when multiple servers run on the same host). {@code null}
169: * is acceptable value
170: * @param mode the access mode, as described
171: * <a href="#mode">above</a>
172: * @return
173: * @throws IOException
174: * @exception IllegalArgumentException if the mode argument is not equal
175: * to one of <tt>"r"</tt>, <tt>"rw"</tt>, or serverName is {@code null}, or mode is {@code null}
176: */
177: public static ObjectContainer openClient(String hostName,
178: String serverName, String mode) throws IOException {
179: if (hostName == null || mode == null) {
180: throw new IllegalArgumentException();
181: }
182: mode = mode.toLowerCase();
183: if (!"rw".startsWith(mode)) {
184: throw new IllegalArgumentException();
185: }
186: if (serverName == null) {
187: serverName = "";
188: }
189: synchronized (_sessionsCache) {
190: URI serverUri = JODBIOBaseProxy.composeURI(hostName,
191: serverName);
192: JODBSessionContainer result = _sessionsCache.get(serverUri);
193: if (result == null) {
194: result = new JODBSessionContainer(serverUri);
195: _sessionsCache.put(result.getDbIdentificator(), result);
196: }
197: result.incOpenCounter();
198: return result;
199: }
200: }
201: }
|