KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JDOEJB20HelperImpl.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
37 import javax.ejb.EJBLocalObject JavaDoc;
38 import javax.ejb.EJBException JavaDoc;
39 import javax.ejb.EJBContext JavaDoc;
40
41 import com.sun.jdo.api.persistence.support.PersistenceManager;
42 import com.sun.jdo.api.persistence.support.JDOHelper;
43 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.CMPHelper;
44 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.JDOEJB20Helper;
45 import com.sun.jdo.spi.persistence.utility.logging.Logger;
46 import com.sun.jdo.spi.persistence.utility.I18NHelper;
47
48
49 /*
50  * This is an abstract class which is a generic implementation of the
51  * JDOEJBHelper interface for conversion of persistence-capable instances
52  * to and from EJB objects of type EJBLocalObject and Collections of those.
53  * It extends JDOEJB11HelperImpl for conversion of instances of other types.
54  *
55  * @author Marina Vatkina
56  */

57 abstract public class JDOEJB20HelperImpl extends JDOEJB11HelperImpl
58     implements JDOEJB20Helper {
59
60     /**
61      * Converts persistence-capable instance to EJBLocalObject.
62      * @param pc the persistence-capable instance to be converted as an Object.
63      * @param pm the associated instance of PersistenceManager.
64      * @return instance of EJBLocalObject.
65      */

66     public EJBLocalObject JavaDoc convertPCToEJBLocalObject (Object JavaDoc pc, PersistenceManager pm) {
67         if (pc == null) return null;
68         Object JavaDoc jdoObjectId = pm.getObjectId(pc);
69         Object JavaDoc key = convertObjectIdToPrimaryKey(jdoObjectId);
70         try {
71             return CMPHelper.getEJBLocalObject(key, getContainer());
72         } catch (Exception JavaDoc ex) {
73             EJBException JavaDoc e = new EJBException JavaDoc(I18NHelper.getMessage(messages,
74                         "EXC_ConvertPCToEJBLocalObject", key.toString()), ex);// NOI18N
75
logger.throwing("JDOEJB20HelperImpl", "convertPCToEJBLocalObject", e); // NOI18N
76
throw e;
77         }
78     }
79
80     /**
81      * Converts persistence-capable instance to EJBLocalObject. Returns null if
82      * the instance is already removed via cascade-delete operation.
83      * @param pc the persistence-capable instance to be converted as an Object.
84      * @param pm the associated instance of PersistenceManager.
85      * @param context the EJBContext of the calling bean.
86      * @return instance of EJBLocalObject.
87      */

88     public EJBLocalObject JavaDoc convertPCToEJBLocalObject (Object JavaDoc pc, PersistenceManager pm,
89         EJBContext JavaDoc context) {
90         if (pc == null) return null;
91         Object JavaDoc jdoObjectId = pm.getObjectId(pc);
92         Object JavaDoc key = convertObjectIdToPrimaryKey(jdoObjectId);
93         try {
94             return CMPHelper.getEJBLocalObject(key, getContainer(), context);
95         } catch (Exception JavaDoc ex) {
96             EJBException JavaDoc e = new EJBException JavaDoc(I18NHelper.getMessage(messages,
97                         "EXC_ConvertPCToEJBLocalObjectCtx", key.toString()), ex);// NOI18N
98
logger.throwing("JDOEJB20HelperImpl", "convertPCToEJBLocalObjectCtx", e); // NOI18N
99
throw e;
100         }
101     }
102
103     /**
104      * Converts EJBLocalObject to persistence-capable instance.
105      * @param o the EJBLocalObject instance to be converted.
106      * @param pm the associated instance of PersistenceManager.
107      * @param validate true if the existence of the instance is to be validated.
108      * @return persistence-capable instance.
109      * @throws IllegalArgumentException if validate is true and instance does
110      * not exist in the database or is deleted.
111      */

112     public Object JavaDoc convertEJBLocalObjectToPC(EJBLocalObject JavaDoc o, PersistenceManager pm, boolean validate) {
113         Object JavaDoc key = null;
114         try {
115             key = o.getPrimaryKey();
116         } catch (Exception JavaDoc ex) {
117             EJBException JavaDoc e = new EJBException JavaDoc(I18NHelper.getMessage(messages,
118                         "EXC_ConvertEJBObjectToPC", o.getClass().getName()), ex);// NOI18N
119
logger.throwing("JDOEJB20HelperImpl", "convertEJBLocalObjectToPC", e); // NOI18N
120
throw e;
121         }
122         return convertPrimaryKeyToPC(key, pm, validate);
123     }
124
125     /**
126      * Converts Collection of persistence-capable instances to a Collection of
127      * EJBLocalObjects.
128      * @param pcs the Collection of persistence-capable instance to be converted.
129      * @param pm the associated instance of PersistenceManager.
130      * @return Collection of EJBLocalObjects.
131      */

132     public Collection JavaDoc convertCollectionPCToEJBLocalObject (Collection JavaDoc pcs, PersistenceManager pm){
133         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
134         Object JavaDoc o = null;
135         boolean debug = false;
136
137         for (java.util.Iterator JavaDoc it = pcs.iterator(); it.hasNext();) {
138             o = convertPCToEJBLocalObject((Object JavaDoc)it.next(), pm);
139             if(logger.isLoggable(Logger.FINEST) ) {
140                 logger.finest(
141                     "\n---JDOEJB20HelperImpl.convertCollectionPCToEJBLocalObject() adding: " + o);// NOI18N
142
}
143             rc.add(o);
144         }
145         return rc;
146     }
147
148     /**
149      * Converts Collection of persistence-capable instances to a Set of
150      * EJBLocalObjects.
151      * @param pcs the Collection of persistence-capable instance to be converted.
152      * @param pm the associated instance of PersistenceManager.
153      * @return Set of EJBLocalObjects.
154      */

155     public Set JavaDoc convertCollectionPCToEJBLocalObjectSet (Collection JavaDoc pcs, PersistenceManager pm) {
156         java.util.Set JavaDoc rc = new java.util.HashSet JavaDoc();
157         Object JavaDoc o = null;
158
159         boolean debug = false;
160
161         for (java.util.Iterator JavaDoc it = pcs.iterator(); it.hasNext();) {
162             o = convertPCToEJBLocalObject((Object JavaDoc)it.next(), pm);
163             if(logger.isLoggable(Logger.FINEST) ) {
164                 logger.finest(
165                     "\n---JDOEJB20HelperImpl.convertCollectionPCToEJBLocalObjectSet() adding: " + o);// NOI18N
166
}
167             rc.add(o);
168         }
169         return rc;
170     }
171
172     /**
173      * Converts Collection of EJBLocalObjects to a Collection of
174      * persistence-capable instances.
175      * @param coll the Collection of EJBLocalObject instances to be converted.
176      * @param pm the associated instance of PersistenceManager.
177      * @param validate true if the existence of the instances is to be validated.
178      * @return Collection of persistence-capable instance.
179      * @throws IllegalArgumentException if validate is true and at least one instance does
180      * not exist in the database or is deleted.
181      */

182     public Collection JavaDoc convertCollectionEJBLocalObjectToPC (Collection JavaDoc coll, PersistenceManager pm,
183                                                            boolean validate) {
184         Collection JavaDoc rc = new java.util.ArrayList JavaDoc();
185         Object JavaDoc o = null;
186
187         boolean debug = false;
188
189         for (java.util.Iterator JavaDoc it = coll.iterator(); it.hasNext();) {
190             o = convertEJBLocalObjectToPC((EJBLocalObject JavaDoc)it.next(), pm, validate);
191             if(logger.isLoggable(Logger.FINEST) ) {
192                 logger.finest(
193                     "\n---JDOEJB20HelperImpl.convertCollectionEJBLocalObjectToPC() adding: " + o);// NOI18N
194
}
195             rc.add(o);
196         }
197         return rc;
198     }
199
200     /**
201      * Validates that this instance is of the correct implementation class
202      * of a local interface type.
203      *
204      * @param o the instance to validate.
205      * @return true if the type is correct.
206      * @throws IllegalArgumentException if validation fails.
207      */

208     abstract public void assertInstanceOfLocalInterfaceImpl(Object JavaDoc o);
209
210    /**
211      * Validates that this instance is of the correct implementation class
212      * of a local interface.
213      * Throws IllegalArgumentException if the passed
214      * argument is of a wrong type.
215      *
216      * @param o the instance to validate.
217      * @param bean name as String.
218      * @throws IllegalArgumentException if validation fails.
219      */

220     protected void assertInstanceOfLocalInterfaceImpl(Object JavaDoc o,
221         String JavaDoc beanName) {
222
223         // We can't check if null is the correct type or not. So
224
// we let it succeed.
225
if (o == null)
226             return;
227
228         try {
229             CMPHelper.assertValidLocalObject(o, getContainer());
230
231         } catch (EJBException JavaDoc ex) {
232             String JavaDoc msg = I18NHelper.getMessage(messages, "EXC_WrongLocalInstance", // NOI18N
233
new Object JavaDoc[] {o.getClass().getName(), beanName,
234                     ex.getMessage()});
235             logger.log(Logger.WARNING, msg);
236             throw new IllegalArgumentException JavaDoc(msg);
237         }
238     }
239
240 }
241
Popular Tags