Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*     */ package org.jboss.aspects.versioned;
  /*     */ 
  /*     */ import java.io.Externalizable;
  /*     */ import java.io.IOException;
  /*     */ import java.io.ObjectInput;
  /*     */ import java.io.ObjectOutput;
  /*     */ import java.lang.ref.WeakReference;
  /*     */ import java.lang.reflect.Field;
  /*     */ import java.util.Collection;
 /*     */ import java.util.HashMap;
 /*     */ import java.util.Iterator;
 /*     */ import java.util.Map.Entry;
 /*     */ import java.util.Set;
 /*     */ import javax.naming.InitialContext;
 /*     */ import javax.transaction.Transaction;
 /*     */ import javax.transaction.TransactionManager;
 /*     */ import org.jboss.aop.Advised;
 /*     */ import org.jboss.aop.ClassAdvisor;
 /*     */ import org.jboss.aop.InstanceAdvised;
 /*     */ import org.jboss.aop.joinpoint.Invocation;
 /*     */ import org.jboss.logging.Logger;
 /*     */ import org.jboss.tm.TransactionLocal;
 /*     */ import org.jboss.util.id.GUID;
 /*     */ 
 /*     */ public class DistributedPOJOState extends StateManager
 /*     */   implements DistributedStateExternalizable
 /*     */ {
 /*     */   private static final long serialVersionUID = 7640633352012924284L;
 /*  54 */   private static Logger log = Logger.getLogger(DistributedPOJOState.class);
 /*     */   protected String classname;
 /*     */   protected HashMap fieldMap;
 /*     */   protected transient TransactionManager tm;
 /*     */   protected transient WeakReference advisedRef;
 /*  60 */   protected transient TransactionLocal txState = new TransactionLocal();
 /*     */   protected transient SynchronizationManager synchManager;
 /*     */   protected transient DistributedVersionManager versionManager;
 /*     */ 
 /*     */   public DistributedPOJOState()
 /*     */   {
 /*     */   }
 /*     */ 
 /*     */   public DistributedPOJOState(GUID daguidlong datimeoutAdvised advisedDistributedVersionManager versionManagerSynchronizationManager synchManager)
 /*     */     throws Exception
 /*     */   {
 /*  69 */     super(daguiddatimeout);
 /*  70 */     this. = new HashMap();
 /*  71 */     this. = advised.getClass().getName();
 /*  72 */     InitialContext ctx = new InitialContext();
 /*  73 */     this. = ((TransactionManager)ctx.lookup("java:/TransactionManager"));
 /*  74 */     this. = synchManager;
 /*  75 */     this. = versionManager;
 /*  76 */     this. = new WeakReference(advised);
 /*     */   }
 /*     */ 
 /*     */   public InstanceAdvised getObject()
 /*     */   {
 /*  81 */     if (this. != null)
 /*     */     {
 /*  83 */       return (InstanceAdvised)this..get();
 /*     */     }
 /*  85 */     return null;
 /*     */   }
 /*     */ 
 /*     */   public boolean equals(Object obj)
 /*     */   {
 /*  90 */     if (!(obj instanceof DistributedPOJOState)) return false;
 /*  91 */     DistributedPOJOState pojo = (DistributedPOJOState)obj;
 /*  92 */     return this..equals(pojo.guid);
 /*     */   }
 /*     */ 
 /*     */   public int hashCode()
 /*     */   {
 /*  97 */     return this..hashCode();
 /*     */   }
 /*     */ 
 /*     */   public InstanceAdvised buildObject(SynchronizationManager managerDistributedVersionManager versionManager)
 /*     */     throws Exception
 /*     */   {
 /* 104 */     .trace("building a " + this. + " of guid " + this.);
 /* 105 */     this. = versionManager;
 /* 106 */     this. = manager;
 /* 107 */     Class clazz = Thread.currentThread().getContextClassLoader().loadClass(this.);
 /* 108 */     Advised advised = (Advised)clazz.newInstance();
 /* 109 */     this. = new WeakReference(advised);
 /* 110 */     versionManager.addVersioning(thisadvised);
 /* 111 */     manager.putState(this.this);
 /* 112 */     manager.putObject(this.advised);
 /*     */ 
 /* 114 */     Iterator it = this..values().iterator();
 /* 115 */     while (it.hasNext())
 /*     */     {
 /* 117 */       DistributedFieldUpdate update = (DistributedFieldUpdate)it.next();
 /* 118 */       ClassAdvisor advisor = (ClassAdvisor)advised._getAdvisor();
 /* 119 */       .trace("build field " + advisor.getAdvisedFields()[update.getFieldIndex()].getName());
 /* 120 */       Object val = update.getNonDereferencedValue();
 /* 121 */       if ((val != null) && ((val instanceof VersionReference)))
 /*     */       {
/* 123 */         VersionReference ref = (VersionReference)val;
/* 124 */         .trace("VersionReference.guid: " + ref.getGUID() + " for field " + advisor.getAdvisedFields()[update.getFieldIndex()].getName());
/* 125 */         val = manager.getObject(ref.getGUID());
/* 126 */         if (val == null)
/*     */         {
/* 128 */           DistributedState fieldVal = manager.getState(ref.getGUID());
/* 129 */           val = fieldVal.buildObject(managerversionManager);
/*     */         }
/* 131 */         ref.set((InstanceAdvised)val);
/*     */       }
/*     */     }
/* 134 */     return advised;
/*     */   }
/*     */ 
/*     */   public HashMap getTxState()
/*     */   {
/* 139 */     return (HashMap)this..get();
/*     */   }
/*     */ 
/*     */   public HashMap getTxState(Transaction tx)
/*     */   {
/* 144 */     return (HashMap)this..get(tx);
/*     */   }
/*     */ 
/*     */   public Object fieldRead(Invocation invocationthrows Throwable
/*     */   {
/* 149 */     acquireReadLock();
/*     */     try
/*     */     {
/* 152 */       FieldReadInvocation fieldInvocation = (FieldReadInvocation)invocation;
/* 153 */       Integer index = new Integer(fieldInvocation.getIndex());
/* 154 */       HashMap map = getTxState();
/* 155 */       if (map == null)
/*     */       {
/* 157 */         map = this.;
/*     */       }
/* 159 */       DistributedFieldUpdate update = (DistributedFieldUpdate)map.get(index);
/* 160 */       Object val = update.getValue();
/* 161 */       Object localObject1 = val;
/*     */       return localObject1; } finally { releaseReadLock(); } throw localObject2;
/*     */   }
/*     */ 
/*     */   public Object fieldWrite(Invocation invocation)
/*     */     throws Throwable
/*     */   {
/* 171 */     FieldWriteInvocation fieldInvocation = (FieldWriteInvocation)invocation;
/* 172 */     Integer index = new Integer(fieldInvocation.getIndex());
/* 173 */     Object val = fieldInvocation.getValue();
/*     */ 
/* 175 */     if ((val instanceof Advised))
/*     */     {
/* 177 */       Advised advisedValue = (Advised)val;
/* 178 */       val = this..makeVersioned(advisedValue);
/*     */     }
/*     */ 
/* 181 */     Transaction tx = this..getTransaction();
/* 182 */     if (tx == null)
/*     */     {
/* 184 */       acquireWriteLock();
/*     */       try
/*     */       {
/* 188 */         DistributedFieldUpdate update = (DistributedFieldUpdate)this..get(index);
/* 189 */         long versionId = update.getVersionId() + 1L;
/* 190 */         update.setVersionId(versionId);
/* 191 */         update.setValue(val);
/* 192 */         HashMap fieldUpdates = new HashMap();
/* 193 */         fieldUpdates.put(indexupdate);
/* 194 */         this..noTxUpdate(new DistributedPOJOUpdate(this.fieldUpdates));
/* 195 */         Object localObject1 = null;
/*     */         return localObject1; } finally { releaseWriteLock();
/*     */       }
/*     */     }
/*     */ 
/* 203 */     acquireReadLock();
/*     */     try
/*     */     {
/* 206 */       HashMap map = (HashMap)this..get();
/* 207 */       if (map == null)
/*     */       {
/* 209 */         map = new HashMap();
/* 210 */         DistributedFieldUpdate update = (DistributedFieldUpdate)this..get(index);
/* 211 */         DistributedFieldUpdate newUpdate = new DistributedFieldUpdate(valupdate.getVersionId() + 1L, index.intValue());
/* 212 */         this..registerUpdate(txthis);
/* 213 */         map.put(indexnewUpdate);
/* 214 */         this..set(txmap);
/*     */       }
/*     */       else
/*     */       {
/* 218 */         DistributedFieldUpdate newUpdate = (DistributedFieldUpdate)map.get(index);
/* 219 */         if (newUpdate == null)
/*     */         {
/* 221 */           DistributedFieldUpdate update = (DistributedFieldUpdate)this..get(index);
/* 222 */           newUpdate = new DistributedFieldUpdate(valupdate.getVersionId() + 1L, index.intValue());
/* 223 */           map.put(indexnewUpdate);
/*     */         }
/*     */         else
/*     */         {
/* 227 */           newUpdate.setValue(val);
/*     */         }
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/* 233 */       releaseReadLock();
/*     */     }
/*     */ 
/* 236 */     return null;
/*     */   }
/*     */ 
/*     */   public DistributedUpdate createTxUpdate(Transaction tx)
/*     */   {
/* 241 */     HashMap state = getTxState(tx);
/* 242 */     return new DistributedPOJOUpdate(this.state);
/*     */   }
/*     */ 
/*     */   public void checkOptimisticLock(Transaction tx)
/*     */   {
/* 248 */     HashMap state = getTxState(tx);
/* 249 */     Iterator it = state.entrySet().iterator();
/* 250 */     while (it.hasNext())
/*     */     {
/* 252 */       Map.Entry entry = (Map.Entry)it.next();
/* 253 */       Integer index = (Integer)entry.getKey();
/* 254 */       DistributedFieldUpdate update = (DistributedFieldUpdate)entry.getValue();
/* 255 */       DistributedFieldUpdate orig = (DistributedFieldUpdate)this..get(index);
/* 256 */       if (update.getVersionId() <= orig.getVersionId())
/*     */       {
/* 258 */         Advised advised = null;
/* 259 */         if (this. != null)
/*     */         {
/* 261 */           advised = (Advised)this..get();
/*     */         }
/* 263 */         if (advised != null)
/*     */         {
/* 265 */           ClassAdvisor advisor = (ClassAdvisor)advised._getAdvisor();
/* 266 */           Field field = advisor.getAdvisedFields()[index.intValue()];
/* 267 */           throw new OptimisticLockFailure("optimistic lock failure for field " + field.getName() + " of class " + field.getDeclaringClass().getName());
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void mergeState(Transaction tx)
/*     */     throws Exception
/*     */   {
/* 276 */     HashMap newState = getTxState(tx);
/* 277 */     mergeState(newState);
/*     */   }
/*     */ 
/*     */   public void mergeState(DistributedUpdate updatethrows Exception
/*     */   {
/* 282 */     HashMap newState = ((DistributedPOJOUpdate)update).;
/* 283 */     mergeState(newState);
/*     */   }
/*     */ 
/*     */   public void mergeState(HashMap newState)
/*     */     throws Exception
/*     */   {
/* 289 */     Iterator it = newState.entrySet().iterator();
/* 290 */     while (it.hasNext())
/*     */     {
/* 292 */       Map.Entry entry = (Map.Entry)it.next();
/* 293 */       DistributedFieldUpdate update = (DistributedFieldUpdate)entry.getValue();
/* 294 */       if ((update.getNonDereferencedValue() instanceof VersionReference))
/*     */       {
/* 296 */         VersionReference ref = (VersionReference)update.getNonDereferencedValue();
/* 297 */         if (ref.get() == nullref.set((InstanceAdvised)this..getObject(ref.getGUID()));
/*     */       }
/*     */     }
/* 300 */     this..putAll(newState);
/*     */   }
/*     */ 
/*     */   public void writeExternal(ObjectOutput out)
/*     */     throws IOException
/*     */   {
/* 306 */     super.writeExternal(out);
/* 307 */     out.writeObject(this.);
/* 308 */     out.writeObject(this.);
/*     */   }
/*     */ 
/*     */   public void readExternal(ObjectInput in)
/*     */     throws IOExceptionClassNotFoundException
/*     */   {
/* 314 */     super.readExternal(in);
/* 315 */     this. = ((String)in.readObject());
/* 316 */     this. = ((HashMap)in.readObject());
/*     */     try
/*     */     {
/* 319 */       InitialContext ctx = new InitialContext();
/* 320 */       this. = ((TransactionManager)ctx.lookup("java:/TransactionManager"));
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 324 */       throw new RuntimeException(ex);
/*     */     }
/* 326 */     this. = new TransactionLocal();
/*     */   }
/*     */ }
/* Location:           /home/mnovotny/projects/EMBEDDED_JBOSS_BETA3_COMMUNITY/embedded/output/lib/embedded-jboss/lib/jboss-embedded-all.jar
 * Qualified Name:     org.jboss.aspects.versioned.DistributedPOJOState
 * JD-Core Version:    0.6.0
 */
New to GrepCode? Check out our FAQ X