1 23 24 package org.enhydra.kelp.forte.node; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.common.PathHandle; 29 import org.enhydra.tool.common.ToolException; 30 31 import org.openide.loaders.DataFolder; 33 import org.openide.loaders.DataObject; 34 import org.openide.nodes.Node; 35 import org.openide.nodes.Children; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.FileStateInvalidException; 38 import org.openide.loaders.DataShadow; 39 import org.openide.filesystems.FileObject; 40 import org.openide.loaders.MultiDataObject; 41 import org.openide.loaders.ExecSupport; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.TopManager; 44 import org.openide.execution.NbClassPath; 45 46 import org.netbeans.modules.text.TXTDataObject; 47 48 import org.enhydra.kelp.common.Constants; 50 import org.enhydra.kelp.common.PathUtil; 51 import org.enhydra.kelp.common.node.OtterDocumentNode; 52 import org.enhydra.kelp.common.node.OtterFileNode; 53 import org.enhydra.kelp.common.node.OtterNode; 54 import org.enhydra.kelp.common.node.OtterTextFileNode; 55 import org.enhydra.kelp.common.node.OtterTemplateNode; 56 import org.enhydra.kelp.common.node.OtterProject; 57 import org.enhydra.kelp.common.node.OtterNodeFactory; 58 import org.enhydra.kelp.common.node.PropertyKeys; 59 import org.enhydra.kelp.common.PropUtil; 60 import org.enhydra.kelp.common.deployer.RunParameters; 61 62 import org.enhydra.kelp.forte.XMLCSettings; 63 import org.enhydra.kelp.forte.DeploySettings; 64 import org.enhydra.kelp.forte.DodsSettings; 65 import org.enhydra.kelp.forte.services.KelpExecutor; 66 67 import org.enhydra.tool.common.ToolException; 68 69 import java.io.File ; 71 import java.io.IOException ; 72 import java.util.Vector ; 73 import java.lang.reflect.Method ; 74 import java.util.Enumeration ; 75 import java.util.StringTokenizer ; 76 import java.util.NoSuchElementException ; 77 78 import org.openide.NotifyDescriptor; 80 81 87 public class ForteProject extends OtterProject { 88 89 public Node nativeProject = null; 93 public ForteNodeFactory factory = null; 94 95 private static final String SOURCE_PATH_ARRAY = "project.sourcepath.array"; 96 97 103 public ForteProject () { 104 nativeProject = TopManager.getDefault().getPlaces().nodes().projectDesktop(); 105 factory = new ForteNodeFactory(); 106 } 107 108 public ForteProject(Node target) 109 { 110 nativeProject = target; 111 factory = new ForteNodeFactory(); 112 } 113 114 public OtterNodeFactory getNodeFactory() { 115 return factory; 116 } 117 118 public String getGenerateToDirectory() { 119 return getClassOutputPath(); 120 } 121 122 public String getRootPath() 123 { 124 return XMLCSettings.getDefault().getRoot(); 125 } 126 127 public String getDefaultRoot() 128 { 129 String root = null; 130 Children kids = nativeProject.getChildren(); 131 if (kids == null) 132 return null; 133 Enumeration nodes = kids.nodes(); 134 135 while (nodes.hasMoreElements()) 136 { 137 Node nextNode = (Node)nodes.nextElement(); 138 DataObject dob = (DataObject)nextNode.getCookie(DataObject.class); 139 if (dob instanceof DataShadow) 140 dob = ((DataShadow)dob).getOriginal(); 141 if (dob instanceof DataFolder) 142 { 143 FileObject file = dob.getPrimaryFile(); 144 if (file != null) 145 { 146 String fileName = file.getPackageName(File.separatorChar); 147 FileSystem fs = null; 148 StringBuffer buf = null; 149 try { 150 fs = file.getFileSystem(); 151 } catch (FileStateInvalidException e) { 152 System.err.println(e); 153 } 154 if (fs != null) 155 { 156 buf = new StringBuffer (fs.getSystemName()); 157 if ((fileName != null) && (fileName.length() > 0)) 158 { 159 buf.append(File.separatorChar).append(fileName); 160 } 161 root = buf.toString(); 162 break; 163 } 164 } 165 } 166 167 } 168 if ((root != null) && (root.length() > 0)) 169 return root; 170 else 171 return null; 172 } 173 174 public FileObject getRootFolder() 175 { 176 FileSystem[] fs = getFileSystems(); 177 FileObject root = null; 178 String path = getRootPath(); 179 for (int i = 0; i < fs.length; i++) 180 { 181 FileObject sysRoot = fs[i].getRoot(); 182 String name = fs[i].getSystemName(); 183 if (name.equals(path)) 184 root = sysRoot; 185 else if (path.startsWith(name)) 186 root = sysRoot.getFileObject(path.substring(name.length() + 1)); 187 if (root != null) 188 break; 189 } 190 191 return root; 192 } 193 194 public boolean inProject(String path) 195 { 196 FileSystem[] fs = getFileSystems(); 197 FileObject target = null; 198 for (int i = 0; i < fs.length; i++) 199 { 200 FileObject sysRoot = fs[i].getRoot(); 201 String name = fs[i].getSystemName(); 202 if (name.equals(path)) 203 target = sysRoot; 204 else if (path.startsWith(name)) 205 target = sysRoot.getFileObject(path.substring(name.length() + 1)); 206 if (target != null) 207 return true; 208 } 209 return false; 210 } 211 212 public Object getNativeNode() 213 { 214 return nativeProject; 215 } 216 217 public void setNativeNode(Object node) 218 { 219 nativeProject = (Node)node; 220 } 221 222 public OtterProject getProject() 223 { 224 return this; } 226 227 public OtterNode getParent() 228 { 229 return null; } 231 232 public FileSystem[] getFileSystems() 233 { 234 Vector systems = new Vector (); 235 236 Children kids = nativeProject.getChildren(); 237 Enumeration nodes = kids.nodes(); 238 while (nodes.hasMoreElements()) 239 { 240 Node nextNode = (Node)nodes.nextElement(); 241 DataObject dob = (DataObject)nextNode.getCookie(DataObject.class); 242 if (dob instanceof DataShadow) 243 dob = ((DataShadow)dob).getOriginal(); 244 if (dob instanceof DataFolder) 245 { 246 FileObject file = dob.getPrimaryFile(); 247 try { 248 FileSystem fs = file.getFileSystem(); 249 systems.add(fs); 250 }catch (FileStateInvalidException e) { 251 System.err.println(e); 252 } 253 } 254 } 255 256 return (FileSystem[])systems.toArray(new FileSystem[0]); 257 258 } 259 260 261 public void save() 262 { 263 } 264 265 public String getCodeGenDefaultDestination() 266 { 267 String dest = new String (); 269 File file = null; 270 271 file = new File (getRootPath()); 272 dest = file.getParent(); 273 274 return dest; 275 } 276 277 280 public OtterTextFileNode getFirstPolicy() 281 { 282 PolicyNodeReader reader = new PolicyNodeReader(); 283 return reader.getFirstNode(nativeProject); 284 } 285 286 289 public String getWorkingPath() 290 { 291 String path = new String (); 292 293 path = getProperty(PropertyKeys.NAME_WORKING_DIR); 294 if (path == null || path.length() == 0) 295 path = getRootPath(); 296 return path; 297 } 298 299 302 public void setWorkingPath(String path) 303 { 304 setProperty(PropertyKeys.NAME_WORKING_DIR, path); 305 } 306 307 public void configureRunClass() 308 { 309 String easRoot = ToolBoxInfo.getEnhydraRoot(); 310 String srcDir = XMLCSettings.getDefault().getSource(); 311 char sep = File.separatorChar; 312 char psep = File.pathSeparatorChar; 313 RunParameters rp = new RunParameters(this); 314 315 FileObject root = getRootFolder(); 316 FileObject src = root.getFileObject(srcDir); 317 if (src == null) 318 { 319 System.out.println("can't get source dir"); 320 return; 321 } 322 src.refresh(); 323 FileObject file = src.getFileObject("StartServer", "java"); 324 if (file == null) 325 { 326 return; 327 } 328 MultiDataObject dob = null; 329 try { 330 dob = (MultiDataObject)DataObject.find(file); 331 } catch (DataObjectNotFoundException e) { 332 System.err.println(e); 333 return; 334 } 335 336 StringBuffer buf = new StringBuffer (); 337 KelpExecutor runExec = new KelpExecutor(); 338 buf.append(' '); 340 buf.append(rp.getVMParameters()); 341 buf.append(' '); 342 buf.append("-cp \""); 344 buf.append(getSourcePath()); 345 buf.append(psep); 346 buf.append(easRoot); 347 buf.append(sep); 348 buf.append("lib"); 349 buf.append(sep); 350 buf.append("build"); 351 buf.append(sep); 352 buf.append("toolbox.jar"); 353 buf.append('\"'); 354 buf.append(' '); 355 buf.append(rp.getTCcpArg()); 356 buf.append(' '); 357 buf.append(rp.getPolicyArg()); 358 buf.append(' '); 359 buf.append(rp.getBootArg()); 360 buf.append(" StartServer "); 361 buf.append(rp.getAppParameters()); 362 363 runExec.setRunOption(buf.toString()); 364 try { 365 ExecSupport.setExecutor(dob.getPrimaryEntry(), runExec); 366 } catch (IOException e) { 367 System.err.println(e); 368 } 369 370 } 371 372 375 public OtterTextFileNode[] getAllDeploymentDescs() 376 { 377 DeploymentNodeReader reader = new DeploymentNodeReader(); 378 return reader.getNodes(nativeProject); 379 } 380 381 387 public OtterFileNode[] findFileNodesByType(String [] extensions) 388 { 389 NodeReader reader = new NodeReader(extensions); 390 return reader.getNodes(nativeProject); 391 } 392 395 public OtterFileNode[] getAllInput() 396 { 397 String [] ext = new String [0]; 398 OtterFileNode[] input = new OtterFileNode[0]; 399 OtterFileNode[] nodes = new OtterFileNode[0]; 400 Vector nodeVector = new Vector (); 401 PathHandle inputPath = null; 402 PathHandle cursorPath = null; 403 404 inputPath = PathHandle.createPathHandle(getDeployInputPath()); 405 if (isDeployInputFilter()) { 406 ext = new String [1]; 407 ext[0] = Constants.TYPE_IN; 408 } 409 nodes = findFileNodesByType(ext); 410 for (int i = 0; i < nodes.length; i++) { 411 cursorPath = PathHandle.createPathHandle(nodes[i].getFilePath()); 412 if (inputPath.parentOf(cursorPath)) { 413 nodeVector.addElement(nodes[i]); 414 } 415 } 416 input = new OtterFileNode[nodeVector.size()]; 417 input = (OtterFileNode[]) nodeVector.toArray(input); 418 nodeVector.clear(); 419 return input; 420 } 421 422 425 public OtterDocumentNode[] getAllDocuments() 426 { 427 DocumentNodeReader reader = new DocumentNodeReader(); 428 return reader.getNodes(this); 429 430 } 431 432 435 public Object getNativeProject() 436 { 437 return nativeProject; 438 } 439 440 443 public boolean isOpenBuild() 444 { 445 return false; } 447 448 453 public String getClassOutputPath() 454 { 455 return getSourcePath(); 456 } 457 458 public void setClassOutputPath(String p) 459 { 460 461 } 462 463 public boolean isDefaultProject() 465 { 466 return false; 467 } 468 469 474 public String getSourcePath() 475 { 476 StringBuffer buf = new StringBuffer (); 477 String src = XMLCSettings.getDefault().getSource(); 478 buf.append(getRootPath()).append(File.separatorChar).append(src); 479 482 return buf.toString() ; 483 } 484 489 public String [] getSourcePathArray() 490 { 491 String in = (String )XMLCSettings.getDefault().propertyValue(SOURCE_PATH_ARRAY); 492 if (in != null && in.length() > 0) 493 return PropUtil.listToArray(in, new String [0]); 494 else 495 return new String [] {getRootPath()}; 496 497 518 } 519 520 525 public void setSourcePathArray(String [] pathAsArray) 526 { 527 String out = PropUtil.arrayToList(pathAsArray); 528 XMLCSettings.getDefault().setProperty(SOURCE_PATH_ARRAY, out); 529 } 530 531 537 public String getClassPath() 538 { 539 StringBuffer buf = new StringBuffer (); 540 541 Enumeration fss = TopManager.getDefault().getRepository().getFileSystems(); 544 while (fss.hasMoreElements ()) 545 { 546 FileSystem fs = (FileSystem) fss.nextElement (); 547 buf = buf.append(fs.getSystemName()); 548 buf = buf.append(File.pathSeparatorChar); 549 } 550 buf.append(NbClassPath.createClassPath().getClassPath()); 552 return buf.toString(); 553 } 554 555 public boolean cpContains(String target) 556 { 557 StringTokenizer tokenizer= new StringTokenizer (getClassPath(), File.pathSeparator); 558 String cur = null; 559 while (tokenizer.hasMoreTokens()) 560 { 561 try{ 562 cur = tokenizer.nextToken(); 563 } catch (NoSuchElementException e) { 564 break; 565 } 566 567 if (cur.equals(target)) 568 return true; 569 } 570 return false; 571 } 572 579 public String getProperty(String property) 580 { 581 if (property.startsWith("enhydra.xmlc")) 582 return XMLCSettings.getDefault().propertyValue(property); 583 else 584 return DeploySettings.getDefault().propertyValue(property); 585 } 586 587 593 public void setProperty(String property, String value) 594 { 595 if (property.startsWith("enhydra.xmlc")) 596 XMLCSettings.getDefault().setProperty(property, value); 597 else 598 DeploySettings.getDefault().setProperty(property, value); 599 } 600 601 607 public void setProperty(String property, int value) 608 { 609 setProperty(property, Integer.toString(value)); 610 } 611 612 public boolean isDeployStartupJavaReadOnly() { 614 return true; 615 } 616 617 618 619 class DeploymentNodeReader { 620 private Vector nodeVector = new Vector (); 621 622 626 protected DeploymentNodeReader() {} 627 628 636 protected OtterTemplateNode[] getNodes(Node nativeProject) { 637 OtterTemplateNode[] nodes = new OtterTemplateNode[0]; 638 639 Node project = nativeProject; 640 nodeVector.clear(); 641 642 FileObject root = getRootFolder(); 643 if (root != null) 644 readFiles(root); 645 nodes = new OtterTemplateNode[nodeVector.size()]; 646 nodes = (OtterTemplateNode[])nodeVector.toArray(nodes); 647 nodeVector.clear(); 648 return nodes; 649 } 650 651 657 private void readFiles(FileObject container) { 658 659 FileObject[] children = container.getChildren(); 660 for (int i = 0; i < children.length; i++) 661 { 662 if (children[i].isData()) 663 { 664 String ext = children[i].getExt(); 665 if (((ext.equals(Constants.FILE_WEB_XML)) 666 || (ext.equals(Constants.FILE_EJB_JAR_XML)))) 667 { 668 ForteTemplateNode node = null; 669 try{ 670 DataObject dob = DataObject.find(children[i]); 671 if (dob instanceof DeployDataObject) try { 673 node = new ForteTemplateNode((DeployDataObject)dob); 674 } catch (ToolException e) { 675 System.err.println(e); 676 } 677 if (node != null) 678 nodeVector.add(node); 679 } 680 catch(DataObjectNotFoundException e) { 681 System.err.println(e); 682 } 683 684 } 685 686 } 687 else if (children[i].isFolder()) 688 readFiles(children[i]); 689 690 } 691 } 692 693 } 694 695 class DocumentNodeReader { 696 private Vector nodeVector = new Vector (); 697 private Node project = null; 698 private String [] docTypes = new String [0]; 699 700 704 protected DocumentNodeReader() {} 705 706 714 protected OtterDocumentNode[] getNodes(OtterProject p) { 715 OtterDocumentNode[] nodes = new OtterDocumentNode[0]; 716 project = (Node)p.getNativeNode(); 717 docTypes = p.getDocTypes(); 718 nodeVector.clear(); 719 FileObject root = getRootFolder(); 720 if (root != null) 721 readFiles(root); 722 nodes = new OtterDocumentNode[nodeVector.size()]; 723 nodes = (OtterDocumentNode[]) nodeVector.toArray(nodes); 724 nodeVector.clear(); 725 return nodes; 726 } 727 728 734 private void readFiles(FileObject container) { 735 736 FileObject[] children = container.getChildren(); 737 for (int i = 0; i < children.length; i++) 738 { 739 if (children[i].isData()) 740 { 741 if (isTarget(children[i].getExt())) 742 { 743 try 744 { 745 DataObject dob = DataObject.find(children[i]); 746 if (dob instanceof XMLCDataObject) nodeVector.add(new ForteDocumentNode((XMLCDataObject)dob)); 748 } 749 catch(DataObjectNotFoundException e) { 750 System.err.println(e); 751 } 752 } 753 754 } 755 else if (children[i].isFolder()) 756 readFiles(children[i]); 757 758 } 759 } 760 761 protected boolean isTarget(String ext) 762 { 763 for (int j = 0; j < docTypes.length; j++) 764 { 765 if (ext.equals(docTypes[j])) 766 return true; 767 } 768 return false; 769 } 770 771 } 772 773 class NodeReader { 774 private Vector nodeVector = new Vector (); 775 private String [] extensions = new String [0]; 776 777 783 protected NodeReader(String [] exts) { 784 extensions = exts; 785 } 786 787 795 protected OtterFileNode[] getNodes(Node nativeProject) { 796 OtterFileNode[] nodes = new OtterFileNode[0]; 797 Node project = nativeProject; 798 nodeVector.clear(); 799 FileObject root = getRootFolder(); 800 if (root != null) 801 readFiles(root); 802 nodes = new OtterFileNode[nodeVector.size()]; 803 nodes = (OtterFileNode[]) nodeVector.toArray(nodes); 804 nodeVector.clear(); 805 return nodes; 806 } 807 808 814 private void readFiles(FileObject container) { 815 FileObject[] children = container.getChildren(); 816 for (int i = 0; i < children.length; i++) 817 { 818 if (children[i].isData()) 819 { 820 if (isTarget(children[i].getExt()) || (extensions.length == 0)) 821 { 822 DataObject dob = null; 823 try 824 { 825 dob = DataObject.find(children[i]); 826 } 827 catch(DataObjectNotFoundException e) { 828 System.err.println(e); 829 } 830 831 if (dob instanceof XMLCDataObject) 832 nodeVector.add(new ForteDocumentNode((XMLCDataObject)dob)); 833 else if (dob instanceof DeployDataObject) 834 { 835 try{ 836 nodeVector.add(new ForteTemplateNode((DeployDataObject)dob)); 837 } catch (ToolException e){ 838 System.err.println(e); 839 } 840 } 841 842 else if (dob instanceof TXTDataObject) 843 nodeVector.add(new ForteTextFileNode((TXTDataObject)dob)); 844 else 845 nodeVector.add(new ForteNode(dob)); 846 847 } 848 849 } 850 else if (children[i].isFolder()) 851 readFiles(children[i]); 852 853 } 854 } 855 856 protected boolean isTarget(String ext) 857 { 858 for (int j = 0; j < extensions.length; j++) 859 { 860 if (ext.equals(extensions[j])) 861 return true; 862 } 863 return false; 864 } 865 } 866 867 class PolicyNodeReader { 868 private OtterTextFileNode node = null; 869 870 878 protected OtterTextFileNode getFirstNode(Node nativeProject) { 879 TXTDataObject target = null; 880 881 FileObject root = getRootFolder(); 882 if (root != null) 883 target = recursiveRead(root); 884 if (target != null) 885 return new ForteTextFileNode(target); 886 else return null; 887 } 888 889 895 private TXTDataObject recursiveRead(FileObject container) { 896 897 FileObject[] children = container.getChildren(); 898 899 for (int i = 0; i < children.length; i++) 900 { 901 902 if (children[i].isData()) 903 { 904 if (children[i].getExt().equals(Constants.FILE_JAVA_POLICY)) 905 { 906 try{ 907 DataObject dob = DataObject.find(children[i]); 908 return (TXTDataObject)dob; 909 } catch(DataObjectNotFoundException e) { 910 System.err.println(e); 911 } 912 } 913 } 914 915 if (children[i].isFolder()) 916 return recursiveRead(children[i]); 917 918 919 } 920 return null; 921 } 922 } 923 924 930 public void setEnhydraPath() { 931 String enhydraPath = getClassPath(); 933 enhydraPath = enhydraPath.substring(0, enhydraPath.indexOf("enhydra.jar")-5); 934 enhydraPath = enhydraPath.substring(enhydraPath.lastIndexOf(";")+1, 935 enhydraPath.length()); 936 setProperty(DODS_ENHYDRA_PATH, enhydraPath); 937 } 938 939 } 940 | Popular Tags |