View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.ebxml.registry.persistence.rdb; 12 13 import com.sun.ebxml.registry.RegistryException; 14 import com.sun.ebxml.registry.util.BindingUtility; 15 import org.oasis.ebxml.registry.bindings.rim.*; 16 17 import java.sql.Connection; 18 import java.sql.ResultSet; 19 import java.sql.SQLException; 20 import java.sql.Statement; 21 import java.util.ArrayList; 22 import java.util.Iterator; 23 24 /*** 25 * Maps to a BindingTemplate in UDDI. 26 * 27 * @see <{Concept}> 28 * @author Farrukh S. Najmi 29 * @author Kathy Walsh 30 * @author Adrian Chong 31 */ 32 public class ServiceBindingDAO extends RegistryObjectDAO { 33 protected ServiceBindingDAO(){} 34 35 public static String getTableNameStatic() { 36 return "ServiceBinding"; 37 } 38 39 public String getTableName() { 40 return getTableNameStatic(); 41 } 42 43 /*** 44 * Does a bulk insert of a Collection of objects that match the type for this persister. 45 * Throw InvalidURLsException if any ExternalLink pointing to any unresolvable Http URL. 46 * Those URLs with response code < 200 or > 299 are considered invalid. 47 */ 48 public void insert(User user, Connection connection, ArrayList serviceBindings) throws RegistryException { 49 if (serviceBindings.size()==0) { 50 return; 51 } 52 53 System.err.println("Inserting " + serviceBindings.size() + " ServiceBindings"); 54 String serviceBindingId = null; 55 Statement stmt = null; 56 try { 57 // Update the exisitng Service 58 serviceBindings = updateExistingObjects(user, connection , serviceBindings); 59 if (serviceBindings.size()==0) { 60 return; 61 } 62 63 // check those ServiceBinding with http url 64 65 String check = com.sun.ebxml.registry.util.RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.ServiceBindingDAO.checkURLs"); 66 67 if (check.equalsIgnoreCase("true")) { 68 ArrayList invalidServiceBindings = com.sun.ebxml.registry.util.Utility.getInstance() 69 .validateURIs(serviceBindings); 70 if (invalidServiceBindings.size() > 0) { 71 throw new com.sun.ebxml.registry.lcm.InvalidURLsException(invalidServiceBindings); 72 } 73 } 74 75 stmt = connection.createStatement(); 76 77 Iterator iter = serviceBindings.iterator(); 78 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 79 BindingUtility bindingUtility = BindingUtility.getInstance(); 80 while (iter.hasNext()) { 81 Object obj = iter.next(); 82 if (!(obj instanceof ServiceBinding)) { 83 throw new RegistryException("Error: expected ServiceBinding, got " + obj); 84 } 85 ServiceBinding serviceBinding = (ServiceBinding)obj; 86 serviceBindingId = serviceBinding.getId(); 87 String targetBindingId = bindingUtility.getObjectId(serviceBinding.getTargetBinding()); 88 if (targetBindingId != null) { 89 targetBindingId = "'" + targetBindingId + "'"; 90 } 91 92 String serviceId = BindingUtility.getInstance(). 93 getObjectId(serviceBinding.getService()); 94 95 if (serviceId == null) { 96 throw new RegistryException("ServiceBinding with id=" + 97 serviceBinding.getId() + " has no parent Service"); 98 } 99 100 String sql = "INSERT INTO ServiceBinding " + 101 "VALUES(null, " + //AccessControlPolicy 102 "'" + serviceBinding.getId() + "', " + 103 "'ServiceBinding', " + 104 "'" + serviceId + "', " + 105 "'" + serviceBinding.getAccessURI() + "', " + 106 targetBindingId + ") "; 107 //System.err.println(sql); 108 stmt.addBatch(sql); 109 110 111 112 //Now insert any composed SpecificationLink 113 org.oasis.ebxml.registry.bindings.rim.SpecificationLink[] specLinks = serviceBinding.getSpecificationLink(); 114 ArrayList specLinksAL = new ArrayList(); 115 for (int i=0; i<specLinks.length; i++) { 116 //Version 2.1 is missing serviceBinding attribute on SPecificationLink 117 //Using special slots to pass the service and binding ids. 118 Slot serviceSlot = new Slot(); 119 serviceSlot.setName("urn:ebxmlrr:SpecificationLink:service"); 120 ValueList values = new ValueList(); 121 ValueListTypeItem item = new ValueListTypeItem(); 122 item.setValue(serviceId); 123 values.addValueListTypeItem(item); 124 serviceSlot.setValueList(values); 125 specLinks[i].addSlot(serviceSlot); 126 127 Slot bindingSlot = new Slot(); 128 bindingSlot.setName("urn:ebxmlrr:SpecificationLink:serviceBinding"); 129 values = new ValueList(); 130 item = new ValueListTypeItem(); 131 item.setValue(serviceBindingId); 132 values.addValueListTypeItem(item); 133 bindingSlot.setValueList(values); 134 specLinks[i].addSlot(bindingSlot); 135 136 specLinksAL.add(specLinks[i]); 137 } 138 if (specLinksAL.size() > 0) { 139 specificationLinkDAO.insert(user, connection, specLinksAL); 140 } 141 142 } 143 144 if (serviceBindings.size() > 0) { 145 int [] updateCounts = stmt.executeBatch(); 146 //stmt.close(); 147 super.insert(user, connection, serviceBindings); 148 // Now generate the AE for inserting ServiceBinding 149 generateAuditbleEvent(connection, serviceBindings, "Created", user); 150 } 151 } 152 catch (SQLException e) { 153 e.printStackTrace(); 154 // It is trying to find out whether the SQLException is because 155 // the ServiceBinding already exist 156 if (serviceBindingId != null && registryObjectExist(connection, serviceBindingId)) { 157 throw new com.sun.ebxml.registry.lcm.RegistryObjectExistsException(serviceBindingId); 158 } 159 RegistryException exception = new RegistryException(e); 160 throw exception; 161 } finally { 162 try { 163 if (stmt != null) 164 stmt.close(); 165 } catch (SQLException sqle) { 166 sqle.printStackTrace(); 167 } 168 } 169 } 170 171 public void update(User user, Connection connection, ArrayList serviceBindings) throws RegistryException { 172 /* 173 * We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 174 * Should we move the authorization to persistance layer?????? 175 */ 176 update(user, connection, serviceBindings, true, false); 177 } 178 179 public void update(User user, Connection connection, ArrayList serviceBindings, boolean checkExistence, boolean checkAuthorization) throws RegistryException { 180 Statement stmt = null; 181 try { 182 // check those ServiceBinding with http url 183 184 String check = com.sun.ebxml.registry.util.RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.ServiceBindingDAO.checkURLs"); 185 186 if (check.equalsIgnoreCase("true")) { 187 ArrayList invalidServiceBindings = com.sun.ebxml.registry.util.Utility.getInstance() 188 .validateURIs(serviceBindings); 189 if (invalidServiceBindings.size() > 0) { 190 throw new com.sun.ebxml.registry.lcm.InvalidURLsException(invalidServiceBindings); 191 } 192 } 193 194 stmt = connection.createStatement(); 195 196 Iterator iter = serviceBindings.iterator(); 197 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 198 java.util.HashMap idMap = new java.util.HashMap(); 199 ArrayList serviceBindingIds = new ArrayList(); 200 ArrayList specificationLinks = new ArrayList(); 201 202 while (iter.hasNext()) { 203 ServiceBinding serviceBinding = (ServiceBinding)iter.next(); 204 String serviceBindingId = serviceBinding.getId(); 205 206 // checking whether it exists 207 if (!registryObjectExist(connection, serviceBindingId, getTableName())) { 208 ArrayList idList = new ArrayList(); 209 idList.add(serviceBindingId); 210 throw new com.sun.ebxml.registry.lcm.ObjectsNotFoundException(idList); 211 } 212 213 String accessURI = serviceBinding.getAccessURI(); 214 if (accessURI != null) { 215 accessURI = "'" + accessURI + "'"; 216 } 217 218 String targetBindingId = BindingUtility.getInstance() 219 .getObjectId(serviceBinding.getTargetBinding()); 220 if (targetBindingId != null) { 221 targetBindingId = "'" + targetBindingId + "'"; 222 } 223 224 String serviceId = BindingUtility.getInstance(). 225 getObjectId(serviceBinding.getService()); 226 227 // ServiceBinding is no longer first level, but are they identified by id or id + parent?? 228 String sql = "UPDATE " + getTableName() + 229 " SET accessControlPolicy=null" + 230 ", service='" + serviceId + "'" + 231 ", accessURI=" + accessURI + 232 ", targetBinding=" + targetBindingId + 233 " WHERE id='" + serviceBindingId + "'"; 234 235 //System.err.println(sql); 236 stmt.addBatch(sql); 237 238 serviceBindingIds.add(serviceBindingId); 239 240 //Now remember any composed SpecificationLinks for updating them later 241 org.oasis.ebxml.registry.bindings.rim.SpecificationLink[] specLinks = serviceBinding.getSpecificationLink(); 242 for (int i = 0; i < specLinks.length; i++) { 243 //Version 2.1 is missing serviceBinding attribute on SPecificationLink 244 //Using special slots to pass the service and binding ids. 245 Slot serviceSlot = new Slot(); 246 serviceSlot.setName("urn:ebxmlrr:SpecificationLink:service"); 247 ValueList values = new ValueList(); 248 ValueListTypeItem item = new ValueListTypeItem(); 249 item.setValue(serviceId); 250 values.addValueListTypeItem(item); 251 serviceSlot.setValueList(values); 252 specLinks[i].addSlot(serviceSlot); 253 254 Slot bindingSlot = new Slot(); 255 bindingSlot.setName("urn:ebxmlrr:SpecificationLink:serviceBinding"); 256 values = new ValueList(); 257 item = new ValueListTypeItem(); 258 item.setValue(serviceBindingId); 259 values.addValueListTypeItem(item); 260 bindingSlot.setValueList(values); 261 specLinks[i].addSlot(bindingSlot); 262 263 specificationLinks.add(specLinks[i]); 264 } 265 266 } // end looping serviceBindings 267 268 if (serviceBindings.size() > 0) { 269 int [] updateCounts = stmt.executeBatch(); 270 //stmt.close(); 271 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 272 super.update(user, connection, serviceBindings); 273 // Now generate the AE for inserting ServiceBinding 274 generateAuditbleEvent(connection, serviceBindings, "Updated", user); 275 276 // Delete the SpecificationLinks that were submitted 277 specificationLinkDAO.deleteByParentIds(user, connection, serviceBindingIds); 278 // Insert the SpecificationLinks in this update request 279 // Firstly fix the ids if composed new SpecificationLinks 280 com.sun.ebxml.registry.lcm.LifeCycleManagerImpl lcm = com.sun.ebxml.registry.lcm.LifeCycleManagerImpl.getInstance(); 281 lcm.fixTemporaryIds(serviceBindings, idMap); 282 // Now insert the composed new SpecificationLinks 283 specificationLinkDAO.insert(user, connection, specificationLinks); 284 } 285 } 286 catch (SQLException e) { 287 e.printStackTrace(); 288 RegistryException exception = new RegistryException(e); 289 throw exception; 290 } finally { 291 try { 292 if (stmt != null) 293 stmt.close(); 294 } catch (SQLException sqle) { 295 sqle.printStackTrace(); 296 } 297 } 298 } 299 300 // Should we make index on targetBinding in ServiceBinding???? 301 protected String checkServiceBindingReferences(Connection conn, String serviceBindingId) throws RegistryException { 302 String referencingServiceBindingId = null; 303 Statement stmt = null; 304 try { 305 stmt = conn.createStatement(); 306 String sql = "SELECT id FROM ServiceBinding WHERE " 307 + "targetBinding='" + serviceBindingId + "' AND targetBinding IS NOT NULL"; 308 ResultSet rs = stmt.executeQuery(sql); 309 if (rs.next()) { 310 referencingServiceBindingId = rs.getString("id"); 311 } 312 //stmt.close(); 313 return referencingServiceBindingId; 314 } 315 catch (SQLException e){ 316 throw new RegistryException(e); 317 } finally { 318 try { 319 if (stmt != null) 320 stmt.close(); 321 } catch (SQLException sqle) { 322 sqle.printStackTrace(); 323 } 324 } 325 } 326 327 /*** 328 * Does a bulk delete of a Collection of objects that match the type for this persister. 329 * 330 */ 331 public void delete(User user, Connection connection, ArrayList serviceBindingsIds) throws RegistryException { 332 Iterator iter = serviceBindingsIds.iterator(); 333 // Check whether this ServiceBinding is targetBinding of any other ServiceBinding 334 while(iter.hasNext()) { 335 String serviceBindingId = (String)iter.next(); 336 String targetBindingId = checkServiceBindingReferences(connection, serviceBindingId); 337 if (targetBindingId != null) { 338 throw new com.sun.ebxml.registry.lcm.ReferencesExistException("ServiceBinding " + targetBindingId + " is referencing " + serviceBindingId); 339 } 340 } 341 super.delete(user, connection, serviceBindingsIds); 342 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 343 specificationLinkDAO.deleteByParentIds(user, connection, serviceBindingsIds); 344 } 345 346 /*** 347 * Does a bulk delete of objects for specified parentIds 348 * 349 */ 350 public void deleteByParentIds(User user, Connection connection, ArrayList parentIds) throws RegistryException { 351 352 Statement stmt = null; 353 try { 354 stmt = connection.createStatement(); 355 ResultSet rs = null; 356 357 ArrayList ids = new ArrayList(); 358 359 Iterator iter = parentIds.iterator(); 360 while (iter.hasNext()) { 361 String parentId = (String)iter.next(); 362 363 rs = stmt.executeQuery("SELECT id from " + getTableName() + " WHERE service = '" + 364 parentId + "' "); 365 366 while (rs.next()) { 367 String id = rs.getString(1); 368 ids.add(id); 369 } 370 } 371 372 if (ids.size() > 0) { 373 delete(user, connection, ids); 374 } 375 376 //stmt.close(); 377 } 378 catch (SQLException e) { 379 RegistryException exception = new RegistryException(e); 380 throw exception; 381 } finally { 382 try { 383 if (stmt != null) 384 stmt.close(); 385 } catch (SQLException sqle) { 386 sqle.printStackTrace(); 387 } 388 } 389 } 390 391 /*** 392 * Gets objects for specified parentIds 393 * 394 */ 395 public ArrayList getByParentId(Connection connection, String parentId, ArrayList objectRefs) throws RegistryException { 396 ArrayList serviceBindings = new ArrayList(); 397 Statement stmt = null; 398 try { 399 stmt = connection.createStatement(); 400 ResultSet rs = null; 401 402 403 rs = stmt.executeQuery("SELECT * from " + getTableName() + " WHERE service = '" + 404 parentId + "' "); 405 406 while (rs.next()) { 407 ServiceBinding obj = new ServiceBinding(); 408 409 org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption = new org.oasis.ebxml.registry.bindings.query.ResponseOption(); 410 411 responseOption.setReturnComposedObjects(true); 412 loadObjectFromResultSet(connection, obj, rs, responseOption, objectRefs); 413 serviceBindings.add(obj); 414 } 415 } 416 catch (SQLException e) { 417 throw new RegistryException(e); 418 } finally { 419 try { 420 if (stmt != null) 421 stmt.close(); 422 } catch (SQLException sqle) { 423 sqle.printStackTrace(); 424 } 425 } 426 427 return serviceBindings; 428 } 429 430 public void loadObjectFromResultSet(Connection conn, Object obj, ResultSet rs, org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 431 if (!(obj instanceof ServiceBinding)) { 432 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.ServiceBinding."); 433 } 434 435 ServiceBinding serviceBinding = (ServiceBinding)obj; 436 437 super.loadObjectFromResultSet(conn, serviceBinding, rs, responseOption, objectRefs); 438 439 String accessUri = null; 440 try { 441 accessUri = rs.getString("accessuri"); 442 serviceBinding.setAccessURI(accessUri); 443 444 String targetBindingId = rs.getString("targetBinding"); 445 if (targetBindingId != null) { 446 org.oasis.ebxml.registry.bindings.rim.ObjectRef targetBinding = new org.oasis.ebxml.registry.bindings.rim.ObjectRef(); 447 objectRefs.add(targetBinding); 448 targetBinding.setId(targetBindingId); 449 serviceBinding.setTargetBinding(targetBinding); 450 } 451 452 String serviceId = rs.getString("service"); 453 if (serviceId != null) { 454 org.oasis.ebxml.registry.bindings.rim.ObjectRef service = new org.oasis.ebxml.registry.bindings.rim.ObjectRef(); 455 objectRefs.add(service); 456 service.setId(serviceId); 457 serviceBinding.setService(service); 458 } 459 } catch (SQLException e) { 460 e.printStackTrace(); 461 throw new RegistryException(e); 462 } 463 464 boolean returnComposedObjects = responseOption.getReturnComposedObjects(); 465 if (returnComposedObjects) { 466 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 467 ArrayList specLinks = specificationLinkDAO.getByParentId(conn, serviceBinding.getId(), objectRefs); 468 Iterator iter = specLinks.iterator(); 469 while (iter.hasNext()) { 470 org.oasis.ebxml.registry.bindings.rim.SpecificationLink sl = (org.oasis.ebxml.registry.bindings.rim.SpecificationLink)iter.next(); 471 serviceBinding.addSpecificationLink(sl); 472 } 473 474 } 475 } 476 477 public ArrayList getLeafObjectList(Connection conn, ResultSet rs, org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 478 ArrayList res = new ArrayList(); 479 480 try { 481 while(rs.next()) { 482 ServiceBinding obj = new ServiceBinding(); 483 loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 484 485 res.add(obj); 486 } 487 } 488 catch (SQLException e) { 489 e.printStackTrace(); 490 throw new RegistryException(e); 491 } 492 493 494 return res; 495 } 496 }

This page was automatically generated by Maven