1 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 43 public class JMSProviderLoader extends ServiceMBeanSupport implements JMSProviderLoaderMBean 44 { 45 46 protected JMSProviderAdapter providerAdapter; 47 48 49 protected Properties properties; 50 51 52 protected String providerName; 53 54 55 protected String providerAdapterClass; 56 57 58 protected String factoryRef; 59 60 61 protected String queueFactoryRef; 62 63 64 protected String topicFactoryRef; 65 66 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 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 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 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 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 } 221 | Popular Tags |