KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > connectionmanager > ConnectionFactoryBindingService


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.resource.connectionmanager;
23
24 import javax.management.ObjectName JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.Name JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.resource.ResourceException JavaDoc;
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 /**
36  * Handles the binding of the connection factory into jndi
37  *
38  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
39  * @author <a HREF="mailto:weston.price@jboss.com">Weston Price</a>
40  * @version $Revision: 44968 $
41  */

42 public class ConnectionFactoryBindingService extends ServiceMBeanSupport
43    implements ConnectionFactoryBindingServiceMBean
44 {
45    /** The logger */
46    private static final Logger log = Logger.getLogger(ConnectionFactoryBindingService.class);
47    
48    /** The connection manager */
49    protected ObjectName JavaDoc cm;
50    
51    /** The jndi name */
52    protected String JavaDoc jndiName;
53    
54    /** The bind name */
55    protected String JavaDoc bindName;
56    
57    /** Whether to use the java naming context */
58    protected boolean useJavaContext = true;
59    
60    /** The connection factory */
61    protected Object JavaDoc cf;
62    
63    protected void startService() throws Exception JavaDoc
64    {
65       determineBindName();
66       createConnectionFactory();
67       bindConnectionFactory();
68    }
69    
70    protected void stopService() throws Exception JavaDoc
71    {
72       unbindConnectionFactory();
73    }
74
75    public ObjectName JavaDoc getConnectionManager()
76    {
77       return cm;
78    }
79
80    public void setConnectionManager(ObjectName JavaDoc cm)
81    {
82       this.cm = cm;
83    }
84
85    public String JavaDoc getBindName()
86    {
87       return bindName;
88    }
89
90    public String JavaDoc getJndiName()
91    {
92       return jndiName;
93    }
94
95    public void setJndiName(String JavaDoc 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    /**
111     * Determine the bind name
112     */

113    protected void determineBindName() throws Exception JavaDoc
114    {
115       bindName = jndiName;
116       if( useJavaContext && jndiName.startsWith("java:") == false )
117          bindName = "java:" + jndiName;
118    }
119
120    /**
121     * Create the connection factory
122     */

123    protected void createConnectionFactory() throws Exception JavaDoc
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 JavaDoc re)
132       {
133          throw new DeploymentException("Could not create ConnectionFactory for adapter: " + cm);
134       }
135    }
136    
137    /**
138     * Bind the connection factory into jndi
139     */

140    protected void bindConnectionFactory() throws Exception JavaDoc
141    {
142       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
143       try
144       {
145          log.debug("Binding object '" + cf + "' into JNDI at '" + bindName + "'");
146          Name JavaDoc 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 JavaDoc 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    /**
161     * Unbind the connection factory into jndi
162     */

163    protected void unbindConnectionFactory() throws Exception JavaDoc
164    {
165       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc 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