001/* 002 * (C) Copyright 2006-2011 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.transaction.TransactionManager; 031 032import org.apache.commons.beanutils.BeanUtils; 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.runtime.jtajca.NuxeoContainer.TransactionManagerConfiguration; 036 037/** 038 * Factory for the TransactionManager. 039 */ 040public class NuxeoTransactionManagerFactory implements ObjectFactory { 041 042 private static final Log log = LogFactory.getLog(NuxeoTransactionManagerFactory.class); 043 044 @Override 045 public Object getObjectInstance(Object obj, Name objName, Context nameCtx, Hashtable<?, ?> env) { 046 Reference ref = (Reference) obj; 047 if (!TransactionManager.class.getName().equals(ref.getClassName())) { 048 return null; 049 } 050 if (NuxeoContainer.tm != null) { 051 return NuxeoContainer.tm; 052 } 053 054 // initialize 055 TransactionManagerConfiguration config = new TransactionManagerConfiguration(); 056 for (RefAddr addr : Collections.list(ref.getAll())) { 057 String name = addr.getType(); 058 String value = (String) addr.getContent(); 059 try { 060 BeanUtils.setProperty(config, name, value); 061 } catch (ReflectiveOperationException e) { 062 log.error(String.format("NuxeoTransactionManagerFactory cannot set %s = %s", name, value)); 063 } 064 } 065 return NuxeoContainer.initTransactionManager(config); 066 } 067 068}