[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/observable ...

Ben Wang bwang at jboss.com
Sat Jan 13 10:55:05 EST 2007


  User: bwang   
  Date: 07/01/13 10:55:05

  Added:       src/org/jboss/cache/pojo/observable     Subject.java
                        SubjectImpl.java Observer.java
                        SubjectInterceptor.java
  Log:
  JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
  
  Revision  Changes    Path
  1.1      date: 2007/01/13 15:55:05;  author: bwang;  state: Exp;JBossCache/src/org/jboss/cache/pojo/observable/Subject.java
  
  Index: Subject.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.cache.pojo.observable;
  
  import java.lang.reflect.Field;
  
  /**
   * A Subject. This interface is exposed on the advised object.
   *
   * @author Ben Wang
   * @version $Revision: 1.1 $
   */
  public interface Subject
  {
     /**
      * Add an observer
      *
      * @param observer the observer
      */
     void addObserver(Observer observer);
  
     /**
      * Remove an observer
      *
      * @param observer the observer
      */
     void removeObserver(Observer observer);
  
     /**
      * Notify all observers
      */
     void notifyObservers(Field modifiedField, boolean pre);
  }
  
  
  
  1.1      date: 2007/01/13 15:55:05;  author: bwang;  state: Exp;JBossCache/src/org/jboss/cache/pojo/observable/SubjectImpl.java
  
  Index: SubjectImpl.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.cache.pojo.observable;
  
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  import java.lang.reflect.Field;
  
  /**
   * The subject implementation.
   *
   * @author Ben Wang
   * @version $Revision: 1.1 $
   */
  public class SubjectImpl implements Subject
  {
     /** The observers */
     private Set observers = Collections.synchronizedSet(new HashSet());
  
     /** The subject */
     private Object subject;
  
     public SubjectImpl(Object subject)
     {
        this.subject = subject;
     }
  
     public void addObserver(Observer observer)
     {
        if(observers.contains(observer))
           return;  // return right away since we have that already.
        observers.add(observer);
     }
  
     public void removeObserver(Observer observer)
     {
        observers.remove(observer);
     }
  
     public void notifyObservers(Field modifiedField, boolean pre)
     {
        Subject obj = (Subject) subject;
  
        synchronized (observers)
        {
           for (Iterator i = observers.iterator(); i.hasNext();)
           {
              Observer observer = (Observer) i.next();
              observer.fireChange(obj, modifiedField, pre);
           }
        }
     }
  }
  
  
  
  1.1      date: 2007/01/13 15:55:05;  author: bwang;  state: Exp;JBossCache/src/org/jboss/cache/pojo/observable/Observer.java
  
  Index: Observer.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.cache.pojo.observable;
  
  import java.lang.reflect.Field;
  
  /**
   * An Observer. Implement this interface to register
   * for subject changes.
   *
   * A variant of
   * {@link org.jboss.aspects.patterns.observable}. We need to
   * customize it for including field information and pre-
   * and post-notification.
   *
   * @author Ben Wang
   * @version $Revision: 1.1 $
   */
  public interface Observer
  {
     /**
      * Notification that a subject has changed
      *
      * @param subject the subject
      * @param modifiedField the field that gets modified
      * @param pre True if before modification
      */
     void fireChange(Subject subject, Field modifiedField, boolean pre);
  }
  
  
  
  1.1      date: 2007/01/13 15:55:05;  author: bwang;  state: Exp;JBossCache/src/org/jboss/cache/pojo/observable/SubjectInterceptor.java
  
  Index: SubjectInterceptor.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.cache.pojo.observable;
  
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.aop.joinpoint.FieldInvocation;
  import org.jboss.aop.joinpoint.Invocation;
  import org.jboss.aop.joinpoint.MethodInvocation;
  
  /**
   * A Subject Interceptor. Traps changes to the object
   * and fires change notifications.
   *
   * @author Ben Wang
   * @version $Revision: 1.1 $
   */
  public class SubjectInterceptor implements Interceptor
  {
     static boolean PRE = true;
  
     public String getName()
     {
        return "Observerable";
     }
  
     public Object invoke(Invocation invocation) throws Throwable
     {
        // Kind of ad hoc now. MethodInvocation should not invoke this.
        if(invocation instanceof MethodInvocation)
           return invocation.invokeNext();
  
        FieldInvocation fi = (FieldInvocation) invocation;
        Subject observable = (Subject) fi.getTargetObject();
  
        observable.notifyObservers(fi.getField(), PRE);
        Object result = invocation.invokeNext();
        observable.notifyObservers(fi.getField(), !PRE);
  
        return result;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list