4922bf068558dbf7921c712e5880e0ccfe38a791
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / DataSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /*
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
28  *
29  *      http://www.apache.org/licenses/LICENSE-2.0
30  *
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.
36  */
37 package org.apache.tomcat.jdbc.pool;
38
39 import java.lang.management.ManagementFactory;
40 import java.util.Hashtable;
41
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;
47
48 import org.apache.juli.logging.Log;
49 import org.apache.juli.logging.LogFactory;
50
51
52 /**
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.
56  * @version 1.0
57  */
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);
60
61     /**
62      * Constructor for reflection only. A default set of pool properties will be created.
63      */
64     public DataSource() {
65         super();
66     }
67
68     /**
69      * Constructs a DataSource object wrapping a connection
70      * @param poolProperties The pool properties
71      */
72     public DataSource(PoolConfiguration poolProperties) {
73         super(poolProperties);
74     }
75
76
77
78
79
80 //===============================================================================
81 //  JMX Operations - Register the actual pool itself under the tomcat.jdbc domain
82 //===============================================================================
83     protected volatile ObjectName oname = null;
84
85     /**
86      * Unregisters the underlying connection pool mbean.<br>
87      * {@inheritDoc}
88      */
89     @Override
90     public void postDeregister() {
91         if (oname!=null) unregisterJmx();
92     }
93
94     /**
95      * no-op<br>
96      * {@inheritDoc}
97      */
98     @Override
99     public void postRegister(Boolean registrationDone) {
100         // NOOP
101     }
102
103
104     /**
105      * no-op<br>
106      * {@inheritDoc}
107      */
108     @Override
109     public void preDeregister() throws Exception {
110         // NOOP
111     }
112
113     /**
114      * If the connection pool MBean exists, it will be registered during this operation.<br>
115      * {@inheritDoc}
116      */
117     @Override
118     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
119         try {
120             if ( isJmxEnabled() ) {
121                 this.oname = createObjectName(name);
122                 if (oname!=null) registerJmx();
123             }
124         }catch (MalformedObjectNameException x) {
125             log.error("Unable to create object name for JDBC pool.",x);
126         }
127         return name;
128     }
129
130     /**
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
135      */
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);
146         }
147         ObjectName name = new ObjectName(domain,properties);
148         return name;
149     }
150
151     /**
152      * Registers the ConnectionPoolMBean under a unique name based on the ObjectName for the DataSource
153      */
154     protected void registerJmx() {
155         try {
156             if (pool.getJmxPool()!=null) {
157                 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
158                 mbs.registerMBean(pool.getJmxPool(), oname);
159             }
160         } catch (Exception e) {
161             log.error("Unable to register JDBC pool with JMX",e);
162         }
163     }
164
165     /**
166      *
167      */
168     protected void unregisterJmx() {
169         try {
170             MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
171             mbs.unregisterMBean(oname);
172         } catch (InstanceNotFoundException ignore) {
173             // NOOP
174         } catch (Exception e) {
175             log.error("Unable to unregister JDBC pool with JMX",e);
176         }
177     }
178
179
180 }