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; 022 023import org.apache.commons.pool2.PooledObject; 024 025/** 026 * Implementation of object that is used to provide information on pooled 027 * objects via JMX. 028 * 029 * @since 2.0 030 */ 031public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { 032 033 private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z"; 034 035 private final PooledObject<?> pooledObject; 036 037 /** 038 * Constructs a new instance for the given pooled object. 039 * 040 * @param pooledObject The pooled object that this instance will represent 041 */ 042 public DefaultPooledObjectInfo(final PooledObject<?> pooledObject) { 043 this.pooledObject = pooledObject; 044 } 045 046 @Override 047 public long getBorrowedCount() { 048 return pooledObject.getBorrowedCount(); 049 } 050 051 @Override 052 public long getCreateTime() { 053 return pooledObject.getCreateInstant().toEpochMilli(); 054 } 055 056 @Override 057 public String getCreateTimeFormatted() { 058 return getTimeFormatted(getCreateTime()); 059 } 060 061 @Override 062 public long getLastBorrowTime() { 063 return pooledObject.getLastBorrowInstant().toEpochMilli(); 064 } 065 066 067 @Override 068 public String getLastBorrowTimeFormatted() { 069 return getTimeFormatted(getLastBorrowTime()); 070 } 071 072 @Override 073 public String getLastBorrowTrace() { 074 final StringWriter sw = new StringWriter(); 075 pooledObject.printStackTrace(new PrintWriter(sw)); 076 return sw.toString(); 077 } 078 079 @Override 080 public long getLastReturnTime() { 081 return pooledObject.getLastReturnInstant().toEpochMilli(); 082 } 083 084 @Override 085 public String getLastReturnTimeFormatted() { 086 return getTimeFormatted(getLastReturnTime()); 087 } 088 089 @Override 090 public String getPooledObjectToString() { 091 return pooledObject.getObject().toString(); 092 } 093 094 @Override 095 public String getPooledObjectType() { 096 return pooledObject.getObject().getClass().getName(); 097 } 098 099 private String getTimeFormatted(final long millis) { 100 return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis)); 101 } 102 103 /** 104 * @since 2.4.3 105 */ 106 @Override 107 public String toString() { 108 final StringBuilder builder = new StringBuilder(); 109 builder.append("DefaultPooledObjectInfo [pooledObject="); 110 builder.append(pooledObject); 111 builder.append("]"); 112 return builder.toString(); 113 } 114}