View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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  package org.apache.juddi.datastore.jdbc;
17  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.Timestamp;
22  import java.util.Vector;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.datatype.binding.AccessPoint;
27  import org.apache.juddi.datatype.binding.BindingTemplate;
28  import org.apache.juddi.datatype.binding.HostingRedirector;
29  import org.apache.juddi.registry.RegistryEngine;
30  import org.apache.juddi.util.Config;
31  
32  /***
33   * @author Steve Viens (sviens@apache.org)
34   */
35  class BindingTemplateTable
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(BindingTemplateTable.class);
39  
40    static String insertSQL = null;
41    static String deleteSQL = null;
42    static String selectSQL = null;
43    static String selectByServiceKeySQL = null;
44    static String deleteByServiceKeySQL = null;
45    static String verifyOwnershipSQL = null;
46    static String tablePrefix = "";
47  
48    static
49    {
50      tablePrefix = Config.getStringProperty(
51          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
52      // buffer used to build SQL statements
53      StringBuffer sql = null;
54  
55      // build insertSQL
56      sql = new StringBuffer(150);
57      sql.append("INSERT INTO ").append(tablePrefix).append("BINDING_TEMPLATE (");
58      sql.append("SERVICE_KEY,");
59      sql.append("BINDING_KEY,");
60      sql.append("ACCESS_POINT_TYPE,");
61      sql.append("ACCESS_POINT_URL,");
62      sql.append("HOSTING_REDIRECTOR,");
63      sql.append("LAST_UPDATE) ");
64      sql.append("VALUES (?,?,?,?,?,?)");
65      insertSQL = sql.toString();
66  
67      // build deleteSQL
68      sql = new StringBuffer(100);
69      sql.append("DELETE FROM ").append(tablePrefix).append("BINDING_TEMPLATE ");
70      sql.append("WHERE BINDING_KEY=?");
71      deleteSQL = sql.toString();
72  
73      // build selectSQL
74      sql = new StringBuffer(200);
75      sql.append("SELECT ");
76      sql.append("SERVICE_KEY,");
77      sql.append("ACCESS_POINT_TYPE,");
78      sql.append("ACCESS_POINT_URL,");
79      sql.append("HOSTING_REDIRECTOR ");
80      sql.append("FROM ").append(tablePrefix).append("BINDING_TEMPLATE ");
81      sql.append("WHERE BINDING_KEY=?");
82      selectSQL = sql.toString();
83  
84      // build selectByServiceKeySQL
85      sql = new StringBuffer(200);
86      sql.append("SELECT ");
87      sql.append("BINDING_KEY,");
88      sql.append("ACCESS_POINT_TYPE,");
89      sql.append("ACCESS_POINT_URL,");
90      sql.append("HOSTING_REDIRECTOR ");
91      sql.append("FROM ").append(tablePrefix).append("BINDING_TEMPLATE ");
92      sql.append("WHERE SERVICE_KEY=?");
93      selectByServiceKeySQL = sql.toString();
94  
95      // build deleteByServiceKeySQL
96      sql = new StringBuffer(100);
97      sql.append("DELETE FROM BINDING_TEMPLATE ");
98      sql.append("WHERE SERVICE_KEY=?");
99      deleteByServiceKeySQL = sql.toString();
100 
101     // build verifyOwnershipSQL
102     sql = new StringBuffer(200);
103     sql.append("SELECT ");
104     sql.append("* ");
105     sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY e, ")
106                        .append(tablePrefix).append("BUSINESS_SERVICE s, ")
107                        .append(tablePrefix).append("BINDING_TEMPLATE t ");
108     sql.append("WHERE s.SERVICE_KEY = t.SERVICE_KEY ");
109     sql.append("AND e.BUSINESS_KEY = s.BUSINESS_KEY ");
110     sql.append("AND t.BINDING_KEY=? ");
111     sql.append("AND e.PUBLISHER_ID=?");
112     verifyOwnershipSQL = sql.toString();
113   }
114 
115   /***
116    * Insert new row into the BINDING_TEMPLATE table.
117    *
118    * @param  binding Binding Template object holding values to be inserted
119    * @param  connection JDBC connection
120    * @throws java.sql.SQLException
121    */
122   public static void insert(BindingTemplate binding,Connection connection)
123     throws java.sql.SQLException
124   {
125     PreparedStatement statement = null;
126     Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
127 
128     try
129     {
130       // pull the raw AccessPoint attributes out (if any)
131       String urlType = null;
132       String url = null;
133       AccessPoint accessPoint = binding.getAccessPoint();
134       if (accessPoint != null)
135       {
136         urlType = accessPoint.getURLType();
137         url = accessPoint.getURL();
138       }
139 
140       // pull the raw HostingRedirector attributes out (if any)
141       String redirectorKey = null;
142       HostingRedirector redirector = binding.getHostingRedirector();
143       if (redirector != null)
144       {
145         if (redirector.getBindingKey() != null)
146           redirectorKey = redirector.getBindingKey();
147       }
148 
149       // prepare and execute the insert
150       statement = connection.prepareStatement(insertSQL);
151       statement.setString(1,binding.getServiceKey().toString());
152       statement.setString(2,binding.getBindingKey().toString());
153       statement.setString(3,urlType);
154       statement.setString(4,url);
155       statement.setString(5,redirectorKey);
156       statement.setTimestamp(6,timeStamp);
157 
158       if (log.isDebugEnabled()) {
159           log.debug("insert into " + tablePrefix + "BINDING_TEMPLATE table:\n\n\t" + insertSQL +
160             "\n\t SERVICE_KEY=" + binding.getServiceKey().toString() +
161             "\n\t BINDING_KEY=" + binding.getBindingKey().toString() +
162             "\n\t ACCESS_POINT_TYPE=" + urlType +
163             "\n\t ACCESS_POINT_URL=" + url +
164             "\n\t HOSTING_REDIRECTOR=" + redirectorKey +
165             "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
166       }
167 
168       statement.executeUpdate();
169     }
170     finally
171     {
172       try { statement.close(); } catch (Exception e) { /* ignored */ }
173     }
174   }
175 
176   /***
177    * Delete row from the BINDING_TEMPLATE table.
178    *
179    * @param bindingKey primary key value
180    * @param connection JDBC connection
181    * @throws java.sql.SQLException
182    */
183   public static void delete(String bindingKey,Connection connection)
184     throws java.sql.SQLException
185   {
186     PreparedStatement statement = null;
187 
188     try
189     {
190       // prepare the delete
191       statement = connection.prepareStatement(deleteSQL);
192       statement.setString(1,bindingKey);
193 
194       if (log.isDebugEnabled()) {
195           log.debug("delete from " + tablePrefix + "BINDING_TEMPLATE table:\n\n\t" + deleteSQL +
196             "\n\t BINDING_KEY=" + bindingKey + "\n");
197       }
198 
199       // execute
200       statement.executeUpdate();
201     }
202     finally
203     {
204       try { statement.close(); } catch (Exception e) { /* ignored */ }
205     }
206   }
207 
208   /***
209    * Select one row from the BINDING_TEMPLATE table.
210    *
211    * @param bindingKey primary key value
212    * @param connection JDBC connection
213    * @throws java.sql.SQLException
214    */
215   public static BindingTemplate select(String bindingKey,Connection connection)
216     throws java.sql.SQLException
217   {
218     BindingTemplate binding = null;
219     PreparedStatement statement = null;
220     ResultSet resultSet = null;
221 
222     try
223     {
224       statement = connection.prepareStatement(selectSQL);
225       statement.setString(1,bindingKey.toString());
226 
227       if (log.isDebugEnabled()) {
228           log.debug("select from " + tablePrefix + "BINDING_TEMPLATE table:\n\n\t" + selectSQL +
229             "\n\t BINDING_KEY=" + bindingKey.toString() + "\n");
230       }
231 
232       resultSet = statement.executeQuery();
233       if (resultSet.next())
234       {
235         binding = new BindingTemplate();
236         binding.setServiceKey(resultSet.getString(1));//("SERVICE_KEY"));
237         binding.setBindingKey(bindingKey);
238 
239         String urlType = resultSet.getString(2);//("ACCESS_POINT_TYPE");
240         String url = resultSet.getString(3);//("ACCESS_POINT_URL");
241         if ((urlType != null) && (url != null))
242           binding.setAccessPoint(new AccessPoint(urlType,url));
243 
244         String redirectorKey = resultSet.getString(4);//("HOSTING_REDIRECTOR");
245         if (redirectorKey != null)
246           binding.setHostingRedirector(new HostingRedirector(redirectorKey));
247       }
248 
249       return binding;
250     }
251     finally
252     {
253       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
254       try { statement.close(); } catch (Exception e) { /* ignored */ }
255     }
256   }
257 
258   /***
259    * Select all rows from the business_service table for a given
260    * BusinessKey.
261    *
262    * @param  serviceKey ServiceKey
263    * @param  connection JDBC connection
264    * @throws java.sql.SQLException
265    */
266   public static Vector selectByServiceKey(String serviceKey,Connection connection)
267     throws java.sql.SQLException
268   {
269     Vector bindList = new Vector();
270     PreparedStatement statement = null;
271     ResultSet resultSet = null;
272 
273     try
274     {
275       // create a statement to query with
276       statement = connection.prepareStatement(selectByServiceKeySQL);
277       statement.setString(1,serviceKey.toString());
278 
279       if (log.isDebugEnabled()) {
280           log.debug("select from " + tablePrefix + "BINDING_TEMPLATE table:\n\n\t" + selectByServiceKeySQL +
281             "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
282       }
283 
284       // execute the statement
285       resultSet = statement.executeQuery();
286 
287       BindingTemplate binding = null;
288       while (resultSet.next())
289       {
290         binding = new BindingTemplate();
291         binding.setServiceKey(serviceKey);
292         binding.setBindingKey(resultSet.getString(1));//("BINDING_KEY"));
293 
294         String urlType = resultSet.getString(2);//("ACCESS_POINT_TYPE");
295         String url = resultSet.getString(3);//("ACCESS_POINT_URL");
296         if ((urlType != null) && (url != null))
297           binding.setAccessPoint(new AccessPoint(urlType,url));
298 
299         String redirectorKey = resultSet.getString(4);//("HOSTING_REDIRECTOR");
300         if (redirectorKey != null)
301           binding.setHostingRedirector(new HostingRedirector(redirectorKey));
302 
303         bindList.add(binding);
304         binding = null;
305       }
306 
307       return bindList;
308     }
309     finally
310     {
311       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
312       try { statement.close(); } catch (Exception e) { /* ignored */ }
313     }
314   }
315 
316   /***
317    * Delete multiple rows from the BINDING_TEMPLATE table that are assigned to
318    * the BusinessKey specified.
319    *
320    * @param  serviceKey ServiceKey
321    * @param  connection JDBC connection
322    * @throws java.sql.SQLException
323    */
324   public static void deleteByServiceKey(String serviceKey,Connection connection)
325     throws java.sql.SQLException
326   {
327     PreparedStatement statement = null;
328 
329     try
330     {
331       // prepare the delete
332       statement = connection.prepareStatement(deleteByServiceKeySQL);
333       statement.setString(1,serviceKey.toString());
334 
335       if (log.isDebugEnabled()) {
336           log.debug("delete from " + tablePrefix + "BINDING_TEMPLATE table:\n\n\t" + deleteByServiceKeySQL +
337             "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
338       }
339 
340       // execute
341       int returnCode = statement.executeUpdate();
342 
343       log.info("delete was successful, rows deleted=" + returnCode);
344     }
345     finally
346     {
347       try { statement.close(); } catch (Exception e) { /* ignored */ }
348     }
349   }
350 
351   /***
352    * Verify that 'authorizedName' has the authority to update or delete
353    * BindingTemplate identified by the bindingKey parameter
354    *
355    * @param bindingKey
356    * @param publisherID
357    * @param connection
358    * @throws java.sql.SQLException
359    */
360   public static boolean verifyOwnership(String bindingKey,String publisherID,Connection connection)
361     throws java.sql.SQLException
362   {
363     if ((bindingKey == null) || (publisherID == null))
364       return false;
365 
366     boolean authorized = false;
367     PreparedStatement statement = null;
368     ResultSet resultSet = null;
369 
370     try
371     {
372       statement = connection.prepareStatement(verifyOwnershipSQL);
373       statement.setString(1,bindingKey);
374       statement.setString(2,publisherID);
375 
376       if (log.isDebugEnabled()) {
377           log.debug("checking ownership of BINDING_TEMPLATE:\n\n\t" + verifyOwnershipSQL +
378             "\n\t BINDNG_KEY=" + bindingKey +
379             "\n\t PUBLISHER_ID=" + publisherID + "\n");
380       }
381 
382       resultSet = statement.executeQuery();
383       if (resultSet.next())
384         authorized = true;
385 
386       return authorized;
387     }
388     finally
389     {
390       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
391       try { statement.close(); } catch (Exception e) { /* ignored */ }
392     }
393   }
394 }