1 55 56 package org.apache.bsf; 57 58 import java.lang.reflect.*; 59 import java.util.*; 60 import java.io.*; 61 import java.beans.*; 62 import java.security.*; 63 64 import org.apache.bsf.util.*; 65 import org.apache.bsf.debug.*; 66 import org.apache.bsf.debug.util.DebugLog; 67 68 import javax.naming.*; 69 70 99 public class BSFManager { 100 protected static Hashtable registeredEngines = new Hashtable(); 102 103 protected static Hashtable extn2Lang = new Hashtable(); 105 106 protected Hashtable loadedEngines = new Hashtable(); 110 111 protected ObjectRegistry objectRegistry = new ObjectRegistry(); 113 114 protected PropertyChangeSupport pcs; 117 118 protected ClassLoader classLoader = getClass().getClassLoader(); 121 122 protected String tempDir = "."; 127 128 protected String classPath; 130 131 protected Vector declaredBeans = new Vector(); 134 135 static BSFDebugManagerImpl gDebugManager; 137 138 144 static { 145 try { 146 ResourceBundle rb = 147 ResourceBundle.getBundle("org.apache.bsf.Languages"); 148 Enumeration keys = rb.getKeys(); 149 150 while (keys.hasMoreElements()) { 151 String key = (String ) keys.nextElement(); 152 String value = rb.getString(key); 153 154 StringTokenizer tokens = new StringTokenizer(value, ","); 155 String className = (String ) tokens.nextToken(); 156 157 String exts = (String ) tokens.nextToken(); 159 StringTokenizer st = new StringTokenizer(exts, "|"); 160 String [] extensions = new String [st.countTokens()]; 161 for (int i = 0; st.hasMoreTokens(); i++) { 162 extensions[i] = ((String ) st.nextToken()).trim(); 163 } 164 165 registerScriptingEngine(key, className, extensions); 166 } 167 } 168 catch (NoSuchElementException nsee) { 169 nsee.printStackTrace(); 170 System.err.println("Syntax error in Languages resource bundle"); 171 } 172 catch (MissingResourceException mre) { 173 mre.printStackTrace(); 174 System.err.println("Initialization error: " + mre.toString()); 175 } 176 177 if (Boolean.getBoolean("org.apache.bsf.serverLaunch")) 178 initBSFDebugManager(); 179 } 180 181 public BSFManager() { 182 pcs = new PropertyChangeSupport(this); 183 if (gDebugManager != null) gDebugManager.registerManager(this); 184 } 185 186 201 public Object apply(String lang, 202 String source, 203 int lineNo, 204 int columnNo, 205 Object funcBody, 206 Vector paramNames, 207 Vector arguments) 208 throws BSFException { 209 final BSFEngine e = loadScriptingEngine(lang); 210 final String sourcef = source; 211 final int lineNof = lineNo, columnNof = columnNo; 212 final Object funcBodyf = funcBody; 213 final Vector paramNamesf = paramNames; 214 final Vector argumentsf = arguments; 215 Object result = null; 216 217 try { 218 final Object resultf = 219 AccessController.doPrivileged(new PrivilegedExceptionAction() { 220 public Object run() throws Exception { 221 return e.apply(sourcef, lineNof, columnNof, 222 funcBodyf, paramNamesf, argumentsf); 223 } 224 }); 225 result = resultf; 226 } 227 catch (PrivilegedActionException prive) { 228 throw (BSFException) prive.getException(); 229 } 230 231 return result; 232 } 233 234 250 public void compileApply(String lang, 251 String source, 252 int lineNo, 253 int columnNo, 254 Object funcBody, 255 Vector paramNames, 256 Vector arguments, 257 CodeBuffer cb) 258 throws BSFException { 259 final BSFEngine e = loadScriptingEngine(lang); 260 final String sourcef = source; 261 final int lineNof = lineNo, columnNof = columnNo; 262 final Object funcBodyf = funcBody; 263 final Vector paramNamesf = paramNames; 264 final Vector argumentsf = arguments; 265 final CodeBuffer cbf = cb; 266 267 try { 268 AccessController.doPrivileged(new PrivilegedExceptionAction() { 269 public Object run() throws Exception { 270 e.compileApply(sourcef, lineNof, columnNof, 271 funcBodyf, paramNamesf, 272 argumentsf, cbf); 273 return null; 274 } 275 }); 276 } 277 catch (PrivilegedActionException prive) { 278 throw (BSFException) prive.getException(); 279 } 280 } 281 282 296 public void compileExpr(String lang, 297 String source, 298 int lineNo, 299 int columnNo, 300 Object expr, 301 CodeBuffer cb) 302 throws BSFException { 303 final BSFEngine e = loadScriptingEngine(lang); 304 final String sourcef = source; 305 final int lineNof = lineNo, columnNof = columnNo; 306 final Object exprf = expr; 307 final CodeBuffer cbf = cb; 308 309 try { 310 AccessController.doPrivileged(new PrivilegedExceptionAction() { 311 public Object run() throws Exception { 312 e.compileExpr(sourcef, lineNof, columnNof, exprf, cbf); 313 return null; 314 } 315 }); 316 } 317 catch (PrivilegedActionException prive) { 318 throw (BSFException) prive.getException(); 319 } 320 } 321 322 336 public void compileScript(String lang, 337 String source, 338 int lineNo, 339 int columnNo, 340 Object script, 341 CodeBuffer cb) 342 throws BSFException { 343 final BSFEngine e = loadScriptingEngine(lang); 344 final String sourcef = source; 345 final int lineNof = lineNo, columnNof = columnNo; 346 final Object scriptf = script; 347 final CodeBuffer cbf = cb; 348 349 try { 350 AccessController.doPrivileged(new PrivilegedExceptionAction() { 351 public Object run() throws Exception { 352 e.compileScript(sourcef, lineNof, columnNof, 353 scriptf, cbf); 354 return null; 355 } 356 }); 357 } 358 catch (PrivilegedActionException prive) { 359 throw (BSFException) prive.getException(); 360 } 361 } 362 363 393 public void declareBean(String beanName, Object bean, Class type) 394 throws BSFException { 395 registerBean(beanName, bean); 396 397 BSFDeclaredBean tempBean = new BSFDeclaredBean(beanName, bean, type); 398 declaredBeans.addElement(tempBean); 399 400 Enumeration enginesEnum = loadedEngines.elements(); 401 BSFEngine engine; 402 while (enginesEnum.hasMoreElements()) { 403 engine = (BSFEngine) enginesEnum.nextElement(); 404 engine.declareBean(tempBean); 405 } 406 } 407 408 421 public Object eval(String lang, 422 String source, 423 int lineNo, 424 int columnNo, 425 Object expr) 426 throws BSFException { 427 final BSFEngine e = loadScriptingEngine(lang); 428 final String sourcef = source; 429 final int lineNof = lineNo, columnNof = columnNo; 430 final Object exprf = expr; 431 Object result = null; 432 433 try { 434 final Object resultf = 435 AccessController.doPrivileged(new PrivilegedExceptionAction() { 436 public Object run() throws Exception { 437 return e.eval(sourcef, lineNof, columnNof, exprf); 438 } 439 }); 440 result = resultf; 441 } 442 catch (PrivilegedActionException prive) { 443 throw (BSFException) prive.getException(); 444 } 445 446 return result; 447 } 448 449 456 468 public void exec(String lang, 469 String source, 470 int lineNo, 471 int columnNo, 472 Object script) 473 throws BSFException { 474 final BSFEngine e = loadScriptingEngine(lang); 475 final String sourcef = source; 476 final int lineNof = lineNo, columnNof = columnNo; 477 final Object scriptf = script; 478 479 try { 480 AccessController.doPrivileged(new PrivilegedExceptionAction() { 481 public Object run() throws Exception { 482 e.exec(sourcef, lineNof, columnNof, scriptf); 483 return null; 484 } 485 }); 486 } 487 catch (PrivilegedActionException prive) { 488 throw (BSFException) prive.getException(); 489 } 490 } 491 492 495 public ClassLoader getClassLoader() { 496 return classLoader; 497 } 498 499 502 public String getClassPath() { 503 if (classPath == null) { 504 try { 505 classPath = System.getProperty("java.class.path"); 506 } 507 catch (Throwable t) { 508 } 510 } 511 return classPath; 512 } 513 514 517 public static BSFDebugManager getDebugManager() { 518 return gDebugManager; 519 } 520 521 533 public static String getLangFromFilename(String fileName) 534 throws BSFException { 535 int dotIndex = fileName.lastIndexOf("."); 536 537 if (dotIndex != -1) { 538 String extn = fileName.substring(dotIndex + 1); 539 String langval = (String ) extn2Lang.get(extn), lang = null; 540 int index = 0, loops = 0; 541 542 if (langval != null) { 543 while ((index = langval.indexOf(":", 0)) != -1) { 544 lang = langval.substring(0, index); 548 langval = langval.substring(index + 1); 549 loops++; 550 551 try { 553 String engineName = 554 (String ) registeredEngines.get(lang); 555 Class.forName(engineName); 556 } 557 catch (ClassNotFoundException cnfe) { 558 lang = langval; 560 continue; 561 } 562 563 break; 565 } 566 if (loops == 0) lang = langval; 567 } 568 569 if (lang != null && lang != "") { 570 return lang; 571 } 572 } 573 throw new BSFException(BSFException.REASON_OTHER_ERROR, 574 "file extension missing or unknown: " 575 + "unable to determine language for '" 576 + fileName 577 + "'"); 578 } 579 580 585 public ObjectRegistry getObjectRegistry() { 586 return objectRegistry; 587 } 588 589 592 public String getTempDir() { 593 return tempDir; 594 } 595 596 603 public static boolean isLanguageRegistered(String lang) { 604 return (registeredEngines.get(lang) != null); 605 } 606 607 613 624 public BSFEngine loadScriptingEngine(String lang) throws BSFException { 625 BSFEngine eng = (BSFEngine) loadedEngines.get(lang); 627 if (eng != null) { 628 return eng; 629 } 630 631 String engineClassName = (String ) registeredEngines.get(lang); 633 if (engineClassName == null) { 634 throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE, 635 "unsupported language: " + lang); 636 } 637 638 try { 641 Class engineClass = 642 (classLoader == null) 643 ? Class.forName(engineClassName) 644 : classLoader.loadClass(engineClassName); 645 final BSFEngine engf = (BSFEngine) engineClass.newInstance(); 646 final BSFManager thisf = this; 647 final String langf = lang; 648 final Vector dbf = declaredBeans; 649 AccessController.doPrivileged(new PrivilegedExceptionAction() { 650 public Object run() throws Exception { 651 engf.initialize(thisf, langf, dbf); 652 return null; 653 } 654 }); 655 eng = engf; 656 loadedEngines.put(lang, eng); 657 pcs.addPropertyChangeListener(eng); 658 return eng; 659 } 660 catch (PrivilegedActionException prive) { 661 throw (BSFException) prive.getException(); 662 } 663 catch (Throwable t) { 664 throw new BSFException(BSFException.REASON_OTHER_ERROR, 665 "unable to load language: " + lang, 666 t); 667 } 668 } 669 670 678 public Object lookupBean(String beanName) { 679 try { 680 return objectRegistry.lookup(beanName); 681 } 682 catch (IllegalArgumentException e) { 683 return null; 684 } 685 } 686 687 703 public static void initBSFDebugManager() { 704 if(gDebugManager != null) 705 DebugLog.stdoutPrintln("BSF Debug Manager already running...", 706 DebugLog.BSF_LOG_L1); 707 else { 708 DebugLog.stdoutPrintln("BSF in Debug Mode...", 709 DebugLog.BSF_LOG_L1); 710 try { 711 AccessController.doPrivileged(new PrivilegedExceptionAction() { 714 public Object run() throws Exception { 715 System.setProperty("org.apache.bsf.isServer", 716 "true"); 717 return null; 718 } 719 }); 720 721 gDebugManager = new BSFDebugManagerImpl(); 722 } 723 catch (PrivilegedActionException prive) { 724 Exception e = prive.getException(); 725 System.err.println("Cannot set up debug manager: " 726 + e.getMessage()); 727 e.printStackTrace(); 728 System.err.println("Execution continues without debugging."); 729 } 730 catch (Exception e) { 731 System.err.println("BSF Factory cannot bound BSF Manager: " 732 + e.getMessage()); 733 e.printStackTrace(); 734 System.err.println("Ignoring... Execution continues..."); 735 } 736 } 737 } 738 739 746 public void registerBean(String beanName, Object bean) { 747 objectRegistry.register(beanName, bean); 748 } 749 750 760 public static void registerScriptingEngine(String lang, 761 String engineClassName, 762 String [] extensions) { 763 registeredEngines.put(lang, engineClassName); 764 if (extensions != null) { 765 for (int i = 0; i < extensions.length; i++) { 766 String langstr = (String ) extn2Lang.get(extensions[i]); 767 langstr = (langstr == null) ? lang : lang + ":" + langstr; 768 extn2Lang.put(extensions[i], langstr); 769 } 770 } 771 } 772 773 779 public void setClassLoader(ClassLoader classLoader) { 780 pcs.firePropertyChange("classLoader", this.classLoader, classLoader); 781 this.classLoader = classLoader; 782 } 783 784 790 public void setClassPath(String classPath) { 791 pcs.firePropertyChange("classPath", this.classPath, classPath); 792 this.classPath = classPath; 793 } 794 795 802 public void setObjectRegistry(ObjectRegistry objectRegistry) { 803 this.objectRegistry = objectRegistry; 804 } 805 806 818 public void setTempDir(String tempDir) { 819 pcs.firePropertyChange("tempDir", this.tempDir, tempDir); 820 this.tempDir = tempDir; 821 } 822 823 826 public void terminate() { 827 Enumeration enginesEnum = loadedEngines.elements(); 828 BSFEngine engine; 829 while (enginesEnum.hasMoreElements()) { 830 engine = (BSFEngine) enginesEnum.nextElement(); 831 engine.terminate(); 832 } 833 834 loadedEngines = new Hashtable(); 835 836 if (gDebugManager != null) 837 gDebugManager.terminateManagerNotify(this); 838 } 839 840 853 public void undeclareBean(String beanName) throws BSFException { 854 unregisterBean(beanName); 855 856 BSFDeclaredBean tempBean = null; 857 for (int i = 0; i < declaredBeans.size(); i++) { 858 tempBean = (BSFDeclaredBean) declaredBeans.elementAt(i); 859 if (tempBean.name.equals(beanName)) { 860 break; 861 } 862 } 863 864 if (tempBean != null) { 865 declaredBeans.removeElement(tempBean); 866 867 Enumeration enginesEnum = loadedEngines.elements(); 868 while (enginesEnum.hasMoreElements()) { 869 BSFEngine engine = (BSFEngine) enginesEnum.nextElement(); 870 engine.undeclareBean(tempBean); 871 } 872 } 873 } 874 875 880 public void unregisterBean(String beanName) { 881 objectRegistry.unregister(beanName); 882 } 883 } 884 | Popular Tags |