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;
25
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.Name;
29 import javax.naming.NameNotFoundException;
30 import javax.naming.NamingException;
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 properties;
50
51 /** The provider name. */
52 protected String providerName;
53
54 /** The provider adapter classname. */
55 protected String providerAdapterClass;
56
57 /** The factory jndi name. */
58 protected String factoryRef;
59
60 /** The queue factory jndi name. */
61 protected String queueFactoryRef;
62
63 /** The topic factory jndi name. */
64 protected String topicFactoryRef;
65
66 /** The JNDI name to bind the adapter to. */
67 protected String jndiName;
68
69 public void setProviderName(String name)
70 {
71 this.providerName = name;
72 }
73
74 public String getProviderName()
75 {
76 return providerName;
77 }
78
79 public void setProviderAdapterClass(String clazz)
80 {
81 providerAdapterClass = clazz;
82 }
83
84 public String getProviderAdapterClass()
85 {
86 return providerAdapterClass;
87 }
88
89 public void setProperties(final Properties properties)
90 {
91 this.properties = properties;
92 }
93
94 public Properties getProperties()
95 {
96 return properties;
97 }
98
99 public void setAdapterJNDIName(final String name)
100 {
101 this.jndiName = name;
102 }
103
104 public String getAdapterJNDIName()
105 {
106 return jndiName;
107 }
108
109 public void setFactoryRef(final String newFactoryRef)
110 {
111 factoryRef = newFactoryRef;
112 }
113
114 public void setQueueFactoryRef(final String newQueueFactoryRef)
115 {
116 queueFactoryRef = newQueueFactoryRef;
117 }
118
119 public void setTopicFactoryRef(final String newTopicFactoryRef)
120 {
121 topicFactoryRef = newTopicFactoryRef;
122 }
123
124 public String getFactoryRef()
125 {
126 return factoryRef;
127 }
128
129 public String getQueueFactoryRef()
130 {
131 return queueFactoryRef;
132 }
133
134 public String getTopicFactoryRef()
135 {
136 return topicFactoryRef;
137 }
138
139 public String getName()
140 {
141 return providerName;
142 }
143
144 protected void startService() throws Exception
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 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 context = new InitialContext();
162 try
163 {
164 // Bind in JNDI
165 if (jndiName == null)
166 {
167 String 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
180 {
181 InitialContext context = new InitialContext();
182
183 try
184 {
185 // Unbind from JNDI
186 String name = providerAdapter.getName();
187 String 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 ctx, String name, Object val) throws NamingException
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 n = ctx.getNameParser("").parse(name);
204 while (n.size() > 1)
205 {
206 String ctxName = n.get(0);
207 try
208 {
209 ctx = (Context) ctx.lookup(ctxName);
210 }
211 catch (NameNotFoundException e)
212 {
213 ctx = ctx.createSubcontext(ctxName);
214 }
215 n = n.getSuffix(1);
216 }
217
218 ctx.bind(n.get(0), val);
219 }
220 }