001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbcp2.datasources;
018
019import java.io.IOException;
020import java.time.Duration;
021import java.util.Map;
022
023import javax.naming.RefAddr;
024import javax.naming.Reference;
025
026/**
027 * A JNDI ObjectFactory which creates {@code SharedPoolDataSource}s
028 *
029 * @since 2.0
030 */
031public class PerUserPoolDataSourceFactory extends InstanceKeyDataSourceFactory {
032    private static final String PER_USER_POOL_CLASSNAME = PerUserPoolDataSource.class.getName();
033
034    @SuppressWarnings("unchecked") // Avoid warnings on deserialization
035    @Override
036    protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
037        final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
038        RefAddr refAddr = ref.get("defaultMaxTotal");
039        if (refAddr != null && refAddr.getContent() != null) {
040            pupds.setDefaultMaxTotal(parseInt(refAddr));
041        }
042
043        refAddr = ref.get("defaultMaxIdle");
044        if (refAddr != null && refAddr.getContent() != null) {
045            pupds.setDefaultMaxIdle(parseInt(refAddr));
046        }
047
048        refAddr = ref.get("defaultMaxWaitMillis");
049        if (refAddr != null && refAddr.getContent() != null) {
050            pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr)));
051        }
052
053        refAddr = ref.get("perUserDefaultAutoCommit");
054        if (refAddr != null && refAddr.getContent() != null) {
055            final byte[] serialized = (byte[]) refAddr.getContent();
056            pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
057        }
058
059        refAddr = ref.get("perUserDefaultTransactionIsolation");
060        if (refAddr != null && refAddr.getContent() != null) {
061            final byte[] serialized = (byte[]) refAddr.getContent();
062            pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
063        }
064
065        refAddr = ref.get("perUserMaxTotal");
066        if (refAddr != null && refAddr.getContent() != null) {
067            final byte[] serialized = (byte[]) refAddr.getContent();
068            pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
069        }
070
071        refAddr = ref.get("perUserMaxIdle");
072        if (refAddr != null && refAddr.getContent() != null) {
073            final byte[] serialized = (byte[]) refAddr.getContent();
074            pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
075        }
076
077        refAddr = ref.get("perUserMaxWaitMillis");
078        if (refAddr != null && refAddr.getContent() != null) {
079            final byte[] serialized = (byte[]) refAddr.getContent();
080            pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
081        }
082
083        refAddr = ref.get("perUserDefaultReadOnly");
084        if (refAddr != null && refAddr.getContent() != null) {
085            final byte[] serialized = (byte[]) refAddr.getContent();
086            pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
087        }
088        return pupds;
089    }
090
091    @Override
092    protected boolean isCorrectClass(final String className) {
093        return PER_USER_POOL_CLASSNAME.equals(className);
094    }
095}