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.service.BusinessService;
27  import org.apache.juddi.registry.RegistryEngine;
28  import org.apache.juddi.util.Config;
29  
30  /***
31   * @author Steve Viens (sviens@apache.org)
32   */
33  class BusinessServiceTable
34  {
35    // private reference to the jUDDI logger
36    private static Log log = LogFactory.getLog(BusinessServiceTable.class);
37  
38    static String insertSQL = null;
39    static String deleteSQL = null;
40    static String selectSQL = null;
41    static String selectByBusinessKeySQL = null;
42    static String deleteByBusinessKeySQL = null;
43    static String verifyOwnershipSQL = null;
44    static String tablePrefix = "";
45  
46    static
47    {
48      tablePrefix = Config.getStringProperty(
49          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
50      // buffer used to build SQL statements
51      StringBuffer sql = null;
52  
53      // build insertSQL
54      sql = new StringBuffer(100);
55      sql.append("INSERT INTO ").append(tablePrefix).append("BUSINESS_SERVICE (");
56      sql.append("BUSINESS_KEY,");
57      sql.append("SERVICE_KEY,");
58      sql.append("LAST_UPDATE) ");
59      sql.append("VALUES (?,?,?)");
60      insertSQL = sql.toString();
61  
62      // build deleteSQL
63      sql = new StringBuffer(100);
64      sql.append("DELETE FROM ").append(tablePrefix).append("BUSINESS_SERVICE ");
65      sql.append("WHERE SERVICE_KEY=?");
66      deleteSQL = sql.toString();
67  
68      // build selectSQL
69      sql = new StringBuffer(100);
70      sql.append("SELECT ");
71      sql.append("BUSINESS_KEY ");
72      sql.append("FROM ").append(tablePrefix).append("BUSINESS_SERVICE ");
73      sql.append("WHERE SERVICE_KEY=?");
74      selectSQL = sql.toString();
75  
76      // build selectByBusinessKeySQL
77      sql = new StringBuffer(200);
78      sql.append("SELECT ");
79      sql.append("SERVICE_KEY ");
80      sql.append("FROM ").append(tablePrefix).append("BUSINESS_SERVICE ");
81      sql.append("WHERE BUSINESS_KEY=?");
82      selectByBusinessKeySQL = sql.toString();
83  
84      // build deleteByBusinessKeySQL
85      sql = new StringBuffer(100);
86      sql.append("DELETE FROM ").append(tablePrefix).append("BUSINESS_SERVICE ");
87      sql.append("WHERE BUSINESS_KEY=?");
88      deleteByBusinessKeySQL = sql.toString();
89  
90      // build verifyOwnershipSQL
91      sql = new StringBuffer(200);
92      sql.append("SELECT ");
93      sql.append("* ");
94      sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY e, ").append(tablePrefix).append("BUSINESS_SERVICE s ");
95      sql.append("WHERE e.BUSINESS_KEY = s.BUSINESS_KEY ");
96      sql.append("AND s.SERVICE_KEY=? ");
97      sql.append("AND e.PUBLISHER_ID=?");
98      verifyOwnershipSQL = sql.toString();
99    }
100 
101   /***
102    * Insert new row into the BUSINESS_ENTITIES table.
103    *
104    * @param service object holding values to be inserted
105    * @param connection JDBC connection
106    * @throws java.sql.SQLException
107    */
108   public static void insert(BusinessService service,Connection connection)
109     throws java.sql.SQLException
110   {
111     PreparedStatement statement = null;
112     Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
113 
114     try
115     {
116       statement = connection.prepareStatement(insertSQL);
117       statement.setString(1,service.getBusinessKey().toString());
118       statement.setString(2,service.getServiceKey().toString());
119       statement.setTimestamp(3,timeStamp);
120 
121       if (log.isDebugEnabled()) {
122           log.debug("insert into " + tablePrefix + "BUSINESS_SERVICE table:\n\n\t" + insertSQL +
123             "\n\t BUSINESS_KEY=" + service.getBusinessKey().toString() +
124             "\n\t SERVICE_KEY=" + service.getServiceKey().toString() +
125             "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
126       }
127 
128       statement.executeUpdate();
129     }
130     finally
131     {
132       try { statement.close(); } catch (Exception e) { /* ignored */ }
133     }
134   }
135 
136   /***
137    * Delete row from the BUSINESS_SERVICE table.
138    *
139    * @param serviceKey Primary key value
140    * @param connection JDBC connection
141    * @throws java.sql.SQLException
142    */
143   public static void delete(String serviceKey,Connection connection)
144     throws java.sql.SQLException
145   {
146     PreparedStatement statement = null;
147 
148     try
149     {
150       // prepare
151       statement = connection.prepareStatement(deleteSQL);
152       statement.setString(1,serviceKey.toString());
153 
154       if (log.isDebugEnabled()) {
155           log.debug("delete from " + tablePrefix + "BUSINESS_SERVICE table:\n\n\t" + deleteSQL +
156             "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
157       }
158 
159       // execute
160       statement.executeUpdate();
161     }
162     finally
163     {
164       try { statement.close(); } catch (Exception e) { /* ignored */ }
165     }
166   }
167 
168   /***
169    * Select one row from the BUSINESS_SERVICE table.
170    *
171    * @param serviceKey primary key value
172    * @param connection JDBC connection
173    * @throws java.sql.SQLException
174    */
175   public static BusinessService select(String serviceKey,Connection connection)
176     throws java.sql.SQLException
177   {
178     BusinessService service = null;
179     PreparedStatement statement = null;
180     ResultSet resultSet = null;
181 
182     try
183     {
184       statement = connection.prepareStatement(selectSQL);
185       statement.setString(1,serviceKey.toString());
186 
187       if (log.isDebugEnabled()) {
188           log.debug("select from " + tablePrefix + "BUSINESS_SERVICE table:\n\n\t" + selectSQL +
189             "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
190       }
191 
192       resultSet = statement.executeQuery();
193       if (resultSet.next())
194       {
195         service = new BusinessService();
196         service.setBusinessKey(resultSet.getString(1));//("BUSINESS_KEY"));
197         service.setServiceKey(serviceKey);
198       }
199 
200       return service;
201     }
202     finally
203     {
204       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
205       try { statement.close(); } catch (Exception e) { /* ignored */ }
206     }
207   }
208 
209   /***
210    * Delete multiple rows from the BUSINESS_SERVICE table that are assigned to
211    * the BusinessKey specified.
212    *
213    * @param businessKey BusinessKey
214    * @param connection JDBC connection
215    * @throws java.sql.SQLException
216    */
217   public static void deleteByBusinessKey(String businessKey,Connection connection)
218     throws java.sql.SQLException
219   {
220     PreparedStatement statement = null;
221 
222     try
223     {
224       // prepare the delete
225       statement = connection.prepareStatement(deleteByBusinessKeySQL);
226       statement.setString(1,businessKey.toString());
227 
228       if (log.isDebugEnabled()) {
229           log.debug("delet from the " + tablePrefix + "BUSINESS_SERVICE table:\n\n\t" + deleteByBusinessKeySQL +
230             "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
231       }
232 
233       // execute
234       statement.executeUpdate();
235     }
236     finally
237     {
238       try { statement.close(); } catch (Exception e) { /* ignored */ }
239     }
240   }
241 
242   /***
243    * Select all rows from the business_service table for
244    * a given BusinessKey.
245    *
246    * @param businessKey BusinessKey
247    * @param connection JDBC connection
248    * @throws java.sql.SQLException
249    */
250   public static Vector selectByBusinessKey(String businessKey,Connection connection)
251     throws java.sql.SQLException
252   {
253     Vector serviceList = new Vector();
254     PreparedStatement statement = null;
255     ResultSet resultSet = null;
256 
257     try
258     {
259      // create a statement to query with
260       statement = connection.prepareStatement(selectByBusinessKeySQL);
261       statement.setString(1,businessKey.toString());
262 
263       if (log.isDebugEnabled()) {
264           log.debug("select from " + tablePrefix + "BUSINESS_SERVICE table:\n\n\t" + selectByBusinessKeySQL +
265             "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
266       }
267 
268       // execute the statement
269       resultSet = statement.executeQuery();
270       while (resultSet.next())
271       {
272         BusinessService service = new BusinessService();
273         service.setBusinessKey(businessKey);
274         service.setServiceKey(resultSet.getString(1));//("SERVICE_KEY"));
275         serviceList.add(service);
276       }
277 
278       return serviceList;
279     }
280     finally
281     {
282       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
283       try { statement.close(); } catch (Exception e) { /* ignored */ }
284     }
285   }
286 
287   /***
288    * Verify that 'authorizedName' has the authority to update or delete
289    * BusinessService identified by the serviceKey parameter
290    *
291    * @param serviceKey
292    * @param publisherID
293    * @param connection
294    * @throws java.sql.SQLException
295    */
296   public static boolean verifyOwnership(String serviceKey,String publisherID,Connection connection)
297     throws java.sql.SQLException
298   {
299     if ((serviceKey == null) || (publisherID == null))
300       return false;
301 
302     boolean authorized = false;
303     PreparedStatement statement = null;
304     ResultSet resultSet = null;
305 
306     try
307     {
308       statement = connection.prepareStatement(verifyOwnershipSQL);
309       statement.setString(1,serviceKey);
310       statement.setString(2,publisherID);
311 
312       if (log.isDebugEnabled()) {
313       log.debug("checking ownership of " + tablePrefix + "BUSINESS_SERVICE:\n\n\t" + verifyOwnershipSQL +
314         "\n\t SERVICE_KEY=" + serviceKey +
315         "\n\t PUBLISHER_ID=" + publisherID + "\n");
316       }
317 
318       resultSet = statement.executeQuery();
319       if (resultSet.next())
320         authorized = true;
321 
322       return authorized;
323     }
324     finally
325     {
326       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
327       try { statement.close(); } catch (Exception e) { /* ignored */ }
328     }
329   }
330 }