/projects/jboss-5.1.0/server/src/main/org/jboss/jms/jndi/JMSProviderLoader.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 220 lines · 143 code · 35 blank · 42 comment · 7 complexity · 074ae3c9ae54653e4ca44c681a985c78 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a 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. import java.util.Properties;
  24. import javax.naming.Context;
  25. import javax.naming.InitialContext;
  26. import javax.naming.Name;
  27. import javax.naming.NameNotFoundException;
  28. import javax.naming.NamingException;
  29. import org.jboss.deployment.DeploymentException;
  30. import org.jboss.system.ServiceMBeanSupport;
  31. /**
  32. * A JMX service to load a JMSProviderAdapter and register it.
  33. *
  34. * @author <a href="mailto:cojonudo14@hotmail.com">Hiram Chirino</a>
  35. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  36. * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
  37. * @version $Revision: 81030 $
  38. */
  39. public class JMSProviderLoader extends ServiceMBeanSupport implements JMSProviderLoaderMBean
  40. {
  41. /** The provider adapter which we are loading. */
  42. protected JMSProviderAdapter providerAdapter;
  43. /** The properties */
  44. protected Properties properties;
  45. /** The provider name. */
  46. protected String providerName;
  47. /** The provider adapter classname. */
  48. protected String providerAdapterClass;
  49. /** The factory jndi name. */
  50. protected String factoryRef;
  51. /** The queue factory jndi name. */
  52. protected String queueFactoryRef;
  53. /** The topic factory jndi name. */
  54. protected String topicFactoryRef;
  55. /** The JNDI name to bind the adapter to. */
  56. protected String jndiName;
  57. public void setProviderName(String name)
  58. {
  59. this.providerName = name;
  60. }
  61. public String getProviderName()
  62. {
  63. return providerName;
  64. }
  65. public void setProviderAdapterClass(String clazz)
  66. {
  67. providerAdapterClass = clazz;
  68. }
  69. public String getProviderAdapterClass()
  70. {
  71. return providerAdapterClass;
  72. }
  73. public void setProperties(final Properties properties)
  74. {
  75. this.properties = properties;
  76. }
  77. public Properties getProperties()
  78. {
  79. return properties;
  80. }
  81. public void setAdapterJNDIName(final String name)
  82. {
  83. this.jndiName = name;
  84. }
  85. public String getAdapterJNDIName()
  86. {
  87. return jndiName;
  88. }
  89. public void setFactoryRef(final String newFactoryRef)
  90. {
  91. factoryRef = newFactoryRef;
  92. }
  93. public void setQueueFactoryRef(final String newQueueFactoryRef)
  94. {
  95. queueFactoryRef = newQueueFactoryRef;
  96. }
  97. public void setTopicFactoryRef(final String newTopicFactoryRef)
  98. {
  99. topicFactoryRef = newTopicFactoryRef;
  100. }
  101. public String getFactoryRef()
  102. {
  103. return factoryRef;
  104. }
  105. public String getQueueFactoryRef()
  106. {
  107. return queueFactoryRef;
  108. }
  109. public String getTopicFactoryRef()
  110. {
  111. return topicFactoryRef;
  112. }
  113. public String getName()
  114. {
  115. return providerName;
  116. }
  117. protected void startService() throws Exception
  118. {
  119. // validate the configuration
  120. if (queueFactoryRef == null)
  121. throw new DeploymentException("missing required attribute: QueueFactoryRef");
  122. if (topicFactoryRef == null)
  123. throw new DeploymentException("missing required attribute: TopicFactoryRef");
  124. Class cls = Thread.currentThread().getContextClassLoader().loadClass(providerAdapterClass);
  125. providerAdapter = (JMSProviderAdapter) cls.newInstance();
  126. providerAdapter.setName(providerName);
  127. providerAdapter.setProperties(properties);
  128. providerAdapter.setFactoryRef(factoryRef);
  129. providerAdapter.setQueueFactoryRef(queueFactoryRef);
  130. providerAdapter.setTopicFactoryRef(topicFactoryRef);
  131. InitialContext context = new InitialContext();
  132. try
  133. {
  134. // Bind in JNDI
  135. if (jndiName == null)
  136. {
  137. String name = providerAdapter.getName();
  138. jndiName = "java:/" + name;
  139. }
  140. bind(context, jndiName, providerAdapter);
  141. log.debug("Bound adapter to " + jndiName);
  142. }
  143. finally
  144. {
  145. context.close();
  146. }
  147. }
  148. protected void stopService() throws Exception
  149. {
  150. InitialContext context = new InitialContext();
  151. try
  152. {
  153. // Unbind from JNDI
  154. String name = providerAdapter.getName();
  155. String jndiname = "java:/" + name;
  156. context.unbind(jndiname);
  157. log.debug("unbound adapter " + name + " from " + jndiname);
  158. }
  159. finally
  160. {
  161. context.close();
  162. }
  163. }
  164. private void bind(Context ctx, String name, Object val) throws NamingException
  165. {
  166. log.debug("attempting to bind " + val + " to " + name);
  167. // Bind val to name in ctx, and make sure that all
  168. // intermediate contexts exist
  169. Name n = ctx.getNameParser("").parse(name);
  170. while (n.size() > 1)
  171. {
  172. String ctxName = n.get(0);
  173. try
  174. {
  175. ctx = (Context) ctx.lookup(ctxName);
  176. }
  177. catch (NameNotFoundException e)
  178. {
  179. ctx = ctx.createSubcontext(ctxName);
  180. }
  181. n = n.getSuffix(1);
  182. }
  183. ctx.bind(n.get(0), val);
  184. }
  185. }