KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > ServerILJMXService


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.il;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.jms.IllegalStateException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import org.jboss.mq.GenericConnectionFactory;
33 import org.jboss.mq.SpyConnectionFactory;
34 import org.jboss.mq.SpyXAConnectionFactory;
35 import org.jboss.system.ServiceMBeanSupport;
36
37 /**
38  * This abstract class handles life cycle managment of the ServeIL. Should be
39  * extended to provide a full implementation.
40  *
41  * @author Hiram Chirino (Cojonudo14@hotmail.com)
42  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks </a>
43  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 45305 $
45  *
46  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
47  */

48 public abstract class ServerILJMXService extends ServiceMBeanSupport implements ServerILJMXServiceMBean
49 {
50
51    private ObjectName JavaDoc jbossMQService;
52
53    protected Invoker jmsServer;
54
55    protected String JavaDoc connectionFactoryJNDIRef;
56
57    protected String JavaDoc xaConnectionFactoryJNDIRef;
58
59    protected long pingPeriod = 60000L;
60    
61    /** The client id */
62    protected String JavaDoc clientID;
63
64    /**
65     * Get the value of JBossMQService.
66     *
67     * @return value of JBossMQService.
68     *
69     * @jmx:managed-attribute
70     */

71    public ObjectName JavaDoc getJBossMQService()
72    {
73       return jbossMQService;
74    }
75
76    /**
77     * Set the value of JBossMQService.
78     *
79     * @param v Value to assign to JBossMQService.
80     *
81     * @jmx:managed-attribute
82     */

83    public void setInvoker(ObjectName JavaDoc jbossMQService)
84    {
85       this.jbossMQService = jbossMQService;
86    }
87
88    public void startService() throws Exception JavaDoc
89    {
90       jmsServer = (Invoker) getServer().getAttribute(jbossMQService, "Invoker");
91       if (jmsServer == null)
92       {
93          throw new IllegalStateException JavaDoc("Cannot find JBossMQService!");
94       } // end of if ()
95
}
96
97    public void stopService() throws Exception JavaDoc
98    {
99       jmsServer = null;
100    }
101
102    /**
103     * @param newConnectionFactoryJNDIRef the JNDI reference where the
104     * connection factory should be bound to
105     *
106     * @jmx:managed-attribute
107     */

108    public void setConnectionFactoryJNDIRef(java.lang.String JavaDoc newConnectionFactoryJNDIRef)
109    {
110       connectionFactoryJNDIRef = newConnectionFactoryJNDIRef;
111    }
112
113    /**
114     * @param newXaConnectionFactoryJNDIRef java.lang.String the JNDI reference
115     * where the xa connection factory should be bound to
116     *
117     * @jmx:managed-attribute
118     */

119    public void setXAConnectionFactoryJNDIRef(java.lang.String JavaDoc newXaConnectionFactoryJNDIRef)
120    {
121       xaConnectionFactoryJNDIRef = newXaConnectionFactoryJNDIRef;
122    }
123
124    /**
125     * @return The ClientConnectionProperties value @returns Properties contains
126     * all the parameters needed to create a connection from the client
127     * to this IL
128     */

129    public java.util.Properties JavaDoc getClientConnectionProperties()
130    {
131       Properties JavaDoc rc = new Properties JavaDoc();
132       rc.setProperty(ServerILFactory.PING_PERIOD_KEY, "" + pingPeriod);
133       if (clientID != null)
134          rc.setProperty(ServerILFactory.CLIENTID, clientID);
135       return rc;
136    }
137
138    /**
139     * @return The ServerIL value @returns ServerIL An instance of the Server
140     * IL, used for
141     */

142    public abstract ServerIL getServerIL();
143
144    /**
145     * @return java.lang.String the JNDI reference where the connection factory
146     * should be bound to
147     *
148     * @jmx:managed-attribute
149     */

150    public java.lang.String JavaDoc getConnectionFactoryJNDIRef()
151    {
152       return connectionFactoryJNDIRef;
153    }
154
155    /**
156     * @return java.lang.String the JNDI reference where the xa connection
157     * factory should be bound to
158     *
159     * @jmx:managed-attribute
160     */

161    public java.lang.String JavaDoc getXAConnectionFactoryJNDIRef()
162    {
163       return xaConnectionFactoryJNDIRef;
164    }
165
166    /**
167     * Binds the connection factories for this IL
168     *
169     * @throws javax.naming.NamingException it cannot be unbound
170     */

171    public void bindJNDIReferences() throws javax.naming.NamingException JavaDoc
172    {
173       GenericConnectionFactory gcf = new GenericConnectionFactory(getServerIL(), getClientConnectionProperties());
174       SpyConnectionFactory scf = new SpyConnectionFactory(gcf);
175       SpyXAConnectionFactory sxacf = new SpyXAConnectionFactory(gcf);
176
177       // Get an InitialContext
178
InitialContext JavaDoc ctx = getInitialContext();
179       rebind(ctx, connectionFactoryJNDIRef, scf);
180       rebind(ctx, xaConnectionFactoryJNDIRef, sxacf);
181
182    }
183
184    protected InitialContext JavaDoc getInitialContext()
185            throws NamingException JavaDoc
186    {
187       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
188       return ctx;
189    }
190
191    protected void rebind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc val) throws NamingException JavaDoc
192    {
193       // Bind val to name in ctx, and make sure that all intermediate contexts
194
// exist
195
javax.naming.Name JavaDoc n = ctx.getNameParser("").parse(name);
196       while (n.size() > 1)
197       {
198          String JavaDoc ctxName = n.get(0);
199          try
200          {
201             ctx = (Context JavaDoc) ctx.lookup(ctxName);
202          }
203          catch (javax.naming.NameNotFoundException JavaDoc e)
204          {
205             ctx = ctx.createSubcontext(ctxName);
206          }
207          n = n.getSuffix(1);
208       }
209
210       ctx.rebind(n.get(0), val);
211    }
212
213    /**
214     * Unbinds the connection factories for this IL
215     *
216     * @throws javax.naming.NamingException it cannot be unbound
217     */

218    public void unbindJNDIReferences() throws javax.naming.NamingException JavaDoc
219    {
220       // Get an InitialContext
221
InitialContext JavaDoc ctx = getInitialContext();
222       ctx.unbind(connectionFactoryJNDIRef);
223       ctx.unbind(xaConnectionFactoryJNDIRef);
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 getJMSServer()
232    {
233       return jmsServer;
234    }
235
236    /**
237     * @return Description of the Returned Value
238     * @exception Exception Description of Exception
239     * @throws javax.naming.NamingException if the server is not found
240     */

241    public Invoker lookupJMSServer()
242    {
243       return getJMSServer();
244    }
245
246    /**
247     * @return long the period of time in ms to wait between connection pings
248     * factory should be bound to
249     *
250     * @jmx:managed-attribute
251     */

252    public long getPingPeriod()
253    {
254       return pingPeriod;
255    }
256
257    /**
258     * @param period long the period of time in ms to wait between connection
259     * pings
260     *
261     * @jmx:managed-attribute
262     */

263    public void setPingPeriod(long period)
264    {
265       pingPeriod = period;
266    }
267
268    /**
269     * Get the client id for this connection factory
270     *
271     * @jmx:managed-attribute
272     * @return the client id
273     */

274    public String JavaDoc getClientID()
275    {
276       return clientID;
277    }
278
279    /**
280     * Set the client id for this connection factory
281     *
282     * @jmx:managed-attribute
283     * @param clientID the client id
284     */

285    public void setClientID(String JavaDoc clientID)
286    {
287       this.clientID = clientID;
288    }
289
290 }
291
Popular Tags