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.mq.il;
023:
024: import java.util.Properties;
025:
026: import javax.jms.IllegalStateException;
027: import javax.management.ObjectName;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: import org.jboss.mq.GenericConnectionFactory;
033: import org.jboss.mq.SpyConnectionFactory;
034: import org.jboss.mq.SpyXAConnectionFactory;
035: import org.jboss.system.ServiceMBeanSupport;
036:
037: /**
038: * This abstract class handles life cycle managment of the ServeIL. Should be
039: * extended to provide a full implementation.
040: *
041: * @author Hiram Chirino (Cojonudo14@hotmail.com)
042: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks </a>
043: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
044: * @version $Revision: 57198 $
045: *
046: * @jmx:mbean extends="org.jboss.system.ServiceMBean"
047: */
048: public abstract class ServerILJMXService extends ServiceMBeanSupport
049: implements ServerILJMXServiceMBean {
050:
051: private ObjectName jbossMQService;
052:
053: protected Invoker jmsServer;
054:
055: protected String connectionFactoryJNDIRef;
056:
057: protected String xaConnectionFactoryJNDIRef;
058:
059: protected long pingPeriod = 60000L;
060:
061: /** The client id */
062: protected String clientID;
063:
064: /**
065: * Get the value of JBossMQService.
066: *
067: * @return value of JBossMQService.
068: *
069: * @jmx:managed-attribute
070: */
071: public ObjectName getJBossMQService() {
072: return jbossMQService;
073: }
074:
075: /**
076: * Set the value of JBossMQService.
077: *
078: * @param v Value to assign to JBossMQService.
079: *
080: * @jmx:managed-attribute
081: */
082: public void setInvoker(ObjectName jbossMQService) {
083: this .jbossMQService = jbossMQService;
084: }
085:
086: public void startService() throws Exception {
087: jmsServer = (Invoker) getServer().getAttribute(jbossMQService,
088: "Invoker");
089: if (jmsServer == null) {
090: throw new IllegalStateException(
091: "Cannot find JBossMQService!");
092: } // end of if ()
093: }
094:
095: public void stopService() throws Exception {
096: jmsServer = null;
097: }
098:
099: /**
100: * @param newConnectionFactoryJNDIRef the JNDI reference where the
101: * connection factory should be bound to
102: *
103: * @jmx:managed-attribute
104: */
105: public void setConnectionFactoryJNDIRef(
106: java.lang.String newConnectionFactoryJNDIRef) {
107: connectionFactoryJNDIRef = newConnectionFactoryJNDIRef;
108: }
109:
110: /**
111: * @param newXaConnectionFactoryJNDIRef java.lang.String the JNDI reference
112: * where the xa connection factory should be bound to
113: *
114: * @jmx:managed-attribute
115: */
116: public void setXAConnectionFactoryJNDIRef(
117: java.lang.String newXaConnectionFactoryJNDIRef) {
118: xaConnectionFactoryJNDIRef = newXaConnectionFactoryJNDIRef;
119: }
120:
121: /**
122: * @return The ClientConnectionProperties value @returns Properties contains
123: * all the parameters needed to create a connection from the client
124: * to this IL
125: */
126: public java.util.Properties getClientConnectionProperties() {
127: Properties rc = new Properties();
128: rc
129: .setProperty(ServerILFactory.PING_PERIOD_KEY, ""
130: + pingPeriod);
131: if (clientID != null)
132: rc.setProperty(ServerILFactory.CLIENTID, clientID);
133: return rc;
134: }
135:
136: /**
137: * @return The ServerIL value @returns ServerIL An instance of the Server
138: * IL, used for
139: */
140: public abstract ServerIL getServerIL();
141:
142: /**
143: * @return java.lang.String the JNDI reference where the connection factory
144: * should be bound to
145: *
146: * @jmx:managed-attribute
147: */
148: public java.lang.String getConnectionFactoryJNDIRef() {
149: return connectionFactoryJNDIRef;
150: }
151:
152: /**
153: * @return java.lang.String the JNDI reference where the xa connection
154: * factory should be bound to
155: *
156: * @jmx:managed-attribute
157: */
158: public java.lang.String getXAConnectionFactoryJNDIRef() {
159: return xaConnectionFactoryJNDIRef;
160: }
161:
162: /**
163: * Binds the connection factories for this IL
164: *
165: * @throws javax.naming.NamingException it cannot be unbound
166: */
167: public void bindJNDIReferences()
168: throws javax.naming.NamingException {
169: GenericConnectionFactory gcf = new GenericConnectionFactory(
170: getServerIL(), getClientConnectionProperties());
171: SpyConnectionFactory scf = new SpyConnectionFactory(gcf);
172: SpyXAConnectionFactory sxacf = new SpyXAConnectionFactory(gcf);
173:
174: // Get an InitialContext
175: InitialContext ctx = getInitialContext();
176: rebind(ctx, connectionFactoryJNDIRef, scf);
177: rebind(ctx, xaConnectionFactoryJNDIRef, sxacf);
178:
179: }
180:
181: protected InitialContext getInitialContext() throws NamingException {
182: InitialContext ctx = new InitialContext();
183: return ctx;
184: }
185:
186: protected void rebind(Context ctx, String name, Object val)
187: throws NamingException {
188: // Bind val to name in ctx, and make sure that all intermediate contexts
189: // exist
190: javax.naming.Name n = ctx.getNameParser("").parse(name);
191: while (n.size() > 1) {
192: String ctxName = n.get(0);
193: try {
194: ctx = (Context) ctx.lookup(ctxName);
195: } catch (javax.naming.NameNotFoundException e) {
196: ctx = ctx.createSubcontext(ctxName);
197: }
198: n = n.getSuffix(1);
199: }
200:
201: ctx.rebind(n.get(0), val);
202: }
203:
204: /**
205: * Unbinds the connection factories for this IL
206: *
207: * @throws javax.naming.NamingException it cannot be unbound
208: */
209: public void unbindJNDIReferences()
210: throws javax.naming.NamingException {
211: // Get an InitialContext
212: InitialContext ctx = getInitialContext();
213: ctx.unbind(connectionFactoryJNDIRef);
214: ctx.unbind(xaConnectionFactoryJNDIRef);
215: }
216:
217: /**
218: * @return Description of the Returned Value
219: * @exception Exception Description of Exception
220: * @throws javax.naming.NamingException if the server is not found
221: */
222: public Invoker getJMSServer() {
223: return jmsServer;
224: }
225:
226: /**
227: * @return Description of the Returned Value
228: * @exception Exception Description of Exception
229: * @throws javax.naming.NamingException if the server is not found
230: */
231: public Invoker lookupJMSServer() {
232: return getJMSServer();
233: }
234:
235: /**
236: * @return long the period of time in ms to wait between connection pings
237: * factory should be bound to
238: *
239: * @jmx:managed-attribute
240: */
241: public long getPingPeriod() {
242: return pingPeriod;
243: }
244:
245: /**
246: * @param period long the period of time in ms to wait between connection
247: * pings
248: *
249: * @jmx:managed-attribute
250: */
251: public void setPingPeriod(long period) {
252: pingPeriod = period;
253: }
254:
255: /**
256: * Get the client id for this connection factory
257: *
258: * @jmx:managed-attribute
259: * @return the client id
260: */
261: public String getClientID() {
262: return clientID;
263: }
264:
265: /**
266: * Set the client id for this connection factory
267: *
268: * @jmx:managed-attribute
269: * @param clientID the client id
270: */
271: public void setClientID(String clientID) {
272: this.clientID = clientID;
273: }
274:
275: }
|