View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.config.attribute.resolver.dataConnector;
18  
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.sql.DataSource;
23  
24  import org.opensaml.xml.util.DatatypeHelper;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.RDBMSColumnDescriptor;
27  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.RDBMSDataConnector;
28  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.TemplateEngine;
29  
30  /**
31   * Spring factory bean that produces {@link RDBMSDataConnector}s.
32   */
33  public class RDBMSDataConnectorFactoryBean extends BaseDataConnectorFactoryBean {
34  
35      /** Template engine used to transform query templates into queries. */
36      private TemplateEngine templateEngine;
37  
38      /** Source of connections to the database. */
39      private DataSource connectionDataSource;
40  
41      /** SQL query template. */
42      private String queryTemplate;
43  
44      /** Result set column descriptors. */
45      private List<RDBMSColumnDescriptor> columnDescriptors;
46  
47      /** Whether the database connections should be read-only. */
48      private boolean readOnlyConnections;
49  
50      /** Whether the SQL query uses stored procedures. */
51      private boolean queryUsesStoredProcedures;
52  
53      /** Whether an empty result set is an error. */
54      private boolean noResultsIsError;
55  
56      /** Whether results should be cached. */
57      private boolean cacheResults;
58  
59      /** {@inheritDoc} */
60      public Class getObjectType() {
61          return RDBMSDataConnector.class;
62      }
63  
64      /**
65       * This returns whether this connector will throw an exception if no search results are found. The default is false.
66       * 
67       * @return <code>boolean</code>
68       */
69      public boolean isNoResultIsError() {
70          return noResultsIsError;
71      }
72  
73      /**
74       * This sets whether this connector will throw an exception if no search results are found.
75       * 
76       * @param b <code>boolean</code>
77       */
78      public void setNoResultIsError(boolean b) {
79          noResultsIsError = b;
80      }
81  
82      /**
83       * Gets whether query results should be cached.
84       * 
85       * @return whether query results should be cached
86       */
87      public boolean getCacheResults() {
88          return cacheResults;
89      }
90  
91      /**
92       * Sets whether query results should be cached.
93       * 
94       * @param cache whether query results should be cached
95       */
96      public void setCacheResults(boolean cache) {
97          cacheResults = cache;
98      }
99  
100     /**
101      * Gets the result set column descriptors.
102      * 
103      * @return result set column descriptors
104      */
105     public List<RDBMSColumnDescriptor> getColumnDescriptors() {
106         return columnDescriptors;
107     }
108 
109     /**
110      * Sets the result set column descriptors.
111      * 
112      * @param descriptors result set column descriptors
113      */
114     public void setColumnDescriptors(List<RDBMSColumnDescriptor> descriptors) {
115         columnDescriptors = descriptors;
116     }
117 
118     /**
119      * Gets the database connection source.
120      * 
121      * @return database connection source.
122      */
123     public DataSource getConnectionDataSource() {
124         return connectionDataSource;
125     }
126 
127     /**
128      * Sets the database connection source.
129      * 
130      * @param source database connection source
131      */
132     public void setConnectionDataSource(DataSource source) {
133         connectionDataSource = source;
134     }
135 
136     /**
137      * Gets the SQL query template.
138      * 
139      * @return SQL query template
140      */
141     public String getQueryTemplate() {
142         return queryTemplate;
143     }
144 
145     /**
146      * Sets the SQL query template.
147      * 
148      * @param template SQL query template
149      */
150     public void setQueryTemplate(String template) {
151         queryTemplate = DatatypeHelper.safeTrimOrNullString(template);
152     }
153 
154     /**
155      * Gets whether the SQL query uses stored procedures.
156      * 
157      * @return whether the SQL query uses stored procedures
158      */
159     public boolean getQueryUsesStoredProcedures() {
160         return queryUsesStoredProcedures;
161     }
162 
163     /**
164      * Sets whether the SQL query uses stored procedures.
165      * 
166      * @param storedProcedures whether the SQL query uses stored procedures
167      */
168     public void setQueryUsesStoredProcedures(boolean storedProcedures) {
169         queryUsesStoredProcedures = storedProcedures;
170     }
171 
172     /**
173      * Gets whether the database connection is read-only.
174      * 
175      * @return whether the database connection is read-only
176      */
177     public boolean isReadOnlyConnections() {
178         return readOnlyConnections;
179     }
180 
181     /**
182      * Sets whether the database connection is read-only.
183      * 
184      * @param readOnly whether the database connection is read-only
185      */
186     public void setReadOnlyConnections(boolean readOnly) {
187         readOnlyConnections = readOnly;
188     }
189 
190     /**
191      * Gets the template engine used to construct the SQL query from the query template.
192      * 
193      * @return template engine used to construct the SQL query from the query template
194      */
195     public TemplateEngine getTemplateEngine() {
196         return templateEngine;
197     }
198 
199     /**
200      * Sets the template engine used to construct the SQL query from the query template.
201      * 
202      * @param engine template engine used to construct the SQL query from the query template
203      */
204     public void setTemplateEngine(TemplateEngine engine) {
205         templateEngine = engine;
206     }
207 
208     /** {@inheritDoc} */
209     protected Object createInstance() throws Exception {
210         RDBMSDataConnector connector = new RDBMSDataConnector(getConnectionDataSource(), getCacheResults());
211         populateDataConnector(connector);
212         connector.setTemplateEngine(getTemplateEngine());
213         connector.setQueryTemplate(getQueryTemplate());
214         connector.setUsesStoredProcedure(getQueryUsesStoredProcedures());
215         connector.setConnectionReadOnly(isReadOnlyConnections());
216         connector.setNoResultIsError(isNoResultIsError());
217 
218         if (getColumnDescriptors() != null) {
219             Map<String, RDBMSColumnDescriptor> columnDecriptors = connector.getColumnDescriptor();
220             for (RDBMSColumnDescriptor descriptor : getColumnDescriptors()) {
221                 columnDecriptors.put(descriptor.getColumnName(), descriptor);
222             }
223         }
224 
225         connector.initialize();
226 
227         return connector;
228     }
229 }