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.dbcp2.datasources;
19  
20  import java.io.IOException;
21  import java.util.Map;
22  
23  import javax.naming.RefAddr;
24  import javax.naming.Reference;
25  
26  /**
27   * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s
28   *
29   * @since 2.0
30   */
31  public class PerUserPoolDataSourceFactory extends InstanceKeyDataSourceFactory {
32      private static final String PER_USER_POOL_CLASSNAME = PerUserPoolDataSource.class.getName();
33  
34      @SuppressWarnings("unchecked") // Avoid warnings on deserialization
35      @Override
36      protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
37          final PerUserPoolDataSourcerUserPoolDataSource.html#PerUserPoolDataSource">PerUserPoolDataSource pupds = new PerUserPoolDataSource();
38          RefAddr ra = ref.get("defaultMaxTotal");
39          if (ra != null && ra.getContent() != null) {
40              pupds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
41          }
42  
43          ra = ref.get("defaultMaxIdle");
44          if (ra != null && ra.getContent() != null) {
45              pupds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
46          }
47  
48          ra = ref.get("defaultMaxWaitMillis");
49          if (ra != null && ra.getContent() != null) {
50              pupds.setDefaultMaxWaitMillis(Integer.parseInt(ra.getContent().toString()));
51          }
52  
53          ra = ref.get("perUserDefaultAutoCommit");
54          if (ra != null && ra.getContent() != null) {
55              final byte[] serialized = (byte[]) ra.getContent();
56              pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
57          }
58  
59          ra = ref.get("perUserDefaultTransactionIsolation");
60          if (ra != null && ra.getContent() != null) {
61              final byte[] serialized = (byte[]) ra.getContent();
62              pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
63          }
64  
65          ra = ref.get("perUserMaxTotal");
66          if (ra != null && ra.getContent() != null) {
67              final byte[] serialized = (byte[]) ra.getContent();
68              pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
69          }
70  
71          ra = ref.get("perUserMaxIdle");
72          if (ra != null && ra.getContent() != null) {
73              final byte[] serialized = (byte[]) ra.getContent();
74              pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
75          }
76  
77          ra = ref.get("perUserMaxWaitMillis");
78          if (ra != null && ra.getContent() != null) {
79              final byte[] serialized = (byte[]) ra.getContent();
80              pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
81          }
82  
83          ra = ref.get("perUserDefaultReadOnly");
84          if (ra != null && ra.getContent() != null) {
85              final byte[] serialized = (byte[]) ra.getContent();
86              pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
87          }
88          return pupds;
89      }
90  
91      @Override
92      protected boolean isCorrectClass(final String className) {
93          return PER_USER_POOL_CLASSNAME.equals(className);
94      }
95  }