2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Licensed to the Apache Software Foundation (ASF) under one or more
23 * contributor license agreements. See the NOTICE file distributed with
24 * this work for additional information regarding copyright ownership.
25 * The ASF licenses this file to You under the Apache License, Version 2.0
26 * (the "License"); you may not use this file except in compliance with
27 * the License. You may obtain a copy of the License at
29 * http://www.apache.org/licenses/LICENSE-2.0
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
37 package org.apache.tomcat.jdbc.pool;
39 import java.lang.management.ManagementFactory;
40 import java.util.Hashtable;
42 import javax.management.InstanceNotFoundException;
43 import javax.management.MBeanRegistration;
44 import javax.management.MBeanServer;
45 import javax.management.MalformedObjectNameException;
46 import javax.management.ObjectName;
48 import org.apache.juli.logging.Log;
49 import org.apache.juli.logging.LogFactory;
53 * A DataSource that can be instantiated through IoC and implements the DataSource interface
54 * since the DataSourceProxy is used as a generic proxy.
55 * The DataSource simply wraps a {@link ConnectionPool} in order to provide a standard interface to the user.
58 public class DataSource extends DataSourceProxy implements javax.sql.DataSource,MBeanRegistration, org.apache.tomcat.jdbc.pool.jmx.ConnectionPoolMBean, javax.sql.ConnectionPoolDataSource {
59 private static final Log log = LogFactory.getLog(DataSource.class);
62 * Constructor for reflection only. A default set of pool properties will be created.
69 * Constructs a DataSource object wrapping a connection
70 * @param poolProperties The pool properties
72 public DataSource(PoolConfiguration poolProperties) {
73 super(poolProperties);
80 //===============================================================================
81 // JMX Operations - Register the actual pool itself under the tomcat.jdbc domain
82 //===============================================================================
83 protected volatile ObjectName oname = null;
86 * Unregisters the underlying connection pool mbean.<br>
90 public void postDeregister() {
91 if (oname!=null) unregisterJmx();
99 public void postRegister(Boolean registrationDone) {
109 public void preDeregister() throws Exception {
114 * If the connection pool MBean exists, it will be registered during this operation.<br>
118 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
120 if ( isJmxEnabled() ) {
121 this.oname = createObjectName(name);
122 if (oname!=null) registerJmx();
124 }catch (MalformedObjectNameException x) {
125 log.error("Unable to create object name for JDBC pool.",x);
131 * Creates the ObjectName for the ConnectionPoolMBean object to be registered
132 * @param original the ObjectName for the DataSource
133 * @return the ObjectName for the ConnectionPoolMBean
134 * @throws MalformedObjectNameException Invalid object name
136 public ObjectName createObjectName(ObjectName original) throws MalformedObjectNameException {
137 String domain = ConnectionPool.POOL_JMX_DOMAIN;
138 Hashtable<String,String> properties = original.getKeyPropertyList();
139 String origDomain = original.getDomain();
140 properties.put("type", "ConnectionPool");
141 properties.put("class", this.getClass().getName());
142 if (original.getKeyProperty("path")!=null || properties.get("context")!=null) {
143 //this ensures that if the registration came from tomcat, we're not losing
144 //the unique domain, but putting that into as an engine attribute
145 properties.put("engine", origDomain);
147 ObjectName name = new ObjectName(domain,properties);
152 * Registers the ConnectionPoolMBean under a unique name based on the ObjectName for the DataSource
154 protected void registerJmx() {
156 if (pool.getJmxPool()!=null) {
157 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
158 mbs.registerMBean(pool.getJmxPool(), oname);
160 } catch (Exception e) {
161 log.error("Unable to register JDBC pool with JMX",e);
168 protected void unregisterJmx() {
170 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
171 mbs.unregisterMBean(oname);
172 } catch (InstanceNotFoundException ignore) {
174 } catch (Exception e) {
175 log.error("Unable to unregister JDBC pool with JMX",e);