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 */
017
018package org.apache.commons.dbcp2.datasources;
019
020import java.io.IOException;
021import java.util.Map;
022
023import javax.naming.RefAddr;
024import javax.naming.Reference;
025
026/**
027 * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>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 ra = ref.get("defaultMaxTotal");
039        if (ra != null && ra.getContent() != null) {
040            pupds.setDefaultMaxTotal(Integer.parseInt(ra.getContent().toString()));
041        }
042
043        ra = ref.get("defaultMaxIdle");
044        if (ra != null && ra.getContent() != null) {
045            pupds.setDefaultMaxIdle(Integer.parseInt(ra.getContent().toString()));
046        }
047
048        ra = ref.get("defaultMaxWaitMillis");
049        if (ra != null && ra.getContent() != null) {
050            pupds.setDefaultMaxWaitMillis(Integer.parseInt(ra.getContent().toString()));
051        }
052
053        ra = ref.get("perUserDefaultAutoCommit");
054        if (ra != null && ra.getContent() != null) {
055            final byte[] serialized = (byte[]) ra.getContent();
056            pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
057        }
058
059        ra = ref.get("perUserDefaultTransactionIsolation");
060        if (ra != null && ra.getContent() != null) {
061            final byte[] serialized = (byte[]) ra.getContent();
062            pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
063        }
064
065        ra = ref.get("perUserMaxTotal");
066        if (ra != null && ra.getContent() != null) {
067            final byte[] serialized = (byte[]) ra.getContent();
068            pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
069        }
070
071        ra = ref.get("perUserMaxIdle");
072        if (ra != null && ra.getContent() != null) {
073            final byte[] serialized = (byte[]) ra.getContent();
074            pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
075        }
076
077        ra = ref.get("perUserMaxWaitMillis");
078        if (ra != null && ra.getContent() != null) {
079            final byte[] serialized = (byte[]) ra.getContent();
080            pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
081        }
082
083        ra = ref.get("perUserDefaultReadOnly");
084        if (ra != null && ra.getContent() != null) {
085            final byte[] serialized = (byte[]) ra.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}