001/* 002 * ModeShape (http://www.modeshape.org) 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 */ 016package org.modeshape.jdbc.delegate; 017 018import java.sql.Connection; 019import java.sql.SQLException; 020import java.util.Properties; 021import java.util.Set; 022import javax.jcr.RepositoryException; 023import org.modeshape.common.logging.Logger; 024import org.modeshape.jdbc.DriverInfo; 025import org.modeshape.jdbc.JcrConnection; 026import org.modeshape.jdbc.JdbcLocalI18n; 027 028/** 029 * The AbstractRepositoryDelegate provides the common logic for the implementation of the {@link RepositoryDelegate} 030 */ 031public abstract class AbstractRepositoryDelegate implements RepositoryDelegate { 032 033 protected final Logger logger = Logger.getLogger(getClass()); 034 035 private Set<String> repositoryNames = null; 036 private ConnectionInfo connInfo = null; 037 private String url; 038 private Properties propertiesInfo; 039 040 public AbstractRepositoryDelegate( String url, 041 Properties info ) { 042 this.url = url; 043 this.propertiesInfo = info; 044 } 045 046 /** 047 * Returns a {@link ConnectionInfo} object which represents the information of a specific connection, from a given url format 048 * and some {@link Properties} 049 * 050 * @param url a {@code non-null} string which represents a jdbc url 051 * @param info a {@code non-null} {@link Properties} instance which may contain extra information needed by the connection 052 * @return {@link ConnectionInfo} instance, never {@code null} 053 */ 054 abstract ConnectionInfo createConnectionInfo( final String url, 055 final Properties info ); 056 057 /** 058 * The implementing class is responsible for creating the repository in an optimal way, since this will be called each time a 059 * connection is created. 060 * 061 * @throws SQLException 062 */ 063 abstract void initRepository() throws SQLException; 064 065 @Override 066 public synchronized ConnectionInfo getConnectionInfo() { 067 if (this.connInfo == null) { 068 this.connInfo = createConnectionInfo(url, propertiesInfo); 069 this.connInfo.init(); 070 } 071 return connInfo; 072 } 073 074 @Override 075 public void closeStatement() { 076 } 077 078 @SuppressWarnings( "unused" ) 079 @Override 080 public void commit() throws RepositoryException { 081 } 082 083 @Override 084 public void close() { 085 } 086 087 @SuppressWarnings( "unused" ) 088 @Override 089 public void rollback() throws RepositoryException { 090 } 091 092 @Override 093 public Connection createConnection( DriverInfo info ) throws SQLException { 094 logger.debug("Creating connection for RepositoryDelegate"); 095 initRepository(); 096 return new JcrConnection(this, info); 097 } 098 099 public String getRepositoryName() { 100 return getConnectionInfo().getRepositoryName(); 101 } 102 103 protected void setRepositoryName( String repositoryName ) { 104 this.getConnectionInfo().setRepositoryName(repositoryName); 105 } 106 107 @Override 108 public Set<String> getRepositoryNames() { 109 return this.repositoryNames; 110 } 111 112 protected void setRepositoryNames( Set<String> repositoryNames ) { 113 this.repositoryNames = repositoryNames; 114 } 115 116 @Override 117 public boolean isWrapperFor( Class<?> iface ) { 118 return iface.isInstance(this); 119 } 120 121 @Override 122 public <T> T unwrap( Class<T> iface ) throws SQLException { 123 if (!isWrapperFor(iface)) { 124 throw new SQLException(JdbcLocalI18n.classDoesNotImplementInterface.text(RepositoryDelegate.class.getSimpleName(), 125 iface.getName())); 126 } 127 128 return iface.cast(this); 129 } 130 131}