View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2.impl;
18  
19  import java.io.PrintWriter;
20  import java.io.StringWriter;
21  import java.text.SimpleDateFormat;
22  import java.util.Objects;
23  
24  import org.apache.commons.pool2.PooledObject;
25  
26  /**
27   * Implementation of object that is used to provide information on pooled
28   * objects via JMX.
29   *
30   * @since 2.0
31   */
32  public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean {
33  
34      private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z";
35  
36      private final PooledObject<?> pooledObject;
37  
38      /**
39       * Constructs a new instance for the given pooled object.
40       *
41       * @param pooledObject The pooled object that this instance will represent
42       * @throws NullPointerException if {@code obj} is {@code null}
43       */
44      public DefaultPooledObjectInfo(final PooledObject<?> pooledObject) {
45          this.pooledObject = Objects.requireNonNull(pooledObject, "pooledObject");
46      }
47  
48      @Override
49      public long getBorrowedCount() {
50          return pooledObject.getBorrowedCount();
51      }
52  
53      @Override
54      public long getCreateTime() {
55          return pooledObject.getCreateInstant().toEpochMilli();
56      }
57  
58      @Override
59      public String getCreateTimeFormatted() {
60          return getTimeMillisFormatted(getCreateTime());
61      }
62  
63      @Override
64      public long getLastBorrowTime() {
65          return pooledObject.getLastBorrowInstant().toEpochMilli();
66      }
67  
68  
69      @Override
70      public String getLastBorrowTimeFormatted() {
71          return getTimeMillisFormatted(getLastBorrowTime());
72      }
73  
74      @Override
75      public String getLastBorrowTrace() {
76          final StringWriter sw = new StringWriter();
77          pooledObject.printStackTrace(new PrintWriter(sw));
78          return sw.toString();
79      }
80  
81      @Override
82      public long getLastReturnTime() {
83          return pooledObject.getLastReturnInstant().toEpochMilli();
84      }
85  
86      @Override
87      public String getLastReturnTimeFormatted() {
88          return getTimeMillisFormatted(getLastReturnTime());
89      }
90  
91      @Override
92      public String getPooledObjectToString() {
93          return Objects.toString(pooledObject.getObject(), null);
94      }
95  
96      @Override
97      public String getPooledObjectType() {
98          final Object object = pooledObject.getObject();
99          return object != null ? object.getClass().getName() : null;
100     }
101 
102     private String getTimeMillisFormatted(final long millis) {
103         return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis));
104     }
105 
106     /**
107      * @since 2.4.3
108      */
109     @Override
110     public String toString() {
111         final StringBuilder builder = new StringBuilder();
112         builder.append("DefaultPooledObjectInfo [pooledObject=");
113         builder.append(pooledObject);
114         builder.append("]");
115         return builder.toString();
116     }
117 }