01: /*
02: This file is part of BORG.
03:
04: BORG is free software; you can redistribute it and/or modify
05: it under the terms of the GNU General Public License as published by
06: the Free Software Foundation; either version 2 of the License, or
07: (at your option) any later version.
08:
09: BORG is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with BORG; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17:
18: Copyright 2003 by Mike Berger
19: */
20:
21: package net.sf.borg.model.db;
22:
23: import java.lang.reflect.Method;
24:
25: import net.sf.borg.common.PrefName;
26: import net.sf.borg.common.Prefs;
27:
28: /**
29: * A singleton instance which creates an
30: * {@link IBeanDataFactory IBeanDataFactory} based on the file type.
31: *
32: * @author Mohan Embar
33: */
34: public class BeanDataFactoryFactory {
35: /**
36: * Singleton.
37: */
38: public static BeanDataFactoryFactory getInstance() {
39: return instance;
40: }
41:
42: public final IBeanDataFactory getFactory(StringBuffer dbdir,
43: boolean shared) throws Exception {
44: String db = dbdir.toString();
45: String factoryClass = null;
46: if (db.startsWith("jdbc:")) {
47: factoryClass = "net.sf.borg.model.db.jdbc.JdbcBeanDataFactory";
48: } else if (db.startsWith("remote:")) {
49: factoryClass = "net.sf.borg.model.db.remote.RemoteBeanDataFactory";
50: } else {
51: // Use default File DB; append parms to url
52: factoryClass = "net.sf.borg.model.db.file.FileBeanDataFactory";
53: dbdir.append("::").append(shared);
54: }
55:
56: Method getInst = Class.forName(factoryClass).getMethod(
57: "getInstance", null);
58: return (IBeanDataFactory) getInst.invoke(null, null);
59: }
60:
61: public static String buildDbDir() {
62: // get dir for DB
63: String dbdir = "";
64: String dbtype = Prefs.getPref(PrefName.DBTYPE);
65: if (dbtype.equals("local")) {
66: dbdir = Prefs.getPref(PrefName.DBDIR);
67: } else if (dbtype.equals("remote")) {
68: dbdir = "remote:" + Prefs.getPref(PrefName.DBURL);
69: } else if (dbtype.equals("hsqldb")) {
70: String hdir = Prefs.getPref(PrefName.HSQLDBDIR);
71: if (hdir.equals("not-set"))
72: return hdir;
73: dbdir = "jdbc:hsqldb:file:"
74: + Prefs.getPref(PrefName.HSQLDBDIR) + "/borg_";
75: } else if (dbtype.equals("jdbc")) {
76: dbdir = Prefs.getPref(PrefName.JDBCURL);
77: } else {
78: // build a mysql URL
79: dbdir = "jdbc:mysql://" + Prefs.getPref(PrefName.DBHOST)
80: + ":" + Prefs.getPref(PrefName.DBPORT) + "/"
81: + Prefs.getPref(PrefName.DBNAME) + "?user="
82: + Prefs.getPref(PrefName.DBUSER) + "&password="
83: + Prefs.getPref(PrefName.DBPASS)
84: + "&autoReconnect=true";
85: }
86:
87: System.out.println("DB URL is: " + dbdir);
88: return (dbdir);
89: }
90:
91: // private //
92: private static final BeanDataFactoryFactory instance = new BeanDataFactoryFactory();
93:
94: private BeanDataFactoryFactory() {
95: }
96: }
|