KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > jndi > JMSProviderLoader


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.jms.jndi;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.Name JavaDoc;
29 import javax.naming.NameNotFoundException JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 import org.jboss.deployment.DeploymentException;
33 import org.jboss.system.ServiceMBeanSupport;
34
35 /**
36  * A JMX service to load a JMSProviderAdapter and register it.
37  *
38  * @author <a HREF="mailto:cojonudo14@hotmail.com">Hiram Chirino</a>
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
41  * @version $Revision: 38361 $
42  */

43 public class JMSProviderLoader extends ServiceMBeanSupport implements JMSProviderLoaderMBean
44 {
45    /** The provider adapter which we are loading. */
46    protected JMSProviderAdapter providerAdapter;
47
48    /** The properties */
49    protected Properties JavaDoc properties;
50
51    /** The provider name. */
52    protected String JavaDoc providerName;
53
54    /** The provider adapter classname. */
55    protected String JavaDoc providerAdapterClass;
56
57    /** The factory jndi name. */
58    protected String JavaDoc factoryRef;
59
60    /** The queue factory jndi name. */
61    protected String JavaDoc queueFactoryRef;
62
63    /** The topic factory jndi name. */
64    protected String JavaDoc topicFactoryRef;
65
66    /** The JNDI name to bind the adapter to. */
67    protected String JavaDoc jndiName;
68
69    public void setProviderName(String JavaDoc name)
70    {
71       this.providerName = name;
72    }
73
74    public String JavaDoc getProviderName()
75    {
76       return providerName;
77    }
78
79    public void setProviderAdapterClass(String JavaDoc clazz)
80    {
81       providerAdapterClass = clazz;
82    }
83
84    public String JavaDoc getProviderAdapterClass()
85    {
86       return providerAdapterClass;
87    }
88
89    public void setProperties(final Properties JavaDoc properties)
90    {
91       this.properties = properties;
92    }
93
94    public Properties JavaDoc getProperties()
95    {
96       return properties;
97    }
98
99    public void setAdapterJNDIName(final String JavaDoc name)
100    {
101       this.jndiName = name;
102    }
103
104    public String JavaDoc getAdapterJNDIName()
105    {
106       return jndiName;
107    }
108
109    public void setFactoryRef(final String JavaDoc newFactoryRef)
110    {
111       factoryRef = newFactoryRef;
112    }
113
114    public void setQueueFactoryRef(final String JavaDoc newQueueFactoryRef)
115    {
116       queueFactoryRef = newQueueFactoryRef;
117    }
118
119    public void setTopicFactoryRef(final String JavaDoc newTopicFactoryRef)
120    {
121       topicFactoryRef = newTopicFactoryRef;
122    }
123
124    public String JavaDoc getFactoryRef()
125    {
126       return factoryRef;
127    }
128
129    public String JavaDoc getQueueFactoryRef()
130    {
131       return queueFactoryRef;
132    }
133
134    public String JavaDoc getTopicFactoryRef()
135    {
136       return topicFactoryRef;
137    }
138
139    public String JavaDoc getName()
140    {
141       return providerName;
142    }
143
144    protected void startService() throws Exception JavaDoc
145    {
146       // validate the configuration
147
if (queueFactoryRef == null)
148          throw new DeploymentException("missing required attribute: QueueFactoryRef");
149
150       if (topicFactoryRef == null)
151          throw new DeploymentException("missing required attribute: TopicFactoryRef");
152
153       Class JavaDoc cls = Thread.currentThread().getContextClassLoader().loadClass(providerAdapterClass);
154       providerAdapter = (JMSProviderAdapter) cls.newInstance();
155       providerAdapter.setName(providerName);
156       providerAdapter.setProperties(properties);
157       providerAdapter.setFactoryRef(factoryRef);
158       providerAdapter.setQueueFactoryRef(queueFactoryRef);
159       providerAdapter.setTopicFactoryRef(topicFactoryRef);
160
161       InitialContext JavaDoc context = new InitialContext JavaDoc();
162       try
163       {
164          // Bind in JNDI
165
if (jndiName == null)
166          {
167             String JavaDoc name = providerAdapter.getName();
168             jndiName = "java:/" + name;
169          }
170          bind(context, jndiName, providerAdapter);
171          log.debug("Bound adapter to " + jndiName);
172       }
173       finally
174       {
175          context.close();
176       }
177    }
178
179    protected void stopService() throws Exception JavaDoc
180    {
181       InitialContext JavaDoc context = new InitialContext JavaDoc();
182
183       try
184       {
185          // Unbind from JNDI
186
String JavaDoc name = providerAdapter.getName();
187          String JavaDoc jndiname = "java:/" + name;
188          context.unbind(jndiname);
189          log.debug("unbound adapter " + name + " from " + jndiname);
190       }
191       finally
192       {
193          context.close();
194       }
195    }
196
197    private void bind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc val) throws NamingException JavaDoc
198    {
199       log.debug("attempting to bind " + val + " to " + name);
200
201       // Bind val to name in ctx, and make sure that all
202
// intermediate contexts exist
203
Name JavaDoc n = ctx.getNameParser("").parse(name);
204       while (n.size() > 1)
205       {
206          String JavaDoc ctxName = n.get(0);
207          try
208          {
209             ctx = (Context JavaDoc) ctx.lookup(ctxName);
210          }
211          catch (NameNotFoundException JavaDoc e)
212          {
213             ctx = ctx.createSubcontext(ctxName);
214          }
215          n = n.getSuffix(1);
216       }
217
218       ctx.bind(n.get(0), val);
219    }
220 }
221
Popular Tags