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    *      https://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 {@link 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      /**
35       * Constructs a new instance.
36       */
37      public PerUserPoolDataSourceFactory() {
38          // empty
39      }
40  
41      @SuppressWarnings("unchecked") // Avoid warnings on deserialization
42      @Override
43      protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
44          final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
45          RefAddr refAddr = ref.get("defaultMaxTotal");
46          if (refAddr != null && refAddr.getContent() != null) {
47              pupds.setDefaultMaxTotal(parseInt(refAddr));
48          }
49  
50          refAddr = ref.get("defaultMaxIdle");
51          if (refAddr != null && refAddr.getContent() != null) {
52              pupds.setDefaultMaxIdle(parseInt(refAddr));
53          }
54  
55          refAddr = ref.get("defaultMaxWaitMillis");
56          if (refAddr != null && refAddr.getContent() != null) {
57              pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr)));
58          }
59  
60          refAddr = ref.get("perUserDefaultAutoCommit");
61          if (refAddr != null && refAddr.getContent() != null) {
62              final byte[] serialized = (byte[]) refAddr.getContent();
63              pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
64          }
65  
66          refAddr = ref.get("perUserDefaultTransactionIsolation");
67          if (refAddr != null && refAddr.getContent() != null) {
68              final byte[] serialized = (byte[]) refAddr.getContent();
69              pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
70          }
71  
72          refAddr = ref.get("perUserMaxTotal");
73          if (refAddr != null && refAddr.getContent() != null) {
74              final byte[] serialized = (byte[]) refAddr.getContent();
75              pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
76          }
77  
78          refAddr = ref.get("perUserMaxIdle");
79          if (refAddr != null && refAddr.getContent() != null) {
80              final byte[] serialized = (byte[]) refAddr.getContent();
81              pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
82          }
83  
84          refAddr = ref.get("perUserMaxWaitMillis");
85          if (refAddr != null && refAddr.getContent() != null) {
86              final byte[] serialized = (byte[]) refAddr.getContent();
87              pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
88          }
89  
90          refAddr = ref.get("perUserDefaultReadOnly");
91          if (refAddr != null && refAddr.getContent() != null) {
92              final byte[] serialized = (byte[]) refAddr.getContent();
93              pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
94          }
95          return pupds;
96      }
97  
98      @Override
99      protected boolean isCorrectClass(final String className) {
100         return PER_USER_POOL_CLASSNAME.equals(className);
101     }
102 }