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