001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cache.bean;
023:
024: import org.jboss.cache.TreeCacheMBean;
025: import org.jboss.mx.util.MBeanProxyExt;
026: import org.jboss.mx.util.MBeanServerLocator;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.management.Attribute;
032: import javax.management.MBeanServer;
033: import javax.management.ObjectName;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.Vector;
037:
038: /**
039: * EJB proxy to the TreeCache MBean service. Used to be able to transport
040: * user transactions from a test client to a TreeCache. Note that TreeCache MBean
041: * is deployed during a test case run and is persistent throughout that run only.
042: *
043: * @author Ben Wang
044: * @version $Revision: 57211 $
045: * @ejb.bean type="Stateful"
046: * name="test/TreeCacheMBeanTester"
047: * jndi-name="ejb/test/TreeCacheMBeanTester"
048: * view-type="remote"
049: * @ejb.transaction type="Supports"
050: */
051: public class TreeCacheMBeanTesterBean implements SessionBean {
052: // Use a different service name so that it won't collide with the regular name.
053: static final String OBJECT_NAME = "jboss.cache:service=testTreeCache";
054: MBeanServer server;
055: ObjectName cacheService;
056: TreeCacheMBean cache = null;
057:
058: /**
059: * @throws CreateException
060: * @ejb.create-method
061: */
062: public void ejbCreate() throws CreateException {
063: log("Creating TreeCache ejb proxy");
064: init();
065: }
066:
067: /**
068: * @param name MBean object name.
069: * @throws CreateException
070: * @ejb.create-method
071: */
072: public void ejbCreate(String name) throws CreateException {
073: log("I'm being created");
074: init(name);
075: }
076:
077: private void init() throws CreateException {
078: init(OBJECT_NAME);
079: }
080:
081: private void init(String name) throws CreateException {
082: try {
083: cacheService = new ObjectName(name);
084: server = MBeanServerLocator.locate();
085: cache = (TreeCacheMBean) MBeanProxyExt.create(
086: TreeCacheMBean.class, cacheService, server);
087: } catch (Exception ex) {
088: throw new CreateException(ex.toString());
089: }
090: }
091:
092: public void ejbActivate() {
093: }
094:
095: public void ejbPassivate() {
096: }
097:
098: public void ejbRemove() {
099: log("I'm being removed");
100: }
101:
102: public void setSessionContext(SessionContext ctx) {
103: }
104:
105: /**
106: * @return
107: * @ejb.interface-method
108: */
109: public Vector getMembers() throws Exception {
110: return cache.getMembers();
111: }
112:
113: /**
114: * @return
115: * @ejb.interface-method
116: */
117: public int getCacheMode() throws Exception {
118: return ((Integer) server
119: .getAttribute(cacheService, "CacheMode")).intValue();
120: }
121:
122: /**
123: * @param mode
124: * @ejb.interface-method
125: */
126: public void setCacheMode(int mode) throws Exception {
127: server.setAttribute(cacheService, new Attribute("CacheMode",
128: new Integer(mode)));
129: }
130:
131: /**
132: * @return
133: * @ejb.interface-method
134: */
135: public boolean getLocking() throws Exception {
136: return ((Boolean) server.getAttribute(cacheService, "Locking"))
137: .booleanValue();
138: }
139:
140: /**
141: * @param flag
142: * @ejb.interface-method
143: */
144: public void setLocking(boolean flag) throws Exception {
145: server.setAttribute(cacheService, new Attribute("Locking",
146: new Boolean(flag)));
147: }
148:
149: /**
150: * @return
151: * @ejb.interface-method
152: */
153: public int getLockingLevel() throws Exception {
154: return ((Integer) server.getAttribute(cacheService,
155: "LockingLevel")).intValue();
156: }
157:
158: /**
159: * @param level
160: * @ejb.interface-method
161: */
162: public void setLocking(int level) throws Exception {
163: server.setAttribute(cacheService, new Attribute("LockingLevel",
164: new Integer(level)));
165: }
166:
167: /**
168: * @param fqn
169: * @return
170: * @ejb.interface-method
171: */
172: public Set getKeys(String fqn) throws Exception {
173: return (Set) server.invoke(cacheService, "getKeys",
174: new Object[] { fqn }, new String[] { String.class
175: .getName() });
176: }
177:
178: /**
179: * @param fqn
180: * @param key
181: * @return
182: * @ejb.interface-method
183: */
184: public Object get(String fqn, String key) throws Exception {
185: return server.invoke(cacheService, "get", new Object[] { fqn,
186: key }, new String[] { String.class.getName(),
187: Object.class.getName() });
188: }
189:
190: /**
191: * @param fqn
192: * @return
193: * @ejb.interface-method
194: */
195: public boolean exists(String fqn) throws Exception {
196: return ((Boolean) server.invoke(cacheService, "exists",
197: new Object[] { fqn }, new String[] { String.class
198: .getName() })).booleanValue();
199: }
200:
201: /**
202: * @param fqn
203: * @param data
204: * @throws Exception
205: * @ejb.interface-method
206: */
207: public void put(String fqn, Map data) throws Exception {
208: server.invoke(cacheService, "put", new Object[] { fqn, data },
209: new String[] { String.class.getName(),
210: Map.class.getName() });
211: }
212:
213: /**
214: * @param fqn
215: * @param key
216: * @param value
217: * @throws Exception
218: * @ejb.interface-method
219: */
220: public void put(String fqn, String key, Object value)
221: throws Exception {
222: Object[] args = { fqn, key, value };
223: String[] sig = { String.class.getName(),
224: Object.class.getName(), Object.class.getName() };
225:
226: server.invoke(cacheService, "put", args, sig);
227: }
228:
229: /**
230: * @param fqn
231: * @throws Exception
232: * @ejb.interface-method
233: */
234: public void remove(String fqn) throws Exception {
235: Object[] args = { fqn };
236: String[] sig = { String.class.getName() };
237:
238: server.invoke(cacheService, "remove", args, sig);
239: }
240:
241: /**
242: * @param fqn
243: * @param key
244: * @return
245: * @throws Exception
246: * @ejb.interface-method
247: */
248: public Object remove(String fqn, String key) throws Exception {
249: return server.invoke(cacheService, "remove", new Object[] {
250: fqn, key }, new String[] { String.class.getName(),
251: String.class.getName() });
252: }
253:
254: /**
255: * @param fqn
256: * @ejb.interface-method
257: */
258: public void releaseAllLocks(String fqn) throws Exception {
259: server.invoke(cacheService, "releaseAllLocks",
260: new Object[] { fqn }, new String[] { String.class
261: .getName() });
262: }
263:
264: /**
265: * @param fqn
266: * @return
267: * @ejb.interface-method
268: */
269: public String print(String fqn) throws Exception {
270: return (String) server.invoke(cacheService, "print",
271: new Object[] { fqn }, new String[] { String.class
272: .getName() });
273: }
274:
275: /**
276: * @param fqn
277: * @return
278: * @ejb.interface-method
279: */
280: public Set getChildrenNames(String fqn) throws Exception {
281: return (Set) server.invoke(cacheService, "getChildrenNames",
282: new Object[] { fqn }, new String[] { String.class
283: .getName() });
284: }
285:
286: /**
287: * @return
288: * @ejb.interface-method
289: */
290: public String printDetails() throws Exception {
291: return (String) server.invoke(cacheService, "printDetails",
292: null, null);
293: }
294:
295: /**
296: * @return
297: * @ejb.interface-method
298: */
299: public String printLockInfo() throws Exception {
300: return (String) server.invoke(cacheService, "printLockInfo",
301: null, null);
302: }
303:
304: private void log(String msg) {
305: System.out.println("-- [" + Thread.currentThread().getName()
306: + "]: " + msg);
307: }
308:
309: }
|