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 * https://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 */ 017package org.apache.commons.dbcp2.datasources; 018 019import java.io.IOException; 020import java.time.Duration; 021import java.util.Map; 022 023import javax.naming.RefAddr; 024import javax.naming.Reference; 025 026/** 027 * A JNDI ObjectFactory which creates {@link SharedPoolDataSource}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 /** 035 * Constructs a new instance. 036 */ 037 public PerUserPoolDataSourceFactory() { 038 // empty 039 } 040 041 @SuppressWarnings("unchecked") // Avoid warnings on deserialization 042 @Override 043 protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException { 044 final PerUserPoolDataSource pupds = new PerUserPoolDataSource(); 045 RefAddr refAddr = ref.get("defaultMaxTotal"); 046 if (refAddr != null && refAddr.getContent() != null) { 047 pupds.setDefaultMaxTotal(parseInt(refAddr)); 048 } 049 050 refAddr = ref.get("defaultMaxIdle"); 051 if (refAddr != null && refAddr.getContent() != null) { 052 pupds.setDefaultMaxIdle(parseInt(refAddr)); 053 } 054 055 refAddr = ref.get("defaultMaxWaitMillis"); 056 if (refAddr != null && refAddr.getContent() != null) { 057 pupds.setDefaultMaxWait(Duration.ofMillis(parseInt(refAddr))); 058 } 059 060 refAddr = ref.get("perUserDefaultAutoCommit"); 061 if (refAddr != null && refAddr.getContent() != null) { 062 final byte[] serialized = (byte[]) refAddr.getContent(); 063 pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized)); 064 } 065 066 refAddr = ref.get("perUserDefaultTransactionIsolation"); 067 if (refAddr != null && refAddr.getContent() != null) { 068 final byte[] serialized = (byte[]) refAddr.getContent(); 069 pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized)); 070 } 071 072 refAddr = ref.get("perUserMaxTotal"); 073 if (refAddr != null && refAddr.getContent() != null) { 074 final byte[] serialized = (byte[]) refAddr.getContent(); 075 pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized)); 076 } 077 078 refAddr = ref.get("perUserMaxIdle"); 079 if (refAddr != null && refAddr.getContent() != null) { 080 final byte[] serialized = (byte[]) refAddr.getContent(); 081 pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized)); 082 } 083 084 refAddr = ref.get("perUserMaxWaitMillis"); 085 if (refAddr != null && refAddr.getContent() != null) { 086 final byte[] serialized = (byte[]) refAddr.getContent(); 087 pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized)); 088 } 089 090 refAddr = ref.get("perUserDefaultReadOnly"); 091 if (refAddr != null && refAddr.getContent() != null) { 092 final byte[] serialized = (byte[]) refAddr.getContent(); 093 pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized)); 094 } 095 return pupds; 096 } 097 098 @Override 099 protected boolean isCorrectClass(final String className) { 100 return PER_USER_POOL_CLASSNAME.equals(className); 101 } 102}