You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestServer.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*******************************************************************************
  2. * Copyright (c) 2006,2017 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *******************************************************************************/
  8. package org.aspectj.testing.server;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.lang.reflect.Method;
  14. import java.net.URL;
  15. import java.net.URLClassLoader;
  16. import java.util.ArrayList;
  17. import java.util.Enumeration;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. import java.util.StringTokenizer;
  23. /**
  24. * @author Matthew Webster
  25. * @author Andy Clement
  26. */
  27. public class TestServer implements Runnable {
  28. private static final boolean debug = Boolean.getBoolean("org.aspectj.testing.server.debug");
  29. private boolean exitOnError = true;
  30. private File workingDirectory;
  31. private ClassLoader rootLoader;
  32. private Map<String,ClassLoader> loaders = new HashMap<>();
  33. private String mainClass = "UnknownClass";
  34. private String mainLoader = "UnknownLoader";
  35. public void initialize () throws IOException {
  36. createRootLoader();
  37. loadConfiguration();
  38. }
  39. private void loadConfiguration () throws IOException {
  40. File file = new File(workingDirectory,"server.properties");
  41. Properties props = new Properties();
  42. FileInputStream in = new FileInputStream(file);
  43. props.load(in);
  44. in.close();
  45. Enumeration<?> enu = props.propertyNames();
  46. while (enu.hasMoreElements()) {
  47. String key = (String)enu.nextElement();
  48. if (key.startsWith("loader.")) {
  49. createLoader(props.getProperty(key));
  50. }
  51. else if (key.equals("main")) {
  52. StringTokenizer st = new StringTokenizer(props.getProperty(key),",");
  53. mainClass = st.nextToken();
  54. mainLoader = st.nextToken();
  55. }
  56. }
  57. }
  58. private void createLoader (String property) throws IOException {
  59. ClassLoader parent = rootLoader;
  60. StringTokenizer st = new StringTokenizer(property,",");
  61. String name = st.nextToken();
  62. String classpath = st.nextToken();
  63. if (debug) System.err.println("Creating loader "+name+" with classpath "+classpath);
  64. if (st.hasMoreTokens()) {
  65. String parentName = st.nextToken();
  66. parent = (ClassLoader)loaders.get(parentName);
  67. if (parent == null) error("No such loader: " + parentName);
  68. }
  69. List<URL> urlList = new ArrayList<>();
  70. st = new StringTokenizer(classpath,";");
  71. while (st.hasMoreTokens()) {
  72. String fileName = st.nextToken();
  73. File file = new File(workingDirectory,fileName).getCanonicalFile();
  74. if (!file.exists()) error("Missing or invalid file: " + file.getPath());
  75. URL url = file.toURI().toURL();
  76. urlList.add(url);
  77. }
  78. URL[] urls = new URL[urlList.size()];
  79. urlList.toArray(urls);
  80. ClassLoader loader = new URLClassLoader(urls, parent);
  81. if (debug) System.err.println("? TestServer.createLoader() loader=" + loader + ", name='" + name + "', urls=" + urlList + ", parent=" + parent);
  82. loaders.put(name,loader);
  83. }
  84. private void createRootLoader() throws IOException {
  85. List<URL> urlList = new ArrayList<>();
  86. // Sandbox
  87. URL url = workingDirectory.getCanonicalFile().toURI().toURL();
  88. urlList.add(url);
  89. // Find the AspectJ root folder (i.e. org.aspectj)
  90. File aspectjBase = new File(".").getCanonicalFile();
  91. while (aspectjBase!= null && !aspectjBase.getName().equals("org.aspectj")) {
  92. aspectjBase = aspectjBase.getParentFile();
  93. }
  94. if (aspectjBase == null) {
  95. error("Unable to locate 'org.aspectj' in "+new File(".").getCanonicalPath());
  96. }
  97. urlList.add(new File(aspectjBase,"runtime/target/classes").toURI().toURL());
  98. // urlList.add(new File(aspectjBase,"aspectjrt/target/classes").toURI().toURL());
  99. // urlList.add(new File(aspectjBase,"aspectj5rt/target/classes").toURI().toURL());
  100. URL[] urls = new URL[urlList.size()];
  101. urlList.toArray(urls);
  102. ClassLoader parent = getClass().getClassLoader().getParent();
  103. rootLoader = new URLClassLoader(urls,parent);
  104. if (debug) System.err.println("? TestServer.createRootLoader() loader=" + rootLoader + ", urlList=" + urlList + ", parent=" + parent);
  105. }
  106. public void setExitOntError (boolean b) {
  107. exitOnError = b;
  108. }
  109. public void setWorkingDirectory (String name) {
  110. workingDirectory = new File(name);
  111. if (!workingDirectory.exists()) error("Missing or invalid working directory: " + workingDirectory.getPath());
  112. }
  113. public static void main(String[] args) throws Exception {
  114. System.out.println("Starting ...");
  115. TestServer server = new TestServer();
  116. server.setWorkingDirectory(args[0]);
  117. server.initialize();
  118. Thread thread = new Thread(server,"application");
  119. thread.start();
  120. thread.join();
  121. System.out.println("Stopping ...");
  122. }
  123. public void run() {
  124. System.out.println("Running " + mainClass);
  125. runClass(mainClass,(ClassLoader)loaders.get(mainLoader));
  126. }
  127. private void runClass (String className, ClassLoader classLoader) {
  128. try {
  129. Class clazz = Class.forName(className,false,classLoader);
  130. invokeMain(clazz,new String[] {});
  131. }
  132. catch (ClassNotFoundException ex) {
  133. ex.printStackTrace();
  134. error(ex.toString());
  135. }
  136. }
  137. public void invokeMain (Class<?> clazz, String[] args)
  138. {
  139. Class<?>[] paramTypes = new Class[1];
  140. paramTypes[0] = args.getClass();
  141. try {
  142. Method method = clazz.getDeclaredMethod("main",paramTypes);
  143. Object[] params = new Object[1];
  144. params[0] = args;
  145. method.invoke(null,params);
  146. }
  147. catch (InvocationTargetException ex) {
  148. Throwable th = ex.getTargetException();
  149. th.printStackTrace();
  150. error(th.toString());
  151. }
  152. catch (Throwable th) {
  153. th.printStackTrace();
  154. error(th.toString());
  155. }
  156. }
  157. private void error (String message) {
  158. System.out.println(message);
  159. if (exitOnError) System.exit(0);
  160. }
  161. }