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    
018    package org.apache.commons.dbcp.datasources;
019    
020    import java.io.IOException;
021    import java.util.Map;
022    
023    import javax.naming.RefAddr;
024    import javax.naming.Reference;
025    
026    /**
027     * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s
028     * 
029     * @version $Revision: 479137 $ $Date: 2006-11-25 10:51:48 -0500 (Sat, 25 Nov 2006) $
030     */
031    public class PerUserPoolDataSourceFactory
032        extends InstanceKeyObjectFactory
033    {
034        private static final String PER_USER_POOL_CLASSNAME =
035            PerUserPoolDataSource.class.getName();
036    
037        protected boolean isCorrectClass(String className) {
038            return PER_USER_POOL_CLASSNAME.equals(className);
039        }
040    
041        protected InstanceKeyDataSource getNewInstance(Reference ref) 
042            throws IOException, ClassNotFoundException {
043            PerUserPoolDataSource pupds =  new PerUserPoolDataSource();
044            RefAddr ra = ref.get("defaultMaxActive");
045            if (ra != null && ra.getContent() != null) {
046                pupds.setDefaultMaxActive(
047                    Integer.parseInt(ra.getContent().toString()));
048            }
049    
050            ra = ref.get("defaultMaxIdle");
051            if (ra != null && ra.getContent() != null) {
052                pupds.setDefaultMaxIdle(
053                    Integer.parseInt(ra.getContent().toString()));
054            }
055    
056            ra = ref.get("defaultMaxWait");
057            if (ra != null && ra.getContent() != null) {
058                pupds.setDefaultMaxWait(
059                    Integer.parseInt(ra.getContent().toString()));
060            }
061    
062            ra = ref.get("perUserDefaultAutoCommit");
063            if (ra != null  && ra.getContent() != null) {
064                byte[] serialized = (byte[]) ra.getContent();
065                pupds.perUserDefaultAutoCommit = (Map) deserialize(serialized);
066            }
067    
068            ra = ref.get("perUserDefaultTransactionIsolation");
069            if (ra != null  && ra.getContent() != null) {
070                byte[] serialized = (byte[]) ra.getContent();
071                pupds.perUserDefaultTransactionIsolation = 
072                    (Map) deserialize(serialized);
073            }
074    
075            ra = ref.get("perUserMaxActive");
076            if (ra != null  && ra.getContent() != null) {
077                byte[] serialized = (byte[]) ra.getContent();
078                pupds.perUserMaxActive = (Map) deserialize(serialized);
079            }
080            
081            ra = ref.get("perUserMaxIdle");
082            if (ra != null  && ra.getContent() != null) {
083                byte[] serialized = (byte[]) ra.getContent();
084                pupds.perUserMaxIdle = (Map) deserialize(serialized);
085            }
086            
087            ra = ref.get("perUserMaxWait");
088            if (ra != null  && ra.getContent() != null) {
089                byte[] serialized = (byte[]) ra.getContent();
090                pupds.perUserMaxWait = (Map) deserialize(serialized);
091            }
092                    
093            ra = ref.get("perUserDefaultReadOnly");
094            if (ra != null  && ra.getContent() != null) {
095                byte[] serialized = (byte[]) ra.getContent();
096                pupds.perUserDefaultReadOnly = (Map) deserialize(serialized);
097            }
098            return pupds;
099        }            
100    }
101