001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.pool2.impl; 018 019import java.io.PrintWriter; 020import java.io.StringWriter; 021import java.text.SimpleDateFormat; 022import java.util.Objects; 023 024import org.apache.commons.pool2.PooledObject; 025 026/** 027 * Implementation of object that is used to provide information on pooled 028 * objects via JMX. 029 * 030 * @since 2.0 031 */ 032public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { 033 034 private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z"; 035 036 private final PooledObject<?> pooledObject; 037 038 /** 039 * Constructs a new instance for the given pooled object. 040 * 041 * @param pooledObject The pooled object that this instance will represent 042 * @throws NullPointerException if {@code obj} is {@code null} 043 */ 044 public DefaultPooledObjectInfo(final PooledObject<?> pooledObject) { 045 this.pooledObject = Objects.requireNonNull(pooledObject, "pooledObject"); 046 } 047 048 @Override 049 public long getBorrowedCount() { 050 return pooledObject.getBorrowedCount(); 051 } 052 053 @Override 054 public long getCreateTime() { 055 return pooledObject.getCreateInstant().toEpochMilli(); 056 } 057 058 @Override 059 public String getCreateTimeFormatted() { 060 return getTimeMillisFormatted(getCreateTime()); 061 } 062 063 @Override 064 public long getLastBorrowTime() { 065 return pooledObject.getLastBorrowInstant().toEpochMilli(); 066 } 067 068 069 @Override 070 public String getLastBorrowTimeFormatted() { 071 return getTimeMillisFormatted(getLastBorrowTime()); 072 } 073 074 @Override 075 public String getLastBorrowTrace() { 076 final StringWriter sw = new StringWriter(); 077 pooledObject.printStackTrace(new PrintWriter(sw)); 078 return sw.toString(); 079 } 080 081 @Override 082 public long getLastReturnTime() { 083 return pooledObject.getLastReturnInstant().toEpochMilli(); 084 } 085 086 @Override 087 public String getLastReturnTimeFormatted() { 088 return getTimeMillisFormatted(getLastReturnTime()); 089 } 090 091 @Override 092 public String getPooledObjectToString() { 093 return Objects.toString(pooledObject.getObject(), null); 094 } 095 096 @Override 097 public String getPooledObjectType() { 098 final Object object = pooledObject.getObject(); 099 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}