KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > runtime > jdo > 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): Christophe Demarey.
23 Contributor(s): Alex Andrushchak.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.runtime.jdo.lib;
28
29 // Package dependencies
30
import org.objectweb.openccm.pss.runtime.common.lib.PIDHelper;
31
32
33 /**
34  * This is a base class for Storage Homes implemented with JDO.
35  *
36  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
37  *
38  * @version 0.1
39  */

40 public abstract class StorageHomeBase
41               extends org.objectweb.openccm.pss.runtime.common.lib.StorageHomeBase
42            implements org.objectweb.openccm.pss.runtime.jdo.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         // First, search in storage type incarnations
95
java.util.Iterator JavaDoc it = _storage_type_incarnations.iterator();
96         org.omg.CosPersistentState.StorageObject so = null;
97
98         while(it.hasNext())
99         {
100             so = (org.omg.CosPersistentState.StorageObject) it.next();
101             if ( java.util.Arrays.equals(so.get_short_pid(), short_pid) )
102             {
103                 return so;
104             }
105         }
106
107         // If not found, search in the database
108
java.lang.Object JavaDoc oid = null,
109                          obj = null;
110         javax.jdo.PersistenceManager pm = null;
111         org.objectweb.openccm.pss.runtime.common.api.CatalogBase catalog = null;
112         java.lang.Class JavaDoc clazz = null;
113
114         pm = ((org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase) get_catalog()).
115                 get_persistence_manager();
116
117         catalog = (org.objectweb.openccm.pss.runtime.common.api.CatalogBase) get_catalog();
118         clazz = catalog.get_connector().get_storage_object_factory( get_st_rid() );
119
120         oid = pm.newObjectIdInstance
121                 (
122                     clazz,
123                     new String JavaDoc(short_pid)
124                 );
125         try
126         {
127             obj = pm.getObjectById(oid, true);
128             ((org.objectweb.openccm.pss.runtime.common.api.StorageObject) obj).initialize(this);
129             // add obj to _storage_type_incarnations
130
_storage_type_incarnations.add(obj);
131         }catch(javax.jdo.JDODataStoreException ex){
132             throw new org.omg.CosPersistentState.NotFound();
133         }
134         return obj;
135     }
136
137     // ==================================================================
138
//
139
// Methods for org.objectweb.openccm.pss.runtime.common.api.StorageHomeBase
140
//
141
// ==================================================================
142

143     // ==================================================================
144
//
145
// Methods for org.objectweb.openccm.pss.runtime.jdo.api.StorageHomeBase
146
//
147
// ==================================================================
148

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

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

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

176     public void
177     after_create(org.objectweb.openccm.pss.runtime.jdo.api.StorageObject st)
178     {
179         org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase catalog = null;
180         javax.jdo.PersistenceManager pm = null;
181
182         st.initialize(this);
183
184         // Commit the storage Type
185
catalog = (org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase) get_catalog();
186         pm = catalog.get_persistence_manager();
187         pm.makePersistent(st);
188
189         catalog.commit_tx();
190     }
191
192     /**
193      * Must be called after create method invokation.
194      *
195      * @param fields_names - Name of fields contained in the key.
196      * @param fields_types - Type of fields contained in the key.
197      * @param key_values - Value of fields contained in the key.
198      *
199      * @return The storage object matching to key values.
200      *
201      * @throws org.omg.CosPersistentState.NotFound if there is no matching storage object.
202      */

203     public Object JavaDoc
204     find_by_key(String JavaDoc[] fields_names,
205                 String JavaDoc[] fields_types,
206                 java.lang.Object JavaDoc[] key_values)
207     throws org.omg.CosPersistentState.NotFound
208     {
209         javax.jdo.PersistenceManager pm = null;
210         javax.jdo.Extent ext = null;
211         javax.jdo.Query q = null;
212         String JavaDoc filter = null,
213                params = null;
214         java.util.Collection JavaDoc c = null;
215         java.lang.Class JavaDoc clazz = null;
216         java.lang.Object JavaDoc[] query_values = null;
217
218         // Get the persistence manager
219
pm = ((org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase) get_catalog()).
220                 get_persistence_manager();
221
222         ((org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase) get_catalog()).begin_tx();
223
224         // Build an extent to work on
225
clazz = ((org.objectweb.openccm.pss.runtime.jdo.api.CatalogBase) get_catalog()).
226                     get_connector().get_storage_object_factory(get_st_rid());
227         ext = pm.getExtent(clazz, false);
228
229         // Create the query
230
filter = "sh_rid == sh_rid_param & ";
231         params = "String sh_rid_param, ";
232         for (int i=0; i<fields_names.length; i++)
233         {
234             filter += fields_names[i] + "==" + fields_names[i] + "_param";
235             params += fields_types[i] +" " + fields_names[i] + "_param";
236             if (i < fields_names.length-1)
237             {
238                 filter += " & ";
239                 params += ", ";
240             }
241         }
242         q = pm.newQuery(ext, filter);
243         q.declareParameters(params);
244
245         // Construct query parameter values array
246
query_values = new java.lang.Object JavaDoc[key_values.length+1];
247         query_values[0] = get_rid();
248         for (int i=0; i<key_values.length; i++)
249         {
250             query_values[i+1] = key_values[i];
251         }
252         // Execute the query
253
c = (java.util.Collection JavaDoc) q.executeWithArray(query_values);
254
255         if (c.size() == 1)
256         {
257             java.util.Iterator JavaDoc it = c.iterator();
258             org.objectweb.openccm.pss.runtime.common.api.StorageObject so = null;
259
260             so = (org.objectweb.openccm.pss.runtime.common.api.StorageObject) it.next();
261             so.initialize(this);
262             return so;
263         }
264
265         throw new org.omg.CosPersistentState.NotFound();
266     }
267 }
268
Popular Tags