1 22 package org.jboss.resource.connectionmanager; 23 24 import javax.management.ObjectName ; 25 import javax.naming.InitialContext ; 26 import javax.naming.Name ; 27 import javax.naming.NamingException ; 28 import javax.resource.ResourceException ; 29 30 import org.jboss.deployment.DeploymentException; 31 import org.jboss.logging.Logger; 32 import org.jboss.system.ServiceMBeanSupport; 33 import org.jboss.util.naming.NonSerializableFactory; 34 35 42 public class ConnectionFactoryBindingService extends ServiceMBeanSupport 43 implements ConnectionFactoryBindingServiceMBean 44 { 45 46 private static final Logger log = Logger.getLogger(ConnectionFactoryBindingService.class); 47 48 49 protected ObjectName cm; 50 51 52 protected String jndiName; 53 54 55 protected String bindName; 56 57 58 protected boolean useJavaContext = true; 59 60 61 protected Object cf; 62 63 protected void startService() throws Exception 64 { 65 determineBindName(); 66 createConnectionFactory(); 67 bindConnectionFactory(); 68 } 69 70 protected void stopService() throws Exception 71 { 72 unbindConnectionFactory(); 73 } 74 75 public ObjectName getConnectionManager() 76 { 77 return cm; 78 } 79 80 public void setConnectionManager(ObjectName cm) 81 { 82 this.cm = cm; 83 } 84 85 public String getBindName() 86 { 87 return bindName; 88 } 89 90 public String getJndiName() 91 { 92 return jndiName; 93 } 94 95 public void setJndiName(String jndiName) 96 { 97 this.jndiName = jndiName; 98 } 99 100 public boolean isUseJavaContext() 101 { 102 return useJavaContext; 103 } 104 105 public void setUseJavaContext(boolean useJavaContext) 106 { 107 this.useJavaContext = useJavaContext; 108 } 109 110 113 protected void determineBindName() throws Exception 114 { 115 bindName = jndiName; 116 if( useJavaContext && jndiName.startsWith("java:") == false ) 117 bindName = "java:" + jndiName; 118 } 119 120 123 protected void createConnectionFactory() throws Exception 124 { 125 try 126 { 127 BaseConnectionManager2 bcm = (BaseConnectionManager2) server.getAttribute(cm, "Instance"); 128 BaseConnectionManager2.ConnectionManagerProxy cmProxy = new BaseConnectionManager2.ConnectionManagerProxy(bcm, cm); 129 cf = bcm.getPoolingStrategy().getManagedConnectionFactory().createConnectionFactory(cmProxy); 130 } 131 catch (ResourceException re) 132 { 133 throw new DeploymentException("Could not create ConnectionFactory for adapter: " + cm); 134 } 135 } 136 137 140 protected void bindConnectionFactory() throws Exception 141 { 142 InitialContext ctx = new InitialContext (); 143 try 144 { 145 log.debug("Binding object '" + cf + "' into JNDI at '" + bindName + "'"); 146 Name name = ctx.getNameParser("").parse(bindName); 147 NonSerializableFactory.rebind(name, cf, true); 148 log.info("Bound ConnectionManager '" + serviceName + "' to JNDI name '" + bindName + "'"); 149 } 150 catch (NamingException ne) 151 { 152 throw new DeploymentException("Could not bind ConnectionFactory into jndi: " + bindName, ne); 153 } 154 finally 155 { 156 ctx.close(); 157 } 158 } 159 160 163 protected void unbindConnectionFactory() throws Exception 164 { 165 InitialContext ctx = new InitialContext (); 166 try 167 { 168 ctx.unbind(bindName); 169 NonSerializableFactory.unbind(bindName); 170 log.info("Unbound ConnectionManager '" + serviceName + "' from JNDI name '" + bindName + "'"); 171 } 172 catch (NamingException ne) 173 { 174 log.error("Could not unbind managedConnectionFactory from jndi: " + bindName, ne); 175 } 176 finally 177 { 178 ctx.close(); 179 } 180 } 181 } 182 | Popular Tags |