001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 *     Julien Carsique
019 */
020package org.nuxeo.runtime.jtajca;
021
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.runtime.jtajca.NuxeoValidationSupport.Validation;
026
027/**
028 * Descriptor of the pool configuration, used by NuxeoContainer when creating a pool directly instead of the previous
029 * way that was using a JNDI factory (NuxeoConnectionManagerFactory).
030 *
031 * @since 5.6
032 */
033@XObject("pool")
034public class NuxeoConnectionManagerConfiguration {
035
036    public static final int DEFAULT_MAX_POOL_SIZE = 20;
037
038    public static final int DEFAULT_MIN_POOL_SIZE = 0;
039
040    public static final int DEFAULT_BLOCKING_TIMEOUT_MILLIS = 100;
041
042    public static final int DEFAULT_IDLE_TIMEOUT_MINUTES = 0; // no timeout
043
044    public static final int DEFAULT_ACTIVE_TIMEOUT_MINUTES = 0; // no timeout
045
046    @XNode("@name")
047    private String name = "NuxeoConnectionManager";
048
049    // transaction
050    @XNode("@xaMode")
051    private Boolean xaMode;
052
053    @XNode("@useTransactionCaching")
054    private Boolean useTransactionCaching;
055
056    @XNode("@useThreadCaching")
057    private Boolean useThreadCaching;
058
059    // pool
060
061    @XNode("@matchOne")
062    private Boolean matchOne; // unused by Geronimo?
063
064    @XNode("@matchAll")
065    private Boolean matchAll;
066
067    @XNode("@selectOneNoMatch")
068    private Boolean selectOneNoMatch;
069
070    @XNode("@maxPoolSize")
071    private Integer maxPoolSize;
072
073    @XNode("@minPoolSize")
074    private Integer minPoolSize;
075
076    @XNode("@blockingTimeoutMillis")
077    private Integer blockingTimeoutMillis;
078
079    @XNode("@idleTimeoutMinutes")
080    private Integer idleTimeoutMinutes;
081
082    @XNode("@activeTimeoutMinutes")
083    private Integer activeTimeoutMinutes;
084
085    Validation testOnBorrow;
086
087    Validation testOnReturn;
088
089    public NuxeoConnectionManagerConfiguration() {
090    }
091
092    /** Copy constructor. */
093    public NuxeoConnectionManagerConfiguration(NuxeoConnectionManagerConfiguration other) {
094        name = other.name;
095        xaMode = other.xaMode;
096        useTransactionCaching = other.useTransactionCaching;
097        useThreadCaching = other.useThreadCaching;
098        matchOne = other.matchOne;
099        matchAll = other.matchAll;
100        selectOneNoMatch = other.selectOneNoMatch;
101        maxPoolSize = other.maxPoolSize;
102        minPoolSize = other.minPoolSize;
103        blockingTimeoutMillis = other.blockingTimeoutMillis;
104        idleTimeoutMinutes = other.idleTimeoutMinutes;
105        activeTimeoutMinutes = other.activeTimeoutMinutes;
106        testOnBorrow = other.testOnBorrow;
107        testOnReturn = other.testOnReturn;
108    }
109
110    public void merge(NuxeoConnectionManagerConfiguration other) {
111        if (other.name != null) {
112            name = other.name;
113        }
114        if (other.xaMode != null) {
115            xaMode = other.xaMode;
116        }
117        if (other.useTransactionCaching != null) {
118            useTransactionCaching = other.useTransactionCaching;
119        }
120        if (other.useThreadCaching != null) {
121            useThreadCaching = other.useThreadCaching;
122        }
123        if (other.matchOne != null) {
124            matchOne = other.matchOne;
125        }
126        if (other.matchAll != null) {
127            matchAll = other.matchAll;
128        }
129        if (other.selectOneNoMatch != null) {
130            selectOneNoMatch = other.selectOneNoMatch;
131        }
132        if (other.maxPoolSize != null) {
133            maxPoolSize = other.maxPoolSize;
134        }
135        if (other.minPoolSize != null) {
136            minPoolSize = other.minPoolSize;
137        }
138        if (other.blockingTimeoutMillis != null) {
139            blockingTimeoutMillis = other.blockingTimeoutMillis;
140        }
141        if (other.idleTimeoutMinutes != null) {
142            idleTimeoutMinutes = other.idleTimeoutMinutes;
143        }
144        if (other.activeTimeoutMinutes != null) {
145            activeTimeoutMinutes = other.activeTimeoutMinutes;
146        }
147        if (other.testOnBorrow != null) {
148            testOnBorrow = other.testOnBorrow;
149        }
150        if (other.testOnReturn != null) {
151            testOnReturn = other.testOnReturn;
152        }
153    }
154
155    /** False if the boolean is null or FALSE, true otherwise. */
156    private static boolean defaultFalse(Boolean bool) {
157        return Boolean.TRUE.equals(bool);
158    }
159
160    /** True if the boolean is null or TRUE, false otherwise. */
161    private static boolean defaultTrue(Boolean bool) {
162        return !Boolean.FALSE.equals(bool);
163    }
164
165    private static int defaultInt(Integer value, int def) {
166        return value == null ? def : value.intValue();
167    }
168
169    public String getName() {
170        return name;
171    }
172
173    public boolean getXAMode() {
174        return defaultTrue(xaMode);
175    }
176
177    public boolean getUseTransactionCaching() {
178        return defaultTrue(useTransactionCaching);
179    }
180
181    public boolean getUseThreadCaching() {
182        return defaultTrue(useThreadCaching);
183    }
184
185    public boolean getMatchOne() {
186        return defaultTrue(matchOne);
187    }
188
189    public boolean getMatchAll() {
190        return defaultTrue(matchAll);
191    }
192
193    public boolean getSelectOneNoMatch() {
194        return defaultFalse(selectOneNoMatch);
195    }
196
197    public int getMaxPoolSize() {
198        return defaultInt(maxPoolSize, DEFAULT_MAX_POOL_SIZE);
199    }
200
201    public int getMinPoolSize() {
202        return defaultInt(minPoolSize, DEFAULT_MIN_POOL_SIZE);
203    }
204
205    public int getBlockingTimeoutMillis() {
206        return defaultInt(blockingTimeoutMillis, DEFAULT_BLOCKING_TIMEOUT_MILLIS);
207    }
208
209    public int getIdleTimeoutMinutes() {
210        return defaultInt(idleTimeoutMinutes, DEFAULT_IDLE_TIMEOUT_MINUTES);
211    }
212
213    public int getActiveTimeoutMinutes() {
214        return defaultInt(activeTimeoutMinutes, DEFAULT_ACTIVE_TIMEOUT_MINUTES);
215    }
216
217    public void setName(String name) {
218        this.name = name;
219    }
220
221    public void setXAMode(boolean xaMode) {
222        this.xaMode = Boolean.valueOf(xaMode);
223    }
224
225    public void setUseTransactionCaching(boolean useTransactionCaching) {
226        this.useTransactionCaching = Boolean.valueOf(useTransactionCaching);
227    }
228
229    public void setUseThreadCaching(boolean useThreadCaching) {
230        this.useThreadCaching = Boolean.valueOf(useThreadCaching);
231    }
232
233    public void setMatchOne(boolean matchOne) {
234        this.matchOne = Boolean.valueOf(matchOne);
235    }
236
237    public void setMatchAll(boolean matchAll) {
238        this.matchAll = Boolean.valueOf(matchAll);
239    }
240
241    public void setSelectOneNoMatch(boolean selectOneNoMatch) {
242        this.selectOneNoMatch = Boolean.valueOf(selectOneNoMatch);
243    }
244
245    public void setMaxPoolSize(int maxPoolSize) {
246        this.maxPoolSize = Integer.valueOf(maxPoolSize);
247    }
248
249    public void setMinPoolSize(int minPoolSize) {
250        this.minPoolSize = Integer.valueOf(minPoolSize);
251    }
252
253    public void setBlockingTimeoutMillis(int blockingTimeoutMillis) {
254        this.blockingTimeoutMillis = Integer.valueOf(blockingTimeoutMillis);
255    }
256
257    public void setIdleTimeoutMinutes(int idleTimeoutMinutes) {
258        this.idleTimeoutMinutes = Integer.valueOf(idleTimeoutMinutes);
259    }
260
261    public void setActiveTimeoutMinutes(int activeTimeoutMinutes) {
262        this.activeTimeoutMinutes = Integer.valueOf(activeTimeoutMinutes);
263    }
264
265    @XNode("@validationQuery")
266    public void setValidationQuery(String sql) {
267        if (sql.isEmpty()) {
268            testOnBorrow = null;
269        } else {
270            testOnBorrow = new NuxeoValidationSupport.QuerySQLConnection(sql);
271        }
272    }
273
274
275    @XNode("@testOnBorrow")
276    public void setTestOnBorrow(Class<? extends Validation> typeof) throws ReflectiveOperationException {
277        testOnBorrow = typeof.newInstance();
278    }
279
280    @XNode("@testOnReturn")
281    public void setTestOnReturn(Class<? extends Validation> typeof) throws ReflectiveOperationException {
282        testOnReturn = typeof.newInstance();
283    }
284
285    @XNode("@maxActive")
286    public void setMaxActive(int num) {
287        maxPoolSize = num;
288        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
289                "maxActive deprecated dbcp pool attribute usage, should use maxPoolSize geronimo pool attribute instead");
290    }
291
292    @XNode("@maxIdle")
293    public void setMaxIdle(int num) {
294        minPoolSize = num;
295        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
296                "maxIdle deprecated dbcp pool attribute usage, should use minPoolSize geronimo pool attribute instead");
297    }
298
299    @XNode("@maxWait")
300    public void setMaxWait(int num) {
301        blockingTimeoutMillis = num;
302        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
303                "maxWait deprecated dbcp pool attribute usage, should use blockingTimeoutMillis geronimo pool attribute instead");
304
305    }
306
307
308}