Commits

Zemian Deng  committed d00eb17

Renamed Main to WinstoneMain for better naming.

  • Participants
  • Parent commits c3dc0bb
  • Tags 2.x

Comments (0)

Files changed (3)

File webnotepad/pom.xml

 							</packagingExcludes>
 							<archive>
 								<manifest>
-									<mainClass>Main</mainClass>
+									<mainClass>WinstoneMain</mainClass>
 								</manifest>
 							</archive>
 						</configuration>
 								<configuration>
 									<target>
 										<move todir="${basedir}/target/${project.artifactId}">
-											<fileset dir="${basedir}/target/classes" includes="Main*.class" />
+											<fileset dir="${basedir}/target/classes" includes="WinstoneMain*.class" />
 										</move>
 									</target>
 								</configuration>

File webnotepad/src/main/winstone/Main.java

-
-import java.net.*;
-import java.io.*;
-import java.util.*;
-import java.util.jar.*;
-import java.lang.reflect.*;
-
-/**
- * A main launcher to start Winston servlet server.
- * @author Zemian Deng <saltnlight5@gmail.com>
- */
-public class Main {
-	/** Main entry */
-	public static void main(String[] args) throws Exception {
-		// Prepare a temp dir for winstone server
-		String webserverDirStr = System.getProperty("notesWebServerDir", System.getProperty("user.home") + "/.winstone");
-		File webserverDir = new File(webserverDirStr);
-		if (!webserverDir.exists())
-			webserverDir.mkdirs();
-		System.out.println("Preparing " + webserverDirStr + " directory to unpack and run web application.");
-		
-		// Extract winstone and other jars
-		URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
-		JarFile jarFile = new JarFile(url.getFile());
-		Enumeration<JarEntry> entries = jarFile.entries();
-		while (entries.hasMoreElements()) {
-			JarEntry entry = entries.nextElement();
-			String entryName = entry.getName();
-			
-			if (entryName.startsWith("WEB-INF/lib-winstone/") && !entry.isDirectory()) {
-				String baseName = new File(entryName).getName();
-				File entryFile = new File(webserverDir, baseName);
-				if (!entryFile.exists()) {
-					System.out.println("Extracting " + entryName);
-					InputStream inStream = jarFile.getInputStream(entry);
-					FileOutputStream outStream = new FileOutputStream(entryFile);
-					while (inStream.available() > 0) {
-						outStream.write(inStream.read());
-					}
-					inStream.close();
-					outStream.close();
-				} else {
-					System.out.println("Already exists: " + entryFile);
-				}
-			}
-		}
-		
-		// Clean up webroot (temp work dir) if exists
-		File tmpDir = new File(webserverDir, "/tmp");
-		if (tmpDir.isDirectory() && tmpDir.exists()) {
-			System.out.println("Cleaning up temp directory: " + tmpDir);
-			deleteDir(tmpDir);
-		}
-		
-		// Load up the jars dynamically.
-		File winstoneJar = findWinstoneJar(webserverDir);
-		URL[] urls = new URL[]{ winstoneJar.toURL() };
-		URLClassLoader child = new URLClassLoader (urls, Main.class.getClassLoader());
-		Class winstoneLauncher = Class.forName("winstone.Launcher", true, child);
-		Method method = winstoneLauncher.getDeclaredMethod("main", String[].class);
-		
-		// Run winstone server
-		String war = url.getFile();
-		System.out.println("Starting Winstone Servlet Container with " + war);
-		List<String> serverArgsList = new ArrayList<String>();
-		serverArgsList.add("--warfile=" + war);
-		serverArgsList.add("--ajp13Port=-1");
-		serverArgsList.add("--webroot=" + tmpDir.getAbsolutePath());
-		serverArgsList.add("--commonLibFolder=" + webserverDirStr);
-		serverArgsList.add("--useJasper");
-		serverArgsList.addAll(Arrays.asList(args));
-		System.out.println("Server args: " + serverArgsList);
-		String[] serverArgs = serverArgsList.toArray(new String[0]);
-		Object result = method.invoke(null, new Object[]{ serverArgs });
-	}
-	
-	/** Delete a dir recursively. */
-	private static void deleteDir(File dir) {
-		File[] files = dir.listFiles();
-		for (File file : files)
-			if (file.isDirectory())
-				deleteDir(file);
-			else
-				file.delete();
-		dir.delete();
-	}
-	
-	/** Find the winstone jar in a dir regardless of version value. */
-	private static File findWinstoneJar(File dir) {
-		File[] files = dir.listFiles();
-		for (File file : files)
-			if (file.getName().startsWith("winstone-"))
-				return file;
-		throw new RuntimeException("Unable to find winstone jar.");
-	}
-}

File webnotepad/src/main/winstone/WinstoneMain.java

+
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import java.util.jar.*;
+import java.lang.reflect.*;
+
+/**
+ * A main launcher to start a tiny Winstone servlet server. We purposely not to use any package name so it can be easily loaded
+ * from inside a .war file.
+ *
+ * @author Zemian Deng <saltnlight5@gmail.com>
+ * @since 08/14/2012
+ */
+public class WinstoneMain {
+	/** Main entry */
+	public static void main(String[] args) throws Exception {
+		// Prepare a temp dir for winstone server
+		String webserverDirStr = System.getProperty("notesWebServerDir", System.getProperty("user.home") + "/.winstone");
+		File webserverDir = new File(webserverDirStr);
+		if (!webserverDir.exists())
+			webserverDir.mkdirs();
+		System.out.println("Preparing " + webserverDirStr + " directory to unpack and run web application.");
+		
+		// Extract winstone and other jars
+		URL url = WinstoneMain.class.getProtectionDomain().getCodeSource().getLocation();
+		JarFile jarFile = new JarFile(url.getFile());
+		Enumeration<JarEntry> entries = jarFile.entries();
+		while (entries.hasMoreElements()) {
+			JarEntry entry = entries.nextElement();
+			String entryName = entry.getName();
+			
+			if (entryName.startsWith("WEB-INF/lib-winstone/") && !entry.isDirectory()) {
+				String baseName = new File(entryName).getName();
+				File entryFile = new File(webserverDir, baseName);
+				if (!entryFile.exists()) {
+					System.out.println("Extracting " + entryName);
+					InputStream inStream = jarFile.getInputStream(entry);
+					FileOutputStream outStream = new FileOutputStream(entryFile);
+					while (inStream.available() > 0) {
+						outStream.write(inStream.read());
+					}
+					inStream.close();
+					outStream.close();
+				} else {
+					System.out.println("Already exists: " + entryFile);
+				}
+			}
+		}
+		
+		// Clean up webroot (temp work dir) if exists
+		File tmpDir = new File(webserverDir, "/tmp");
+		if (tmpDir.isDirectory() && tmpDir.exists()) {
+			System.out.println("Cleaning up temp directory: " + tmpDir);
+			deleteDir(tmpDir);
+		}
+		
+		// Load up the jars dynamically.
+		File winstoneJar = findWinstoneJar(webserverDir);
+		URL[] urls = new URL[]{ winstoneJar.toURL() };
+		URLClassLoader child = new URLClassLoader (urls, WinstoneMain.class.getClassLoader());
+		Class winstoneLauncher = Class.forName("winstone.Launcher", true, child);
+		Method method = winstoneLauncher.getDeclaredMethod("main", String[].class);
+		
+		// Run winstone server
+		String war = url.getFile();
+		System.out.println("Starting Winstone Servlet Container with " + war);
+		List<String> serverArgsList = new ArrayList<String>();
+		serverArgsList.add("--warfile=" + war);
+		serverArgsList.add("--ajp13Port=-1");
+		serverArgsList.add("--webroot=" + tmpDir.getAbsolutePath());
+		serverArgsList.add("--commonLibFolder=" + webserverDirStr);
+		serverArgsList.add("--useJasper");
+		serverArgsList.addAll(Arrays.asList(args));
+		System.out.println("Server args: " + serverArgsList);
+		String[] serverArgs = serverArgsList.toArray(new String[0]);
+		Object result = method.invoke(null, new Object[]{ serverArgs });
+	}
+	
+	/** Delete a dir recursively. */
+	private static void deleteDir(File dir) {
+		File[] files = dir.listFiles();
+		for (File file : files)
+			if (file.isDirectory())
+				deleteDir(file);
+			else
+				file.delete();
+		dir.delete();
+	}
+	
+	/** Find the winstone jar in a dir regardless of version value. */
+	private static File findWinstoneJar(File dir) {
+		File[] files = dir.listFiles();
+		for (File file : files)
+			if (file.getName().startsWith("winstone-"))
+				return file;
+		throw new RuntimeException("Unable to find winstone jar.");
+	}
+}