PerUserPoolDataSourceFactory.java

  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. import java.io.IOException;
  19. import java.time.Duration;
  20. import java.util.Map;

  21. import javax.naming.RefAddr;
  22. import javax.naming.Reference;

  23. /**
  24.  * A JNDI ObjectFactory which creates {@code SharedPoolDataSource}s
  25.  *
  26.  * @since 2.0
  27.  */
  28. public class PerUserPoolDataSourceFactory extends InstanceKeyDataSourceFactory {
  29.     private static final String PER_USER_POOL_CLASSNAME = PerUserPoolDataSource.class.getName();

  30.     @SuppressWarnings("unchecked") // Avoid warnings on deserialization
  31.     @Override
  32.     protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
  33.         final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
  34.         RefAddr refAddr = ref.get("defaultMaxTotal");
  35.         if (refAddr != null && refAddr.getContent() != null) {
  36.             pupds.setDefaultMaxTotal(parseInt(refAddr));
  37.         }

  38.         refAddr = ref.get("defaultMaxIdle");
  39.         if (refAddr != null && refAddr.getContent() != null) {
  40.             pupds.setDefaultMaxIdle(parseInt(refAddr));
  41.         }

  42.         refAddr = ref.get("defaultMaxWaitMillis");
  43.         if (refAddr != null && refAddr.getContent() != null) {
  44.             pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr)));
  45.         }

  46.         refAddr = ref.get("perUserDefaultAutoCommit");
  47.         if (refAddr != null && refAddr.getContent() != null) {
  48.             final byte[] serialized = (byte[]) refAddr.getContent();
  49.             pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
  50.         }

  51.         refAddr = ref.get("perUserDefaultTransactionIsolation");
  52.         if (refAddr != null && refAddr.getContent() != null) {
  53.             final byte[] serialized = (byte[]) refAddr.getContent();
  54.             pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
  55.         }

  56.         refAddr = ref.get("perUserMaxTotal");
  57.         if (refAddr != null && refAddr.getContent() != null) {
  58.             final byte[] serialized = (byte[]) refAddr.getContent();
  59.             pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
  60.         }

  61.         refAddr = ref.get("perUserMaxIdle");
  62.         if (refAddr != null && refAddr.getContent() != null) {
  63.             final byte[] serialized = (byte[]) refAddr.getContent();
  64.             pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
  65.         }

  66.         refAddr = ref.get("perUserMaxWaitMillis");
  67.         if (refAddr != null && refAddr.getContent() != null) {
  68.             final byte[] serialized = (byte[]) refAddr.getContent();
  69.             pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
  70.         }

  71.         refAddr = ref.get("perUserDefaultReadOnly");
  72.         if (refAddr != null && refAddr.getContent() != null) {
  73.             final byte[] serialized = (byte[]) refAddr.getContent();
  74.             pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
  75.         }
  76.         return pupds;
  77.     }

  78.     @Override
  79.     protected boolean isCorrectClass(final String className) {
  80.         return PER_USER_POOL_CLASSNAME.equals(className);
  81.     }
  82. }