1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.support.ejb.cmp; 33 34 import java.util.Collection ; 35 import java.util.Set ; 36 import java.util.ResourceBundle ; 37 import java.io.*; 38 39 import javax.ejb.EJBObject ; 40 import javax.ejb.EJBException ; 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 65 abstract public class JDOEJB11HelperImpl implements JDOEJB11Helper { 66 67 70 protected final static ResourceBundle messages = I18NHelper.loadBundle( 71 JDOEJB11HelperImpl.class); 72 73 protected static Logger logger = LogHelperEntityInternal.getLogger(); 75 76 82 public EJBObject convertPCToEJBObject (Object pc, PersistenceManager pm) { 83 if (pc == null) return null; 84 Object jdoObjectId = pm.getObjectId(pc); 85 Object key = convertObjectIdToPrimaryKey(jdoObjectId); 86 try { 87 return CMPHelper.getEJBObject(key, getContainer()); 88 } catch (Exception ex) { 89 EJBException e = new EJBException (I18NHelper.getMessage(messages, 90 "EXC_ConvertPCToEJBObject", key.toString()), ex); logger.throwing("JDOEJB11HelperImpl", "convertPCToEJBObject", e); throw e; 93 } 94 } 95 96 105 public Object convertEJBObjectToPC(EJBObject o, PersistenceManager pm, boolean validate) { 106 Object key = null; 107 try { 108 key = o.getPrimaryKey(); 109 } catch (Exception ex) { 110 EJBException e = new EJBException (I18NHelper.getMessage(messages, 111 "EXC_ConvertEJBObjectToPC", o.getClass().getName()), ex); logger.throwing("JDOEJB11HelperImpl", "convertEJBObjectToPC", e); throw e; 114 } 115 return convertPrimaryKeyToPC(key, pm, validate); 116 } 117 118 127 protected Object convertPrimaryKeyToPC(Object key, PersistenceManager pm, boolean validate) { 128 Object pc = null; 129 try { 130 Object jdoObjectId = convertPrimaryKeyToObjectId(key); 131 pc = pm.getObjectById(jdoObjectId, validate); 132 } catch (JDOObjectNotFoundException ex) { 133 logger.fine("---JDOEJB11HelperImpl.convertPrimaryKeyToPC: Object not found for: " + key); 135 throw new IllegalArgumentException (I18NHelper.getMessage(messages, 136 "EXC_DeletedInstanceOtherTx", key.toString())); } 138 139 if (validate && JDOHelper.isDeleted(pc)) { 140 logger.fine("---JDOEJB11HelperImpl.convertPrimaryKeyToPC: Object is deleted for: " + key); 142 throw new IllegalArgumentException (I18NHelper.getMessage(messages, 143 "EXC_DeletedInstanceThisTx", key.toString())); } 145 146 return pc; 147 } 148 149 156 public Collection convertCollectionPCToEJBObject (Collection pcs, PersistenceManager pm) { 157 Collection rc = new java.util.ArrayList (); 158 159 Object o = null; 160 for (java.util.Iterator it = pcs.iterator(); it.hasNext();) { 161 o = convertPCToEJBObject((Object )it.next(), pm); 162 if(logger.isLoggable(Logger.FINEST) ) { 163 logger.finest( 164 "\n---JDOEJB11HelperImpl.convertCollectionPCToEJBObject() adding: " + o); } 166 rc.add(o); 167 } 168 return rc; 169 } 170 171 178 public Set convertCollectionPCToEJBObjectSet (Collection pcs, PersistenceManager pm) { 179 java.util.Set rc = new java.util.HashSet (); 180 181 Object o = null; 182 for (java.util.Iterator it = pcs.iterator(); it.hasNext();) { 183 o = convertPCToEJBObject((Object )it.next(), pm); 184 if(logger.isLoggable(Logger.FINEST) ) { 185 logger.finest( 186 "\n---JDOEJB11HelperImpl.convertCollectionPCToEJBObjectSet() adding: " + o); } 188 rc.add(o); 189 } 190 return rc; 191 } 192 193 203 public Collection convertCollectionEJBObjectToPC (Collection coll, PersistenceManager pm, 204 boolean validate) { 205 Collection rc = new java.util.ArrayList (); 206 207 Object o = null; 208 for (java.util.Iterator it = coll.iterator(); it.hasNext();) { 209 o = convertEJBObjectToPC((EJBObject )it.next(), pm, validate); 210 if(logger.isLoggable(Logger.FINEST) ) { 211 logger.finest( 212 "\n---JDOEJB11HelperImpl.convertCollectionEJBObjectToPC() adding: " + o); } 214 rc.add(o); 215 } 216 return rc; 217 } 218 219 225 public Object convertPCToPrimaryKey (Object pc, PersistenceManager pm) { 226 if (pc == null) return null; 227 Object rc = convertObjectIdToPrimaryKey(pm.getObjectId(pc)); 228 229 if(logger.isLoggable(Logger.FINEST) ) { 230 logger.finest("\n---JDOEJB11HelperImpl.convertPCToPrimaryKey() PK: " + rc); } 232 return rc; 233 } 234 235 236 243 public Collection convertCollectionPCToPrimaryKey (Collection pcs, PersistenceManager pm) { 244 Collection rc = new java.util.ArrayList (); 245 246 Object o = null; 247 for (java.util.Iterator 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); } 253 rc.add(o); 254 } 255 return rc; 256 } 257 258 264 abstract public Object convertObjectIdToPrimaryKey (Object objectId); 265 266 272 abstract public Object convertPrimaryKeyToObjectId (Object key); 273 274 280 public Collection convertCollectionObjectIdToPrimaryKey (Collection oids) { 281 Collection rc = new java.util.ArrayList (); 282 283 Object o = null; 284 for (java.util.Iterator 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); } 290 rc.add(o); 291 } 292 return rc; 293 } 294 295 301 public Collection convertCollectionPrimaryKeyToObjectId (Collection keys) { 302 Collection rc = new java.util.ArrayList (); 303 304 Object o = null; 305 for (java.util.Iterator 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); } 311 rc.add(o); 312 } 313 return rc; 314 } 315 316 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 e) 335 { 336 String clsName = serializableObject.getClass().getName(); 337 throw new JDOUserException(I18NHelper.getMessage(messages, 338 "EXC_IOWriteSerializableObject", clsName), e); } 340 } 341 return byteArray; 342 } 343 344 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 ClassLoader cl = this.getClass().getClassLoader(); 363 try 364 { 365 ois = new HelperObjectInputStream(bis, cl); 366 serializableObject = (Serializable) ois.readObject(); 367 } 368 catch (ClassNotFoundException e) 369 { 370 throw new JDOFatalDataStoreException(I18NHelper.getMessage(messages, 371 "EXC_CNFReadSerializableObject"), e); } 373 catch(java.io.IOException e) 374 { 375 throw new JDOFatalDataStoreException(I18NHelper.getMessage(messages, 376 "EXC_IOReadSerializableObject"), e); } 378 } 379 return serializableObject; 380 } 381 382 388 public NumericConverter getNumericConverter() { 389 int policy = CMPHelper.getNumericConverterPolicy(getContainer()); 390 return NumericConverterFactory.getNumericConverter(policy); 391 } 392 393 398 abstract public Class getPCClass (); 399 400 408 abstract public void assertInstanceOfRemoteInterfaceImpl(Object o); 409 410 419 protected void assertInstanceOfRemoteInterfaceImpl(Object o, 420 String beanName) { 421 422 if (o == null) 425 return; 426 427 try { 428 CMPHelper.assertValidRemoteObject(o, getContainer()); 429 430 } catch (EJBException ex) { 431 String msg = I18NHelper.getMessage(messages, "EXC_WrongRemoteInstance", new Object [] {o.getClass().getName(), beanName, 433 ex.getMessage()}); 434 logger.log(Logger.WARNING, msg); 435 throw new IllegalArgumentException (msg); 436 } 437 } 438 439 445 protected void assertPrimaryKeyNotNull(Object pk) { 446 if (pk == null) { 447 throw new IllegalArgumentException (I18NHelper.getMessage( 448 messages, "EXC_pknull_exception")); } 450 } 451 452 460 public void assertPrimaryKeyFieldNotNull(Object pkfield, String pkfieldName, 461 String beanName) { 462 463 if (pkfield == null) { 464 throw new IllegalArgumentException (I18NHelper.getMessage( 465 messages, "EXC_pkfieldnull_exception", pkfieldName, beanName)); 467 } 468 } 469 470 471 477 protected void assertObjectIdNotNull(Object oid) { 478 if (oid == null) { 479 throw new JDOFatalInternalException(I18NHelper.getMessage( 480 messages, "EXC_oidnull_exception")); } 482 } 483 484 487 static class HelperObjectInputStream extends ObjectInputStream { 488 489 java.lang.ClassLoader classLoader; 490 491 492 public HelperObjectInputStream(InputStream is, ClassLoader cl) 493 throws IOException, StreamCorruptedException { 494 super(is); 495 classLoader = cl; 496 } 497 498 499 protected Class resolveClass(ObjectStreamClass v) 500 throws IOException, ClassNotFoundException { 501 return Class.forName(v.getName(), true, classLoader); 502 } 503 504 } 505 } 506 | Popular Tags |