#副標=Java於Web Tier上的應用 #大標=Java 動態編譯、載入與執行 Part1 #眉標=Java #作者=文/歐宣修 -----------------------box 程式1----------------------- /** Programmatic interface. * @param args The command line parameters. */ public static int compile(String[] args) {  com.sun.tools.javac.main.Main compiler = new com.sun.tools.javac.main.Main("javac");  return compiler.compile(args); } ---------------------------- end ----------------------------- -------------------------- box 程式2 ----------------------- /** Programmatic interface for main function. * @param args The command line parameters. */ public int compile(String[] args, Context context) {  boolean assertionsEnabled = false;  assert assertionsEnabled = true;  if (!assertionsEnabled) {   // Log.printLines(out, "fatal error: assertions must    be enabled when running javac"); // return EXIT_ABNORMAL;  } 以下略… } -------------------------------- end -------------------------------- ---------------------- box 程式3 ----------------------------- Object objectParameters[] = {new String[]{}}; Class classParameters[] = {objectParameters[0]. getClass()}; Method theMethod = aClass.getDeclaredMethod( "main", classParameters); // Static method, no instance needed theMethod.invoke(null, objectParameters); -------------------------- end ---------------------------- ------------------------------- box 桯式4 ----------------------------- Process.java public interface Process { public String doProcess(String arg); //…(1) } ------------------------------------- end --------------------------------- -------------------------------- box 程式5------------------------------- AProcess.java public class AProcess implements Process { public String doProcess(String arg) { //…(1) return "Hello!!! "+arg + " at "+Time. getNowTime(); } } ------------------------------------------- end --------------------------------------- ------------------------------- box 程式 6 -------------------- ProcessFactory.java public interface ProcessFactory { public void newClassLoader(); //…(1) public Process getProcess(String name); //…(2) } ------------------------------- end ----------------------------------- ------------------------------------------- box 程式7 ---------------------------------- ProcessFactoryImpl.java /** * Reset ClassLoader //…(1) */ public void newClassLoader() { try { File file = new File(PROCESS_PATH); //…(2) loader = new URLClassLoader(new URL[] { file.toURL() }); } catch (MalformedURLException e) { e.printStackTrace(); } processes = new HashMap();//…(3) log.debug("reset ok"); } /** * 依名稱取得Process //…(4) */ public Process getProcess(String name) { idv.senshaw.dynamic.Process process = (idv.senshaw.dynamic.Process) processes.get(name); String full_name = "idv.senshaw.dynamic.userprocess." + name; log.debug("full_name=" + full_name); if (process != null) { //…(5) log.debug("已有Process"); return process; } log.debug("new Process"); try { log.debug("目前ClassLoader=" + (this.getClass().getClassLoader() == null ? "bootstrap" : this.getClass().getClassLoader(). getClass().getName() ) ); // load class through new loader Class processClass = loader.loadClass( full_name); //…(6) log.debug("class_name=" + processClass. getName()); log.debug("processClass classloader=" + (processClass.getClassLoader() == null ? "bootstrap" : processClass.getClassLoader().getClass().getName())); Object obj = processClass.newInstance(); log.debug("obj class=" + obj.getClass(). getName()); log.debug("new instance ok"); Class[] interfaces = obj.getClass().getInterfaces(); for (int i = 0; i < interfaces.length; i++) { log.debug("interface name=" + interfaces[i]. getName()); log.debug("interface classloader=" + ( interfaces[i].getClassLoader() == null ? "bootstrap" : interfaces[i].getClassLoader().getClass().getName())); } if (obj instanceof idv.senshaw.dynamic.Process) { log.debug("yes!"); } else { log.debug("no!"); } process = (idv.senshaw.dynamic.Process) obj; processes.put(name, process); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return process; } 以下略… -------------------------------------- end ------------------------------ --------------------------------- box 程式8 --------------------- ShowServlet.java /** * 展現內容 //…(1) * @param request * @param response */ private void show(HttpServletRequest request, HttpServletResponse response) { String process_name = request.getParameter( "process"); //…(2) if (process_name == null) { process_name = "AProcess"; } log.debug("process_name="+process_name); String arg = request.getParameter("arg"); //…(3) if (arg == null) { arg = "nana"; } log.debug("arg="+arg); Process process = processFactory.getProcess( process_name); //…(4) String result = process.doProcess(arg); 以下略… } ---------------------------------- end------------------------------------------ ---------------------------------- box 程式9 ---------------------------- loader = new URLClassLoader(new URL[] { file.toURL() }); ----------------------------------- end -------------------------- ---------------------------------- box 程式10 ------------------------ loader = new URLClassLoader(new URL[] { file.toURL() }, this.getClass().getClassLoader()); --------------------------------------- end ----------------------------- ------------------------- box 程式11 ------------------------- ResetServlet.java //…(1) private void reset(HttpServletRequest request, HttpServletResponse response) { processFactory.newClassLoader();//…(2) 以下略.. } -------------------------------------- end ----------------------- ----------------------- box 程式12 ------------------------ public String doProcess(String arg) { return "Hello!!! "+arg + " at "+Time.getNowTime() + "in Kaohsiung."; } --------------------------- end -----------------------------