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