KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > pm > none > PersistenceManager


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.mq.pm.none;
23 import javax.jms.JMSException JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25
26 import org.jboss.mq.SpyDestination;
27 import org.jboss.mq.SpyMessage;
28 import org.jboss.mq.pm.CacheStore;
29 import org.jboss.mq.pm.Tx;
30 import org.jboss.mq.pm.TxManager;
31 import org.jboss.mq.server.JMSDestination;
32 import org.jboss.mq.server.MessageCache;
33 import org.jboss.mq.server.MessageReference;
34 import org.jboss.system.ServiceMBeanSupport;
35
36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
37 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
38
39 /**
40  * A persistence manager and cache store that does not persistence.
41  *
42  * It can do persistence by delegating to a real persistence manager,
43  * depending upon destination configuration.
44  *
45  * @jmx:mbean extends="org.jboss.system.ServiceMBean, org.jboss.mq.pm.PersistenceManagerMBean, org.jboss.mq.pm.CacheStoreMBean"
46  *
47  * @author Adrian Brock (adrian@jboss.org)
48  *
49  * @version $Revision: 37459 $
50  */

51 public class PersistenceManager
52    extends ServiceMBeanSupport
53    implements PersistenceManagerMBean, org.jboss.mq.pm.PersistenceManager, CacheStore
54 {
55    // Constants --------------------------------------------------------------------
56

57    // Attributes -------------------------------------------------------------------
58

59    /** The next transaction id */
60    SynchronizedLong nextTransactionid = new SynchronizedLong(0l);
61    
62    /** The oject name of the delegate persistence manger */
63    ObjectName JavaDoc delegateName;
64    
65    /** The delegate persistence manager */
66    org.jboss.mq.pm.PersistenceManager delegate;
67    
68    /** The jbossmq transaction manager */
69    TxManager txManager;
70    
71    /** The cache */
72    ConcurrentHashMap cache = new ConcurrentHashMap();
73    
74    // Constructors -----------------------------------------------------------------
75

76    // Public -----------------------------------------------------------------------
77

78    /**
79     * Retrieve the delegate persistence manager
80     *
81     * @jmx:managed-attribute
82     * @return the object name of the delegate persistence manager
83     */

84    public ObjectName JavaDoc getDelegatePM()
85    {
86       return delegateName;
87    }
88
89    /**
90     * Set the delegate persistence manager
91     *
92     * @jmx:managed-attribute
93     * @param delegatePM the delegate persistence manager
94     */

95    public void setDelegatePM(ObjectName JavaDoc delegateName)
96    {
97       this.delegateName = delegateName;
98    }
99
100    // PersistenceManager implementation --------------------------------------------
101

102    public void add(MessageReference message, Tx txId) throws JMSException JavaDoc
103    {
104       if (delegate != null && message.inMemory() == false)
105          delegate.add(message, txId);
106    }
107    
108    public void closeQueue(JMSDestination jmsDest, SpyDestination dest) throws JMSException JavaDoc
109    {
110       if (delegate != null)
111          delegate.closeQueue(jmsDest, dest);
112    }
113    
114    public void commitPersistentTx(Tx txId) throws JMSException JavaDoc
115    {
116       if (delegate != null)
117          delegate.commitPersistentTx(txId);
118    }
119    
120    public Tx createPersistentTx() throws JMSException JavaDoc
121    {
122       if (delegate != null)
123          return delegate.createPersistentTx();
124
125       Tx tx = new Tx(nextTransactionid.increment());
126       return tx;
127    }
128    
129    public MessageCache getMessageCacheInstance()
130    {
131       if (delegate != null)
132          return delegate.getMessageCacheInstance();
133          
134       throw new UnsupportedOperationException JavaDoc("This is now set on the destination manager");
135    }
136    
137    public TxManager getTxManager()
138    {
139       return txManager;
140    }
141    
142    public void remove(MessageReference message, Tx txId) throws JMSException JavaDoc
143    {
144       if (delegate != null && message.inMemory() == false)
145          delegate.remove(message, txId);
146    }
147    
148    public void restoreQueue(JMSDestination jmsDest, SpyDestination dest) throws JMSException JavaDoc
149    {
150       if (delegate != null)
151          delegate.restoreQueue(jmsDest, dest);
152    }
153    
154    public void rollbackPersistentTx(Tx txId) throws JMSException JavaDoc
155    {
156       if (delegate != null)
157          delegate.rollbackPersistentTx(txId);
158    }
159    
160    public void update(MessageReference message, Tx txId) throws JMSException JavaDoc
161    {
162       if (delegate != null && message.inMemory() == false)
163          delegate.update(message, txId);
164    }
165    
166    // PersistenceManagerMBean implementation ---------------------------------------
167

168    public Object JavaDoc getInstance()
169    {
170       return this;
171    }
172
173    /**
174     * Unsupported operation
175     */

176    public ObjectName JavaDoc getMessageCache()
177    {
178       if (delegateName != null)
179       {
180          try
181          {
182             return (ObjectName JavaDoc) server.getAttribute(delegateName, "MessageCache");
183          }
184          catch (Exception JavaDoc e)
185          {
186             log.trace("Unable to retrieve message cache from delegate", e);
187          }
188       }
189       throw new UnsupportedOperationException JavaDoc("This is now set on the destination manager");
190    }
191
192    /**
193     * Unsupported operation
194     */

195    public void setMessageCache(ObjectName JavaDoc messageCache)
196    {
197       throw new UnsupportedOperationException JavaDoc("This is now set on the destination manager");
198    }
199
200    // CacheStore implementation ----------------------------------------------------
201

202    public SpyMessage loadFromStorage(MessageReference mh) throws JMSException JavaDoc
203    {
204       if (delegate == null || mh.inMemory())
205          return (SpyMessage) cache.get(mh);
206       else
207          return ((CacheStore) delegate).loadFromStorage(mh);
208    }
209    
210    public void removeFromStorage(MessageReference mh) throws JMSException JavaDoc
211    {
212       if (delegate == null || mh.inMemory())
213       {
214          cache.remove(mh);
215          mh.setStored(MessageReference.NOT_STORED);
216       }
217       else
218          ((CacheStore) delegate).removeFromStorage(mh);
219
220    }
221    
222    public void saveToStorage(MessageReference mh, SpyMessage message) throws JMSException JavaDoc
223    {
224       if (delegate == null || mh.inMemory())
225       {
226          cache.put(mh, message);
227          mh.setStored(MessageReference.STORED);
228       }
229       else
230          ((CacheStore) delegate).saveToStorage(mh, message);
231    }
232    
233    // ServerMBeanSupport overrides -------------------------------------------------
234

235    protected void startService() throws Exception JavaDoc
236    {
237       //Is there a delegate?
238
if (delegateName != null)
239       {
240          delegate = (org.jboss.mq.pm.PersistenceManager) getServer().getAttribute(delegateName, "Instance");
241          if ((delegate instanceof CacheStore) == false)
242             throw new UnsupportedOperationException JavaDoc("The delegate persistence manager must also be a CacheStore");
243          txManager = delegate.getTxManager();
244       }
245       else
246       {
247          txManager = new TxManager(this);
248       }
249    }
250    
251    // Protected --------------------------------------------------------------------
252

253    // Inner Classes ----------------------------------------------------------------
254
}
255
Popular Tags