001/* 002 * (C) Copyright 2006-2014 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 */ 019package org.nuxeo.ecm.core.storage.sql.ra; 020 021import javax.naming.InitialContext; 022import javax.naming.NamingException; 023import javax.resource.ResourceException; 024import javax.resource.spi.ConnectionManager; 025 026import org.nuxeo.ecm.core.repository.RepositoryFactory; 027import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor; 028import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepositoryService; 029import org.nuxeo.runtime.api.Framework; 030import org.nuxeo.runtime.jtajca.NuxeoConnectionManagerConfiguration; 031import org.nuxeo.runtime.jtajca.NuxeoContainer; 032 033/** 034 * Repository factory for VCS, the repository implements internally a JCA pool of sessions. 035 */ 036public class PoolingRepositoryFactory implements RepositoryFactory { 037 038 protected final String repositoryName; 039 040 public PoolingRepositoryFactory(String repositoryName) { 041 this.repositoryName = repositoryName; 042 } 043 044 @Override 045 public Object call() { 046 SQLRepositoryService sqlRepositoryService = Framework.getLocalService(SQLRepositoryService.class); 047 RepositoryDescriptor descriptor = sqlRepositoryService.getRepositoryDescriptor(repositoryName); 048 ManagedConnectionFactoryImpl managedConnectionFactory = new ManagedConnectionFactoryImpl(); 049 managedConnectionFactory.setName(descriptor.name); 050 try { 051 NuxeoConnectionManagerConfiguration pool = descriptor.pool; 052 if (pool == null) { 053 pool = new NuxeoConnectionManagerConfiguration(); 054 pool.setName("repository/" + repositoryName); 055 } 056 ConnectionManager connectionManager = lookupConnectionManager(pool); 057 return managedConnectionFactory.createConnectionFactory(connectionManager); 058 } catch (NamingException | ResourceException e) { 059 throw new RuntimeException(e); 060 } 061 } 062 063 /** 064 * Various binding names for the ConnectionManager. They depend on the application server used and how the 065 * configuration is done. 066 */ 067 private static final String[] CM_NAMES_PREFIXES = { "java:comp/NuxeoConnectionManager/", 068 "java:comp/env/NuxeoConnectionManager/", "java:NuxeoConnectionManager/" }; 069 070 protected static ConnectionManager lookupConnectionManager(NuxeoConnectionManagerConfiguration pool) 071 throws NamingException { 072 String name = pool.getName(); 073 // Check in container 074 ConnectionManager cm = NuxeoContainer.getConnectionManager(name); 075 if (cm != null) { 076 return cm; 077 } 078 // Check in JNDI 079 InitialContext context = new InitialContext(); 080 for (String prefix : CM_NAMES_PREFIXES) { 081 try { 082 cm = (ConnectionManager) context.lookup(prefix + name); 083 if (cm != null) { 084 return cm; 085 } 086 } catch (NamingException e) { 087 // try next one 088 } 089 } 090 // Creation from descriptor pool config 091 return NuxeoContainer.initConnectionManager(pool); 092 } 093 094}