001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019 020package org.nuxeo.runtime.jtajca; 021 022import java.util.Collections; 023import java.util.Hashtable; 024 025import javax.naming.Context; 026import javax.naming.Name; 027import javax.naming.RefAddr; 028import javax.naming.Reference; 029import javax.naming.spi.ObjectFactory; 030import javax.resource.spi.ConnectionManager; 031 032import org.apache.commons.beanutils.BeanUtils; 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035 036/** 037 * Factory for the ConnectionManager. 038 */ 039public class NuxeoConnectionManagerFactory implements ObjectFactory { 040 041 private static final Log log = LogFactory.getLog(NuxeoConnectionManagerFactory.class); 042 043 @Override 044 public Object getObjectInstance(Object obj, Name objName, Context nameCtx, Hashtable<?, ?> env) { 045 Reference ref = (Reference) obj; 046 if (!ConnectionManager.class.getName().equals(ref.getClassName())) { 047 return null; 048 } 049 String name; 050 int size = objName.size(); 051 if (size == 1) { 052 name = "default"; 053 } else { 054 name = objName.get(size - 1); 055 } 056 057 final ConnectionManager cm = NuxeoContainer.connectionManagers.get(name); 058 if (cm != null) { 059 return cm; 060 } 061 062 NuxeoConnectionManagerConfiguration config = new NuxeoConnectionManagerConfiguration(); 063 for (RefAddr addr : Collections.list(ref.getAll())) { 064 String type = addr.getType(); 065 String content = (String) addr.getContent(); 066 try { 067 BeanUtils.setProperty(config, type, content); 068 } catch (ReflectiveOperationException e) { 069 log.error(String.format("NuxeoConnectionManagerFactory cannot set %s = %s", type, content)); 070 } 071 } 072 return NuxeoContainer.initConnectionManager(config); 073 } 074 075 public static NuxeoConnectionManagerConfiguration getConfig(Reference ref) { 076 NuxeoConnectionManagerConfiguration config = new NuxeoConnectionManagerConfiguration(); 077 IllegalArgumentException errors = new IllegalArgumentException("wrong naming config"); 078 for (RefAddr addr : Collections.list(ref.getAll())) { 079 String name = addr.getType(); 080 String value = (String) addr.getContent(); 081 try { 082 BeanUtils.setProperty(config, name, value); 083 } catch (ReflectiveOperationException cause) { 084 errors.addSuppressed(cause); 085 } 086 } 087 if (errors.getSuppressed().length > 0) { 088 throw errors; 089 } 090 return config; 091 } 092 093}