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  package org.apache.commons.dbcp2.datasources;
18  
19  import java.io.IOException;
20  import java.time.Duration;
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}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 PerUserPoolDataSource pupds = new PerUserPoolDataSource();
38          RefAddr refAddr = ref.get("defaultMaxTotal");
39          if (refAddr != null && refAddr.getContent() != null) {
40              pupds.setDefaultMaxTotal(parseInt(refAddr));
41          }
42  
43          refAddr = ref.get("defaultMaxIdle");
44          if (refAddr != null && refAddr.getContent() != null) {
45              pupds.setDefaultMaxIdle(parseInt(refAddr));
46          }
47  
48          refAddr = ref.get("defaultMaxWaitMillis");
49          if (refAddr != null && refAddr.getContent() != null) {
50              pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr)));
51          }
52  
53          refAddr = ref.get("perUserDefaultAutoCommit");
54          if (refAddr != null && refAddr.getContent() != null) {
55              final byte[] serialized = (byte[]) refAddr.getContent();
56              pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
57          }
58  
59          refAddr = ref.get("perUserDefaultTransactionIsolation");
60          if (refAddr != null && refAddr.getContent() != null) {
61              final byte[] serialized = (byte[]) refAddr.getContent();
62              pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
63          }
64  
65          refAddr = ref.get("perUserMaxTotal");
66          if (refAddr != null && refAddr.getContent() != null) {
67              final byte[] serialized = (byte[]) refAddr.getContent();
68              pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
69          }
70  
71          refAddr = ref.get("perUserMaxIdle");
72          if (refAddr != null && refAddr.getContent() != null) {
73              final byte[] serialized = (byte[]) refAddr.getContent();
74              pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
75          }
76  
77          refAddr = ref.get("perUserMaxWaitMillis");
78          if (refAddr != null && refAddr.getContent() != null) {
79              final byte[] serialized = (byte[]) refAddr.getContent();
80              pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
81          }
82  
83          refAddr = ref.get("perUserDefaultReadOnly");
84          if (refAddr != null && refAddr.getContent() != null) {
85              final byte[] serialized = (byte[]) refAddr.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  }