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.SQLException; 019import java.util.Properties; 020import org.modeshape.common.util.StringUtil; 021import org.modeshape.jdbc.JdbcLocalI18n; 022import org.modeshape.jdbc.LocalJcrDriver; 023import org.modeshape.jdbc.LocalJcrDriver.JcrContextFactory; 024 025/** 026 * The RepositoryDelegateFactory is used to create the required type of {@link RepositoryDelegate} based upon the <i>url</i> 027 * provided. The <i>url</i> must be prefixed by {@value LocalJcrDriver#JNDI_URL_PREFIX}. 028 */ 029public class RepositoryDelegateFactory { 030 031 protected static final int PROTOCOL_UNKNOWN = -1; 032 protected static final int PROTOCOL_JNDI = 1; 033 034 protected RepositoryDelegateFactory() { 035 } 036 037 /** 038 * Create a RepositoryDelegate instance, given the connection information. 039 * 040 * @param url the JDBC URL; may be null 041 * @param info the connection properties 042 * @param contextFactory the factory for a JCR context; may not be null 043 * @return the RepositoryDelegate for the supplied connection information 044 * @throws SQLException if the URL is unknown 045 */ 046 public RepositoryDelegate createRepositoryDelegate( String url, 047 Properties info, 048 JcrContextFactory contextFactory ) throws SQLException { 049 if (!acceptUrl(url)) { 050 throw new SQLException(JdbcLocalI18n.invalidUrlPrefix.text(LocalJcrDriver.JNDI_URL_PREFIX)); 051 } 052 return create(determineProtocol(url), url, info, contextFactory); 053 } 054 055 /** 056 * Determine if this factory accepts the supplied URL. 057 * 058 * @param url the connection URL 059 * @return true if this factory accepts the supplied URL, or false otherwise 060 */ 061 public boolean acceptUrl( String url ) { 062 return !StringUtil.isBlank(url) && determineProtocol(url.trim()) > 0; 063 } 064 065 protected int determineProtocol( String url ) { 066 assert url != null; 067 assert url.length() != 0; 068 if (url.startsWith(LocalJcrDriver.JNDI_URL_PREFIX) && url.length() > LocalJcrDriver.JNDI_URL_PREFIX.length()) { 069 // This fits the pattern so far ... 070 return PROTOCOL_JNDI; 071 } 072 return PROTOCOL_UNKNOWN; 073 } 074 075 protected RepositoryDelegate create( int protocol, 076 String url, 077 Properties info, 078 JcrContextFactory contextFactory ) { 079 switch (protocol) { 080 case PROTOCOL_JNDI: 081 return new LocalRepositoryDelegate(url, info, contextFactory); 082 default: 083 throw new IllegalArgumentException("Invalid protocol: " + protocol); 084 } 085 } 086}