jspcompiler.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 857 行 · 第 1/2 页
JAVA
857 行
* @param uri the uri for the JSP file * * @return a JspPage instance */ public Page compile(Path jspPath, String uri) throws Exception { return getCompilerInstance(jspPath, uri).compile(); } /** * Returns the compilation instance. */ public JspCompilerInstance getCompilerInstance(Path jspPath, String uri) throws Exception { return getCompilerInstance(jspPath, uri, null); } public void init() throws JspParseException, IOException { getTaglibManager(); } /** * Returns the compilation instance. */ public JspCompilerInstance getCompilerInstance(Path jspPath, String uri, String className) throws Exception { JspCompilerInstance instance = new JspCompilerInstance(this); instance.setJspPath(jspPath); instance.setURI(uri); instance.setClassName(className); instance.init(); return instance; } /** * Loads an already-compiled JSP class. * * @param className the mangled classname for the JSP file. */ public Page loadPage(String className, boolean isAutoCompile) throws Throwable { JspPage jspPage = (JspPage) loadClass(className, isAutoCompile); Page page; if (jspPage instanceof Page) page = (Page) jspPage; else if (jspPage instanceof SingleThreadModel) page = new SingleThreadWrapperPage((HttpJspPage) jspPage); else page = new WrapperPage((HttpJspPage) jspPage); return page; } /** * Loads an already-compiled JSP class. * * @param className the mangled classname for the JSP file. */ public Object loadClass(String className, boolean autoCompile) throws Throwable { ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); ClassLoader jspLoader = SimpleLoader.create(parentLoader, getClassDir(), null); // If the loading fails, remove the class because it may be corrupted try { Class cl = CauchoSystem.loadClass(className, false, jspLoader); readSmap(parentLoader, className); return cl.newInstance(); } catch (Throwable e) { if (autoCompile) { try { String pathName = className.replace('.', '/') + ".class"; Path classPath = getClassDir().lookup(pathName); classPath.remove(); } catch (IOException e1) { log.log(Level.FINE, e1.toString(), e1); } } throw e; } } /** * Loads an already-compiled JSP class. * * @param className the mangled classname for the JSP file. */ public Page loadStatic(String className, boolean isSession) throws Exception { ClassLoader loader = Thread.currentThread().getContextClassLoader(); // If the loading fails, remove the class because it may be corrupted String staticName = className.replace('.', '/') + ".static"; Path path = getClassDir().lookup(staticName); return new StaticPage(path, isSession); } private void readSmap(ClassLoader loader, String className) { if (loader == null) return; String smapName = className.replace('.', '/') + ".java.smap"; InputStream is = null; try { is = loader.getResourceAsStream(smapName); } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } } } public static void main(String []args) throws Exception { if (args.length == 0) { System.out.println("usage: com.caucho.jsp.JspCompiler [flags] jsp1 jsp2 ..."); System.out.println(" -app-dir : The directory root of the web-app."); System.out.println(" -class-dir: The working directory to use as output."); System.out.println(" -compiler: sets the javac."); System.out.println(" -conf: A configuration file for the compiler."); System.exit(1); } // needed at minimum to handle the qa jsp/1933 Thread thread = Thread.currentThread(); ClassLoader oldLoader = thread.getContextClassLoader(); try { JspCompiler compiler = new JspCompiler(); int i = compiler.configureFromArgs(args); ClassLoader loader = compiler.getClassLoader(); thread.setContextClassLoader(loader); ArrayList<String> pendingClasses = new ArrayList<String>(); if (i == args.length) { compiler.compilePath(pendingClasses, "."); } for (; i < args.length; i++) { String uri = args[i]; compiler.compilePath(pendingClasses, uri); } String files[] = new String[pendingClasses.size()]; pendingClasses.toArray(files); compiler.compileBatch(files); } finally { Thread.currentThread().setContextClassLoader(oldLoader); } } /** * Callable by applications to initialize the compiler. This call * will configure the JspCompiler, but not start any compilations. */ public int configureFromArgs(String []args) throws Exception { if (args.length == 0) { System.out.println("usage: com.caucho.jsp.JspCompiler [flags] jsp1 jsp2 ..."); System.out.println(" -app-dir : The directory root of the web-app."); System.out.println(" -class-dir: The working directory to use as output."); System.out.println(" -conf: A configuration file for the compiler."); System.exit(1); } // needed at minimum to handle the qa jsp/1933 Thread thread = Thread.currentThread(); ClassLoader oldLoader = thread.getContextClassLoader(); try { ClassLoader loader = getClassLoader(); thread.setContextClassLoader(loader); JspPropertyGroup jsp = createJsp(); jsp.setRequireSource(false); int i = 0; boolean hasConf = false; while (i < args.length) { if (args[i].equals("-app-dir")) { Path appDir = Vfs.lookup(args[i + 1]); WebApp app = createWebApp(appDir); setWebApp(app); setAppDir(appDir); i += 2; } else if (args[i].equals("-class-dir") || args[i].equals("-d")) { setClassDirectory(Vfs.lookup(args[i + 1])); i += 2; } else if (args[i].equals("-compiler")) { JavacConfig.getLocalConfig().setCompiler(args[i + 1]); i += 2; } else if (args[i].equals("-conf")) { Path path = Vfs.lookup(args[i + 1]); new Config().configureBean(this, path); hasConf = true; i += 2; } else break; } WebApp app = getWebApp(); if (app != null && ! hasConf) { Path appDir = app.getAppDir(); DynamicClassLoader dynLoader = app.getEnvironmentClassLoader(); dynLoader.addLoader(new CompilingLoader(appDir.lookup("WEB-INF/classes"))); dynLoader.addLoader(new DirectoryLoader(appDir.lookup("WEB-INF/lib"))); Path webXml = appDir.lookup("WEB-INF/web.xml"); if (webXml.canRead()) { try { new Config().configureBean(app, webXml); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } } Path appDir = null; if (app == null && getAppDir() != null) { app = createWebApp(null); app.setRootDirectory(getAppDir()); setWebApp(app); } if (app != null) { app.init(); appDir = getWebApp().getAppDir(); setClassLoader(getWebApp().getClassLoader()); } if (appDir == null) { appDir = Vfs.lookup(); if (getAppDir() == null && getWebApp() == null) { System.err.println(L.l("-app-dir must be specified for JspCompiler")); return -1; } } setResourceManager(new AppDirResourceManager(appDir)); return i; } finally { Thread.currentThread().setContextClassLoader(oldLoader); } } public void compilePath(ArrayList<String> pendingClasses, String uri) throws Exception { Thread thread = Thread.currentThread(); ClassLoader oldLoader = thread.getContextClassLoader(); try { thread.setContextClassLoader(getClassLoader()); Path path = Vfs.lookup(uri); if (path.isDirectory()) compileDirectory(path, getAppDir(), this, pendingClasses); else compileJsp(path, getAppDir(), this, pendingClasses); } finally { thread.setContextClassLoader(oldLoader); } } public void compileBatch(String []pendingClasses) throws Exception { Thread thread = Thread.currentThread(); ClassLoader oldLoader = thread.getContextClassLoader(); try { thread.setContextClassLoader(getClassLoader()); JavaCompiler javaCompiler = JavaCompiler.create(getClassLoader()); javaCompiler.setClassDir(getClassDir()); javaCompiler.compileBatch(pendingClasses); } finally { thread.setContextClassLoader(oldLoader); } } private static void compileDirectory(Path path, Path appDir, JspCompiler compiler, ArrayList<String> pendingClasses) throws Exception { if (path.isDirectory()) { String []list = path.list(); for (int i = 0; i < list.length; i++) { Path subpath = path.lookup(list[i]); compileDirectory(subpath, appDir, compiler, pendingClasses); } } else if (path.getPath().endsWith(".jsp") || path.getPath().endsWith(".jsfx") || path.getPath().endsWith(".jspx") || path.getPath().endsWith(".jsfx")) { compileJsp(path, appDir, compiler, pendingClasses); } } private static void compileJsp(Path path, Path appDir, JspCompiler compiler, ArrayList<String> pendingClasses) throws Exception { String uri; uri = path.getPath().substring(appDir.getPath().length()); if (uri.endsWith("x")) compiler.setXml(true); else compiler.setXml(false); String className = JspCompiler.urlToClassName(uri); JspCompilerInstance compInst; compInst = compiler.getCompilerInstance(path, uri, className); JspGenerator gen = compInst.generate(); if (! gen.isStatic()) pendingClasses.add(className.replace('.', '/') + ".java"); } public class ApplicationConfig { private Path _rootDir; private ContainerProgram _program = new ContainerProgram(); ApplicationConfig() { _rootDir = Vfs.lookup(); } public void setRootDirectory(Path path) { _rootDir = path; } public void setDocumentDirectory(Path path) { _rootDir = path; } public void setAppDir(Path path) { _rootDir = path; } public void addBuilderProgram(ConfigProgram program) { _program.addProgram(program); } @PostConstruct public void init() throws Exception { WebApp webApp = createWebApp(_rootDir); _program.configure(webApp); Config.init(webApp); webApp.init(); webApp.start(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?