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.resource.connectionmanager;
023:
024: import javax.management.ObjectName;
025: import javax.naming.InitialContext;
026: import javax.naming.Name;
027: import javax.naming.NamingException;
028: import javax.resource.ResourceException;
029:
030: import org.jboss.deployment.DeploymentException;
031: import org.jboss.logging.Logger;
032: import org.jboss.system.ServiceMBeanSupport;
033: import org.jboss.util.naming.NonSerializableFactory;
034:
035: /**
036: * Handles the binding of the connection factory into jndi
037: *
038: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
039: * @version $Revision: 57189 $
040: */
041: public class ConnectionFactoryBindingService extends
042: ServiceMBeanSupport implements
043: ConnectionFactoryBindingServiceMBean {
044: /** The logger */
045: private static final Logger log = Logger
046: .getLogger(ConnectionFactoryBindingService.class);
047:
048: /** The connection manager */
049: protected ObjectName cm;
050:
051: /** The jndi name */
052: protected String jndiName;
053:
054: /** The bind name */
055: protected String bindName;
056:
057: /** Whether to use the java naming context */
058: protected boolean useJavaContext = true;
059:
060: /** The connection factory */
061: protected Object cf;
062:
063: protected void startService() throws Exception {
064: determineBindName();
065: createConnectionFactory();
066: bindConnectionFactory();
067: }
068:
069: protected void stopService() throws Exception {
070: unbindConnectionFactory();
071: }
072:
073: public ObjectName getConnectionManager() {
074: return cm;
075: }
076:
077: public void setConnectionManager(ObjectName cm) {
078: this .cm = cm;
079: }
080:
081: public String getBindName() {
082: return bindName;
083: }
084:
085: public String getJndiName() {
086: return jndiName;
087: }
088:
089: public void setJndiName(String jndiName) {
090: this .jndiName = jndiName;
091: }
092:
093: public boolean isUseJavaContext() {
094: return useJavaContext;
095: }
096:
097: public void setUseJavaContext(boolean useJavaContext) {
098: this .useJavaContext = useJavaContext;
099: }
100:
101: /**
102: * Determine the bind name
103: */
104: protected void determineBindName() throws Exception {
105: bindName = jndiName;
106: if (useJavaContext && jndiName.startsWith("java:") == false)
107: bindName = "java:" + jndiName;
108: }
109:
110: /**
111: * Create the connection factory
112: */
113: protected void createConnectionFactory() throws Exception {
114: try {
115: BaseConnectionManager2 bcm = (BaseConnectionManager2) server
116: .getAttribute(cm, "Instance");
117: BaseConnectionManager2.ConnectionManagerProxy cmProxy = new BaseConnectionManager2.ConnectionManagerProxy(
118: bcm, cm);
119: cf = bcm.getPoolingStrategy().getManagedConnectionFactory()
120: .createConnectionFactory(cmProxy);
121: } catch (ResourceException re) {
122: throw new DeploymentException(
123: "Could not create ConnectionFactory for adapter: "
124: + cm);
125: }
126: }
127:
128: /**
129: * Bind the connection factory into jndi
130: */
131: protected void bindConnectionFactory() throws Exception {
132: InitialContext ctx = new InitialContext();
133: try {
134: log.debug("Binding object '" + cf + "' into JNDI at '"
135: + bindName + "'");
136: Name name = ctx.getNameParser("").parse(bindName);
137: NonSerializableFactory.rebind(name, cf, true);
138: log.info("Bound ConnectionManager '" + serviceName
139: + "' to JNDI name '" + bindName + "'");
140: } catch (NamingException ne) {
141: throw new DeploymentException(
142: "Could not bind ConnectionFactory into jndi: "
143: + bindName, ne);
144: } finally {
145: ctx.close();
146: }
147: }
148:
149: /**
150: * Unbind the connection factory into jndi
151: */
152: protected void unbindConnectionFactory() throws Exception {
153: InitialContext ctx = new InitialContext();
154: try {
155: ctx.unbind(bindName);
156: NonSerializableFactory.unbind(bindName);
157: log.info("Unbound ConnectionManager '" + serviceName
158: + "' from JNDI name '" + bindName + "'");
159: } catch (NamingException ne) {
160: log.error(
161: "Could not unbind managedConnectionFactory from jndi: "
162: + bindName, ne);
163: } finally {
164: ctx.close();
165: }
166: }
167: }
|