View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jdbc.delegate;
25  
26  import java.sql.Connection;
27  import java.sql.SQLException;
28  import java.util.Properties;
29  import java.util.Set;
30  
31  import javax.jcr.Repository;
32  import javax.jcr.RepositoryException;
33  
34  import org.modeshape.jdbc.JcrConnection;
35  import org.modeshape.jdbc.JdbcI18n;
36  import org.modeshape.jdbc.util.Logger;
37  
38  /**
39   * The AbstractRepositoryDelegate provides the common logic for the implementation of the {@link RepositoryDelegate}
40   */
41  public abstract class AbstractRepositoryDelegate implements RepositoryDelegate {
42      protected static final Logger LOGGER = Logger.getLogger("org.modeshape.jdbc.delegate");
43  
44      private Repository repository = null;
45      private Set<String> repositoryNames = null;
46      private ConnectionInfo connInfo = null;
47      private String url;
48      private Properties propertiesInfo;
49    
50      public AbstractRepositoryDelegate(String url, Properties info) {
51  	super();
52  		this.url = url;
53  		this.propertiesInfo = info;
54      }
55     
56      /**
57       * The implementor must return a @link ConnectionInfo that provides the information
58       * that details 
59       * r
60       * @param url
61       * @param info
62       * @return ConnectionInfo
63       */
64      abstract ConnectionInfo createConnectionInfo(final String url, final Properties info);
65      	   
66  	/**
67  	 * Implementor is responsible for creating the repository.
68  	 * @throws SQLException
69  	 */
70      abstract void createRepository() throws SQLException;
71  
72  
73      @Override
74  	public synchronized ConnectionInfo getConnectionInfo() {
75      	if (this.connInfo == null) {
76      		this.connInfo = createConnectionInfo(url, propertiesInfo);
77      		this.connInfo.init();
78      	}
79      	return connInfo;
80      }
81      
82      /**
83       * {@inheritDoc}
84       *
85       * @see org.modeshape.jdbc.delegate.RepositoryDelegate#closeStatement()
86       */
87      @Override
88      public void closeStatement() {
89      }
90  	
91      /**
92       * {@inheritDoc}
93       * 
94       * @see java.sql.Connection#commit()
95       */
96      @Override
97      public void commit() throws RepositoryException {
98      }
99  	
100 
101     /**
102      * {@inheritDoc}
103      * 
104      * @see java.sql.Connection#close()
105      */
106     @Override
107     public void close() {
108     }
109     
110     /**
111      * {@inheritDoc}
112      * 
113      * @see java.sql.Connection#rollback()
114      */
115     @Override
116     public void rollback() throws RepositoryException {
117     }
118 
119     
120     @Override
121 	public  Connection createConnection() throws SQLException {
122        	LOGGER.debug("Creating connection for RepositoryDelegte" );
123        	if (this.repository == null) {
124        		createRepository();
125        	}
126 
127 		return new JcrConnection(this);
128     }  
129        
130     public synchronized Repository getRepository()  {
131     	return this.repository;
132     }
133     
134     protected void setRepository(Repository repository) {
135     	this.repository = repository;
136     }
137     
138     public String getRepositoryName()  {
139     	return getConnectionInfo().getRepositoryName();
140     }
141     
142     protected void setRepositoryName(String repositoryName) {
143     	this.getConnectionInfo().setRepositoryName(repositoryName);
144     }
145        
146     public Set<String> getRepositoryNames()  {
147     	return this.repositoryNames;
148     }
149     
150     protected void setRepositoryNames(Set<String> repositoryNames) {
151     	this.repositoryNames = repositoryNames;
152     }    
153 
154     /**
155      * {@inheritDoc}
156      * 
157      * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
158      */
159     @Override
160     public boolean isWrapperFor( Class<?> iface ) {
161         return iface.isInstance(this);
162     }
163 
164     /**
165      * {@inheritDoc}
166      * 
167      * @see java.sql.Wrapper#unwrap(java.lang.Class)
168      */
169     @Override
170     public <T> T unwrap( Class<T> iface ) throws SQLException {
171     	if (!isWrapperFor(iface)) {
172     	       throw new SQLException(JdbcI18n.classDoesNotImplementInterface.text(RepositoryDelegate.class.getSimpleName(),
173                        iface.getName()));
174     	}
175     	
176     	return iface.cast(this);
177      }
178    
179 }
180