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
032    extends InstanceKeyDataSourceFactory
033{
034    private static final String PER_USER_POOL_CLASSNAME =
035        PerUserPoolDataSource.class.getName();
036
037    @Override
038    protected boolean isCorrectClass(final String className) {
039        return PER_USER_POOL_CLASSNAME.equals(className);
040    }
041
042    @SuppressWarnings("unchecked") // Avoid warnings on deserialization
043    @Override
044    protected InstanceKeyDataSource getNewInstance(final Reference ref)
045        throws IOException, ClassNotFoundException {
046        final PerUserPoolDataSource pupds =  new PerUserPoolDataSource();
047        RefAddr ra = ref.get("defaultMaxTotal");
048        if (ra != null && ra.getContent() != null) {
049            pupds.setDefaultMaxTotal(
050                Integer.parseInt(ra.getContent().toString()));
051        }
052
053        ra = ref.get("defaultMaxIdle");
054        if (ra != null && ra.getContent() != null) {
055            pupds.setDefaultMaxIdle(
056                Integer.parseInt(ra.getContent().toString()));
057        }
058
059        ra = ref.get("defaultMaxWaitMillis");
060        if (ra != null && ra.getContent() != null) {
061            pupds.setDefaultMaxWaitMillis(
062                Integer.parseInt(ra.getContent().toString()));
063        }
064
065        ra = ref.get("perUserDefaultAutoCommit");
066        if (ra != null  && ra.getContent() != null) {
067            final byte[] serialized = (byte[]) ra.getContent();
068            pupds.setPerUserDefaultAutoCommit(
069                    (Map<String,Boolean>) deserialize(serialized));
070        }
071
072        ra = ref.get("perUserDefaultTransactionIsolation");
073        if (ra != null  && ra.getContent() != null) {
074            final byte[] serialized = (byte[]) ra.getContent();
075            pupds.setPerUserDefaultTransactionIsolation(
076                    (Map<String,Integer>) deserialize(serialized));
077        }
078
079        ra = ref.get("perUserMaxTotal");
080        if (ra != null  && ra.getContent() != null) {
081            final byte[] serialized = (byte[]) ra.getContent();
082            pupds.setPerUserMaxTotal(
083                    (Map<String,Integer>) deserialize(serialized));
084        }
085
086        ra = ref.get("perUserMaxIdle");
087        if (ra != null  && ra.getContent() != null) {
088            final byte[] serialized = (byte[]) ra.getContent();
089            pupds.setPerUserMaxIdle(
090                    (Map<String,Integer>) deserialize(serialized));
091        }
092
093        ra = ref.get("perUserMaxWaitMillis");
094        if (ra != null  && ra.getContent() != null) {
095            final byte[] serialized = (byte[]) ra.getContent();
096            pupds.setPerUserMaxWaitMillis(
097                    (Map<String,Long>) deserialize(serialized));
098        }
099
100        ra = ref.get("perUserDefaultReadOnly");
101        if (ra != null  && ra.getContent() != null) {
102            final byte[] serialized = (byte[]) ra.getContent();
103            pupds.setPerUserDefaultReadOnly(
104                    (Map<String,Boolean>) deserialize(serialized));
105        }
106        return pupds;
107    }
108}
109