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 ConnectionManager connectionManager = lookupConnectionManager(descriptor.pool); 052 return managedConnectionFactory.createConnectionFactory(connectionManager); 053 } catch (NamingException | ResourceException e) { 054 throw new RuntimeException(e); 055 } 056 } 057 058 /** 059 * Various binding names for the ConnectionManager. They depend on the application server used and how the 060 * configuration is done. 061 */ 062 private static final String[] CM_NAMES_PREFIXES = { "java:comp/NuxeoConnectionManager/", 063 "java:comp/env/NuxeoConnectionManager/", "java:NuxeoConnectionManager/" }; 064 065 protected static ConnectionManager lookupConnectionManager(NuxeoConnectionManagerConfiguration pool) 066 throws NamingException { 067 String name = pool.getName(); 068 // Check in container 069 ConnectionManager cm = NuxeoContainer.getConnectionManager(name); 070 if (cm != null) { 071 return cm; 072 } 073 // Check in JNDI 074 InitialContext context = new InitialContext(); 075 for (String prefix : CM_NAMES_PREFIXES) { 076 try { 077 cm = (ConnectionManager) context.lookup(prefix + name); 078 if (cm != null) { 079 return cm; 080 } 081 } catch (NamingException e) { 082 // try next one 083 } 084 } 085 // Creation from descriptor pool config 086 return NuxeoContainer.initConnectionManager(pool); 087 } 088 089}