View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.database;
17  
18  import james.SimSystem;
19  import james.core.data.DBConnectionData;
20  import p3j.database.hibernate.P3MDatabase;
21  import p3j.misc.Misc;
22  import p3j.misc.gui.GUI;
23  
24  /**
25   * A factory for the database layer. Not implemented properly yet.
26   * 
27   * Created on January 11, 2007
28   * 
29   * @author Christina Bohk
30   * @author Roland Ewald
31   * 
32   */
33  public final class DatabaseFactory {
34  
35  	/**
36  	 * This class should not be instantiated.
37  	 */
38  	private DatabaseFactory() {
39  	}
40  
41  	/**
42  	 * Serialization ID.
43  	 */
44  	private static boolean useSQLDatabase = true;
45  
46  	/**
47  	 * Implementing singleton pattern.
48  	 */
49  	private static IP3MDatabase sqlDatabase;
50  
51  	/**
52  	 * Connection data to be used.
53  	 */
54  	private static DBConnectionData dbConnData = Misc.DEFAULT_DB_CONN;
55  
56  	/**
57  	 * Get database interface.
58  	 * 
59  	 * @return database interface
60  	 */
61  	public static IP3MDatabase getDatabaseSingleton() {
62  		if (sqlDatabase == null) {
63  			sqlDatabase = createDatabase();
64  		}
65  		return sqlDatabase;
66  	}
67  
68  	/**
69  	 * Creates a new Database object.
70  	 * 
71  	 * @return the newly created database interface object
72  	 */
73  	public static IP3MDatabase createDatabase() {
74  		if (DatabaseFactory.useSQLDatabase) {
75  			P3MDatabase database = new P3MDatabase();
76  			database.init(dbConnData);
77  			try {
78  				database.open();
79  			} catch (Exception ex) {
80  				SimSystem.report(ex);
81  				database = null;
82  			}
83  			return database;
84  		}
85  		return null;
86  	}
87  
88  	/**
89  	 * Creates a new Database object, given a certain hibernate configuration
90  	 * file.
91  	 * 
92  	 * @param hibernateConfigFile
93  	 *          the hibernate config file
94  	 * 
95  	 * @return the database
96  	 */
97  	public static IP3MDatabase createDatabase(String hibernateConfigFile) {
98  		P3MDatabase.setHibernateConfigFile(hibernateConfigFile);
99  		return createDatabase();
100 	}
101 
102 	/**
103 	 * Gets the DB connection data.
104 	 * 
105 	 * @return the DB connection data
106 	 */
107 	public static DBConnectionData getDbConnData() {
108 		return dbConnData;
109 	}
110 
111 	/**
112 	 * Sets the DB connection data.
113 	 * 
114 	 * @param dbConnData
115 	 *          the new DB connection data
116 	 */
117 	public static void setDbConnData(DBConnectionData dbConnData) {
118 		try {
119 			reset();
120 		} catch (Exception ex) {
121 			GUI.printErrorMessage("Error closing database connection", ex);
122 		}
123 		DatabaseFactory.dbConnData = dbConnData;
124 	}
125 
126 	/**
127 	 * Reset.
128 	 */
129 	public static void reset() {
130 		if (sqlDatabase != null) {
131 			sqlDatabase.close();
132 		}
133 		sqlDatabase = null;
134 	}
135 
136 }