KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > BindingTemplateTable


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 JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Vector JavaDoc;
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
30 /**
31  * @author Steve Viens (sviens@apache.org)
32  */

33 class BindingTemplateTable
34 {
35   // private reference to the jUDDI logger
36
private static Log log = LogFactory.getLog(BindingTemplateTable.class);
37
38   static String JavaDoc insertSQL = null;
39   static String JavaDoc deleteSQL = null;
40   static String JavaDoc selectSQL = null;
41   static String JavaDoc selectByServiceKeySQL = null;
42   static String JavaDoc deleteByServiceKeySQL = null;
43   static String JavaDoc verifyOwnershipSQL = null;
44
45   static
46   {
47     // buffer used to build SQL statements
48
StringBuffer JavaDoc sql = null;
49
50     // build insertSQL
51
sql = new StringBuffer JavaDoc(150);
52     sql.append("INSERT INTO BINDING_TEMPLATE (");
53     sql.append("SERVICE_KEY,");
54     sql.append("BINDING_KEY,");
55     sql.append("ACCESS_POINT_TYPE,");
56     sql.append("ACCESS_POINT_URL,");
57     sql.append("HOSTING_REDIRECTOR,");
58     sql.append("LAST_UPDATE) ");
59     sql.append("VALUES (?,?,?,?,?,?)");
60     insertSQL = sql.toString();
61
62     // build deleteSQL
63
sql = new StringBuffer JavaDoc(100);
64     sql.append("DELETE FROM BINDING_TEMPLATE ");
65     sql.append("WHERE BINDING_KEY=?");
66     deleteSQL = sql.toString();
67
68     // build selectSQL
69
sql = new StringBuffer JavaDoc(200);
70     sql.append("SELECT ");
71     sql.append("SERVICE_KEY,");
72     sql.append("ACCESS_POINT_TYPE,");
73     sql.append("ACCESS_POINT_URL,");
74     sql.append("HOSTING_REDIRECTOR ");
75     sql.append("FROM BINDING_TEMPLATE ");
76     sql.append("WHERE BINDING_KEY=?");
77     selectSQL = sql.toString();
78
79     // build selectByServiceKeySQL
80
sql = new StringBuffer JavaDoc(200);
81     sql.append("SELECT ");
82     sql.append("BINDING_KEY,");
83     sql.append("ACCESS_POINT_TYPE,");
84     sql.append("ACCESS_POINT_URL,");
85     sql.append("HOSTING_REDIRECTOR ");
86     sql.append("FROM BINDING_TEMPLATE ");
87     sql.append("WHERE SERVICE_KEY=?");
88     selectByServiceKeySQL = sql.toString();
89
90     // build deleteByServiceKeySQL
91
sql = new StringBuffer JavaDoc(100);
92     sql.append("DELETE FROM BINDING_TEMPLATE ");
93     sql.append("WHERE SERVICE_KEY=?");
94     deleteByServiceKeySQL = sql.toString();
95
96     // build verifyOwnershipSQL
97
sql = new StringBuffer JavaDoc(200);
98     sql.append("SELECT ");
99     sql.append("* ");
100     sql.append("FROM BUSINESS_ENTITY e, BUSINESS_SERVICE s, BINDING_TEMPLATE t ");
101     sql.append("WHERE s.SERVICE_KEY = t.SERVICE_KEY ");
102     sql.append("AND e.BUSINESS_KEY = s.BUSINESS_KEY ");
103     sql.append("AND t.BINDING_KEY=? ");
104     sql.append("AND e.PUBLISHER_ID=?");
105     verifyOwnershipSQL = sql.toString();
106   }
107
108   /**
109    * Insert new row into the BINDING_TEMPLATE table.
110    *
111    * @param binding Binding Template object holding values to be inserted
112    * @param connection JDBC connection
113    * @throws java.sql.SQLException
114    */

115   public static void insert(BindingTemplate binding,Connection JavaDoc connection)
116     throws java.sql.SQLException JavaDoc
117   {
118     PreparedStatement JavaDoc statement = null;
119     Timestamp JavaDoc timeStamp = new Timestamp JavaDoc(System.currentTimeMillis());
120
121     try
122     {
123       // pull the raw AccessPoint attributes out (if any)
124
String JavaDoc urlType = null;
125       String JavaDoc url = null;
126       AccessPoint accessPoint = binding.getAccessPoint();
127       if (accessPoint != null)
128       {
129         urlType = accessPoint.getURLType();
130         url = accessPoint.getURL();
131       }
132
133       // pull the raw HostingRedirector attributes out (if any)
134
String JavaDoc redirectorKey = null;
135       HostingRedirector redirector = binding.getHostingRedirector();
136       if (redirector != null)
137       {
138         if (redirector.getBindingKey() != null)
139           redirectorKey = redirector.getBindingKey();
140       }
141
142       // prepare and execute the insert
143
statement = connection.prepareStatement(insertSQL);
144       statement.setString(1,binding.getServiceKey().toString());
145       statement.setString(2,binding.getBindingKey().toString());
146       statement.setString(3,urlType);
147       statement.setString(4,url);
148       statement.setString(5,redirectorKey);
149       statement.setTimestamp(6,timeStamp);
150
151       log.debug("insert into BINDING_TEMPLATE table:\n\n\t" + insertSQL +
152         "\n\t SERVICE_KEY=" + binding.getServiceKey().toString() +
153         "\n\t BINDING_KEY=" + binding.getBindingKey().toString() +
154         "\n\t ACCESS_POINT_TYPE=" + urlType +
155         "\n\t ACCESS_POINT_URL=" + url +
156         "\n\t HOSTING_REDIRECTOR=" + redirectorKey +
157         "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
158
159       statement.executeUpdate();
160     }
161     finally
162     {
163       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
164     }
165   }
166
167   /**
168    * Delete row from the BINDING_TEMPLATE table.
169    *
170    * @param bindingKey primary key value
171    * @param connection JDBC connection
172    * @throws java.sql.SQLException
173    */

174   public static void delete(String JavaDoc bindingKey,Connection JavaDoc connection)
175     throws java.sql.SQLException JavaDoc
176   {
177     PreparedStatement JavaDoc statement = null;
178
179     try
180     {
181       // prepare the delete
182
statement = connection.prepareStatement(deleteSQL);
183       statement.setString(1,bindingKey);
184
185       log.debug("delete from BINDING_TEMPLATE table:\n\n\t" + deleteSQL +
186         "\n\t BINDING_KEY=" + bindingKey + "\n");
187
188       // execute
189
statement.executeUpdate();
190     }
191     finally
192     {
193       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
194     }
195   }
196
197   /**
198    * Select one row from the BINDING_TEMPLATE table.
199    *
200    * @param bindingKey primary key value
201    * @param connection JDBC connection
202    * @throws java.sql.SQLException
203    */

204   public static BindingTemplate select(String JavaDoc bindingKey,Connection JavaDoc connection)
205     throws java.sql.SQLException JavaDoc
206   {
207     BindingTemplate binding = null;
208     PreparedStatement JavaDoc statement = null;
209     ResultSet JavaDoc resultSet = null;
210
211     try
212     {
213       statement = connection.prepareStatement(selectSQL);
214       statement.setString(1,bindingKey.toString());
215
216       log.debug("select from BINDING_TEMPLATE table:\n\n\t" + selectSQL +
217         "\n\t BINDING_KEY=" + bindingKey.toString() + "\n");
218
219       resultSet = statement.executeQuery();
220       if (resultSet.next())
221       {
222         binding = new BindingTemplate();
223         binding.setServiceKey(resultSet.getString(1));//("SERVICE_KEY"));
224
binding.setBindingKey(bindingKey);
225
226         String JavaDoc urlType = resultSet.getString(2);//("ACCESS_POINT_TYPE");
227
String JavaDoc url = resultSet.getString(3);//("ACCESS_POINT_URL");
228
if ((urlType != null) && (url != null))
229           binding.setAccessPoint(new AccessPoint(urlType,url));
230
231         String JavaDoc redirectorKey = resultSet.getString(4);//("HOSTING_REDIRECTOR");
232
if (redirectorKey != null)
233           binding.setHostingRedirector(new HostingRedirector(redirectorKey));
234       }
235
236       return binding;
237     }
238     finally
239     {
240       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
241       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
242     }
243   }
244
245   /**
246    * Select all rows from the business_service table for a given
247    * BusinessKey.
248    *
249    * @param serviceKey ServiceKey
250    * @param connection JDBC connection
251    * @throws java.sql.SQLException
252    */

253   public static Vector JavaDoc selectByServiceKey(String JavaDoc serviceKey,Connection JavaDoc connection)
254     throws java.sql.SQLException JavaDoc
255   {
256     Vector JavaDoc bindList = new Vector JavaDoc();
257     PreparedStatement JavaDoc statement = null;
258     ResultSet JavaDoc resultSet = null;
259
260     try
261     {
262       // create a statement to query with
263
statement = connection.prepareStatement(selectByServiceKeySQL);
264       statement.setString(1,serviceKey.toString());
265
266       log.debug("select from BINDING_TEMPLATE table:\n\n\t" + selectByServiceKeySQL +
267         "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
268
269       // execute the statement
270
resultSet = statement.executeQuery();
271
272       BindingTemplate binding = null;
273       while (resultSet.next())
274       {
275         binding = new BindingTemplate();
276         binding.setServiceKey(serviceKey);
277         binding.setBindingKey(resultSet.getString(1));//("BINDING_KEY"));
278

279         String JavaDoc urlType = resultSet.getString(2);//("ACCESS_POINT_TYPE");
280
String JavaDoc url = resultSet.getString(3);//("ACCESS_POINT_URL");
281
if ((urlType != null) && (url != null))
282           binding.setAccessPoint(new AccessPoint(urlType,url));
283
284         String JavaDoc redirectorKey = resultSet.getString(4);//("HOSTING_REDIRECTOR");
285
if (redirectorKey != null)
286           binding.setHostingRedirector(new HostingRedirector(redirectorKey));
287
288         bindList.add(binding);
289         binding = null;
290       }
291
292       return bindList;
293     }
294     finally
295     {
296       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
297       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
298     }
299   }
300
301   /**
302    * Delete multiple rows from the BINDING_TEMPLATE table that are assigned to
303    * the BusinessKey specified.
304    *
305    * @param serviceKey ServiceKey
306    * @param connection JDBC connection
307    * @throws java.sql.SQLException
308    */

309   public static void deleteByServiceKey(String JavaDoc serviceKey,Connection JavaDoc connection)
310     throws java.sql.SQLException JavaDoc
311   {
312     PreparedStatement JavaDoc statement = null;
313
314     try
315     {
316       // prepare the delete
317
statement = connection.prepareStatement(deleteByServiceKeySQL);
318       statement.setString(1,serviceKey.toString());
319
320       log.debug("delete from BINDING_TEMPLATE table:\n\n\t" + deleteByServiceKeySQL +
321         "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
322
323       // execute
324
int returnCode = statement.executeUpdate();
325
326       log.info("delete was successful, rows deleted=" + returnCode);
327     }
328     finally
329     {
330       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
331     }
332   }
333
334   /**
335    * Verify that 'authorizedName' has the authority to update or delete
336    * BindingTemplate identified by the bindingKey parameter
337    *
338    * @param bindingKey
339    * @param publisherID
340    * @param connection
341    * @throws java.sql.SQLException
342    */

343   public static boolean verifyOwnership(String JavaDoc bindingKey,String JavaDoc publisherID,Connection JavaDoc connection)
344     throws java.sql.SQLException JavaDoc
345   {
346     if ((bindingKey == null) || (publisherID == null))
347       return false;
348
349     boolean authorized = false;
350     PreparedStatement JavaDoc statement = null;
351     ResultSet JavaDoc resultSet = null;
352
353     try
354     {
355       statement = connection.prepareStatement(verifyOwnershipSQL);
356       statement.setString(1,bindingKey);
357       statement.setString(2,publisherID);
358
359       log.debug("checking ownership of BINDING_TEMPLATE:\n\n\t" + verifyOwnershipSQL +
360         "\n\t BINDNG_KEY=" + bindingKey +
361         "\n\t PUBLISHER_ID=" + publisherID + "\n");
362
363       resultSet = statement.executeQuery();
364       if (resultSet.next())
365         authorized = true;
366
367       return authorized;
368     }
369     finally
370     {
371       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
372       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
373     }
374   }
375 }
Popular Tags