KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > cmp > JDOEJB11HelperImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25
26 /*
27  * JDOEJB11HelperImpl.java
28  *
29  * Created on January 17, 2002
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.cmp;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.io.*;
38
39 import javax.ejb.EJBObject JavaDoc;
40 import javax.ejb.EJBException JavaDoc;
41
42 import com.sun.jdo.api.persistence.support.PersistenceManager;
43 import com.sun.jdo.api.persistence.support.JDOHelper;
44 import com.sun.jdo.api.persistence.support.JDOFatalDataStoreException;
45 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
46 import com.sun.jdo.api.persistence.support.JDOObjectNotFoundException;
47 import com.sun.jdo.api.persistence.support.JDOUserException;
48
49 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.JDOEJB11Helper;
50 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.CMPHelper;
51 import com.sun.jdo.spi.persistence.support.sqlstore.utility.NumericConverter;
52 import com.sun.jdo.spi.persistence.support.sqlstore.utility.NumericConverterFactory;
53
54 import com.sun.jdo.spi.persistence.utility.logging.Logger;
55 import com.sun.jdo.spi.persistence.utility.I18NHelper;
56
57 /*
58  * This is an abstract class which is a generic implementation of the
59  * JDOEJB11Helper interface for conversion of persistence-capable instances
60  * to and from EJB objects of type: EJBObject, PrimaryKey, and Collections of those.
61  * These implementations are common for CMP1.1 and CMP2.0 beans.
62  *
63  * @author Marina Vatkina
64  */

65 abstract public class JDOEJB11HelperImpl implements JDOEJB11Helper {
66
67     /**
68      * I18N message handler
69      */

70     protected final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
71         JDOEJB11HelperImpl.class);
72
73     //The logger
74
protected static Logger logger = LogHelperEntityInternal.getLogger();
75
76     /**
77      * Converts persistence-capable instance to EJBObject.
78      * @param pc the persistence-capable instance to be converted as an Object.
79      * @param pm the associated instance of PersistenceManager.
80      * @return instance of EJBObject.
81      */

82     public EJBObject JavaDoc convertPCToEJBObject (Object JavaDoc pc, PersistenceManager pm) {
83         if (pc == null) return null;
84         Object JavaDoc jdoObjectId = pm.getObjectId(pc);
85         Object JavaDoc key = convertObjectIdToPrimaryKey(jdoObjectId);
86         try {
87             return CMPHelper.getEJBObject(key, getContainer());
88         } catch (Exception JavaDoc ex) {
89             EJBException JavaDoc e = new EJBException JavaDoc(I18NHelper.getMessage(messages,
90                         "EXC_ConvertPCToEJBObject", key.toString()), ex);// NOI18N
91
logger.throwing("JDOEJB11HelperImpl", "convertPCToEJBObject", e); // NOI18N
92
throw e;
93         }
94     }
95
96     /**
97      * Converts EJBObject to persistence-capable instance.
98      * @param o the EJBObject instance to be converted.
99      * @param pm the associated instance of PersistenceManager.
100      * @param validate true if the existence of the instance is to be validated.
101      * @return persistence-capable instance.
102      * @throws IllegalArgumentException if validate is true and instance does
103      * not exist in the database or is deleted.
104      */

105     public Object JavaDoc convertEJBObjectToPC(EJBObject JavaDoc o, PersistenceManager pm, boolean validate) {
106         Object JavaDoc key = null;
107         try {
108             key = o.getPrimaryKey();
109         } catch (Exception JavaDoc ex) {
110             EJBException JavaDoc e = new EJBException JavaDoc(I18NHelper.getMessage(messages,
111                         "EXC_ConvertEJBObjectToPC", o.getClass().getName()), ex);// NOI18N
112
logger.throwing("JDOEJB11HelperImpl", "convertEJBObjectToPC", e); // NOI18N
113
throw e;
114         }
115         return convertPrimaryKeyToPC(key, pm, validate);
116     }
117
118     /**
119      * Converts PrimaryKey object to persistence-capable instance.
120      * @param key the PrimaryKey object to be converted.
121      * @param pm the associated instance of PersistenceManager.
122      * @param validate true if the existence of the instance is to be validated.
123      * @return persistence-capable instance.
124      * @throws IllegalArgumentException if validate is true and instance does
125      * not exist in the database or is deleted.
126      */

127     protected Object JavaDoc convertPrimaryKeyToPC(Object JavaDoc key, PersistenceManager pm, boolean validate) {
128         Object JavaDoc pc = null;
129         try {
130             Object JavaDoc jdoObjectId = convertPrimaryKeyToObjectId(key);
131             pc = pm.getObjectById(jdoObjectId, validate);
132         } catch (JDOObjectNotFoundException ex) {
133             logger.fine("---JDOEJB11HelperImpl.convertPrimaryKeyToPC: Object not found for: " + key); // NOI18N
134

135             throw new IllegalArgumentException JavaDoc(I18NHelper.getMessage(messages,
136                         "EXC_DeletedInstanceOtherTx", key.toString()));// NOI18N
137
}
138
139         if (validate && JDOHelper.isDeleted(pc)) {
140             logger.fine("---JDOEJB11HelperImpl.convertPrimaryKeyToPC: Object is deleted for: " + key); // NOI18N
141

142             throw new IllegalArgumentException JavaDoc(I18NHelper.getMessage(messages,
143                         "EXC_DeletedInstanceThisTx", key.toString()));// NOI18N
144
}
145
146         return pc;
147     }
148
149     /**
150      * Converts Collection of persistence-capable instances to a Collection of
151      * EJBObjects.
152      * @param pcs the Collection of persistence-capable instance to be converted.
153      * @param pm the associated instance of PersistenceManager.
154      * @return Collection of EJBObjects.
155      */

156     public Collection JavaDoc convertCollectionPCToEJBObject (Collection JavaDoc pcs, PersistenceManager pm) {
157         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
158
159         Object JavaDoc o = null;
160         for (java.util.Iterator JavaDoc it = pcs.iterator(); it.hasNext();) {
161             o = convertPCToEJBObject((Object JavaDoc)it.next(), pm);
162             if(logger.isLoggable(Logger.FINEST) ) {
163                 logger.finest(
164                     "\n---JDOEJB11HelperImpl.convertCollectionPCToEJBObject() adding: " + o);// NOI18N
165
}
166             rc.add(o);
167         }
168         return rc;
169     }
170
171     /**
172      * Converts Collection of persistence-capable instances to a Set of
173      * EJBObjects.
174      * @param pcs the Collection of persistence-capable instance to be converted.
175      * @param pm the associated instance of PersistenceManager.
176      * @return Set of EJBObjects.
177      */

178     public Set JavaDoc convertCollectionPCToEJBObjectSet (Collection JavaDoc pcs, PersistenceManager pm) {
179         java.util.Set JavaDoc rc = new java.util.HashSet JavaDoc();
180
181         Object JavaDoc o = null;
182         for (java.util.Iterator JavaDoc it = pcs.iterator(); it.hasNext();) {
183             o = convertPCToEJBObject((Object JavaDoc)it.next(), pm);
184             if(logger.isLoggable(Logger.FINEST) ) {
185                 logger.finest(
186                     "\n---JDOEJB11HelperImpl.convertCollectionPCToEJBObjectSet() adding: " + o);// NOI18N
187
}
188             rc.add(o);
189         }
190         return rc;
191     }
192
193     /**
194      * Converts Collection of EJBObjects to a Collection of
195      * persistence-capable instances.
196      * @param coll the Collection of EJBObject instances to be converted.
197      * @param pm the associated instance of PersistenceManager.
198      * @param validate true if the existence of the instances is to be validated.
199      * @return Collection of persistence-capable instance.
200      * @throws IllegalArgumentException if validate is true and at least one instance does
201      * not exist in the database or is deleted.
202      */

203     public Collection JavaDoc convertCollectionEJBObjectToPC (Collection JavaDoc coll, PersistenceManager pm,
204                                                       boolean validate) {
205         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
206
207         Object JavaDoc o = null;
208         for (java.util.Iterator JavaDoc it = coll.iterator(); it.hasNext();) {
209             o = convertEJBObjectToPC((EJBObject JavaDoc)it.next(), pm, validate);
210             if(logger.isLoggable(Logger.FINEST) ) {
211                 logger.finest(
212                     "\n---JDOEJB11HelperImpl.convertCollectionEJBObjectToPC() adding: " + o);// NOI18N
213
}
214             rc.add(o);
215         }
216         return rc;
217     }
218
219     /**
220      * Converts persistence-capable instance to an instance of the PrimaryKey Class.
221      * @param pc the persistence-capable instance to be converted as an Object.
222      * @param pm the associated instance of PersistenceManager.
223      * @return instance of the PrimaryKey Class.
224      */

225     public Object JavaDoc convertPCToPrimaryKey (Object JavaDoc pc, PersistenceManager pm) {
226         if (pc == null) return null;
227         Object JavaDoc rc = convertObjectIdToPrimaryKey(pm.getObjectId(pc));
228
229         if(logger.isLoggable(Logger.FINEST) ) {
230             logger.finest("\n---JDOEJB11HelperImpl.convertPCToPrimaryKey() PK: " + rc);// NOI18N
231
}
232         return rc;
233     }
234
235
236     /**
237      * Converts Collection of persistence-capable instances to a Collection of
238      * the PrimaryKey Class instances.
239      * @param pcs Collection of the persistence-capable instances.
240      * @param pm the associated instance of PersistenceManager.
241      * @return Collection of the PrimaryKey Class instances.
242      */

243     public Collection JavaDoc convertCollectionPCToPrimaryKey (Collection JavaDoc pcs, PersistenceManager pm) {
244         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
245
246         Object JavaDoc o = null;
247         for (java.util.Iterator JavaDoc it = pcs.iterator(); it.hasNext();) {
248             o = convertPCToPrimaryKey(it.next(), pm);
249             if(logger.isLoggable(Logger.FINEST) ) {
250                 logger.finest(
251                     "\n---JDOEJB11HelperImpl.convertCollectionPCToPrimaryKey() adding: " + o);// NOI18N
252
}
253             rc.add(o);
254         }
255         return rc;
256     }
257
258    /**
259      * Converts Object Id of a persistence-capable instance to an instance of the
260      * PrimaryKey Class.
261      * @param objectId the Object Id to be converted.
262      * @return instance of the PrimaryKey Class.
263      */

264     abstract public Object JavaDoc convertObjectIdToPrimaryKey (Object JavaDoc objectId);
265
266    /**
267      * Converts instance of a PrimaryKey Class to an instance of the Object Id of a
268      * corresponding persistence-capable Class.
269      * @param key the PrimaryKey instance to be converted.
270      * @return instance of the Object Id.
271      */

272     abstract public Object JavaDoc convertPrimaryKeyToObjectId (Object JavaDoc key);
273
274    /**
275      * Converts Collection of Object Id's of persistence-capable instances to a
276      * Collection of of the PrimaryKey instances.
277      * @param oids Collection of the Object Id to be converted.
278      * @return Collection of of the PrimaryKey Class instances.
279      */

280     public Collection JavaDoc convertCollectionObjectIdToPrimaryKey (Collection JavaDoc oids) {
281         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
282
283         Object JavaDoc o = null;
284         for (java.util.Iterator JavaDoc it = oids.iterator(); it.hasNext();) {
285             o = convertObjectIdToPrimaryKey(it.next());
286             if(logger.isLoggable(Logger.FINEST) ) {
287                 logger.finest(
288                     "\n---JDOEJB11HelperImpl.convertCollectionObjectIdToPrimaryKey() adding: " + o);// NOI18N
289
}
290             rc.add(o);
291         }
292         return rc;
293     }
294
295    /**
296      * Converts Collection of PrimaryKey instances to a Collection of Object Id's
297      * of a corresponding persistence-capable Class.
298      * @param keys Collection of the PrimaryKey instances to be converted.
299      * @return Collection of the Object Id's.
300      */

301     public Collection JavaDoc convertCollectionPrimaryKeyToObjectId (Collection JavaDoc keys) {
302         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
303
304         Object JavaDoc o = null;
305         for (java.util.Iterator JavaDoc it = keys.iterator(); it.hasNext();) {
306             o = convertPrimaryKeyToObjectId(it.next());
307             if(logger.isLoggable(Logger.FINEST) ) {
308                 logger.finest(
309                     "\n---JDOEJB11HelperImpl.convertCollectionPrimaryKeyToObjectId() adding: " + o);// NOI18N
310
}
311             rc.add(o);
312         }
313         return rc;
314     }
315
316     /**
317      * Serializes serializableObject into a byte array
318      * @param serializableObject Instance of a Serializable Object
319      * @return serializableObject serialized into a byte array
320      */

321     public byte[] writeSerializableObjectToByteArray(Serializable serializableObject)
322     {
323         byte[] byteArray = null;
324         if(serializableObject != null)
325         {
326             ByteArrayOutputStream bos = new ByteArrayOutputStream();
327             ObjectOutputStream oos = null;
328             try
329             {
330                 oos = new ObjectOutputStream(bos);
331                 oos.writeObject(serializableObject);
332                 byteArray = bos.toByteArray();
333             }
334             catch(java.io.IOException JavaDoc e)
335             {
336                 String JavaDoc clsName = serializableObject.getClass().getName();
337                 throw new JDOUserException(I18NHelper.getMessage(messages,
338                         "EXC_IOWriteSerializableObject", clsName), e);// NOI18N
339
}
340         }
341         return byteArray;
342     }
343
344     /**
345      * Constructs a Serializable object from byteArray. It is expected that
346      * byteArray was constructed using a previous call to writeSerializableObjectToByteArray
347      * @param byteArray Array of byte obtained from a call to writeSerializableObjectToByteArray
348      * @return A Serializable object contructed from byteArray
349      * @see #writeSerializableObjectToByteArray(Serializable)
350      */

351     public Serializable readSerializableObjectFromByteArray(byte[] byteArray)
352     {
353         Serializable serializableObject = null;
354         if(byteArray != null)
355         {
356             ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
357             HelperObjectInputStream ois = null;
358
359             //
360
// Take the current class loader to resolve the class to be deserialized.
361
//
362
ClassLoader JavaDoc cl = this.getClass().getClassLoader();
363             try
364             {
365                 ois = new HelperObjectInputStream(bis, cl);
366                 serializableObject = (Serializable) ois.readObject();
367             }
368             catch (ClassNotFoundException JavaDoc e)
369             {
370                 throw new JDOFatalDataStoreException(I18NHelper.getMessage(messages,
371                         "EXC_CNFReadSerializableObject"), e);// NOI18N
372
}
373             catch(java.io.IOException JavaDoc e)
374             {
375                 throw new JDOFatalDataStoreException(I18NHelper.getMessage(messages,
376                         "EXC_IOReadSerializableObject"), e);// NOI18N
377
}
378         }
379         return serializableObject;
380     }
381
382     /**
383      * Return NumericConverter for conversion from Number to BigDecimal or
384      * BigInteger for this bean type. It is responsible for passing the
385      * correct policy value to the NumericConverterFactory.
386      * @return NumericConverter for given object policy
387      */

388     public NumericConverter getNumericConverter() {
389         int policy = CMPHelper.getNumericConverterPolicy(getContainer());
390         return NumericConverterFactory.getNumericConverter(policy);
391     }
392
393     /**
394      * Returns the class object of the corresponding persistence-capable class
395      * of the concrete bean class.
396      * @return the pc class object
397      */

398     abstract public Class JavaDoc getPCClass ();
399
400     /**
401      * Validates that this instance is of the correct implementation class
402      * of a remote interface type.
403      *
404      * @param o the instance to validate.
405      * @return true if the type is correct.
406      * @throws IllegalArgumentException if validation fails.
407      */

408     abstract public void assertInstanceOfRemoteInterfaceImpl(Object JavaDoc o);
409
410    /**
411      * Validates that this instance is of the correct implementation class
412      * of bean remote interface.
413      * Throws IllegalArgumentException if the argument is of a wrong type.
414      *
415      * @param o the instance to validate.
416      * @param bean name as String.
417      * @throws IllegalArgumentException if validation fails.
418      */

419     protected void assertInstanceOfRemoteInterfaceImpl(Object JavaDoc o,
420         String JavaDoc beanName) {
421
422         // We can't check if null is the correct type or not. So
423
// we let it succeed.
424
if (o == null)
425             return;
426
427         try {
428             CMPHelper.assertValidRemoteObject(o, getContainer());
429
430         } catch (EJBException JavaDoc ex) {
431             String JavaDoc msg = I18NHelper.getMessage(messages, "EXC_WrongRemoteInstance", // NOI18N
432
new Object JavaDoc[] {o.getClass().getName(), beanName,
433                     ex.getMessage()});
434             logger.log(Logger.WARNING, msg);
435             throw new IllegalArgumentException JavaDoc(msg);
436         }
437     }
438
439    /**
440      * Validates that the primary key instance is not null.
441      * Throws IllegalArgumentException otherwise.
442      * @param pk the primary key instance to validate.
443      * @throws IllegalArgumentException if validation fails.
444      */

445     protected void assertPrimaryKeyNotNull(Object JavaDoc pk) {
446         if (pk == null) {
447             throw new IllegalArgumentException JavaDoc(I18NHelper.getMessage(
448                 messages, "EXC_pknull_exception")); // NOI18N
449
}
450     }
451
452    /**
453      * Validates that the primary key field of an Object type is not null.
454      * Throws IllegalArgumentException otherwise.
455      * @param pkfield the primary key field instance to validate.
456      * @param pkfieldName the primary key field name.
457      * @param beanName the EJB name.
458      * @throws IllegalArgumentException if validation fails.
459      */

460     public void assertPrimaryKeyFieldNotNull(Object JavaDoc pkfield, String JavaDoc pkfieldName,
461         String JavaDoc beanName) {
462
463         if (pkfield == null) {
464             throw new IllegalArgumentException JavaDoc(I18NHelper.getMessage(
465                 messages, "EXC_pkfieldnull_exception", // NOI18N
466
pkfieldName, beanName));
467         }
468     }
469
470
471    /**
472      * Validates that the object id instance is not null.
473      * Throws JDOFatalInternalException otherwise.
474      * @param oid the object id instance to validate.
475      * @throws JDOFatalInternalException if validation fails.
476      */

477     protected void assertObjectIdNotNull(Object JavaDoc oid) {
478         if (oid == null) {
479             throw new JDOFatalInternalException(I18NHelper.getMessage(
480                 messages, "EXC_oidnull_exception")); // NOI18N
481
}
482     }
483
484     /** Helper class that allows to use specified class loader to resolve
485       * class name.
486       */

487      static class HelperObjectInputStream extends ObjectInputStream {
488
489          java.lang.ClassLoader JavaDoc classLoader;
490
491          /** Creates new HelperObjectInputStream */
492          public HelperObjectInputStream(InputStream is, ClassLoader JavaDoc cl)
493           throws IOException, StreamCorruptedException {
494              super(is);
495              classLoader = cl;
496          }
497
498          /** Overrides the same method of the base class */
499          protected Class JavaDoc resolveClass(ObjectStreamClass v)
500               throws IOException, ClassNotFoundException JavaDoc {
501               return Class.forName(v.getName(), true, classLoader);
502          }
503
504      }
505 }
506
Popular Tags