1 25 26 package org.objectweb.jonas_ejb.container.jorm; 27 28 import org.objectweb.jonas_ejb.container.JContainer; 29 import org.objectweb.jonas_ejb.container.JEntityFactory; 30 import org.objectweb.jonas_ejb.container.JEntitySwitch; 31 import org.objectweb.jonas_ejb.container.TraceEjb; 32 import org.objectweb.jonas_ejb.deployment.api.EjbRelationshipRoleDesc; 33 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc; 34 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 35 import org.objectweb.jonas_ejb.lib.JormType; 36 import org.objectweb.jorm.api.PBinding; 37 import org.objectweb.jorm.api.PClassMapping; 38 import org.objectweb.jorm.api.PClassMappingCtrl; 39 import org.objectweb.jorm.api.PException; 40 import org.objectweb.jorm.api.PMapCluster; 41 import org.objectweb.jorm.api.PMapper; 42 import org.objectweb.jorm.api.PMappingCallback; 43 import org.objectweb.jorm.facility.naming.basidir.BasidBinder; 44 import org.objectweb.jorm.naming.api.PBinder; 45 import org.objectweb.jorm.naming.api.PExceptionNaming; 46 import org.objectweb.jorm.naming.api.PName; 47 import org.objectweb.jorm.naming.api.PNameCoder; 48 import org.objectweb.jorm.naming.api.PNamingContext; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 51 import javax.ejb.EJBException ; 52 import javax.ejb.EntityBean ; 53 54 import java.io.Serializable ; 55 import java.util.Iterator ; 56 57 64 public abstract class JormFactory extends JEntityFactory implements PClassMapping, PClassMappingCtrl { 65 66 protected int relNonInit; 67 protected boolean mapped; 68 protected PMapper mapper = null; 69 protected EntityCmp2Desc ecd = null; 70 71 74 private Class bindingClass = null; 75 76 77 78 81 public JormFactory() { 82 super(); 83 } 84 85 protected abstract void setMapper(String mapperName) throws PException; 86 public abstract Object getConnection(Object hints) throws PException; 87 public abstract void releaseConnection(Object conn) throws PException; 88 89 93 public void init(EntityDesc ed, JContainer c, String mapperName) { 94 if (TraceEjb.isDebugFactory()) { 95 TraceEjb.factory.log(BasicLevel.DEBUG, ed.getEjbName()); 96 } 97 98 super.init(ed, c); 101 ecd = (EntityCmp2Desc) ed; 102 try { 103 setMapper(mapperName); 104 } catch (PException e) { 105 throw new EJBException ("JormFactory cannot create the mapper", e); 106 } 107 108 if (TraceEjb.isDebugFactory()) { 110 TraceEjb.factory.log(BasicLevel.DEBUG, "Jorm initialisation"); 111 } 112 PBinder binder = null; 113 Class binderClass = null; 114 int binderCT = 0; 115 try { 116 binderClass = c.getClassLoader().loadClass(ecd.getJormBinderClassName()); 118 binder = (PBinder) binderClass.newInstance(); 119 if (ecd.hasPrimaryKeyField()) { 120 binderCT = JormType.getCodingType(ecd.getCmpFieldDesc(ecd.getPrimaryKeyFieldName()).getFieldType(), true); 121 ((BasidBinder) binder).setCodingType(binderCT); 122 } else { 123 } 127 if (TraceEjb.isDebugFactory()) { 128 TraceEjb.factory.log(BasicLevel.DEBUG, "binder " + ecd.getJormBinderClassName() + "instanciated"); 129 } 130 131 binder.setPClassMapping(this); 133 setPBinder(binder); 134 setClassPNameCoder(binder); 135 if (TraceEjb.isDebugFactory()) { 136 TraceEjb.factory.log(BasicLevel.DEBUG, "binder linked to the mapping"); 137 } 138 139 } catch (Exception e) { 140 TraceEjb.factory.log(BasicLevel.ERROR, "Impossible to create the binder", e); 141 throw new EJBException ("Impossible to create the binder: bean:" + ecd.getEjbName(), e); 142 } 143 144 relNonInit = 0; 146 try { 147 for (Iterator it = ecd.getEjbRelationshipRoleDescIterator(); it.hasNext();) { 148 EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) it.next(); 149 String source = rsr.getSourceBean().getEjbName(); 150 String target = rsr.getTargetBean().getEjbName(); 151 JormFactory pcm2 = source.equals(target) ? this : (JormFactory) c.getBeanFactory(target); 152 if (TraceEjb.isDebugFactory()) { 153 TraceEjb.factory.log(BasicLevel.DEBUG, 154 "treatement of the relation " + rsr.getRelation().getName() 155 + ": current-bean=" + ecd.getEjbName() 156 + ", source-bean=" + source 157 + ", dest-bean=" + target 158 + ", cmr-field=" + rsr.getCmrFieldName()); 159 } 160 if (rsr.hasCmrField()) { 161 PClassMapping gcm = null; 162 if (rsr.isTargetMultiple()) { 164 gcm = newGCMInstance(mapperName); 166 gcm.init((PMappingCallback) mapper, null); 167 setGenClassMapping(rsr.getCmrFieldName(), gcm); 168 if (TraceEjb.isDebugFactory()) { 169 TraceEjb.factory.log(BasicLevel.DEBUG, 170 "assign a GenClassMapping for the CMR " 171 + rsr.getCmrFieldName() + " / gcm=" + gcm); 172 } 173 174 PBinder gcmBinder = null; 175 try { 176 gcmBinder = (PBinder) binderClass.newInstance(); 177 } catch (Exception e) { 178 TraceEjb.factory.log(BasicLevel.ERROR, 179 "Impossible to create the binder of the GCM bean: " 180 + ecd.getEjbName() + " / CMR: " 181 + rsr.getCmrFieldName(), e); 182 throw new EJBException ( 183 "Impossible to create the binder of the GCM bean: " 184 + ecd.getEjbName() + " / CMR: " 185 + rsr.getCmrFieldName(), e); 186 } 187 if (ecd.hasPrimaryKeyField()) { 188 ((BasidBinder) gcmBinder).setCodingType(binderCT); 189 } else { 190 } 194 gcm.setPBinder(gcmBinder); 195 gcmBinder.setPClassMapping(gcm); 196 setPNameCoder(rsr.getCmrFieldName(), (PNameCoder) gcmBinder); 197 } 198 199 if (pcm2 != null) { 202 if (TraceEjb.isDebugFactory()) { 203 TraceEjb.factory.log(BasicLevel.DEBUG, "Pnc Assignement"); 204 } 205 if (rsr.isTargetMultiple()) { 206 ((PClassMappingCtrl)gcm).setPNameCoder((PNameCoder) pcm2.getPBinder()); 207 } else { 208 setPNameCoder(rsr.getCmrFieldName(), (PNameCoder) pcm2.getPBinder()); 209 } 210 if (pcm2.isPrefetch()) { 211 initGenClassPrefetch(gcm, pcm2); 212 } 213 } else { 214 relNonInit++; 215 if (TraceEjb.isDebugFactory()) { 216 TraceEjb.factory.log(BasicLevel.DEBUG, "the Pnc is not reachable currently. relNonInit=" + relNonInit); 217 } 218 } 220 } 221 EjbRelationshipRoleDesc rsr2 = rsr.getOppositeRelationshipRole(); 222 if (pcm2 != null && rsr2.hasCmrField() && pcm2 != this) { 223 if (TraceEjb.isDebugFactory()) { 229 TraceEjb.factory.log(BasicLevel.DEBUG, 230 "later Pnc assignement of the opposite CMR field: " 231 + rsr2.getCmrFieldName()); 232 } 233 pcm2.configurePnc(rsr2.getCmrFieldName(), (PNamingContext) getPBinder(), this, rsr2.isTargetMultiple()); 234 } 235 } 236 } catch (EJBException e) { 237 throw e; 238 } catch (Exception e) { 239 TraceEjb.factory.log(BasicLevel.ERROR, 240 "Impossible to assign the naming context to the PClassMapping", e); 241 throw new EJBException ("Impossible to assign the naming context to the PClassMapping", e); 242 } 243 mapped = false; 244 if (relNonInit == 0) { 245 mapClass(); 246 } 247 248 String cn = ((EntityCmp2Desc) dd).getJormBindingClassName(); 250 try { 251 bindingClass = getContainer().getClassLoader().loadClass(cn); 252 } catch (ClassNotFoundException e) { 253 String err = "Impossible to load binding class '" + cn + "'."; 254 TraceEjb.factory.log(BasicLevel.ERROR, err, e); 255 throw new EJBException (err, e); 256 } 257 258 } 259 260 263 public PMapper getMapper() { 264 return mapper; 265 } 266 267 273 public void configurePnc(String n, PNamingContext pnc, JormFactory target, boolean isMultiple) throws PException { 274 if (isMultiple) { 275 PClassMappingCtrl gcm = (PClassMappingCtrl) getGenClassMapping(n); 276 gcm.setPNameCoder(pnc); 277 if (target.isPrefetch()) { 278 initGenClassPrefetch((PClassMapping) gcm, target); 280 } 281 282 } else { 283 setPNameCoder(n, pnc); 284 } 285 relNonInit--; 286 if (TraceEjb.isDebugFactory()) { 287 TraceEjb.factory.log(BasicLevel.DEBUG, "PNamingContext assigned, relNonInit=" + relNonInit); 288 } 289 if (relNonInit == 0) { 290 mapClass(); 291 } 292 } 293 294 private void mapClass() { 295 if (mapped) { 296 throw new EJBException ("The class is already mapped"); 297 } 298 299 try { 300 mapper.map(this); 302 PMapCluster pmapclust = mapper.getPMapCluster(getClassName()); 304 if (pmapclust.isDefined()) { 305 switch (ecd.getCleanupPolicy()) { 306 case EntityDesc.CLEANUP_REMOVEDATA: 307 pmapclust.createMappingStructures(false); 308 pmapclust.deleteData(); 309 break; 310 case EntityDesc.CLEANUP_REMOVEALL: 311 pmapclust.deleteMappingStructures(); 312 pmapclust.createMappingStructures(true); 313 break; 314 case EntityDesc.CLEANUP_NONE: 315 break; 316 case EntityDesc.CLEANUP_CREATE: 317 pmapclust.createMappingStructures(false); 318 break; 319 default: 320 throw new EJBException ("Unknown cleanup policy: " + ecd.getCleanupPolicy()); 321 } 322 } else { 323 } 327 mapped = true; 328 if (TraceEjb.isDebugFactory()) { 329 TraceEjb.factory.log(BasicLevel.DEBUG, getClassName() + " is mapped"); 330 } 331 } catch (PException pe) { 332 Exception e = pe; 333 while ((e instanceof PException) && ((PException) e).getNestedException() != null) { 334 e = ((PException) e).getNestedException(); 335 } 336 TraceEjb.factory.log(BasicLevel.ERROR, "Impossible to map the class on the rdb mapper", e); 337 throw new EJBException ("Impossible to map the class on the rdb mapper", e); 338 } 339 } 340 341 public void stop() { 342 super.stop(); 343 try { 344 mapper.unmap(getClassName()); 345 mapped = false; 346 } catch (PException e) { 347 TraceEjb.factory.log(BasicLevel.ERROR, 348 "Impossible to unmap the class " + getClassName(), e); 349 } 350 } 351 352 358 protected org.objectweb.jonas_ejb.container.JEntityContext createNewContext(EntityBean bean) { 359 return new org.objectweb.jonas_ejb.container.jorm.JEntityContext(this, bean); 360 } 361 362 public JEntitySwitch getJEntitySwitch() { 363 Object result = null; 364 try { 365 result = bindingClass.newInstance(); 366 } catch (Exception e) { 367 TraceEjb.factory.log(BasicLevel.ERROR, "Impossible to create a new JEntitySwitch as specified in BeanNaming: " + bindingClass.getName(), e); 368 return super.getJEntitySwitch(); 369 } 370 try { 371 ((PBinding) result).init(this); 372 } catch (PException e) { 373 TraceEjb.factory.log(BasicLevel.ERROR, "Impossible to initialized the new JEntitySwitch as specified in BeanNaming: " + bindingClass.getName(), e); 374 } 375 return (JEntitySwitch) result; 376 } 377 378 382 protected PClassMapping newGCMInstance(String mapperName) throws Exception { 383 int idx = mapperName.indexOf("."); 384 String mn = (idx != -1) ? mapperName.substring(0, idx) : mapperName; 385 return (PClassMapping) Class.forName("org.objectweb.jorm.mapper." + mn + ".genclass." 386 + Character.toUpperCase(mn.charAt(0)) 387 + mn.substring(1, mn.length()) + "GenClassMapping" 388 ).newInstance(); 389 } 390 391 395 public Serializable encodePK(Serializable pk) { 396 try { 397 return (Serializable ) ((PName) pk).encodeString(); 398 } catch (PExceptionNaming e) { 399 TraceEjb.factory.log(BasicLevel.ERROR, "impossible to serialize PK" + e); 400 return pk; 401 } 402 } 403 404 408 public Serializable decodePK(Serializable strpk) { 409 try { 410 return getPBinder().decodeString((String ) strpk); 411 } catch (PExceptionNaming e) { 412 TraceEjb.factory.log(BasicLevel.ERROR, "impossible to deserialize PK" + e); 413 return strpk; 414 } 415 } 416 417 423 protected abstract void initGenClassPrefetch(PClassMapping gcm, PClassMapping targetPCM); 424 } 425 | Popular Tags |