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