KickJava   Java API By Example, From Geeks To Geeks.

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


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): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.runtime.hibernate.lib;
28
29 import org.objectweb.openccm.pss.runtime.common.lib.PIDHelper;
30
31
32 /**
33  * This is a base class for Storage Homes implemented with Hibernate.
34  *
35  * @author <a HREF="mailto:pretend@ukr.net">Alex Andrushchak</a>
36  *
37  * @version 0.1
38  */

39
40 public abstract class StorageHomeBase
41     extends org.objectweb.openccm.pss.runtime.common.lib.StorageHomeBase
42     implements org.objectweb.openccm.pss.runtime.hibernate.api.StorageHomeBase
43 {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     // ==================================================================
51
//
52
// Constructor.
53
//
54
// ==================================================================
55

56     /**
57      * The default constructor.
58      */

59     public StorageHomeBase()
60     {
61     }
62
63     // ==================================================================
64
//
65
// Internal methods.
66
//
67
// ==================================================================
68

69     // ==================================================================
70
//
71
// Public methods.
72
//
73
// ==================================================================
74

75     // ==================================================================
76
//
77
// Methods for org.omg.CosPersistentState.StorageHomeBase
78
//
79
// ==================================================================
80

81     /**
82      * Retrieve a storage object with its short pid.
83      *
84      * @param short_pid - The short pid to find.
85      *
86      * @throws org.omg.CosPersistentState.NotFound ifstorage object can't be found.
87      *
88      * @return The binding storage object.
89      */

90     public java.lang.Object JavaDoc
91     find_by_short_pid(byte[] short_pid)
92     throws org.omg.CosPersistentState.NotFound
93     {
94         String JavaDoc sshort_pid = new String JavaDoc(short_pid);
95         // System.out.println("StorageHomeBase.find_by_short_pid: sshort_pid=" + sshort_pid);
96
Integer JavaDoc identifier = null;
97         try {
98             identifier = new Integer JavaDoc(sshort_pid);
99         } catch (NumberFormatException JavaDoc ex) {
100             ex.printStackTrace();
101             throw new org.omg.CosPersistentState.NotFound();
102         }
103
104         // First, search in storage type incarnations
105
java.util.Iterator JavaDoc it = _storage_type_incarnations.iterator();
106         org.omg.CosPersistentState.StorageObject so = null;
107
108         while(it.hasNext())
109         {
110             so = (org.omg.CosPersistentState.StorageObject) it.next();
111             if ( java.util.Arrays.equals(so.get_short_pid(), short_pid) )
112             {
113                 return so;
114             }
115         }
116
117         // If not found, search in the database
118
java.lang.Object JavaDoc obj = null;
119         net.sf.hibernate.Session session = null;
120         org.objectweb.openccm.pss.runtime.common.api.CatalogBase catalog = null;
121         java.lang.Class JavaDoc clazz = null;
122
123         session = ((org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase) get_catalog()).
124                 get_hibernate_session();
125
126         catalog = (org.objectweb.openccm.pss.runtime.common.api.CatalogBase) get_catalog();
127         clazz = catalog.get_connector().get_storage_object_factory( get_st_rid() );
128
129         try {
130             obj = session.load(clazz, identifier);
131             _storage_type_incarnations.add(obj);
132             ((org.objectweb.openccm.pss.runtime.common.api.StorageObject) obj).initialize(this);
133         } catch (net.sf.hibernate.HibernateException ex) {
134             ex.printStackTrace();
135             throw new org.omg.CosPersistentState.NotFound();
136         }
137         return obj;
138     }
139
140     // ==================================================================
141
//
142
// Methods for org.objectweb.openccm.pss.runtime.common.api.StorageHomeBase
143
//
144
// ==================================================================
145

146     // ==================================================================
147
//
148
// Methods for org.objectweb.openccm.pss.runtime.hibernate.api.StorageHomeBase
149
//
150
// ==================================================================
151

152     /**
153      * Create a pid for a storage type.
154      *
155      * @param short_pid - The short_pid of the storage type to encode.
156      *
157      * @return The storage type PID.
158      */

159     public byte[]
160     create_pid(String JavaDoc short_pid)
161     {
162         return PIDHelper.encode(get_rid(), short_pid);
163     }
164
165     /**
166      * Must be called before create method invokation.
167      */

168     public void
169     before_create()
170     {
171         ((org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase) get_catalog()).begin_tx();
172     }
173
174     /**
175      * Must be called after create method invokation.
176      *
177      * @param st - The storage type to make persistent.
178      */

179     public void
180     after_create(org.objectweb.openccm.pss.runtime.hibernate.api.StorageObject st)
181     {
182         org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase catalog = null;
183         net.sf.hibernate.Session session = null;
184         st.initialize(this);
185
186         // Commit the storage Type
187
catalog = (org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase) get_catalog();
188         session = catalog.get_hibernate_session();
189
190         try {
191             session.save(st);
192         } catch (net.sf.hibernate.HibernateException ex) {
193             throw new RuntimeException JavaDoc(ex);
194         }
195
196         catalog.commit_tx();
197
198     }
199
200     /**
201      * Must be called after create method invokation.
202      *
203      * @param st - The storage type to make persistent.
204      */

205     public Object JavaDoc
206     find_by_key(String JavaDoc[] fields_names, String JavaDoc[] fields_types, java.lang.Object JavaDoc[] key_values)
207     {
208         net.sf.hibernate.Session session = null;
209         String JavaDoc query = null;
210         java.lang.Class JavaDoc clazz = null;
211         java.util.List JavaDoc l = null;
212         org.objectweb.openccm.pss.runtime.common.api.StorageObject so;
213         net.sf.hibernate.type.Type[] types = TypeMapping.toTypes(fields_types);
214
215         // Get the hibernate session
216
session = ((org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase) get_catalog()).
217                 get_hibernate_session();
218
219         // Get ST class
220
clazz = ((org.objectweb.openccm.pss.runtime.hibernate.api.CatalogBase) get_catalog()).
221                     get_connector().get_storage_object_factory(get_st_rid());
222
223         // Create the query
224
query = "from " + clazz.getName() + " where ";
225         for (int i=0; i<fields_names.length; i++) {
226             query += fields_names[i] + "=?";
227             if (i < fields_names.length-1) {
228                 query += " and ";
229             }
230         }
231        
232         try {
233             l = session.find(query, key_values, types);
234         } catch (net.sf.hibernate.HibernateException ex) {
235             throw new RuntimeException JavaDoc(ex);
236         }
237
238         if ( 1 == l.size() ) {
239             so = (org.objectweb.openccm.pss.runtime.common.api.StorageObject) l.get(0);
240             so.initialize(this);
241             return so;
242         }
243         return null;
244     }
245 }
246
Popular Tags