    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 getBorrowedCount() {
    046        return pooledObject.getBorrowedCount();
    047    }
    048
    049    @Override
    050    public long getCreateTime() {
    051        return pooledObject.getCreateTime();
    052    }
    053
    054    @Override
    055    public String getCreateTimeFormatted() {
    056        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    057        return sdf.format(Long.valueOf(pooledObject.getCreateTime()));
    058    }
    059
    060    @Override
    061    public long getLastBorrowTime() {
    062        return pooledObject.getLastBorrowTime();
    063    }
    064
    065    @Override
    066    public String getLastBorrowTimeFormatted() {
    067        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    068        return sdf.format(Long.valueOf(pooledObject.getLastBorrowTime()));
    069    }
    070
    071    @Override
    072    public String getLastBorrowTrace() {
    073        final StringWriter sw = new StringWriter();
    074        pooledObject.printStackTrace(new PrintWriter(sw));
    075        return sw.toString();
    076    }
    077
    078    @Override
    079    public long getLastReturnTime() {
    080        return pooledObject.getLastReturnTime();
    081    }
    082
    083    @Override
    084    public String getLastReturnTimeFormatted() {
    085        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    086        return sdf.format(Long.valueOf(pooledObject.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    /**
    100     * @since 2.4.3
    101     */
    102    @Override
    103    public String toString() {
    104        final StringBuilder builder = new StringBuilder();
    105        builder.append("DefaultPooledObjectInfo [pooledObject=");
    106        builder.append(pooledObject);
    107        builder.append("]");
    108        return builder.toString();
    109    }
    110}