001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.jdo;
021:
022: import java.io.*;
023: import java.util.*;
024:
025: import javax.jdo.*;
026: import javax.jdo.PersistenceManager;
027:
028: import org.polepos.framework.*;
029: import org.polepos.teams.jdbc.*;
030:
031: import com.versant.core.jdo.tools.ant.*;
032:
033: /**
034: * @author Herkules
035: */
036: public class JdoCar extends Car {
037:
038: private PersistenceManagerFactory mFactory;
039:
040: private final String mDbName;
041: private final String mName;
042:
043: JdoCar(String name, String dbName) throws CarMotorFailureException {
044:
045: mName = name;
046: mDbName = dbName;
047:
048: mWebsite = Jdo.settings().getWebsite(name);
049: mDescription = Jdo.settings().getDescription(name);
050:
051: initialize();
052:
053: }
054:
055: private boolean isSQL() {
056: return mDbName != null;
057: }
058:
059: private void versantCreateSchema() {
060: try {
061: CreateJdbcSchemaTask t = new CreateJdbcSchemaTask();
062: t.setDroptables("true");
063: t.setCreatetables("true");
064:
065: // This is a bit of an ugly hack.
066: // JDBC Connection information is redundant in the
067: // versant.[dbname] files. They should be written here.
068: t.setConfig("versant." + mDbName + ".properties");
069:
070: t.execute();
071:
072: } catch (Exception e) {
073: e.printStackTrace();
074: }
075: }
076:
077: private void initialize() {
078:
079: if (mName.equals("voa")) {
080: versantCreateSchema();
081: }
082:
083: Properties properties = new Properties();
084:
085: properties.setProperty(
086: "javax.jdo.PersistenceManagerFactoryClass", Jdo
087: .settings().getFactory(mName));
088:
089: properties.setProperty("javax.jdo.option.NontransactionalRead",
090: "true");
091:
092: properties.setProperty("versant.metadata.0",
093: "org/polepos/teams/jdo/data/package.jdo");
094: // properties.setProperty("versant.useClassloader", "true");
095: properties.setProperty("versant.hyperdrive", "false");
096:
097: // turning metric snapshots off.
098: properties.setProperty("versant.metricSnapshotIntervalMs",
099: "1000000000");
100:
101: properties.setProperty("versant.logging.logEventsToSysOut",
102: "false");
103:
104: if (isSQL()) {
105: try {
106: Class.forName(Jdbc.settings().getDriverClass(mDbName))
107: .newInstance();
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111:
112: properties.setProperty(
113: "javax.jdo.option.ConnectionDriverName", Jdbc
114: .settings().getDriverClass(mDbName));
115: properties.setProperty("javax.jdo.option.ConnectionURL",
116: Jdbc.settings().getConnectUrl(mDbName));
117: String user = Jdbc.settings().getUsername(mDbName);
118: if (user != null) {
119: properties.setProperty(
120: "javax.jdo.option.ConnectionUserName", user);
121: }
122:
123: String password = Jdbc.settings().getPassword(mDbName);
124: if (password != null) {
125: properties
126: .setProperty(
127: "javax.jdo.option.ConnectionPassword",
128: password);
129: }
130: } else {
131:
132: properties.setProperty("javax.jdo.option.ConnectionURL",
133: Jdo.settings().getURL(mName));
134:
135: String user = Jdo.settings().getUsername(mName);
136: if (user != null) {
137: properties.setProperty(
138: "javax.jdo.option.ConnectionUserName", user);
139: }
140:
141: String password = Jdo.settings().getPassword(mName);
142: if (password != null) {
143: properties
144: .setProperty(
145: "javax.jdo.option.ConnectionPassword",
146: password);
147: }
148:
149: properties.setProperty(
150: "javax.jdo.option.ConnectionUserName", "login");
151: properties.setProperty(
152: "javax.jdo.option.ConnectionPassword", "password");
153: }
154:
155: properties.setProperty("org.jpox.autoCreateSchema", "true");
156: properties.setProperty("org.jpox.validateTables", "false");
157: properties.setProperty("org.jpox.validateConstraints", "false");
158:
159: // deleteObjectDBFile();
160:
161: mFactory = JDOHelper.getPersistenceManagerFactory(properties,
162: JDOHelper.class.getClassLoader());
163:
164: // mFactory = JDOHelper.getPersistenceManagerFactory(properties);
165: }
166:
167: /**
168: *
169: */
170: public PersistenceManager getPersistenceManager() {
171: return mFactory.getPersistenceManager();
172: }
173:
174: /**
175: * Delete the file in case of an local ObjectDB setup.
176: */
177: private void deleteObjectDBFile() {
178: String path = Jdo.settings().getConnectUrl();
179: if (!path.startsWith("objectdb://")) // only local
180: {
181: new File(path).delete();
182: }
183: }
184:
185: @Override
186: public String name() {
187:
188: String name = mName;
189:
190: String publicName = Jdo.settings().getName(name);
191: if (publicName != null && publicName.length() > 1) {
192: name = publicName;
193: }
194:
195: if (isSQL()) {
196: return name + "/" + mDbName;
197: }
198: return name;
199: }
200:
201: }
|