001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit.extensions.fixtures;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.ext.*;
028: import com.db4o.internal.*;
029:
030: import db4ounit.extensions.*;
031: import db4ounit.extensions.util.*;
032:
033: public class Db4oClientServer extends AbstractDb4oFixture implements
034: Db4oClientServerFixture {
035:
036: protected static final String FILE = "Db4oClientServer.yap";
037:
038: public static final String HOST = "localhost";
039:
040: public static final String USERNAME = "db4o";
041:
042: public static final String PASSWORD = USERNAME;
043:
044: private ObjectServer _server;
045:
046: private final File _yap;
047:
048: private boolean _embeddedClient;
049:
050: private ExtObjectContainer _objectContainer;
051:
052: private String _label;
053:
054: private int _port;
055:
056: private Configuration _serverConfig;
057:
058: public Db4oClientServer(ConfigurationSource configSource,
059: String fileName, boolean embeddedClient, String label) {
060: super (configSource);
061: _yap = new File(fileName);
062: _embeddedClient = embeddedClient;
063: _label = label;
064: }
065:
066: public Db4oClientServer(ConfigurationSource configSource,
067: boolean embeddedClient, String label) {
068: this (configSource, filePath(), embeddedClient, label);
069: }
070:
071: public void open() throws Exception {
072: openServer();
073: _objectContainer = _embeddedClient ? openEmbeddedClient().ext()
074: : Db4o.openClient(config(), HOST, _port, USERNAME,
075: PASSWORD).ext();
076: }
077:
078: public ExtObjectContainer openNewClient() {
079: return _embeddedClient ? openEmbeddedClient().ext() : Db4o
080: .openClient(
081: cloneDb4oConfiguration((Config4Impl) config()),
082: HOST, _port, USERNAME, PASSWORD).ext();
083: }
084:
085: private void openServer() throws Exception {
086: _serverConfig = cloneDb4oConfiguration(config());
087: _server = Db4o.openServer(_serverConfig,
088: _yap.getAbsolutePath(), -1);
089: _port = _server.ext().port();
090: _server.grantAccess(USERNAME, PASSWORD);
091: }
092:
093: public void close() throws Exception {
094: if (null != _objectContainer) {
095: _objectContainer.close();
096: _objectContainer = null;
097: }
098: closeServer();
099: }
100:
101: private void closeServer() throws Exception {
102: if (null != _server) {
103: _server.close();
104: _server = null;
105: }
106: }
107:
108: public ExtObjectContainer db() {
109: return _objectContainer;
110: }
111:
112: protected void doClean() {
113: _yap.delete();
114: }
115:
116: public ObjectServer server() {
117: return _server;
118: }
119:
120: public boolean embeddedClients() {
121: return _embeddedClient;
122: }
123:
124: /**
125: * Does not accept a clazz which is assignable from OptOutCS, or not
126: * assignable from Db4oTestCase.
127: *
128: * @return returns false if the clazz is assignable from OptOutCS, or not
129: * assignable from Db4oTestCase. Otherwise, returns true.
130: */
131: public boolean accept(Class clazz) {
132: if (!AbstractDb4oTestCase.class.isAssignableFrom(clazz)) {
133: return false;
134: }
135: if (OptOutCS.class.isAssignableFrom(clazz)) {
136: return false;
137: }
138: if (_embeddedClient
139: && (OptOutAllButNetworkingCS.class
140: .isAssignableFrom(clazz))) {
141: return false;
142: }
143: return true;
144: }
145:
146: public LocalObjectContainer fileSession() {
147: return (LocalObjectContainer) _server.ext().objectContainer();
148: }
149:
150: public void defragment() throws Exception {
151: defragment(filePath());
152: }
153:
154: private ObjectContainer openEmbeddedClient() {
155: return _server.openClient(config());
156: }
157:
158: private Config4Impl cloneDb4oConfiguration(Configuration config) {
159: return (Config4Impl) ((Config4Impl) config).deepClone(this );
160: }
161:
162: public String getLabel() {
163: return _label;
164: }
165:
166: public int serverPort() {
167: return _port;
168: }
169:
170: private static String filePath() {
171: return CrossPlatformServices.databasePath(FILE);
172: }
173:
174: public void configureAtRuntime(RuntimeConfigureAction action) {
175: action.apply(config());
176: action.apply(_serverConfig);
177: }
178: }
|