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.jms.jndi;
023:
024: import java.util.Properties;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.Name;
029: import javax.naming.NameNotFoundException;
030: import javax.naming.NamingException;
031:
032: import org.jboss.deployment.DeploymentException;
033: import org.jboss.system.ServiceMBeanSupport;
034:
035: /**
036: * A JMX service to load a JMSProviderAdapter and register it.
037: *
038: * @author <a href="mailto:cojonudo14@hotmail.com">Hiram Chirino</a>
039: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
040: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
041: * @version $Revision: 57209 $
042: */
043: public class JMSProviderLoader extends ServiceMBeanSupport implements
044: JMSProviderLoaderMBean {
045: /** The provider adapter which we are loading. */
046: protected JMSProviderAdapter providerAdapter;
047:
048: /** The properties */
049: protected Properties properties;
050:
051: /** The provider name. */
052: protected String providerName;
053:
054: /** The provider adapter classname. */
055: protected String providerAdapterClass;
056:
057: /** The factory jndi name. */
058: protected String factoryRef;
059:
060: /** The queue factory jndi name. */
061: protected String queueFactoryRef;
062:
063: /** The topic factory jndi name. */
064: protected String topicFactoryRef;
065:
066: /** The JNDI name to bind the adapter to. */
067: protected String jndiName;
068:
069: public void setProviderName(String name) {
070: this .providerName = name;
071: }
072:
073: public String getProviderName() {
074: return providerName;
075: }
076:
077: public void setProviderAdapterClass(String clazz) {
078: providerAdapterClass = clazz;
079: }
080:
081: public String getProviderAdapterClass() {
082: return providerAdapterClass;
083: }
084:
085: public void setProperties(final Properties properties) {
086: this .properties = properties;
087: }
088:
089: public Properties getProperties() {
090: return properties;
091: }
092:
093: public void setAdapterJNDIName(final String name) {
094: this .jndiName = name;
095: }
096:
097: public String getAdapterJNDIName() {
098: return jndiName;
099: }
100:
101: public void setFactoryRef(final String newFactoryRef) {
102: factoryRef = newFactoryRef;
103: }
104:
105: public void setQueueFactoryRef(final String newQueueFactoryRef) {
106: queueFactoryRef = newQueueFactoryRef;
107: }
108:
109: public void setTopicFactoryRef(final String newTopicFactoryRef) {
110: topicFactoryRef = newTopicFactoryRef;
111: }
112:
113: public String getFactoryRef() {
114: return factoryRef;
115: }
116:
117: public String getQueueFactoryRef() {
118: return queueFactoryRef;
119: }
120:
121: public String getTopicFactoryRef() {
122: return topicFactoryRef;
123: }
124:
125: public String getName() {
126: return providerName;
127: }
128:
129: protected void startService() throws Exception {
130: // validate the configuration
131: if (queueFactoryRef == null)
132: throw new DeploymentException(
133: "missing required attribute: QueueFactoryRef");
134:
135: if (topicFactoryRef == null)
136: throw new DeploymentException(
137: "missing required attribute: TopicFactoryRef");
138:
139: Class cls = Thread.currentThread().getContextClassLoader()
140: .loadClass(providerAdapterClass);
141: providerAdapter = (JMSProviderAdapter) cls.newInstance();
142: providerAdapter.setName(providerName);
143: providerAdapter.setProperties(properties);
144: providerAdapter.setFactoryRef(factoryRef);
145: providerAdapter.setQueueFactoryRef(queueFactoryRef);
146: providerAdapter.setTopicFactoryRef(topicFactoryRef);
147:
148: InitialContext context = new InitialContext();
149: try {
150: // Bind in JNDI
151: if (jndiName == null) {
152: String name = providerAdapter.getName();
153: jndiName = "java:/" + name;
154: }
155: bind(context, jndiName, providerAdapter);
156: log.debug("Bound adapter to " + jndiName);
157: } finally {
158: context.close();
159: }
160: }
161:
162: protected void stopService() throws Exception {
163: InitialContext context = new InitialContext();
164:
165: try {
166: // Unbind from JNDI
167: String name = providerAdapter.getName();
168: String jndiname = "java:/" + name;
169: context.unbind(jndiname);
170: log.debug("unbound adapter " + name + " from " + jndiname);
171: } finally {
172: context.close();
173: }
174: }
175:
176: private void bind(Context ctx, String name, Object val)
177: throws NamingException {
178: log.debug("attempting to bind " + val + " to " + name);
179:
180: // Bind val to name in ctx, and make sure that all
181: // intermediate contexts exist
182: Name n = ctx.getNameParser("").parse(name);
183: while (n.size() > 1) {
184: String ctxName = n.get(0);
185: try {
186: ctx = (Context) ctx.lookup(ctxName);
187: } catch (NameNotFoundException e) {
188: ctx = ctx.createSubcontext(ctxName);
189: }
190: n = n.getSuffix(1);
191: }
192:
193: ctx.bind(n.get(0), val);
194: }
195: }
|