    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 *      https://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 * Implements providing 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    @Override
    069    public String getLastBorrowTimeFormatted() {
    070        return getTimeMillisFormatted(getLastBorrowTime());
    071    }
    072
    073    @Override
    074    public String getLastBorrowTrace() {
    075        final StringWriter sw = new StringWriter();
    076        pooledObject.printStackTrace(new PrintWriter(sw));
    077        return sw.toString();
    078    }
    079
    080    @Override
    081    public long getLastReturnTime() {
    082        return pooledObject.getLastReturnInstant().toEpochMilli();
    083    }
    084
    085    @Override
    086    public String getLastReturnTimeFormatted() {
    087        return getTimeMillisFormatted(getLastReturnTime());
    088    }
    089
    090    @Override
    091    public String getPooledObjectToString() {
    092        return Objects.toString(pooledObject.getObject(), null);
    093    }
    094
    095    @Override
    096    public String getPooledObjectType() {
    097        final Object object = pooledObject.getObject();
    098        return object != null ? object.getClass().getName() : null;
    099    }
    100
    101    private String getTimeMillisFormatted(final long millis) {
    102        return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis));
    103    }
    104
    105    /**
    106     * @since 2.4.3
    107     */
    108    @Override
    109    public String toString() {
    110        final StringBuilder builder = new StringBuilder();
    111        builder.append("DefaultPooledObjectInfo [pooledObject=");
    112        builder.append(pooledObject);
    113        builder.append("]");
    114        return builder.toString();
    115    }
    116}