    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 final PooledObject<?> pooledObject;
    034
    035    /**
    036     * Create a new instance for the given pooled object.
    037     *
    038     * @param pooledObject The pooled object that this instance will represent
    039     */
    040    public DefaultPooledObjectInfo(final PooledObject<?> pooledObject) {
    041        this.pooledObject = pooledObject;
    042    }
    043
    044    @Override
    045    public long getCreateTime() {
    046        return pooledObject.getCreateTime();
    047    }
    048
    049    @Override
    050    public String getCreateTimeFormatted() {
    051        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    052        return sdf.format(Long.valueOf(pooledObject.getCreateTime()));
    053    }
    054
    055    @Override
    056    public long getLastBorrowTime() {
    057        return pooledObject.getLastBorrowTime();
    058    }
    059
    060    @Override
    061    public String getLastBorrowTimeFormatted() {
    062        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    063        return sdf.format(Long.valueOf(pooledObject.getLastBorrowTime()));
    064    }
    065
    066    @Override
    067    public String getLastBorrowTrace() {
    068        final StringWriter sw = new StringWriter();
    069        pooledObject.printStackTrace(new PrintWriter(sw));
    070        return sw.toString();
    071    }
    072
    073    @Override
    074    public long getLastReturnTime() {
    075        return pooledObject.getLastReturnTime();
    076    }
    077
    078    @Override
    079    public String getLastReturnTimeFormatted() {
    080        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    081        return sdf.format(Long.valueOf(pooledObject.getLastReturnTime()));
    082    }
    083
    084    @Override
    085    public String getPooledObjectType() {
    086        return pooledObject.getObject().getClass().getName();
    087    }
    088
    089    @Override
    090    public String getPooledObjectToString() {
    091        return pooledObject.getObject().toString();
    092    }
    093
    094    @Override
    095    public long getBorrowedCount() {
    096        // TODO Simplify this once getBorrowedCount has been added to PooledObject
    097        if (pooledObject instanceof DefaultPooledObject) {
    098            return ((DefaultPooledObject<?>) pooledObject).getBorrowedCount();
    099        }
    100        return -1;
    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}