1 package org.apache.ojb.broker.core.proxy; 2 3 17 18 import java.lang.reflect.Method ; 19 import java.util.ArrayList ; 20 21 import org.apache.ojb.broker.Identity; 22 import org.apache.ojb.broker.OJBRuntimeException; 23 import org.apache.ojb.broker.PBFactoryException; 24 import org.apache.ojb.broker.PBKey; 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.PersistenceBrokerException; 27 import org.apache.ojb.broker.PersistenceBrokerFactory; 28 import org.apache.ojb.broker.core.PersistenceBrokerThreadMapping; 29 import org.apache.ojb.broker.util.logging.LoggerFactory; 30 31 36 public class IndirectionHandlerDefaultImpl implements IndirectionHandler 37 { 38 static final long serialVersionUID = -1993879565033755826L; 39 40 41 private transient PersistenceBroker _broker = null; 42 43 private PBKey _brokerKey; 44 45 private Object _realSubject = null; 46 48 private Identity _id = null; 49 50 private transient ArrayList _listeners; 51 57 private boolean _needsClose; 58 59 65 public IndirectionHandlerDefaultImpl(PBKey brokerKey, Identity id) 66 { 67 setBrokerKey(brokerKey); 68 setIdentity(id); 69 } 70 71 76 public Identity getIdentity() 77 { 78 return _id; 79 } 80 81 86 protected void setIdentity(Identity identity) 87 { 88 _id = identity; 89 } 90 91 96 public PBKey getBrokerKey() 97 { 98 return _brokerKey; 99 } 100 101 106 protected void setBrokerKey(PBKey brokerKey) 107 { 108 _brokerKey = brokerKey; 109 } 110 111 116 public synchronized void addListener(MaterializationListener listener) 117 { 118 if (_listeners == null) 119 { 120 _listeners = new ArrayList (); 121 } 122 if(!_listeners.contains(listener)) 124 { 125 _listeners.add(listener); 126 } 127 } 128 129 134 public synchronized void removeListener(MaterializationListener listener) 135 { 136 if (_listeners != null) 137 { 138 _listeners.remove(listener); 139 } 140 } 141 142 146 protected void beforeMaterialization() 147 { 148 if (_listeners != null) 149 { 150 MaterializationListener listener; 151 152 for (int idx = _listeners.size() - 1; idx >= 0; idx--) 153 { 154 listener = (MaterializationListener)_listeners.get(idx); 155 listener.beforeMaterialization(this, _id); 156 } 157 } 158 } 159 160 164 protected void afterMaterialization() 165 { 166 if (_listeners != null) 167 { 168 MaterializationListener listener; 169 170 for (int idx = _listeners.size() - 1; idx >= 0; idx--) 174 { 175 listener = (MaterializationListener)_listeners.get(idx); 176 listener.afterMaterialization(this, _realSubject); 177 } 178 } 179 } 180 181 187 protected synchronized PersistenceBroker getBroker() throws PBFactoryException 188 { 189 PersistenceBroker broker; 190 191 if (getBrokerKey() == null) 192 { 193 198 throw new OJBRuntimeException("Can't find associated PBKey. Need PBKey to obtain a valid" + 199 "PersistenceBroker instance from intern resources."); 200 } 201 broker = PersistenceBrokerThreadMapping.currentPersistenceBroker(getBrokerKey()); 203 if (broker == null) 205 { 206 if (_broker == null) 207 { 208 _broker = PersistenceBrokerFactory.createPersistenceBroker(getBrokerKey()); 209 _needsClose = true; 211 broker = _broker; 212 } 213 } 214 return broker; 215 } 216 217 220 protected void releaseBroker() 221 { 222 if (_broker != null && _needsClose) 223 { 224 _needsClose = false; 225 _broker.close(); 226 _broker = null; 227 } 228 } 229 230 281 public Object invoke(Object proxy, Method method, Object [] args) 282 { 283 Object subject = null; 284 String methodName = method.getName(); 285 286 try 287 { 288 301 302 315 316 if ("equals".equals(methodName) && args[0] != null) 319 { 320 Object otherObj = ProxyHelper.getRealObject(args[0]); 321 args[0] = otherObj; 322 } 323 324 subject = getRealSubject(); 325 return method.invoke(subject, args); 326 } 332 catch (Exception ex) 333 { 334 throw new PersistenceBrokerException("Error invoking method "+method.getName(), ex); 335 } 336 } 337 338 343 public Object getRealSubject() throws PersistenceBrokerException 344 { 345 if (_realSubject == null) 346 { 347 beforeMaterialization(); 348 _realSubject = materializeSubject(); 349 afterMaterialization(); 350 } 351 return _realSubject; 352 } 353 354 360 public void setRealSubject(Object object) 361 { 362 _realSubject = object; 363 } 364 365 371 protected synchronized Object materializeSubject() throws PersistenceBrokerException 372 { 373 try 374 { 375 Object realSubject = getBroker().getObjectByIdentity(_id); 376 if (realSubject == null) 377 { 378 LoggerFactory.getLogger(IndirectionHandler.class).warn("Can not materialize object for Identity " + _id + " - using PBKey " + _brokerKey); 379 } 380 return realSubject; 381 } 382 catch (Exception ex) 383 { 384 throw new PersistenceBrokerException(ex); 385 } 386 finally 387 { 388 releaseBroker(); 389 } 390 } 391 392 397 public boolean alreadyMaterialized() 398 { 399 return _realSubject != null; 400 } 401 } 402 | Popular Tags |