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.ecm.core.storage.sql.ra; 021 022import java.util.Calendar; 023 024import javax.naming.Reference; 025import javax.resource.ResourceException; 026import javax.resource.cci.ConnectionSpec; 027import javax.resource.cci.RecordFactory; 028import javax.resource.cci.ResourceAdapterMetaData; 029import javax.resource.spi.ConnectionManager; 030 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.core.storage.sql.Repository; 034import org.nuxeo.ecm.core.storage.sql.Session; 035import org.nuxeo.ecm.core.storage.sql.coremodel.SQLSession; 036import org.nuxeo.runtime.jtajca.NuxeoConnectionManagerConfiguration; 037import org.nuxeo.runtime.jtajca.NuxeoContainer; 038import org.nuxeo.runtime.jtajca.NuxeoContainer.ConnectionManagerWrapper; 039 040/** 041 * The connection factory delegates connection requests to the application server {@link ConnectionManager}. 042 * <p> 043 * An instance of this class is returned to the application when a JNDI lookup is done. 044 * 045 * @author Florent Guillaume 046 */ 047public class ConnectionFactoryImpl implements Repository, org.nuxeo.ecm.core.model.Repository { 048 049 private static final long serialVersionUID = 1L; 050 051 private final ManagedConnectionFactoryImpl managedConnectionFactory; 052 053 private final ConnectionManager connectionManager; 054 055 private final String name; 056 057 private Reference reference; 058 059 /** 060 * This is {@code true} if the connectionManager comes from an application server, or {@code false} if the 061 * {@link ConnectionFactoryImpl} was constructed by application code and passed our manual 062 * {@link ConnectionManagerImpl}. 063 */ 064 @SuppressWarnings("unused") 065 private final boolean managed; 066 067 public ConnectionFactoryImpl(ManagedConnectionFactoryImpl managedConnectionFactory, 068 ConnectionManager connectionManager) { 069 this.managedConnectionFactory = managedConnectionFactory; 070 this.connectionManager = connectionManager; 071 managed = !(connectionManager instanceof ConnectionManagerImpl); 072 name = managedConnectionFactory.getName(); 073 } 074 075 /* 076 * ----- javax.resource.cci.ConnectionFactory ----- 077 */ 078 079 /** 080 * Gets a new connection. 081 * 082 * @param connectionSpec the connection spec (unused) 083 * @return the connection 084 */ 085 @Override 086 public Session getConnection(ConnectionSpec connectionSpec) { 087 return getConnection(); 088 } 089 090 /** 091 * Gets a new connection. 092 * 093 * @return the connection 094 */ 095 @Override 096 public Session getConnection() { 097 try { 098 return (Session) connectionManager.allocateConnection(managedConnectionFactory, null); 099 } catch (ResourceException e) { 100 String msg = e.getMessage(); 101 if (msg != null && msg.startsWith("No ManagedConnections available")) { 102 String err = "Connection pool is fully used"; 103 if (connectionManager instanceof ConnectionManagerWrapper) { 104 ConnectionManagerWrapper cmw = (ConnectionManagerWrapper) connectionManager; 105 NuxeoConnectionManagerConfiguration config = cmw.getConfiguration(); 106 err = err + ", consider increasing " + "nuxeo.vcs.blocking-timeout-millis (currently " 107 + config.getBlockingTimeoutMillis() + ") or " + "nuxeo.vcs.max-pool-size (currently " 108 + config.getMaxPoolSize() + ")"; 109 } 110 throw new NuxeoException(err, e); 111 } 112 throw new NuxeoException(e); 113 } 114 } 115 116 @Override 117 public ResourceAdapterMetaData getMetaData() throws ResourceException { 118 // TODO Auto-generated method stub 119 throw new UnsupportedOperationException("Not implemented"); 120 } 121 122 @Override 123 public RecordFactory getRecordFactory() throws ResourceException { 124 throw new UnsupportedOperationException("Not implemented"); 125 } 126 127 /* 128 * ----- javax.naming.Referenceable ----- 129 */ 130 131 @Override 132 public Reference getReference() { 133 return reference; 134 } 135 136 @Override 137 public void setReference(Reference reference) { 138 this.reference = reference; 139 } 140 141 /* 142 * ----- Repository ----- 143 */ 144 145 @Override 146 public void close() { 147 throw new UnsupportedOperationException("Not implemented"); 148 } 149 150 /* 151 * ----- org.nuxeo.ecm.core.model.Repository ----- 152 */ 153 154 @Override 155 public String getName() { 156 return name; 157 } 158 159 @Override 160 public org.nuxeo.ecm.core.model.Session getSession() { 161 return new SQLSession(getConnection(), this); 162 } 163 164 @Override 165 public void shutdown() { 166 try { 167 NuxeoContainer.disposeConnectionManager(connectionManager); 168 } catch (RuntimeException e) { 169 LogFactory.getLog(ConnectionFactoryImpl.class).warn("cannot dispose connection manager of " + name); 170 } 171 try { 172 managedConnectionFactory.shutdown(); 173 } catch (NuxeoException e) { 174 LogFactory.getLog(ConnectionFactoryImpl.class).warn("cannot shutdown connection factory " + name); 175 } 176 } 177 178 /* 179 * ----- org.nuxeo.ecm.core.model.RepositoryManagement ----- 180 */ 181 182 @Override 183 public int getActiveSessionsCount() { 184 return managedConnectionFactory.getActiveSessionsCount(); 185 } 186 187 @Override 188 public long getCacheSize() { 189 return managedConnectionFactory.getCacheSize(); 190 } 191 192 @Override 193 public long getCachePristineSize() { 194 return managedConnectionFactory.getCachePristineSize(); 195 } 196 197 @Override 198 public long getCacheSelectionSize() { 199 return managedConnectionFactory.getCacheSelectionSize(); 200 } 201 202 @Override 203 public int clearCaches() { 204 return managedConnectionFactory.clearCaches(); 205 } 206 207 @Override 208 public void processClusterInvalidationsNext() { 209 managedConnectionFactory.processClusterInvalidationsNext(); 210 } 211 212 @Override 213 public void markReferencedBinaries() { 214 managedConnectionFactory.markReferencedBinaries(); 215 } 216 217 @Override 218 public int cleanupDeletedDocuments(int max, Calendar beforeTime) { 219 return managedConnectionFactory.cleanupDeletedDocuments(max, beforeTime); 220 } 221 222}