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     */
017    
018    package org.apache.commons.pool;
019    
020    import java.util.List;
021    import java.util.ArrayList;
022    
023    /**
024     * A poolable object factory that tracks how {@link MethodCall methods are called}.
025     *
026     * @author Sandy McArthur
027     * @version $Revision: 606064 $ $Date: 2007-12-20 19:12:02 -0500 (Thu, 20 Dec 2007) $
028     * @see MethodCall
029     */
030    public class MethodCallPoolableObjectFactory implements PoolableObjectFactory {
031        private final List methodCalls = new ArrayList();
032        private int count = 0;
033        private boolean valid = true;
034        private boolean makeObjectFail;
035        private boolean activateObjectFail;
036        private boolean validateObjectFail;
037        private boolean passivateObjectFail;
038        private boolean destroyObjectFail;
039    
040        public void reset() {
041            count = 0;
042            getMethodCalls().clear();
043            setMakeObjectFail(false);
044            setActivateObjectFail(false);
045            setValid(true);
046            setValidateObjectFail(false);
047            setPassivateObjectFail(false);
048            setDestroyObjectFail(false);
049        }
050    
051        public List getMethodCalls() {
052            return methodCalls;
053        }
054    
055        public int getCurrentCount() {
056            return count;
057        }
058    
059        public void setCurrentCount(final int count) {
060            this.count = count;
061        }
062    
063        public boolean isMakeObjectFail() {
064            return makeObjectFail;
065        }
066    
067        public void setMakeObjectFail(final boolean makeObjectFail) {
068            this.makeObjectFail = makeObjectFail;
069        }
070    
071        public boolean isDestroyObjectFail() {
072            return destroyObjectFail;
073        }
074    
075        public void setDestroyObjectFail(final boolean destroyObjectFail) {
076            this.destroyObjectFail = destroyObjectFail;
077        }
078    
079        public boolean isValid() {
080            return valid;
081        }
082    
083        public void setValid(final boolean valid) {
084            this.valid = valid;
085        }
086    
087        public boolean isValidateObjectFail() {
088            return validateObjectFail;
089        }
090    
091        public void setValidateObjectFail(final boolean validateObjectFail) {
092            this.validateObjectFail = validateObjectFail;
093        }
094    
095        public boolean isActivateObjectFail() {
096            return activateObjectFail;
097        }
098    
099        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    }