KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > Interceptor


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.cache.interceptors;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.jboss.cache.CacheSPI;
27 import org.jboss.cache.config.Configuration;
28 import org.jboss.cache.marshall.MethodCall;
29 import org.jboss.cache.marshall.MethodDeclarations;
30
31 import javax.transaction.Status JavaDoc;
32 import javax.transaction.SystemException JavaDoc;
33 import javax.transaction.Transaction JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Class representing an interceptor.
39  * <em>Note that this will be replaced by {@link org.jboss.aop.advice.Interceptor} in one of the next releases</em>
40  *
41  * @author Bela Ban
42  * @version $Id: Interceptor.java,v 1.26 2006/12/30 19:48:46 msurtani Exp $
43  */

44 public abstract class Interceptor implements InterceptorMBean
45 {
46    protected Interceptor next = null, last = null;
47    protected CacheSPI cache;
48    protected Log log = null;
49    protected Configuration configuration;
50    private boolean statsEnabled = false;
51
52    public Interceptor()
53    {
54       log = LogFactory.getLog(getClass());
55    }
56
57    public void setNext(Interceptor i)
58    {
59       next = i;
60    }
61
62    public Interceptor getNext()
63    {
64       return next;
65    }
66
67    public void setCache(CacheSPI cache)
68    {
69       this.cache = cache;
70       this.configuration = cache.getConfiguration();
71    }
72
73    public Object JavaDoc invoke(MethodCall m) throws Throwable JavaDoc
74    {
75       return next.invoke(m);
76    }
77
78    public boolean getStatisticsEnabled()
79    {
80       return statsEnabled;
81    }
82
83    public void setStatisticsEnabled(boolean enabled)
84    {
85       statsEnabled = enabled;
86    }
87
88    public Interceptor getLast()
89    {
90       return last;
91    }
92
93    public void setLast(Interceptor last)
94    {
95       this.last = last;
96    }
97
98    /**
99     * This implementation returns an empty Map. If individual Interceptors wish to expose statistics, they can override this
100     * method.
101     */

102    public Map JavaDoc<String JavaDoc, Object JavaDoc> dumpStatistics()
103    {
104       return Collections.emptyMap();
105    }
106
107    /**
108     * This implementation is a no-op. If individual Interceptors wish to expose statistics, they can override this
109     * method.
110     */

111    public void resetStatistics()
112    {
113    }
114
115    /**
116     * Returns true if transaction is ACTIVE, false otherwise
117     */

118    protected boolean isActive(Transaction JavaDoc tx)
119    {
120       if (tx == null) return false;
121       int status = -1;
122       try
123       {
124          status = tx.getStatus();
125          return status == Status.STATUS_ACTIVE;
126       }
127       catch (SystemException JavaDoc e)
128       {
129          log.error("failed getting transaction status", e);
130          return false;
131       }
132    }
133
134    /**
135     * Returns true if transaction is PREPARING, false otherwise
136     */

137    protected boolean isPreparing(Transaction JavaDoc tx)
138    {
139       if (tx == null) return false;
140       int status = -1;
141       try
142       {
143          status = tx.getStatus();
144          return status == Status.STATUS_PREPARING;
145       }
146       catch (SystemException JavaDoc e)
147       {
148          log.error("failed getting transaction status", e);
149          return false;
150       }
151    }
152
153    /**
154     * Return s true of tx's status is ACTIVE or PREPARING
155     *
156     * @param tx
157     * @return true if the tx is active or preparing
158     */

159    protected boolean isValid(Transaction JavaDoc tx)
160    {
161       return isActive(tx) || isPreparing(tx);
162    }
163
164    /**
165     * This only works for prepare() and optimisticPrepare() method calls.
166     *
167     * @param m
168     */

169    protected boolean isOnePhaseCommitPrepareMehod(MethodCall m)
170    {
171       switch (m.getMethodId())
172       {
173          case MethodDeclarations.prepareMethod_id:
174             return (Boolean JavaDoc) m.getArgs()[3];
175          case MethodDeclarations.optimisticPrepareMethod_id:
176             return (Boolean JavaDoc) m.getArgs()[4];
177          default:
178             return false;
179       }
180    }
181
182    public String JavaDoc toString()
183    {
184       return getClass()
185               + "{next: "
186               + (getNext() == null ? null : getNext().getClass())
187               + "; last: "
188               + (getLast() == null ? null : getLast().getClass())
189               + "}";
190    }
191 }
192
Popular Tags