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;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.pool2.impl.DefaultPooledObject;
24  
25  /**
26   * A poolable object factory that tracks how {@link MethodCall methods are called}.
27   *
28   * @see MethodCall
29   */
30  public class MethodCallPoolableObjectFactory implements PooledObjectFactory<Object> {
31      private final List<MethodCall> methodCalls = new ArrayList<>();
32      private int count;
33      private boolean valid = true;
34      private boolean makeObjectFail;
35      private boolean activateObjectFail;
36      private boolean validateObjectFail;
37      private boolean passivateObjectFail;
38      private boolean destroyObjectFail;
39  
40      @Override
41      public void activateObject(final PooledObject<Object> obj) {
42          methodCalls.add(new MethodCall("activateObject", obj.getObject()));
43          if (activateObjectFail) {
44              throw new PrivateException("activateObject");
45          }
46      }
47  
48      @Override
49      public void destroyObject(final PooledObject<Object> obj) {
50          methodCalls.add(new MethodCall("destroyObject", obj.getObject()));
51          if (destroyObjectFail) {
52              throw new PrivateException("destroyObject");
53          }
54      }
55  
56      public int getCurrentCount() {
57          return count;
58      }
59  
60      public List<MethodCall> getMethodCalls() {
61          return methodCalls;
62      }
63  
64      public boolean isActivateObjectFail() {
65          return activateObjectFail;
66      }
67  
68      public boolean isDestroyObjectFail() {
69          return destroyObjectFail;
70      }
71  
72      public boolean isMakeObjectFail() {
73          return makeObjectFail;
74      }
75  
76      public boolean isPassivateObjectFail() {
77          return passivateObjectFail;
78      }
79  
80      public boolean isValid() {
81          return valid;
82      }
83  
84      public boolean isValidateObjectFail() {
85          return validateObjectFail;
86      }
87  
88      @Override
89      public PooledObject<Object> makeObject() {
90          final MethodCall call = new MethodCall("makeObject");
91          methodCalls.add(call);
92          final int originalCount = this.count++;
93          if (makeObjectFail) {
94              throw new PrivateException("makeObject");
95          }
96          // Generate new object, don't use cache via Integer.valueOf(...)
97          final Integer obj = Integer.valueOf(originalCount);
98          call.setReturned(obj);
99          return new DefaultPooledObject<>(obj);
100     }
101 
102     @Override
103     public void passivateObject(final PooledObject<Object> obj) {
104         methodCalls.add(new MethodCall("passivateObject", obj.getObject()));
105         if (passivateObjectFail) {
106             throw new PrivateException("passivateObject");
107         }
108     }
109 
110     public void reset() {
111         count = 0;
112         getMethodCalls().clear();
113         setMakeObjectFail(false);
114         setActivateObjectFail(false);
115         setValid(true);
116         setValidateObjectFail(false);
117         setPassivateObjectFail(false);
118         setDestroyObjectFail(false);
119     }
120 
121     public void setActivateObjectFail(final boolean activateObjectFail) {
122         this.activateObjectFail = activateObjectFail;
123     }
124 
125     public void setCurrentCount(final int count) {
126         this.count = count;
127     }
128 
129     public void setDestroyObjectFail(final boolean destroyObjectFail) {
130         this.destroyObjectFail = destroyObjectFail;
131     }
132 
133     public void setMakeObjectFail(final boolean makeObjectFail) {
134         this.makeObjectFail = makeObjectFail;
135     }
136 
137     public void setPassivateObjectFail(final boolean passivateObjectFail) {
138         this.passivateObjectFail = passivateObjectFail;
139     }
140 
141     public void setValid(final boolean valid) {
142         this.valid = valid;
143     }
144 
145     public void setValidateObjectFail(final boolean validateObjectFail) {
146         this.validateObjectFail = validateObjectFail;
147     }
148 
149     @Override
150     public boolean validateObject(final PooledObject<Object> obj) {
151         final MethodCall call = new MethodCall("validateObject", obj.getObject());
152         methodCalls.add(call);
153         if (validateObjectFail) {
154             throw new PrivateException("validateObject");
155         }
156         final boolean r = valid;
157         call.returned(Boolean.valueOf(r));
158         return r;
159     }
160 }