1 package org.ashkelon; 2 7 8 import java.io.Serializable ; 9 import java.sql.Connection ; 10 import java.sql.PreparedStatement ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.ashkelon.db.DBMgr; 19 import org.ashkelon.db.DBUtils; 20 import org.ashkelon.db.PKManager; 21 import org.ashkelon.util.JDocUtil; 22 import org.ashkelon.util.Logger; 23 import org.ashkelon.util.StringUtils; 24 import org.ashkelon.util.TreeNode; 25 26 import com.sun.javadoc.ClassDoc; 27 import com.sun.javadoc.PackageDoc; 28 29 36 public class JPackage implements JDoc, Serializable 37 { 38 private String name; 39 private DocInfo doc; 40 private List classes; 41 42 private List ordinaryClasses; 43 private List exceptionClasses; 44 private List errorClasses; 45 private List interfaces; 46 47 private static String SEQUENCE = "PKG_SEQ"; 48 private static String TABLENAME = "PACKAGE"; 49 50 private int id; 51 private boolean idSet = false; 52 53 private API api; 54 55 private transient Logger log; 56 57 public JPackage(String name) 58 { 59 log = Logger.getInstance(); 60 setName(name); 61 setClasses(new ArrayList ()); 62 setDoc(new DocInfo()); 63 } 64 65 public JPackage(PackageDoc packageDoc, boolean recurseClasses, API api) 66 { 67 this(packageDoc.name()); 68 this.api = api; 69 setDoc(new DocInfo(packageDoc)); 70 if (recurseClasses) 71 { 72 addClasses(packageDoc.allClasses()); 73 } 74 } 75 76 public void store(Connection conn) throws SQLException 77 { 78 String prefix = log.getPrefix(); 79 log.setPrefix("JPackage"); 80 if (exists(conn)) 81 { 82 return; 85 } 86 87 Map fieldInfo = new HashMap (5); 88 fieldInfo.put("ID", new Integer (getId(conn))); 89 fieldInfo.put("NAME", StringUtils.truncate(name, 60)); 90 fieldInfo.put("DOCID", new Integer (getDoc().getId(conn))); 91 fieldInfo.put("API_ID", new Integer (api.getId(conn))); 92 93 DBUtils.insert(conn, TABLENAME, fieldInfo); 94 getDoc().store(conn); 95 for (int i=0; i<classes.size(); i++) 96 { 97 ClassType cls = (ClassType) classes.get(i); 98 log.verbose("about to store class: "+cls.getQualifiedName()); 99 (cls).store(conn); 100 } 101 log.setPrefix(prefix); 102 } 103 104 public boolean exists(Connection conn) throws SQLException 105 { 106 Map constraints = new HashMap (); 107 constraints.put("NAME", getName()); 108 constraints.put("API_ID", new Integer (api.getId(conn))); 109 Object obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints); 110 if (obj == null) 111 return false; 112 113 if (!idSet) 114 { 115 id = ((Number ) obj).intValue(); 116 idSet = true; 117 } 118 return true; 119 } 120 121 125 public static boolean delete(Connection conn, String pkgName) throws SQLException 126 { 127 Logger log = Logger.getInstance(); 128 129 String sql = "select p.id, p.docid, c.id, c.docid " + 130 "from PACKAGE p, CLASSTYPE c " + 131 "where p.name=? and c.packageid=p.id"; 132 PreparedStatement pstmt = conn.prepareStatement(sql); 133 pstmt.setString(1, pkgName); 134 ResultSet rset = pstmt.executeQuery(); 135 136 if (!rset.next()) 137 { 138 log.traceln("Skipping Package " + pkgName + " (not in repository)"); 139 rset.close(); 140 pstmt.close(); 141 142 return (deleteNoChildren(conn, pkgName)); 144 } 145 146 int pid = rset.getInt(1); 147 int docid = rset.getInt(2); 148 int classid = rset.getInt(3); 149 int class_docid = rset.getInt(4); 150 151 ClassType.delete(conn, classid, class_docid); 153 154 while (rset.next()) 155 { 156 classid = rset.getInt(3); 157 class_docid = rset.getInt(4); 158 ClassType.delete(conn, classid, class_docid); 159 } 160 161 rset.close(); 162 pstmt.close(); 163 164 DocInfo.delete(conn, docid); 166 167 sql = "update REFERENCE set refdoc_id = null where refdoc_id = ?"; 169 pstmt = conn.prepareStatement(sql); 170 pstmt.setInt(1, pid); 171 pstmt.executeUpdate(); 172 pstmt.close(); 173 174 HashMap constraint = new HashMap (); 176 constraint.put("ID", new Integer (pid)); 177 DBUtils.delete(conn, TABLENAME, constraint); 178 179 return true; 180 } 181 182 public static boolean deleteNoChildren(Connection conn, String pkgName) 183 throws SQLException 184 { 185 HashMap constraint = new HashMap (); 186 constraint.put("NAME", pkgName); 187 int num_deleted = DBUtils.delete(conn, TABLENAME, constraint); 188 return (num_deleted > 0); 189 } 190 191 192 196 public int getId(Connection conn) throws SQLException 197 { 198 if (!idSet) 199 { 200 id = PKManager.getInstance().nextVal(SEQUENCE); 202 idSet = true; 203 } 204 return id; 205 } 206 207 public int getId() 208 { 209 return id; 210 } 211 212 public void setId(int id) 213 { 214 this.id = id; 215 idSet = true; 216 } 217 218 public String getName() 220 { 221 return name; 222 } 223 public void setName(String name) 224 { 225 this.name = name; 226 } 227 228 public DocInfo getDoc() 229 { 230 return doc; 231 } 232 public void setDoc(DocInfo doc) 233 { 234 this.doc = doc; 235 } 236 237 public List getClasses() 238 { 239 return classes; 240 } 241 public void setClasses(List classes) 242 { 243 this.classes = new ArrayList (35); 244 ordinaryClasses = new ArrayList (12); 245 interfaces = new ArrayList (12); 246 exceptionClasses = new ArrayList (12); 247 errorClasses = new ArrayList (12); 248 249 for (int i=0; i<classes.size(); i++) 250 { 251 addClass((ClassType)classes.get(i)); 252 } 253 } 254 255 public void addClasses(ClassDoc[] classes) 256 { 257 for (int i=0; i<classes.length; i++) 258 { 259 if (classes[i].containingClass() == null) 262 addClass(classes[i]); 263 } 264 } 265 266 public void addClass(ClassDoc classdoc) 267 { 268 log.verbose(classdoc.qualifiedName()); 269 addClass(new ClassType(classdoc, this, getAPI())); 270 } 271 272 public void addClass(ClassType classtype) 273 { 274 classes.add(classtype); 275 switch (classtype.getClassType()) 276 { 277 case ClassType.ORDINARY_CLASS: 278 { 279 ordinaryClasses.add(classtype); 280 break; 281 } 282 case ClassType.INTERFACE: 283 { 284 interfaces.add(classtype); 285 break; 286 } 287 case ClassType.ERROR_CLASS: 288 { 289 errorClasses.add(classtype); 290 break; 291 } 292 case ClassType.EXCEPTION_CLASS: 293 { 294 exceptionClasses.add(classtype); 295 break; 296 } 297 } 298 } 299 300 public List getOrdinaryClasses() { return ordinaryClasses; } 301 public List getExceptionClasses() { return exceptionClasses; } 302 public List getInterfaces() { return interfaces; } 303 public List getErrorClasses() { return errorClasses; } 304 305 public boolean hasOrdinaryClasses() { return !ordinaryClasses.isEmpty(); } 306 public boolean hasExceptionClasses() { return !exceptionClasses.isEmpty(); } 307 public boolean hasInterfaces() { return !interfaces.isEmpty(); } 308 public boolean hasErrorClasses() { return !errorClasses.isEmpty(); } 309 310 public API getAPI() { return api; } 311 public void setAPI(API api) { this.api = api; } 312 313 public static JPackage makePackageFor(Connection conn, int pkgId) throws SQLException 314 { 315 324 String sql = DBMgr.getInstance().getStatement("makepackage"); 325 326 PreparedStatement pstmt = conn.prepareStatement(sql); 327 pstmt.setInt(1, pkgId); 328 ResultSet rset = pstmt.executeQuery(); 329 330 if (!rset.next()) 331 return null; 332 333 JPackage pkg = new JPackage(rset.getString(2)); 334 pkg.setId(rset.getInt(1)); 335 pkg.setDoc(new DocInfo(rset.getString(3), rset.getString(5), rset.getString(6), rset.getString(4))); 336 pkg.getDoc().setId(rset.getInt(7)); 337 String apiname = rset.getString(8); 338 if (!StringUtils.isBlank(apiname)) 339 { 340 API api = new API(rset.getString(8)); 341 api.setId(rset.getInt(9)); 342 pkg.setAPI(api); 343 } 344 345 rset.close(); 346 pstmt.close(); 347 348 pkg.getClassInfo(conn); 349 pkg.getDoc().fetchRefs(conn); 350 351 return pkg; 352 } 353 354 public void getClassInfo(Connection conn) throws SQLException 355 { 356 365 String sql = DBMgr.getInstance().getStatement("classinfo"); 366 367 PreparedStatement pstmt = conn.prepareStatement(sql); 368 pstmt.setInt(1, getId()); 369 ResultSet rset = pstmt.executeQuery(); 370 371 ClassType classtype; 372 while (rset.next()) 373 { 374 classtype = new ClassType(rset.getString(1)); 375 classtype.setClassType(rset.getInt(2)); 376 classtype.setId(rset.getInt(3)); 377 classtype.setDoc(new DocInfo(rset.getString(6), rset.getString(7), rset.getString(8))); 378 classtype.setSuperClassName(rset.getString(4)); 379 classtype.setStatic(rset.getBoolean(9)); 380 classtype.setFinal(rset.getBoolean(10)); 381 classtype.setAbstract(rset.getBoolean(11)); 382 classtype.setAccessibility(rset.getInt(12)); 383 classtype.setModifiers(rset.getString(13)); 384 385 int superid = rset.getInt(5); 386 if (superid > 0) 387 { 388 classtype.setSuperClass(new ClassType(classtype.getSuperClassName())); 389 classtype.getSuperClass().setId(superid); 390 } 391 addClass(classtype); 392 } 393 394 rset.close(); 395 pstmt.close(); 396 } 397 398 399 public TreeNode buildTree(Connection conn) throws SQLException 400 { 401 List classes = getClasses(); 402 List clstreelist = new ArrayList (); 403 ClassType c; 404 405 for (int i=0; i<classes.size(); i++) 406 { 407 c = (ClassType) classes.get(i); 408 410 TreeNode clstree = c.getSuperclasses(conn); 411 412 clstreelist.add(clstree); 414 } 415 416 TreeNode pkgNode = new TreeNode(this); 417 418 TreeNode clstree; 419 420 log.debug(clstreelist.size()+" little trees to merge"); 421 422 for (int i=0; i<clstreelist.size(); i++) 423 { 424 clstree = (TreeNode) clstreelist.get(i); 425 String key = ((ClassType) clstree.getValue()).getQualifiedName(); 426 428 TreeNode node = pkgNode; 429 TreeNode targetnode = clstree; 430 String targetkey = key; 431 432 while(node.getChild(targetkey)!=null) 433 { 434 node = node.getChild(targetkey); 436 targetnode = targetnode.getOnlyChild(); 437 if (targetnode == null) 438 { 439 continue; 440 } 441 targetkey = ((ClassType) targetnode.getValue()).getQualifiedName(); 442 } 443 if (targetnode == null) 444 { 445 continue; 446 } 447 448 if (node.getValue() instanceof ClassType) 449 { 450 log.debug(targetkey + " not in tree\n" + 451 " adding to : "+((ClassType) node.getValue()).getQualifiedName()+ "\n" + 452 " ( should be : "+((ClassType)targetnode.getValue()).getSuperClassName()+")"); 453 } 454 455 String clsname = ((ClassType)targetnode.getValue()).getQualifiedName(); 456 String supercls = ((ClassType)targetnode.getValue()).getSuperClassName(); 457 if (!StringUtils.isBlank(supercls) && !clsname.equals("java.lang.Object") && 458 node.getValue() instanceof JPackage) 459 { 460 if (node.getChild("java.lang.Object")==null) 461 continue; 462 463 TreeNode newnode = null; 464 465 if (node.getChild("java.lang.Object").getChild(supercls)==null) 466 { 467 newnode = new TreeNode(); 468 } else 469 { 470 newnode = node.getChild("java.lang.Object").getChild(supercls); 471 } 472 ClassType absentparent = new ClassType(supercls); 474 absentparent.setClassType(JDocUtil.UNKNOWN_TYPE); 475 absentparent.setPackage(this); 476 absentparent.setLevel(2); 477 absentparent.setDoc(new DocInfo("","","")); 478 absentparent.setSuperClassName("java.lang.Object"); 479 absentparent.setSuperClass((ClassType) node.getChild("java.lang.Object").getValue()); 480 481 newnode.setValue(absentparent); 482 483 ClassType targetCls = (ClassType) targetnode.getValue(); 484 targetCls.setLevel(3); 485 486 targetnode.setParent(newnode); 487 newnode.addChild(targetkey, targetnode); 488 489 node.getChild("java.lang.Object").addChild(supercls, newnode); 490 } 491 else 492 { 493 node.addChild(targetkey, targetnode); 494 } 495 496 } 499 500 return pkgNode; 501 } 502 503 private TreeNode makeSimpleClassTree(List ctree) 504 { 505 TreeNode root = new TreeNode(ctree.get(0)); 506 ClassType c = null; 507 TreeNode n = root; 508 for (int i=1; i<ctree.size(); i++) 509 { 510 c = (ClassType) ctree.get(i); 511 512 n.addChild(c.getQualifiedName(), new TreeNode(c)); 513 n = n.getOnlyChild(); 514 } 515 return root; 516 } 517 518 public String toString() 519 { 520 return getName(); 521 } 522 523 public static void main(String [] args) 524 { 525 DBMgr mgr = null; 526 Connection conn = null; 527 Logger.getInstance().setTraceLevel(Logger.DEBUG); 528 529 try 530 { 531 mgr = DBMgr.getInstance(); 532 conn = mgr.getConnection(); 533 JPackage pkg = JPackage.makePackageFor(conn, 102); 534 TreeNode pkgTree = pkg.buildTree(conn); 535 System.out.println(TreeNode.printTree(pkgTree,0)); 536 } catch (SQLException ex) 537 { 538 ex.printStackTrace(); 539 DBUtils.logSQLException(ex); 540 } 541 finally 542 { 543 mgr.releaseConnection(conn); 544 } 545 } 546 547 548 public String getSummaryDescription() 549 { 550 return getDoc().getSummaryDescription(); 551 } 552 public String getDescription() 553 { 554 return getDoc().getDescription(); 555 } 556 public String getSince() 557 { 558 return getDoc().getSince(); 559 } 560 public String getDeprecatedDescr() 561 { 562 return getDoc().getDeprecated(); 563 } 564 public boolean isDeprecated() 565 { 566 return getDoc().isDeprecated(); 567 } 568 public List getReferences() 569 { 570 return getDoc().getReferences(); 571 } 572 573 public static String getTableName() 574 { 575 return TABLENAME; 576 } 577 578 public String getStyle() { return "package"; } 579 580 581 } 582 | Popular Tags |