View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pool2.impl;
19  
20  import java.util.concurrent.locks.ReentrantReadWriteLock;
21  import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
22  
23  import org.apache.commons.pool2.PooledObject;
24  import org.apache.commons.pool2.PooledObjectFactory;
25  
26  /**
27   * Copies PoolUtil's private static class SynchronizedPooledObjectFactory.
28   *
29   * A fully synchronized PooledObjectFactory that wraps a PooledObjectFactory and
30   * synchronizes access to the wrapped factory methods.
31   * <p>
32   * <b>Note:</b> This should not be used on pool implementations that already
33   * provide proper synchronization such as the pools provided in the Commons Pool
34   * library.
35   * </p>
36   */
37  final class TestSynchronizedPooledObjectFactory<T> implements PooledObjectFactory<T> {
38  
39  	/** Synchronization lock */
40  	private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock();
41  
42  	/** Wrapped factory */
43  	private final PooledObjectFactory<T> factory;
44  
45  	/**
46  	 * Constructs a SynchronizedPoolableObjectFactory wrapping the given factory.
47  	 *
48  	 * @param factory
49  	 *            underlying factory to wrap
50  	 * @throws IllegalArgumentException
51  	 *             if the factory is null
52  	 */
53  	TestSynchronizedPooledObjectFactory(final PooledObjectFactory<T> factory) throws IllegalArgumentException {
54  		if (factory == null) {
55  			throw new IllegalArgumentException("factory must not be null.");
56  		}
57  		this.factory = factory;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@Override
64  	public void activateObject(final PooledObject<T> p) throws Exception {
65  		writeLock.lock();
66  		try {
67  			factory.activateObject(p);
68  		} finally {
69  			writeLock.unlock();
70  		}
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	@Override
77  	public void destroyObject(final PooledObject<T> p) throws Exception {
78  		writeLock.lock();
79  		try {
80  			factory.destroyObject(p);
81  		} finally {
82  			writeLock.unlock();
83  		}
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	@Override
90  	public PooledObject<T> makeObject() throws Exception {
91  		writeLock.lock();
92  		try {
93  			return factory.makeObject();
94  		} finally {
95  			writeLock.unlock();
96  		}
97  	}
98  
99  	/**
100 	 * {@inheritDoc}
101 	 */
102 	@Override
103 	public void passivateObject(final PooledObject<T> p) throws Exception {
104 		writeLock.lock();
105 		try {
106 			factory.passivateObject(p);
107 		} finally {
108 			writeLock.unlock();
109 		}
110 	}
111 
112 	/**
113 	 * {@inheritDoc}
114 	 */
115 	@Override
116 	public String toString() {
117 		final StringBuilder sb = new StringBuilder();
118 		sb.append("SynchronizedPoolableObjectFactory");
119 		sb.append("{factory=").append(factory);
120 		sb.append('}');
121 		return sb.toString();
122 	}
123 
124 	/**
125 	 * {@inheritDoc}
126 	 */
127 	@Override
128 	public boolean validateObject(final PooledObject<T> p) {
129 		writeLock.lock();
130 		try {
131 			return factory.validateObject(p);
132 		} finally {
133 			writeLock.unlock();
134 		}
135 	}
136 }