KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > ListEntityInterceptor


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.proxy.ejb;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.jboss.ejb.ListCacheKey;
33 import org.jboss.invocation.Invocation;
34
35 /**
36  * An EJB CMP entity bean proxy class holds info about the List that the entity belongs to,
37  * is used for reading ahead.
38  *
39  * @author <a HREF="mailto:on@ibis.odessa.ua">Oleg Nitz</a>
40  * @version $Revision: 37459 $
41  *
42  * @todo: (marcf) methinks that this behavior should be moved to a REAL interceptor (i.e not as extends)
43  */

44 public class ListEntityInterceptor
45       extends EntityInterceptor
46 {
47    /** Serial Version Identifier. @since 1.1 */
48    private static final long serialVersionUID = -5165912623246270565L;
49
50    protected static final Method JavaDoc GET_READ_AHEAD_VALUES;
51
52    // Attributes ----------------------------------------------------
53

54    /**
55     * A List that this entity belongs to (used for reading ahead).
56     */

57    private List JavaDoc list;
58
59    /**
60     * A hash map of read ahead values, maps Methods to values.
61     */

62    private transient HashMap JavaDoc readAheadValues;
63
64    // Static --------------------------------------------------------
65

66    static
67    {
68       try
69       {
70          final Class JavaDoc[] empty = {};
71
72          GET_READ_AHEAD_VALUES = ReadAheadBuffer.class.getMethod("getReadAheadValues", empty);
73       }
74       catch (Exception JavaDoc e)
75       {
76          e.printStackTrace();
77          throw new ExceptionInInitializerError JavaDoc(e);
78       }
79    }
80
81    // Constructors --------------------------------------------------
82

83    /**
84     * No-argument constructor for externalization.
85     */

86    public ListEntityInterceptor()
87    {
88    }
89
90    /**
91     * Construct a <tt>ListEntityProxy</tt>.
92     *
93     * @param name The JNDI name of the container that we proxy for.
94     * @param container The remote interface of the invoker for which
95     * this is a proxy for.
96     * @param id The primary key of the entity.
97     * @param optimize True if the proxy will attempt to optimize
98     * VM-local calls.
99     * @param list A List that this entity belongs to (used for reading ahead).
100     * @param listId The list id.
101     * @param index The index of this entity in the list.
102     *
103     * @throws NullPointerException Id may not be null.
104     */

105
106    public ListEntityInterceptor(List JavaDoc list)
107    {
108       this.list = list;
109    }
110
111    // Public --------------------------------------------------------
112

113    public Map JavaDoc getReadAheadValues()
114    {
115       if (readAheadValues == null)
116       {
117          readAheadValues = new HashMap JavaDoc();
118       }
119       return readAheadValues;
120    }
121
122
123    /**
124     * InvocationHandler implementation.
125     *
126     * @param proxy The proxy object.
127     * @param m The method being invoked.
128     * @param args The arguments for the method.
129     *
130     * @throws Throwable Any exception or error thrown while processing.
131     */

132    public Object JavaDoc invoke(Invocation invocation)
133          throws Throwable JavaDoc
134    {
135       Object JavaDoc result;
136       ReadAheadResult raResult;
137       Object JavaDoc[] aheadResult;
138       int from;
139       int to;
140       ReadAheadBuffer buf;
141
142       Method JavaDoc m = invocation.getMethod();
143
144       if (m.equals(GET_READ_AHEAD_VALUES))
145       {
146          return getReadAheadValues();
147       }
148
149       // have we read ahead the result?
150
if (readAheadValues != null)
151       {
152          result = readAheadValues.get(m);
153          if (readAheadValues.containsKey(m))
154          {
155             return readAheadValues.remove(m);
156          }
157       }
158
159       result = super.invoke(invocation);
160
161       // marcf : I think all these will map nicely to the in/out of real interceptor, i.e. do not "extend"
162

163       if (result instanceof ReadAheadResult)
164       {
165          raResult = (ReadAheadResult) result;
166          aheadResult = raResult.getAheadResult();
167          ListCacheKey key = (ListCacheKey) invocation.getInvocationContext().getCacheId();
168          from = key.getIndex() + 1;
169          to = Math.min(from + aheadResult.length, list.size());
170          for (int i = from; i < to; i++)
171          {
172             buf = (ReadAheadBuffer) list.get(i);
173             buf.getReadAheadValues().put(m, aheadResult[i - from]);
174          }
175          return raResult.getMainResult();
176       }
177       else
178       {
179          return result;
180       }
181    }
182
183    // Package protected ---------------------------------------------
184

185    // Protected -----------------------------------------------------
186

187    /**
188     * Externalization support.
189     *
190     * @param out
191     *
192     * @throws IOException
193     */

194    public void writeExternal(final ObjectOutput JavaDoc out)
195          throws IOException JavaDoc
196    {
197       super.writeExternal(out);
198       out.writeObject(list);
199    }
200
201    /**
202     * Externalization support.
203     *
204     * @param in
205     *
206     * @throws IOException
207     * @throws ClassNotFoundException
208     */

209    public void readExternal(final ObjectInput JavaDoc in)
210          throws IOException JavaDoc, ClassNotFoundException JavaDoc
211    {
212       super.readExternal(in);
213       list = (List JavaDoc) in.readObject();
214    }
215
216
217    // Private -------------------------------------------------------
218

219    // Inner classes -------------------------------------------------
220
}
221
222
Popular Tags