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   * Implements providing 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      @Override
69      public String getLastBorrowTimeFormatted() {
70          return getTimeMillisFormatted(getLastBorrowTime());
71      }
72  
73      @Override
74      public String getLastBorrowTrace() {
75          final StringWriter sw = new StringWriter();
76          pooledObject.printStackTrace(new PrintWriter(sw));
77          return sw.toString();
78      }
79  
80      @Override
81      public long getLastReturnTime() {
82          return pooledObject.getLastReturnInstant().toEpochMilli();
83      }
84  
85      @Override
86      public String getLastReturnTimeFormatted() {
87          return getTimeMillisFormatted(getLastReturnTime());
88      }
89  
90      @Override
91      public String getPooledObjectToString() {
92          return Objects.toString(pooledObject.getObject(), null);
93      }
94  
95      @Override
96      public String getPooledObjectType() {
97          final Object object = pooledObject.getObject();
98          return object != null ? object.getClass().getName() : null;
99      }
100 
101     private String getTimeMillisFormatted(final long millis) {
102         return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis));
103     }
104 
105     /**
106      * @since 2.4.3
107      */
108     @Override
109     public String toString() {
110         final StringBuilder builder = new StringBuilder();
111         builder.append("DefaultPooledObjectInfo [pooledObject=");
112         builder.append(pooledObject);
113         builder.append("]");
114         return builder.toString();
115     }
116 }