KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > jdo > JdoCar


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.teams.jdo;
21
22 import java.io.*;
23 import java.util.*;
24
25
26 import javax.jdo.*;
27 import javax.jdo.PersistenceManager;
28
29 import org.polepos.framework.*;
30 import org.polepos.teams.jdbc.*;
31
32 import com.versant.core.jdo.tools.ant.*;
33
34 /**
35  * @author Herkules
36  */

37 public class JdoCar extends Car {
38
39     private PersistenceManagerFactory mFactory;
40
41     private final String JavaDoc mDbName;
42     private final String JavaDoc mName;
43
44     JdoCar(String JavaDoc name, String JavaDoc dbName) throws CarMotorFailureException {
45
46         mName = name;
47         mDbName = dbName;
48
49         mWebsite = Jdo.settings().getWebsite(name);
50         mDescription = Jdo.settings().getDescription(name);
51
52         initialize();
53
54     }
55
56     private boolean isSQL() {
57         return mDbName != null;
58     }
59     
60     private void versantCreateSchema(){
61         try{
62             CreateJdbcSchemaTask t = new CreateJdbcSchemaTask();
63             t.setDroptables("true");
64             t.setCreatetables("true");
65             
66             // This is a bit of an ugly hack.
67
// JDBC Connection information is redundant in the
68
// versant.[dbname] files. They should be written here.
69
t.setConfig("versant." + mDbName + ".properties");
70             
71             t.execute();
72             
73         }catch(Exception JavaDoc e){
74             e.printStackTrace();
75         }
76     }
77
78     
79     private void initialize() {
80         
81         if(mName.equals("voa")){
82             versantCreateSchema();
83         }
84         
85         
86         
87         Properties properties = new Properties();
88
89         properties.setProperty("javax.jdo.PersistenceManagerFactoryClass", Jdo.settings()
90             .getFactory(mName));
91         
92         properties.setProperty("javax.jdo.option.NontransactionalRead", "true");
93         
94         properties.setProperty("versant.metadata.0", "org/polepos/teams/jdo/data/package.jdo");
95         // properties.setProperty("versant.useClassloader", "true");
96
properties.setProperty("versant.hyperdrive", "false");
97         
98         // turning metric snapshots off.
99
properties.setProperty("versant.metricSnapshotIntervalMs", "1000000000");
100         
101         properties.setProperty("versant.logging.logEventsToSysOut", "false");
102
103         if (isSQL()) {
104             try {
105                 Class.forName(Jdbc.settings().getDriverClass(mDbName)).newInstance();
106             } catch (Exception JavaDoc ex) {
107                 ex.printStackTrace();
108             }
109
110             properties.setProperty("javax.jdo.option.ConnectionDriverName", Jdbc.settings()
111                 .getDriverClass(mDbName));
112             properties.setProperty("javax.jdo.option.ConnectionURL", Jdbc.settings().getConnectUrl(
113                 mDbName));
114             String JavaDoc user = Jdbc.settings().getUsername(mDbName);
115             if (user != null) {
116                 properties.setProperty("javax.jdo.option.ConnectionUserName", user);
117             }
118
119             String JavaDoc password = Jdbc.settings().getPassword(mDbName);
120             if (password != null) {
121                 properties.setProperty("javax.jdo.option.ConnectionPassword", password);
122             }
123         } else {
124
125             properties.setProperty("javax.jdo.option.ConnectionURL", Jdo.settings().getURL(mName));
126
127             String JavaDoc user = Jdo.settings().getUsername(mName);
128             if (user != null) {
129                 properties.setProperty("javax.jdo.option.ConnectionUserName", user);
130             }
131
132             String JavaDoc password = Jdo.settings().getPassword(mName);
133             if (password != null) {
134                 properties.setProperty("javax.jdo.option.ConnectionPassword", password);
135             }
136
137             properties.setProperty("javax.jdo.option.ConnectionUserName", "login");
138             properties.setProperty("javax.jdo.option.ConnectionPassword", "password");
139         }
140
141         properties.setProperty("org.jpox.autoCreateSchema", "true");
142         properties.setProperty("org.jpox.validateTables", "false");
143         properties.setProperty("org.jpox.validateConstraints", "false");
144
145         // deleteObjectDBFile();
146

147         mFactory = JDOHelper.getPersistenceManagerFactory(properties, JDOHelper.class
148             .getClassLoader());
149         
150         // mFactory = JDOHelper.getPersistenceManagerFactory(properties);
151
}
152
153     /**
154      *
155      */

156     public PersistenceManager getPersistenceManager() {
157         return mFactory.getPersistenceManager();
158     }
159
160     /**
161      * Delete the file in case of an local ObjectDB setup.
162      */

163     private void deleteObjectDBFile() {
164         String JavaDoc path = Jdo.settings().getConnectUrl();
165         if (!path.startsWith("objectdb://")) // only local
166
{
167             new File(path).delete();
168         }
169     }
170
171     @Override JavaDoc
172     public String JavaDoc name() {
173         
174         String JavaDoc name = mName;
175         
176         String JavaDoc publicName=Jdo.settings().getName(name);
177         if(publicName != null && publicName.length() > 1){
178             name = publicName;
179         }
180         
181         if (isSQL()) {
182             return name + "/" + mDbName;
183         }
184         return name;
185     }
186
187 }
188
Popular Tags