KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > CacheKey


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.rmi.MarshalledObject JavaDoc;
30
31 import org.jboss.logging.Logger;
32
33 /**
34  * CacheKey is an encapsulation of both the PrimaryKey and a
35  * cache specific key.
36  *
37  * <p>This implementation is a safe implementation in the sense that it
38  * doesn't rely on the user supplied hashcode and equals. It is also
39  * fast since the hashCode operation is pre-calculated.
40  *
41  * @see org.jboss.ejb.plugins.NoPassivationInstanceCache.java
42  * @see org.jboss.ejb.plugins.EntityInstanceCache
43  * @see org.jboss.ejb.plugins.EntityProxy
44  *
45  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
46  * @author <a HREF="bill@burkecentral.com">Bill Burke</a>
47  * @author <a HREF="Scott.Stark@jboss.org">Scott Stark</a>
48  * @version $Revision: 37459 $
49  */

50 public class CacheKey
51    implements Externalizable JavaDoc
52 {
53    // Constants -----------------------------------------------------
54
static final long serialVersionUID = -7108821554259950778L;
55     
56    // Attributes ----------------------------------------------------
57

58    /**
59     * The database primaryKey.
60     *
61     * This primaryKey is used by:
62     *
63     * org.jboss.ejb.plugins.EntityInstanceCache.setKey() - to set the EntityEnterpriseContext id
64     * org.jboss.ejb.plugins.jrmp.interfaces.EntityProxy.invoke():
65     * - implementing Entity.toString() --> cacheKey.getId().toString()
66     * - implementing Entity.hashCode() --> cacheKey.getId().hashCode()
67     * - etc...
68     * org.jboss.ejb.plugins.local.BaseLocalProxyFactory.EntityProxy.getId()
69     */

70    protected Object JavaDoc id;
71    
72    public Object JavaDoc getId()
73    {
74       return id;
75    }
76      
77    /** The Marshalled Object representing the key */
78    protected MarshalledObject JavaDoc mo;
79     
80    /** The Marshalled Object's hashcode */
81    protected int hashCode;
82     
83    // Static --------------------------------------------------------
84

85    // Public --------------------------------------------------------
86

87    public CacheKey()
88    {
89       // For externalization only
90
}
91
92    public CacheKey(Object JavaDoc id)
93    {
94       // why does this throw an error and not an IllegalArgumentException ?
95
if (id == null) throw new Error JavaDoc("id may not be null");
96          
97       this.id = id;
98       try
99       {
100          /* See if the key directly implements equals and hashCode. The
101           *getDeclaredMethod method only returns method declared in the argument
102           *class, not its superclasses.
103          */

104          try
105          {
106             Class JavaDoc[] equalsArgs = {Object JavaDoc.class};
107             Method JavaDoc equals = id.getClass().getDeclaredMethod("equals", equalsArgs);
108             Class JavaDoc[] hashCodeArgs = {};
109             Method JavaDoc hash = id.getClass().getDeclaredMethod("hashCode", hashCodeArgs);
110             // Both equals and hashCode are defined, use the id methods
111
hashCode = id.hashCode();
112          }
113          catch(NoSuchMethodException JavaDoc ex)
114          {
115             // Rely on the MarshalledObject for equals and hashCode
116
mo = new MarshalledObject JavaDoc(id);
117             // Precompute the hashCode (speed)
118
hashCode = mo.hashCode();
119          }
120       }
121       catch (Exception JavaDoc e)
122       {
123          Logger log = Logger.getLogger(getClass());
124          log.error("failed to initialize, id="+id, e);
125       }
126    }
127
128    // Z implementation ----------------------------------------------
129

130    // Package protected ---------------------------------------------
131

132    // Protected -----------------------------------------------------
133

134    // Private -------------------------------------------------------
135

136    public void writeExternal(ObjectOutput JavaDoc out)
137       throws IOException JavaDoc
138    {
139       out.writeObject(id);
140       out.writeObject(mo);
141       out.writeInt(hashCode);
142    }
143    
144    public void readExternal(ObjectInput JavaDoc in)
145       throws IOException JavaDoc, ClassNotFoundException JavaDoc
146    {
147       id = in.readObject();
148       mo = (MarshalledObject JavaDoc) in.readObject();
149       hashCode = in.readInt();
150    }
151
152    // HashCode and Equals over write --------------------------------
153

154    /**
155     * these should be overwritten by extending Cache key
156     * since they define what the cache does in the first place
157     */

158    public int hashCode()
159    {
160       // we default to the pK id
161
return hashCode;
162    }
163     
164    /** This method uses the id implementation of equals if the mo is
165     *null since this indicates that the id class did implement equals.
166     *If mo is not null, then the MarshalledObject equals is used to
167     *compare keys based on their serialized form. Relying on the
168     *serialized form does not always work.
169     */

170    public boolean equals(Object JavaDoc object)
171    {
172       boolean equals = false;
173       if (object instanceof CacheKey)
174       {
175          CacheKey ckey = (CacheKey) object;
176          Object JavaDoc key = ckey.id;
177          // If mo is null, the id class implements equals
178
if( mo == null )
179             equals = id.equals(key);
180          else
181             equals = mo.equals(ckey.mo);
182       }
183       return equals;
184    }
185
186    public String JavaDoc toString()
187    {
188       return id.toString();
189    }
190     
191    // Inner classes -------------------------------------------------
192
}
193
Popular Tags