geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdam...@apache.org
Subject cvs commit: incubator-geronimo/sandbox/messaging/src/test/org/apache/geronimo/messaging/reference ReferenceManagerImplTest.java MockReferenceableImpl.java
Date Mon, 24 May 2004 13:02:55 GMT
gdamour     2004/05/24 06:02:55

  Modified:    sandbox/messaging/src/java/org/apache/geronimo/messaging/reference
                        ReferenceableManager.java
                        ReferenceableManagerImpl.java
               sandbox/messaging/src/test/org/apache/geronimo/messaging/reference
                        ReferenceManagerImplTest.java
                        MockReferenceableImpl.java
  Log:
  When a Referenceable is registered twice, the returned
  ReferenceableInfo have the same (Referenceable) identifier.
  
  Revision  Changes    Path
  1.2       +8 -1      incubator-geronimo/sandbox/messaging/src/java/org/apache/geronimo/messaging/reference/ReferenceableManager.java
  
  Index: ReferenceableManager.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/sandbox/messaging/src/java/org/apache/geronimo/messaging/reference/ReferenceableManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ReferenceableManager.java	11 May 2004 12:06:42 -0000	1.1
  +++ ReferenceableManager.java	24 May 2004 13:02:55 -0000	1.2
  @@ -42,6 +42,13 @@
       
       /**
        * Registers a Referenceable.
  +     * <BR>
  +     * If the same Referenceable is registered twice, then the returned
  +     * ReferenceableInfos have the same identifier.
  +     * <BR>
  +     * If a Referenceable is equal to another Referenceable via the equals
  +     * contract, then the ReferenceableInfos have distinct Referenceable
  +     * identifiers. 
        * 
        * @param aReference Referenceable to be registered.
        * @return Referenceable meta-data.
  
  
  
  1.2       +34 -18    incubator-geronimo/sandbox/messaging/src/java/org/apache/geronimo/messaging/reference/ReferenceableManagerImpl.java
  
  Index: ReferenceableManagerImpl.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/sandbox/messaging/src/java/org/apache/geronimo/messaging/reference/ReferenceableManagerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ReferenceableManagerImpl.java	11 May 2004 12:06:42 -0000	1.1
  +++ ReferenceableManagerImpl.java	24 May 2004 13:02:55 -0000	1.2
  @@ -18,6 +18,7 @@
   package org.apache.geronimo.messaging.reference;
   
   import java.util.HashMap;
  +import java.util.IdentityHashMap;
   import java.util.Map;
   
   import org.apache.geronimo.messaging.AbstractEndPoint;
  @@ -41,13 +42,20 @@
       /**
        * Used to generate reference identifiers.
        */
  -    private static int seqID = 0;
  +    private int seqID = 0;
   
  +    private final Object mapsLock = new Object();
  +    
       /**
        * identifier to Referenceable map.
        */
  -    private final Map registered;
  -
  +    private final Map idToReferenceable;
  +    
  +    /**
  +     * Referenceable to ReferenceableInfo map.
  +     */
  +    private final IdentityHashMap referenceableToID;
  +    
       /**
        * Creates a manager mounted by the specified node and having the specified
        * identifier.
  @@ -58,7 +66,9 @@
       public ReferenceableManagerImpl(Node aNode, Object anID) {
           super(aNode, anID);
           
  -        registered = new HashMap();
  +        idToReferenceable = new HashMap();
  +        referenceableToID = new IdentityHashMap();
  +        
           // Adds the ReferenceReplacerResolver to the current chain.
           node.getReplacerResolver().append(new ReferenceReplacerResolver(this));
       }
  @@ -70,8 +80,8 @@
               // The referenceble is contained by this EndPoint. Returns it.
               Integer refID = new Integer(aReferenceInfo.getRefID());
               Object opaque;
  -            synchronized(registered) {
  -                opaque = registered.get(refID);
  +            synchronized(idToReferenceable) {
  +                opaque = idToReferenceable.get(refID);
               }
               if ( null == opaque ) {
                   throw new IllegalArgumentException("Referenceable {" +
  @@ -86,26 +96,32 @@
       }
   
       public ReferenceableInfo register(Referenceable aReference) {
  -        Integer refID;
  -        synchronized(registered) {
  -            refID = new Integer(++seqID);
  -            registered.put(refID, aReference);
  +        ReferenceableInfo info;
  +        synchronized(mapsLock) {
  +            // Checks if the Referenceable has already been registered.
  +            info = (ReferenceableInfo) referenceableToID.get(aReference);
  +            if ( null == info ) {
  +                Integer refID = new Integer(++seqID);
  +                info = new ReferenceableInfo(node.getNodeInfo(), id,
  +                    aReference.getClass().getInterfaces(), refID.intValue());
  +                idToReferenceable.put(refID, aReference);
  +                referenceableToID.put(aReference, info);
  +            }
           }
  -        return new ReferenceableInfo(node.getNodeInfo(), id,
  -            aReference.getClass().getInterfaces(),
  -            refID.intValue());
  +        return info;
       }
   
       public void unregister(ReferenceableInfo aReferenceInfo) {
  -        synchronized(registered) {
  -            registered.remove(new Integer(aReferenceInfo.getRefID()));
  +        synchronized(mapsLock) {
  +            idToReferenceable.remove(new Integer(aReferenceInfo.getRefID()));
  +            referenceableToID.remove(aReferenceInfo);
           }
       }
       
       public Object invoke(int anId, Request aRequest) throws Exception {
           Referenceable reference;
  -        synchronized(registered) {
  -            reference = (Referenceable) registered.get(new Integer(anId));
  +        synchronized(mapsLock) {
  +            reference = (Referenceable) idToReferenceable.get(new Integer(anId));
           }
           if ( null == reference ) {
               throw new IllegalArgumentException("Unknown Reference.");
  
  
  
  1.3       +20 -1     incubator-geronimo/sandbox/messaging/src/test/org/apache/geronimo/messaging/reference/ReferenceManagerImplTest.java
  
  Index: ReferenceManagerImplTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/sandbox/messaging/src/test/org/apache/geronimo/messaging/reference/ReferenceManagerImplTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ReferenceManagerImplTest.java	24 May 2004 12:03:34 -0000	1.2
  +++ ReferenceManagerImplTest.java	24 May 2004 13:02:55 -0000	1.3
  @@ -67,4 +67,23 @@
           Object opaque = manager.factoryProxy(info);
           assertTrue(reference == opaque);
       }
  +    
  +    public void testRegister() throws Exception {
  +        InetAddress address = InetAddress.getLocalHost();
  +        NodeInfo nodeInfo = new NodeInfo("Node", address, 8081);
  +        MockNode node = new MockNode();
  +        node.setNodeInfo(nodeInfo);
  +        
  +        ReferenceableManager manager = new ReferenceableManagerImpl(node, "A");
  +        MockReferenceableImpl reference1 = new MockReferenceableImpl();
  +
  +        ReferenceableInfo info1 = manager.register(reference1);
  +        ReferenceableInfo info2 = manager.register(reference1);
  +        assertTrue(info1.getRefID() == info2.getRefID());
  +        
  +        MockReferenceableImpl reference2 = new MockReferenceableImpl();
  +        ReferenceableInfo info3 = manager.register(reference2);
  +        assertTrue(info1.getRefID() != info3.getRefID());
  +    }
  +    
   }
  
  
  
  1.2       +11 -0     incubator-geronimo/sandbox/messaging/src/test/org/apache/geronimo/messaging/reference/MockReferenceableImpl.java
  
  Index: MockReferenceableImpl.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/sandbox/messaging/src/test/org/apache/geronimo/messaging/reference/MockReferenceableImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockReferenceableImpl.java	11 May 2004 12:06:43 -0000	1.1
  +++ MockReferenceableImpl.java	24 May 2004 13:02:55 -0000	1.2
  @@ -31,4 +31,15 @@
           return param;
       }
       
  +    public int hashCode() {
  +        return 1;
  +    }
  +    
  +    public boolean equals(Object obj) {
  +        if ( false == obj instanceof MockReferenceableImpl ) {
  +            return false;
  +        }
  +        return true;
  +    }
  +    
   }
  
  
  

Mime
View raw message