DefaultPooledObjectInfo.java

  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. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Objects;

  22. import org.apache.commons.pool2.PooledObject;

  23. /**
  24.  * Implements providing information on pooled
  25.  * objects via JMX.
  26.  *
  27.  * @since 2.0
  28.  */
  29. public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean {

  30.     private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z";

  31.     private final PooledObject<?> pooledObject;

  32.     /**
  33.      * Constructs a new instance for the given pooled object.
  34.      *
  35.      * @param pooledObject The pooled object that this instance will represent
  36.      * @throws NullPointerException if {@code obj} is {@code null}
  37.      */
  38.     public DefaultPooledObjectInfo(final PooledObject<?> pooledObject) {
  39.         this.pooledObject = Objects.requireNonNull(pooledObject, "pooledObject");
  40.     }

  41.     @Override
  42.     public long getBorrowedCount() {
  43.         return pooledObject.getBorrowedCount();
  44.     }

  45.     @Override
  46.     public long getCreateTime() {
  47.         return pooledObject.getCreateInstant().toEpochMilli();
  48.     }

  49.     @Override
  50.     public String getCreateTimeFormatted() {
  51.         return getTimeMillisFormatted(getCreateTime());
  52.     }

  53.     @Override
  54.     public long getLastBorrowTime() {
  55.         return pooledObject.getLastBorrowInstant().toEpochMilli();
  56.     }

  57.     @Override
  58.     public String getLastBorrowTimeFormatted() {
  59.         return getTimeMillisFormatted(getLastBorrowTime());
  60.     }

  61.     @Override
  62.     public String getLastBorrowTrace() {
  63.         final StringWriter sw = new StringWriter();
  64.         pooledObject.printStackTrace(new PrintWriter(sw));
  65.         return sw.toString();
  66.     }

  67.     @Override
  68.     public long getLastReturnTime() {
  69.         return pooledObject.getLastReturnInstant().toEpochMilli();
  70.     }

  71.     @Override
  72.     public String getLastReturnTimeFormatted() {
  73.         return getTimeMillisFormatted(getLastReturnTime());
  74.     }

  75.     @Override
  76.     public String getPooledObjectToString() {
  77.         return Objects.toString(pooledObject.getObject(), null);
  78.     }

  79.     @Override
  80.     public String getPooledObjectType() {
  81.         final Object object = pooledObject.getObject();
  82.         return object != null ? object.getClass().getName() : null;
  83.     }

  84.     private String getTimeMillisFormatted(final long millis) {
  85.         return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis));
  86.     }

  87.     /**
  88.      * @since 2.4.3
  89.      */
  90.     @Override
  91.     public String toString() {
  92.         final StringBuilder builder = new StringBuilder();
  93.         builder.append("DefaultPooledObjectInfo [pooledObject=");
  94.         builder.append(pooledObject);
  95.         builder.append("]");
  96.         return builder.toString();
  97.     }
  98. }