ed0e9dbc41efb7b9825a0205f7e80f57bd72b0ae
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / DisposableConnectionFacade.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.reflect.Method;
40 import java.lang.reflect.Proxy;
41 import java.sql.SQLException;
42
43 /**
44  * A DisposableConnectionFacade object is the top most interceptor that wraps an
45  * object of type {@link PooledConnection}. The ProxyCutOffConnection intercepts
46  * two methods:
47  * <ul>
48  *   <li>{@link java.sql.Connection#close()} - returns the connection to the
49  *       pool then breaks the link between cutoff and the next interceptor.
50  *       May be called multiple times.</li>
51  *   <li>{@link java.lang.Object#toString()} - returns a custom string for this
52  *       object</li>
53  * </ul>
54  * By default method comparisons is done on a String reference level, unless the
55  * {@link PoolConfiguration#setUseEquals(boolean)} has been called with a
56  * <code>true</code> argument.
57  */
58 public class DisposableConnectionFacade extends JdbcInterceptor {
59     protected DisposableConnectionFacade(JdbcInterceptor interceptor) {
60         setUseEquals(interceptor.isUseEquals());
61         setNext(interceptor);
62     }
63
64     @Override
65     public void reset(ConnectionPool parent, PooledConnection con) {
66     }
67
68
69
70     @Override
71     public int hashCode() {
72         return System.identityHashCode(this);
73     }
74
75     @Override
76     public boolean equals(Object obj) {
77         return this==obj;
78     }
79
80     @Override
81     public Object invoke(Object proxy, Method method, Object[] args)
82             throws Throwable {
83         if (compare(EQUALS_VAL, method)) {
84             return Boolean.valueOf(
85                     this.equals(Proxy.getInvocationHandler(args[0])));
86         } else if (compare(HASHCODE_VAL, method)) {
87             return Integer.valueOf(this.hashCode());
88         } else if (getNext()==null) {
89             if (compare(ISCLOSED_VAL, method)) {
90                 return Boolean.TRUE;
91             }
92             else if (compare(CLOSE_VAL, method)) {
93                 return null;
94             }
95             else if (compare(ISVALID_VAL, method)) {
96                 return Boolean.FALSE;
97             }
98         }
99
100         try {
101             return super.invoke(proxy, method, args);
102         } catch (NullPointerException e) {
103             if (getNext() == null) {
104                 if (compare(TOSTRING_VAL, method)) {
105                     return "DisposableConnectionFacade[null]";
106                 }
107                 throw new SQLException(
108                         "PooledConnection has already been closed.");
109             }
110
111             throw e;
112         } finally {
113             if (compare(CLOSE_VAL, method)) {
114                 setNext(null);
115             }
116         }
117     }
118 }