KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > runtime > hibernate > lib > Connector


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Alex Andrushchak.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.runtime.hibernate.lib;
28
29 import net.sf.hibernate.cfg.Configuration;
30 import net.sf.hibernate.SessionFactory;
31 import net.sf.hibernate.Session;
32
33 /**
34  * This is a class to connect an hibernate implementation.
35  *
36  * @author <a HREF="mailto:pretend@ukr.net">Alex Andrushchak</a>
37  *
38  * @version 0.1
39  */

40
41 public class Connector
42     extends org.objectweb.openccm.pss.runtime.common.lib.ConnectorBase
43     implements org.objectweb.openccm.pss.runtime.hibernate.api.Connector
44 {
45     // ==================================================================
46
//
47
// Constants.
48
//
49
// ==================================================================
50

51     public static final String JavaDoc NAME = "org.objectweb.openccm.pss.runtime.Hibernate";
52
53     // ==================================================================
54
//
55
// Internal state.
56
//
57
// ==================================================================
58

59     /** Hibernate Configuration. */
60     private Configuration _configuration = null;
61     
62     /** Hibernate Session Factory. */
63     private SessionFactory _session_factory = null;
64
65     /** Configuration properties. */
66     java.util.Properties JavaDoc props_ = null;
67
68     // ==================================================================
69
//
70
// Constructor.
71
//
72
// ==================================================================
73

74     /**
75      * The default constructor.
76      */

77     public Connector()
78     {
79         super(NAME);
80     }
81
82     // ==================================================================
83
//
84
// Internal methods.
85
//
86
// ==================================================================
87

88     /**
89      * Load Hibernate configuration file.
90      *
91      * @param file - The configuration file to load.
92      */

93     private void
94     load_hibernate_properties(String JavaDoc file)
95     {
96         props_ = new java.util.Properties JavaDoc();
97
98         try {
99             java.io.InputStream JavaDoc propStream = null;
100     
101             propStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
102             props_.load(propStream);
103         }catch (Exception JavaDoc e) {
104             throw new RuntimeException JavaDoc("Cannot load " + file + " file" + e);
105         }
106
107         System.getProperties().putAll(props_);
108         getConfiguration().setProperties(props_);
109     }
110
111     /**
112      * Configure the Hibernate connector.
113      *
114      * @return An hibernate configuration instance.
115      */

116     protected Configuration
117     getConfiguration()
118     {
119         if ( _configuration == null )
120         {
121             _configuration = new Configuration();
122             _configuration.setProperties(props_);
123         }
124         
125         return _configuration;
126     }
127
128     /**
129      * Register a persistent class to an Hibernate configuration.
130      * This registration allows Hibernate to find associated hbm.xml files.
131      *
132      * @param clazz - The class to register.
133      */

134     protected void
135     registerPersistentClass(Class JavaDoc clazz)
136     {
137         if ( (clazz != null) &&
138              (clazz.getName().compareTo("org.objectweb.openccm.pss.runtime.hibernate.lib.StorageObjectBase") != 0) )
139         {
140             registerPersistentClass( clazz.getSuperclass() );
141             try {
142                 getConfiguration().addClass(clazz);
143             } catch (net.sf.hibernate.MappingException ex) {
144                 // Configuration file not found
145
// Nothing to do as mapping file may not be needeed
146
}
147         }
148     }
149     
150     /**
151      * Get an hibernate Session Factory
152      *
153      * @return an hibernate Session Factory.
154      */

155     protected SessionFactory
156     getSessionFactory()
157     {
158         if ( null == _session_factory ) {
159             try {
160                 _session_factory = getConfiguration().buildSessionFactory();
161             } catch (net.sf.hibernate.HibernateException ex) {
162                 throw new RuntimeException JavaDoc("Cannot instantiate hibernate session factory", ex);
163             }
164         }
165         return _session_factory;
166     }
167
168     // ==================================================================
169
//
170
// Public methods.
171
//
172
// ==================================================================
173

174     // ==================================================================
175
//
176
// Methods for org.omg.CosPersistentState.Connector
177
//
178
// ==================================================================
179

180     /**
181      * Create a basic session.
182      */

183     public org.omg.CosPersistentState.Session
184     create_basic_session(short access_mode,
185                          org.omg.CosPersistentState.Parameter[] additional_parameters)
186     {
187         String JavaDoc db_url = null,
188                db_driver = null,
189                user_name = null,
190                password = null;
191         java.sql.Connection JavaDoc connection = null;
192
193         // Load Hibernate configuration properties
194
if (additional_parameters.length == 1)
195         {
196             org.omg.CosPersistentState.Parameter param = additional_parameters[0];
197
198             if (param.name.compareTo(Connector.CONF_FILE_KEY) == 0)
199             {
200                 load_hibernate_properties( param.val.extract_string() );
201             }
202             else
203                 throw new RuntimeException JavaDoc("CONF_FILE parameter is missing!");
204         }
205         else
206             throw new RuntimeException JavaDoc("CONF_FILE parameter is missing!");
207
208        db_driver = props_.getProperty("hibernate.connection.driver_class");
209        db_url = props_.getProperty("hibernate.connection.url");
210        user_name = props_.getProperty("hibernate.connection.username");
211        password = props_.getProperty("hibernate.connection.password");
212        // System.out.println(db_driver);
213
// System.out.println(db_url);
214
// System.out.println(user_name);
215
// System.out.println(password);
216

217         // JDBC driver loading
218
try {
219             Class.forName(db_driver);
220         } catch ( ClassNotFoundException JavaDoc ex ) {
221             throw new RuntimeException JavaDoc(ex);
222         }
223
224         // Create database connection
225
try {
226
227             if ( (user_name == null) || (password == null) ) {
228                 connection = java.sql.DriverManager.getConnection(db_url);
229             } else {
230                 connection = java.sql.DriverManager.getConnection(db_url, user_name, password);
231             }
232         } catch ( java.sql.SQLException JavaDoc ex ) {
233             throw new RuntimeException JavaDoc(ex);
234         }
235
236         // Create Hibernate session
237
Session session = getSessionFactory().openSession(connection);
238         return new org.objectweb.openccm.pss.runtime.hibernate.lib.Session(get_free_id(), session);
239 /*
240         try {
241             Session session = getSessionFactory().openSession();
242
243             return new org.objectweb.openccm.pss.runtime.hibernate.lib.Session(get_free_id(), session);
244         } catch (net.sf.hibernate.HibernateException ex) {
245             throw new RuntimeException("Cannot create hibernate session", ex);
246         }
247 */

248     }
249
250     /**
251      * Create a transactional session.
252      */

253     public org.omg.CosPersistentState.TransactionalSession
254     create_transactional_session(short access_mode,
255                                  short default_isolation_level,
256                                  org.omg.CosPersistentState.EndOfAssociationCallback callback,
257                                  org.omg.CosPersistentState.Parameter[] additional_parameters)
258     {
259         // TO DO
260
return null;
261     }
262
263     /**
264      * Create a session pool.
265      */

266     public org.omg.CosPersistentState.SessionPool
267     create_session_pool(short access_mode,
268                         short tx_policy,
269                         org.omg.CosPersistentState.Parameter[] additional_parameters)
270     {
271         // TO DO
272
return null;
273     }
274
275     /**
276      * Get the current session.
277      */

278     public org.omg.CosPersistentState.TransactionalSession
279     current_session()
280     {
281         // TO DO
282
return null;
283     }
284
285     /**
286      * Get sessions.
287      */

288     public org.omg.CosPersistentState.TransactionalSession[]
289     sessions(org.omg.CosTransactions.Coordinator transaction)
290     {
291         // TO DO
292
return null;
293     }
294
295
296     // ==================================================================
297
//
298
// Methods for org.objectweb.openccm.services.pss.api.Connector
299
//
300
// ==================================================================
301

302     /**
303      * Register a storage object factory.
304      */

305     public java.lang.Class JavaDoc
306     register_storage_object_factory(String JavaDoc storage_type_name,
307                                     java.lang.Class JavaDoc storage_object_factory)
308     {
309         registerPersistentClass(storage_object_factory);
310         return super.register_storage_object_factory(storage_type_name, storage_object_factory);
311     }
312
313 }
314
Popular Tags