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;
018
019/**
020 * A base implementation of <code>PoolableObjectFactory</code>.
021 * <p>
022 * All operations defined here are essentially no-op's.
023 * <p>
024 * This class is immutable, and therefore thread-safe
025 *
026 * @param <T> Type of element managed in this factory.
027 *
028 * @see PooledObjectFactory
029 * @see BaseKeyedPooledObjectFactory
030 *
031 * @since 2.0
032 */
033public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {
034    /**
035     * Creates an object instance, to be wrapped in a {@link PooledObject}.
036     * <p>This method <strong>must</strong> support concurrent, multi-threaded
037     * activation.</p>
038     *
039     * @return an instance to be served by the pool
040     *
041     * @throws Exception if there is a problem creating a new instance,
042     *    this will be propagated to the code requesting an object.
043     */
044    public abstract T create() throws Exception;
045
046    /**
047     * Wrap the provided instance with an implementation of
048     * {@link PooledObject}.
049     *
050     * @param obj the instance to wrap
051     *
052     * @return The provided instance, wrapped by a {@link PooledObject}
053     */
054    public abstract PooledObject<T> wrap(T obj);
055
056    @Override
057    public PooledObject<T> makeObject() throws Exception {
058        return wrap(create());
059    }
060
061    /**
062     *  No-op.
063     *
064     *  @param p ignored
065     */
066    @Override
067    public void destroyObject(final PooledObject<T> p)
068        throws Exception  {
069        // The default implementation is a no-op.
070    }
071
072    /**
073     * This implementation always returns {@code true}.
074     *
075     * @param p ignored
076     *
077     * @return {@code true}
078     */
079    @Override
080    public boolean validateObject(final PooledObject<T> p) {
081        return true;
082    }
083
084    /**
085     *  No-op.
086     *
087     *  @param p ignored
088     */
089    @Override
090    public void activateObject(final PooledObject<T> p) throws Exception {
091        // The default implementation is a no-op.
092    }
093
094    /**
095     *  No-op.
096     *
097     * @param p ignored
098     */
099    @Override
100    public void passivateObject(final PooledObject<T> p)
101        throws Exception {
102        // The default implementation is a no-op.
103    }
104}