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;
025
026/**
027 * Descriptor of the pool configuration, used by NuxeoContainer when creating a pool directly instead of the previous
028 * way that was using a JNDI factory (NuxeoConnectionManagerFactory).
029 *
030 * @since 5.6
031 */
032@XObject("pool")
033public class NuxeoConnectionManagerConfiguration {
034
035    public static final int DEFAULT_MAX_POOL_SIZE = 20;
036
037    public static final int DEFAULT_MIN_POOL_SIZE = 0;
038
039    public static final int DEFAULT_BLOCKING_TIMEOUT_MILLIS = 100;
040
041    public static final int DEFAULT_IDLE_TIMEOUT_MINUTES = 0; // no timeout
042
043    @XNode("@name")
044    private String name = "NuxeoConnectionManager";
045
046    // transaction
047    @XNode("@xaMode")
048    private Boolean xaMode;
049
050    @XNode("@useTransactionCaching")
051    private Boolean useTransactionCaching;
052
053    @XNode("@useThreadCaching")
054    private Boolean useThreadCaching;
055
056    // pool
057
058    @XNode("@matchOne")
059    private Boolean matchOne; // unused by Geronimo?
060
061    @XNode("@matchAll")
062    private Boolean matchAll;
063
064    @XNode("@selectOneNoMatch")
065    private Boolean selectOneNoMatch;
066
067    @XNode("@maxPoolSize")
068    private Integer maxPoolSize;
069
070    @XNode("@minPoolSize")
071    private Integer minPoolSize;
072
073    @XNode("@blockingTimeoutMillis")
074    private Integer blockingTimeoutMillis;
075
076    @XNode("@idleTimeoutMinutes")
077    private Integer idleTimeoutMinutes;
078
079    public NuxeoConnectionManagerConfiguration() {
080    }
081
082    /** Copy constructor. */
083    public NuxeoConnectionManagerConfiguration(NuxeoConnectionManagerConfiguration other) {
084        name = other.name;
085        useTransactionCaching = other.useTransactionCaching;
086        useThreadCaching = other.useThreadCaching;
087        matchOne = other.matchOne;
088        matchAll = other.matchAll;
089        selectOneNoMatch = other.selectOneNoMatch;
090        maxPoolSize = other.maxPoolSize;
091        minPoolSize = other.minPoolSize;
092        blockingTimeoutMillis = other.blockingTimeoutMillis;
093        idleTimeoutMinutes = other.idleTimeoutMinutes;
094    }
095
096    public void merge(NuxeoConnectionManagerConfiguration other) {
097        if (other.name != null) {
098            name = other.name;
099        }
100        if (other.xaMode) {
101            xaMode = other.xaMode;
102        }
103        if (other.useTransactionCaching != null) {
104            useTransactionCaching = other.useTransactionCaching;
105        }
106        if (other.useThreadCaching != null) {
107            useThreadCaching = other.useThreadCaching;
108        }
109        if (other.matchOne != null) {
110            matchOne = other.matchOne;
111        }
112        if (other.matchAll != null) {
113            matchAll = other.matchAll;
114        }
115        if (other.selectOneNoMatch != null) {
116            selectOneNoMatch = other.selectOneNoMatch;
117        }
118        if (other.maxPoolSize != null) {
119            maxPoolSize = other.maxPoolSize;
120        }
121        if (other.minPoolSize != null) {
122            minPoolSize = other.minPoolSize;
123        }
124        if (other.blockingTimeoutMillis != null) {
125            blockingTimeoutMillis = other.blockingTimeoutMillis;
126        }
127        if (other.idleTimeoutMinutes != null) {
128            idleTimeoutMinutes = other.idleTimeoutMinutes;
129        }
130    }
131
132    /** False if the boolean is null or FALSE, true otherwise. */
133    private static boolean defaultFalse(Boolean bool) {
134        return Boolean.TRUE.equals(bool);
135    }
136
137    /** True if the boolean is null or TRUE, false otherwise. */
138    private static boolean defaultTrue(Boolean bool) {
139        return !Boolean.FALSE.equals(bool);
140    }
141
142    private static int defaultInt(Integer value, int def) {
143        return value == null ? def : value.intValue();
144    }
145
146    public String getName() {
147        return name;
148    }
149
150    public boolean getXAMode() {
151        return defaultTrue(xaMode);
152    }
153
154    public boolean getUseTransactionCaching() {
155        return defaultTrue(useTransactionCaching);
156    }
157
158    public boolean getUseThreadCaching() {
159        return defaultTrue(useThreadCaching);
160    }
161
162    public boolean getMatchOne() {
163        return defaultTrue(matchOne);
164    }
165
166    public boolean getMatchAll() {
167        return defaultTrue(matchAll);
168    }
169
170    public boolean getSelectOneNoMatch() {
171        return defaultFalse(selectOneNoMatch);
172    }
173
174    public int getMaxPoolSize() {
175        return defaultInt(maxPoolSize, DEFAULT_MAX_POOL_SIZE);
176    }
177
178    public int getMinPoolSize() {
179        return defaultInt(minPoolSize, DEFAULT_MIN_POOL_SIZE);
180    }
181
182    public int getBlockingTimeoutMillis() {
183        return defaultInt(blockingTimeoutMillis, DEFAULT_BLOCKING_TIMEOUT_MILLIS);
184    }
185
186    public int getIdleTimeoutMinutes() {
187        return defaultInt(idleTimeoutMinutes, DEFAULT_IDLE_TIMEOUT_MINUTES);
188    }
189
190    public void setName(String name) {
191        this.name = name;
192    }
193
194    public void setXAMode(boolean xaMode) {
195        this.xaMode = Boolean.valueOf(xaMode);
196    }
197
198    public void setUseTransactionCaching(boolean useTransactionCaching) {
199        this.useTransactionCaching = Boolean.valueOf(useTransactionCaching);
200    }
201
202    public void setUseThreadCaching(boolean useThreadCaching) {
203        this.useThreadCaching = Boolean.valueOf(useThreadCaching);
204    }
205
206    public void setMatchOne(boolean matchOne) {
207        this.matchOne = Boolean.valueOf(matchOne);
208    }
209
210    public void setMatchAll(boolean matchAll) {
211        this.matchAll = Boolean.valueOf(matchAll);
212    }
213
214    public void setSelectOneNoMatch(boolean selectOneNoMatch) {
215        this.selectOneNoMatch = Boolean.valueOf(selectOneNoMatch);
216    }
217
218    public void setMaxPoolSize(int maxPoolSize) {
219        this.maxPoolSize = Integer.valueOf(maxPoolSize);
220    }
221
222    public void setMinPoolSize(int minPoolSize) {
223        this.minPoolSize = Integer.valueOf(minPoolSize);
224    }
225
226    public void setBlockingTimeoutMillis(int blockingTimeoutMillis) {
227        this.blockingTimeoutMillis = Integer.valueOf(blockingTimeoutMillis);
228    }
229
230    public void setIdleTimeoutMinutes(int idleTimeoutMinutes) {
231        this.idleTimeoutMinutes = Integer.valueOf(idleTimeoutMinutes);
232    }
233
234    @XNode("@maxActive")
235    public void setMaxActive(int num) {
236        maxPoolSize = num;
237        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
238                "maxActive deprecated dbcp pool attribute usage, should use maxPoolSize geronimo pool attribute instead");
239    }
240
241    @XNode("@maxIdle")
242    public void setMaxIdle(int num) {
243        minPoolSize = num;
244        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
245                "maxIdle deprecated dbcp pool attribute usage, should use minPoolSize geronimo pool attribute instead");
246    }
247
248    @XNode("@maxWait")
249    public void setMaxWait(int num) {
250        blockingTimeoutMillis = num;
251        LogFactory.getLog(NuxeoConnectionManagerConfiguration.class).warn(
252                "maxWait deprecated dbcp pool attribute usage, should use blockingTimeoutMillis geronimo pool attribute instead");
253
254    }
255}