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    @XNode("@name")
045    private String name = "NuxeoConnectionManager";
046
047    // transaction
048    @XNode("@xaMode")
049    private Boolean xaMode;
050
051    @XNode("@useTransactionCaching")
052    private Boolean useTransactionCaching;
053
054    @XNode("@useThreadCaching")
055    private Boolean useThreadCaching;
056
057    // pool
058
059    @XNode("@matchOne")
060    private Boolean matchOne; // unused by Geronimo?
061
062    @XNode("@matchAll")
063    private Boolean matchAll;
064
065    @XNode("@selectOneNoMatch")
066    private Boolean selectOneNoMatch;
067
068    @XNode("@maxPoolSize")
069    private Integer maxPoolSize;
070
071    @XNode("@minPoolSize")
072    private Integer minPoolSize;
073
074    @XNode("@blockingTimeoutMillis")
075    private Integer blockingTimeoutMillis;
076
077    @XNode("@idleTimeoutMinutes")
078    private Integer idleTimeoutMinutes;
079
080    Validation testOnBorrow;
081
082    Validation testOnReturn;
083
084    public NuxeoConnectionManagerConfiguration() {
085    }
086
087    /** Copy constructor. */
088    public NuxeoConnectionManagerConfiguration(NuxeoConnectionManagerConfiguration other) {
089        name = other.name;
090        useTransactionCaching = other.useTransactionCaching;
091        useThreadCaching = other.useThreadCaching;
092        matchOne = other.matchOne;
093        matchAll = other.matchAll;
094        selectOneNoMatch = other.selectOneNoMatch;
095        maxPoolSize = other.maxPoolSize;
096        minPoolSize = other.minPoolSize;
097        blockingTimeoutMillis = other.blockingTimeoutMillis;
098        idleTimeoutMinutes = other.idleTimeoutMinutes;
099        testOnBorrow = other.testOnBorrow;
100        testOnReturn = other.testOnReturn;
101    }
102
103    public void merge(NuxeoConnectionManagerConfiguration other) {
104        if (other.name != null) {
105            name = other.name;
106        }
107        if (other.xaMode) {
108            xaMode = other.xaMode;
109        }
110        if (other.useTransactionCaching != null) {
111            useTransactionCaching = other.useTransactionCaching;
112        }
113        if (other.useThreadCaching != null) {
114            useThreadCaching = other.useThreadCaching;
115        }
116        if (other.matchOne != null) {
117            matchOne = other.matchOne;
118        }
119        if (other.matchAll != null) {
120            matchAll = other.matchAll;
121        }
122        if (other.selectOneNoMatch != null) {
123            selectOneNoMatch = other.selectOneNoMatch;
124        }
125        if (other.maxPoolSize != null) {
126            maxPoolSize = other.maxPoolSize;
127        }
128        if (other.minPoolSize != null) {
129            minPoolSize = other.minPoolSize;
130        }
131        if (other.blockingTimeoutMillis != null) {
132            blockingTimeoutMillis = other.blockingTimeoutMillis;
133        }
134        if (other.idleTimeoutMinutes != null) {
135            idleTimeoutMinutes = other.idleTimeoutMinutes;
136        }
137        if (other.testOnBorrow != null) {
138            testOnBorrow = other.testOnBorrow;
139        }
140        if (other.testOnReturn != null) {
141            testOnReturn = other.testOnReturn;
142        }
143    }
144
145    /** False if the boolean is null or FALSE, true otherwise. */
146    private static boolean defaultFalse(Boolean bool) {
147        return Boolean.TRUE.equals(bool);
148    }
149
150    /** True if the boolean is null or TRUE, false otherwise. */
151    private static boolean defaultTrue(Boolean bool) {
152        return !Boolean.FALSE.equals(bool);
153    }
154
155    private static int defaultInt(Integer value, int def) {
156        return value == null ? def : value.intValue();
157    }
158
159    public String getName() {
160        return name;
161    }
162
163    public boolean getXAMode() {
164        return defaultTrue(xaMode);
165    }
166
167    public boolean getUseTransactionCaching() {
168        return defaultTrue(useTransactionCaching);
169    }
170
171    public boolean getUseThreadCaching() {
172        return defaultTrue(useThreadCaching);
173    }
174
175    public boolean getMatchOne() {
176        return defaultTrue(matchOne);
177    }
178
179    public boolean getMatchAll() {
180        return defaultTrue(matchAll);
181    }
182
183    public boolean getSelectOneNoMatch() {
184        return defaultFalse(selectOneNoMatch);
185    }
186
187    public int getMaxPoolSize() {
188        return defaultInt(maxPoolSize, DEFAULT_MAX_POOL_SIZE);
189    }
190
191    public int getMinPoolSize() {
192        return defaultInt(minPoolSize, DEFAULT_MIN_POOL_SIZE);
193    }
194
195    public int getBlockingTimeoutMillis() {
196        return defaultInt(blockingTimeoutMillis, DEFAULT_BLOCKING_TIMEOUT_MILLIS);
197    }
198
199    public int getIdleTimeoutMinutes() {
200        return defaultInt(idleTimeoutMinutes, DEFAULT_IDLE_TIMEOUT_MINUTES);
201    }
202
203    public void setName(String name) {
204        this.name = name;
205    }
206
207    public void setXAMode(boolean xaMode) {
208        this.xaMode = Boolean.valueOf(xaMode);
209    }
210
211    public void setUseTransactionCaching(boolean useTransactionCaching) {
212        this.useTransactionCaching = Boolean.valueOf(useTransactionCaching);
213    }
214
215    public void setUseThreadCaching(boolean useThreadCaching) {
216        this.useThreadCaching = Boolean.valueOf(useThreadCaching);
217    }
218
219    public void setMatchOne(boolean matchOne) {
220        this.matchOne = Boolean.valueOf(matchOne);
221    }
222
223    public void setMatchAll(boolean matchAll) {
224        this.matchAll = Boolean.valueOf(matchAll);
225    }
226
227    public void setSelectOneNoMatch(boolean selectOneNoMatch) {
228        this.selectOneNoMatch = Boolean.valueOf(selectOneNoMatch);
229    }
230
231    public void setMaxPoolSize(int maxPoolSize) {
232        this.maxPoolSize = Integer.valueOf(maxPoolSize);
233    }
234
235    public void setMinPoolSize(int minPoolSize) {
236        this.minPoolSize = Integer.valueOf(minPoolSize);
237    }
238
239    public void setBlockingTimeoutMillis(int blockingTimeoutMillis) {
240        this.blockingTimeoutMillis = Integer.valueOf(blockingTimeoutMillis);
241    }
242
243    public void setIdleTimeoutMinutes(int idleTimeoutMinutes) {
244        this.idleTimeoutMinutes = Integer.valueOf(idleTimeoutMinutes);
245    }
246
247    @XNode("@validationQuery")
248    public void setValidationQuery(String sql) {
249        if (sql.isEmpty()) {
250            testOnBorrow = null;
251        } else {
252            testOnBorrow = new NuxeoValidationSupport.QuerySQLConnection(sql);
253        }
254    }
255
256
257    @XNode("@testOnBorrow")
258    public void setTestOnBorrow(Class<? extends Validation> typeof) throws ReflectiveOperationException {
259        testOnBorrow = typeof.newInstance();
260    }
261
262    @XNode("@testOnReturn")
263    public void setTestOnReturn(Class<? extends Validation> typeof) throws ReflectiveOperationException {
264        testOnReturn = typeof.newInstance();
265    }
266
267    @XNode("@maxActive")
268    public void setMaxActive(int num) {
269        maxPoolSize = num;
270        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
271                "maxActive deprecated dbcp pool attribute usage, should use maxPoolSize geronimo pool attribute instead");
272    }
273
274    @XNode("@maxIdle")
275    public void setMaxIdle(int num) {
276        minPoolSize = num;
277        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
278                "maxIdle deprecated dbcp pool attribute usage, should use minPoolSize geronimo pool attribute instead");
279    }
280
281    @XNode("@maxWait")
282    public void setMaxWait(int num) {
283        blockingTimeoutMillis = num;
284        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
285                "maxWait deprecated dbcp pool attribute usage, should use blockingTimeoutMillis geronimo pool attribute instead");
286
287    }
288}