You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by tc...@apache.org on 2007/03/17 01:08:21 UTC

svn commit: r519193 - in /jakarta/commons/sandbox/jci/trunk/examples: ./ src/main/java/org/apache/commons/jci/examples/commandline/ src/main/java/org/apache/commons/jci/examples/serverpages/ src/main/webapp/ src/main/webapp/WEB-INF/ src/main/webapp/WEB...

Author: tcurdt
Date: Fri Mar 16 17:08:20 2007
New Revision: 519193

URL: http://svn.apache.org/viewvc?view=rev&rev=519193
Log:
command line example working (with commons cli 1.0.1 SNAPSHOT)
basic serverpages servlet example,
use jetty:run to start servlet example


Added:
    jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/
    jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/
    jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/
    jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java   (with props)
    jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml   (with props)
Modified:
    jakarta/commons/sandbox/jci/trunk/examples/pom.xml
    jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/commandline/CommandlineCompiler.java
    jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/serverpages/ServerPageServlet.java

Modified: jakarta/commons/sandbox/jci/trunk/examples/pom.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/examples/pom.xml?view=diff&rev=519193&r1=519192&r2=519193
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/examples/pom.xml (original)
+++ jakarta/commons/sandbox/jci/trunk/examples/pom.xml Fri Mar 16 17:08:20 2007
@@ -29,5 +29,26 @@
             <artifactId>commons-cli</artifactId>
             <version>1.0</version>
         </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.4</version>
+        </dependency>
     </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.mortbay.jetty</groupId>
+                <artifactId>maven-jetty-plugin</artifactId>
+                <configuration>
+                    <systemProperties>
+                        <systemProperty>
+                            <name>org.apache.commons.logging.Log</name>
+                            <value>org.apache.commons.logging.impl.SimpleLog</value>
+                        </systemProperty>
+                    </systemProperties>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>

Modified: jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/commandline/CommandlineCompiler.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/commandline/CommandlineCompiler.java?view=diff&rev=519193&r1=519192&r2=519193
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/commandline/CommandlineCompiler.java (original)
+++ jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/commandline/CommandlineCompiler.java Fri Mar 16 17:08:20 2007
@@ -25,7 +25,6 @@
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
 import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
@@ -96,8 +95,8 @@
 				OptionBuilder.withDescription("Generate no warnings")
 					.create( "nowarn" ));
 		
-		final HelpFormatter formatter = new HelpFormatter();
-		formatter.printHelp("jci", options);
+//		final HelpFormatter formatter = new HelpFormatter();
+//		formatter.printHelp("jci", options);
 		
 		final CommandLineParser parser = new GnuParser();
 		final CommandLine cmd = parser.parse(options, args, true);

Modified: jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/serverpages/ServerPageServlet.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/serverpages/ServerPageServlet.java?view=diff&rev=519193&r1=519192&r2=519193
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/serverpages/ServerPageServlet.java (original)
+++ jakarta/commons/sandbox/jci/trunk/examples/src/main/java/org/apache/commons/jci/examples/serverpages/ServerPageServlet.java Fri Mar 16 17:08:20 2007
@@ -17,11 +17,165 @@
 
 package org.apache.commons.jci.examples.serverpages;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.jci.ReloadingClassLoader;
+import org.apache.commons.jci.listeners.CompilingListener;
+import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
+import org.apache.commons.jci.monitor.FilesystemAlterationObserver;
+
 
 /**
- * 
  * @author tcurdt
  */
-public final class ServerPageServlet {
+public final class ServerPageServlet extends HttpServlet {
+
+	private static final long serialVersionUID = 1L;
+
+	private final ReloadingClassLoader classloader = new ReloadingClassLoader(ServerPageServlet.class.getClassLoader());
+	private CompilingListener listener;
+	private FilesystemAlterationMonitor fam;
+
+	private Map serverPagesByClassName = new HashMap();
+
+	public void init() throws ServletException {
+		super.init();
+		
+		final File directory = new File(getServletContext().getRealPath("/") + getInitParameter("directory"));
+		
+		log("monitoring classes in " + directory);
+
+        listener = new CompilingListener() {
+
+			public void onStop( final FilesystemAlterationObserver pObserver ) {
+				super.onStop(pObserver);
+
+				final File root = pObserver.getRootDirectory();
+								
+				final Collection changedFiles = getChangedFiles();
+				changedFiles.addAll(getCreatedFiles());
+				final Collection deletedFiles = getDeletedFiles();
+				
+				boolean reload = false;
+				
+				final Map newServerPagesByClassName = new HashMap(serverPagesByClassName);
+				
+				for (Iterator it = deletedFiles.iterator(); it.hasNext();) {
+					final File file = (File) it.next();
+					final String serverPageClassName = convertFileToServerPageClassName(root, file);
+					newServerPagesByClassName.remove(serverPageClassName);
+					reload = true;
+					log("removing " + serverPageClassName);
+				}
+
+				for (Iterator it = changedFiles.iterator(); it.hasNext();) {
+					final File file = (File) it.next();
+					
+					final String serverPageClassName = convertFileToServerPageClassName(root, file);
+					
+					if (serverPageClassName == null) {
+						continue;
+					}
+					
+					try {
+						final Class clazz = classloader.loadClass(serverPageClassName);
+						final HttpServlet serverPage = (HttpServlet) clazz.newInstance();
+						newServerPagesByClassName.put(serverPageClassName, serverPage);
+						reload = true;
+						log("compiled " + serverPageClassName);
+					} catch (ClassNotFoundException e) {
+						log("", e);
+					} catch (InstantiationException e) {
+						log("", e);
+					} catch (IllegalAccessException e) {
+						log("", e);
+					}
+				}
+
+				if (reload) {
+					log("activating new map of serverpages "+ newServerPagesByClassName);
+					serverPagesByClassName = newServerPagesByClassName;					
+				}
+
+			}        	
+        };
+        listener.addReloadNotificationListener(classloader);
+        
+        fam = new FilesystemAlterationMonitor();
+        fam.addListener(directory, listener);
+        fam.start();
+	}
+
+	private String convertFileToServerPageClassName( final File root, final File file ) {
+
+		if (!file.getName().endsWith(".java")) {
+			return null;
+		}
+		
+		final String relativeName = file.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
+
+		log("relative: " + relativeName);
+		
+		final String clazzName = relativeName.replace('/', '.').substring(0, relativeName.length() - 5); 
+
+		log("clazz: " + clazzName);
+
+		return clazzName;
+	}
+	
+	private String convertRequestToServerPageClassName( final HttpServletRequest request ) {
+
+		final String path = request.getPathInfo().substring(1);
+
+//		log("1 " + request.getContextPath());
+//		log("2 " + request.getPathInfo());
+//		log("3 " + request.getPathTranslated());
+//		log("4 " + request.getRequestURI());
+//		log("5 " + request.getServletPath());
+//		log("6 " + request.getRequestURL());
+		
+		// FIXME
+		// /some/page/bla.jsp -> some.page.Bla
+		
+		final String clazz = path;
+		
+		return clazz;
+	}
+	
+	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		log("request " + request.getRequestURI());
+		
+		final String serverPageNameClassName = convertRequestToServerPageClassName(request);
+
+		log("checking for serverpage " + serverPageNameClassName);
+		
+		final HttpServlet serverPage = (HttpServlet) serverPagesByClassName.get(serverPageNameClassName);
+		
+		if (serverPage == null) {
+			log("no serverpage  for " + request.getRequestURI());
+			response.sendError(404);
+			return;
+		}
+
+		log("delegating request to " + serverPageNameClassName);
+		
+		serverPage.service(request, response);
+	}
 
+	public void destroy() {
+		
+		fam.stop();
+		
+		super.destroy();		
+	}
 }

Added: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java?view=auto&rev=519193
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java (added)
+++ jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java Fri Mar 16 17:08:20 2007
@@ -0,0 +1,12 @@
+import java.io.IOException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletException;
+
+
+public class Test extends HttpServlet {
+    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        System.out.println("SERVERPAGE!!");
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/classes/Test.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml?view=auto&rev=519193
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml (added)
+++ jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml Fri Mar 16 17:08:20 2007
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
+    <servlet>
+        <servlet-name>serverpages</servlet-name>
+        <servlet-class>org.apache.commons.jci.examples.serverpages.ServerPageServlet</servlet-class>
+        <init-param>
+            <param-name>directory</param-name>
+            <param-value>/WEB-INF/classes</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>serverpages</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: jakarta/commons/sandbox/jci/trunk/examples/src/main/webapp/WEB-INF/web.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org