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 * https://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 package org.apache.commons.pool2.impl;
18
19 import java.util.concurrent.locks.ReentrantReadWriteLock;
20 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
21
22 import org.apache.commons.pool2.PooledObject;
23 import org.apache.commons.pool2.PooledObjectFactory;
24
25 /**
26 * Copies PoolUtil's private static final class SynchronizedPooledObjectFactory.
27 *
28 * A fully synchronized PooledObjectFactory that wraps a PooledObjectFactory and synchronizes access to the wrapped factory methods.
29 * <p>
30 * <strong>Note:</strong> This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons
31 * Pool library.
32 * </p>
33 *
34 * @param <T> Type of element managed in this factory.
35 */
36 final class TestSynchronizedPooledObjectFactory<T> implements PooledObjectFactory<T> {
37 /** Synchronization lock */
38 private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock();
39 /** Wrapped factory */
40 private final PooledObjectFactory<T> factory;
41
42 /**
43 * Constructs a SynchronizedPoolableObjectFactory wrapping the given factory.
44 *
45 * @param factory underlying factory to wrap
46 * @throws IllegalArgumentException if the factory is null
47 */
48 TestSynchronizedPooledObjectFactory(final PooledObjectFactory<T> factory) throws IllegalArgumentException {
49 if (factory == null) {
50 throw new IllegalArgumentException("factory must not be null.");
51 }
52 this.factory = factory;
53 }
54
55 /**
56 * {@inheritDoc}
57 */
58 @Override
59 public void activateObject(final PooledObject<T> p) throws Exception {
60 writeLock.lock();
61 try {
62 factory.activateObject(p);
63 } finally {
64 writeLock.unlock();
65 }
66 }
67
68 /**
69 * {@inheritDoc}
70 */
71 @Override
72 public void destroyObject(final PooledObject<T> p) throws Exception {
73 writeLock.lock();
74 try {
75 factory.destroyObject(p);
76 } finally {
77 writeLock.unlock();
78 }
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 public PooledObject<T> makeObject() throws Exception {
86 writeLock.lock();
87 try {
88 return factory.makeObject();
89 } finally {
90 writeLock.unlock();
91 }
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 @Override
98 public void passivateObject(final PooledObject<T> p) throws Exception {
99 writeLock.lock();
100 try {
101 factory.passivateObject(p);
102 } finally {
103 writeLock.unlock();
104 }
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public String toString() {
112 final StringBuilder sb = new StringBuilder();
113 sb.append("SynchronizedPoolableObjectFactory");
114 sb.append("{factory=").append(factory);
115 sb.append('}');
116 return sb.toString();
117 }
118
119 /**
120 * {@inheritDoc}
121 */
122 @Override
123 public boolean validateObject(final PooledObject<T> p) {
124 writeLock.lock();
125 try {
126 return factory.validateObject(p);
127 } finally {
128 writeLock.unlock();
129 }
130 }
131 }