[seam-commits] Seam SVN: r9483 - in trunk: src/main/org/jboss/seam/deployment and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Nov 3 18:10:46 EST 2008
Author: pete.muir at jboss.org
Date: 2008-11-03 18:10:46 -0500 (Mon, 03 Nov 2008)
New Revision: 9483
Added:
trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketInstrumentationTask.java
Modified:
trunk/build.xml
trunk/src/main/org/jboss/seam/deployment/ClassDescriptor.java
trunk/src/main/org/jboss/seam/init/Initialization.java
trunk/src/main/org/jboss/seam/util/Resources.java
trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketClassLoader.java
trunk/src/wicket/org/jboss/seam/wicket/web/WicketFilterInstantiator.java
Log:
JBSEAM-3505, thanks to Clint Popetz
Modified: trunk/build.xml
===================================================================
--- trunk/build.xml 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/build.xml 2008-11-03 23:10:46 UTC (rev 9483)
@@ -327,7 +327,14 @@
</target>
<target name="jarwicket" depends="compilewicket" description="Build the distribution .jar file for the Wicket Integration package">
- <archive classesdir="${classes.wicket.dir}" module="jboss-seam-wicket" pom="${wicket.pom}" srcdir="${src.wicket.dir}"/>
+ <archive classesdir="${classes.wicket.dir}" module="jboss-seam-wicket" pom="${wicket.pom}" srcdir="${src.wicket.dir}">
+ <excludes>
+ <exclude name="**/WicketInstrumentationTask*"/>
+ </excludes>
+ </archive>
+ <jar jarfile="${lib.dir}/jboss-seam-wicket-ant.jar" basedir="${classes.wicket.dir}">
+ <include name="**/WicketInstrumentationTask*" />
+ </jar>
</target>
<!-- ########################### JUL TARGETS ###########################-->
Modified: trunk/src/main/org/jboss/seam/deployment/ClassDescriptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/deployment/ClassDescriptor.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/main/org/jboss/seam/deployment/ClassDescriptor.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -51,7 +51,7 @@
/**
* Convert a path to a class file to a class name
*/
- protected static String filenameToClassname(String filename)
+ public static String filenameToClassname(String filename)
{
return filename.substring( 0, filename.lastIndexOf(".class") )
.replace('/', '.').replace('\\', '.');
Modified: trunk/src/main/org/jboss/seam/init/Initialization.java
===================================================================
--- trunk/src/main/org/jboss/seam/init/Initialization.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/main/org/jboss/seam/init/Initialization.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -5,10 +5,11 @@
*/
package org.jboss.seam.init;
+import static org.jboss.seam.util.Resources.getRealFile;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@@ -815,42 +816,6 @@
}
}
- private static File getRealFile(ServletContext servletContext, String path)
- {
- String realPath = servletContext.getRealPath(path);
- if (realPath==null) //WebLogic!
- {
- try
- {
- URL resourcePath = servletContext.getResource(path);
- if ((resourcePath != null) && (resourcePath.getProtocol().equals("file")))
- {
- realPath = resourcePath.getPath();
- }
- else
- {
- log.warn("Unable to determine real path from servlet context for \"" + path + "\" path does not exist.");
- }
- }
- catch (MalformedURLException e)
- {
- log.warn("Unable to determine real path from servlet context for : " + path);
- log.debug("Caused by MalformedURLException", e);
- }
-
- }
-
- if (realPath != null)
- {
- File file = new File(realPath);
- if (file.exists())
- {
- return file;
- }
- }
- return null;
- }
-
private static boolean isDebugEnabled()
{
return Resources.getResource("META-INF/debug.xhtml", null) != null;
Modified: trunk/src/main/org/jboss/seam/util/Resources.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/Resources.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/main/org/jboss/seam/util/Resources.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -1,7 +1,9 @@
package org.jboss.seam.util;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletContext;
@@ -131,5 +133,41 @@
//
}
}
+
+ public static File getRealFile(ServletContext servletContext, String path)
+ {
+ String realPath = servletContext.getRealPath(path);
+ if (realPath==null) //WebLogic!
+ {
+ try
+ {
+ URL resourcePath = servletContext.getResource(path);
+ if ((resourcePath != null) && (resourcePath.getProtocol().equals("file")))
+ {
+ realPath = resourcePath.getPath();
+ }
+ else
+ {
+ log.warn("Unable to determine real path from servlet context for \"" + path + "\" path does not exist.");
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ log.warn("Unable to determine real path from servlet context for : " + path);
+ log.debug("Caused by MalformedURLException", e);
+ }
+ }
+
+ if (realPath != null)
+ {
+ File file = new File(realPath);
+ if (file.exists())
+ {
+ return file;
+ }
+ }
+ return null;
+ }
+
}
Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -1,9 +1,5 @@
package org.jboss.seam.wicket.ioc;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtBehavior;
@@ -12,13 +8,10 @@
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
-import javassist.LoaderClassPath;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.CtField.Initializer;
-import javax.servlet.ServletContext;
-
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.wicket.WicketComponent;
@@ -29,109 +22,32 @@
private static LogProvider log = Logging.getLogProvider(JavassistInstrumentor.class);
- public static String DEFAULT_WICKET_COMPONENT_DIRECTORY_PATH = "WEB-INF/wicket";
+ private ClassPool classPool;
- private ClassLoader classLoader;
-
- private final List<String> classes = new ArrayList<String>();
- private File wicketComponentDirectory;
- private ClassPool classPool = new ClassPool();
-
- public JavassistInstrumentor(ServletContext servletContext)
+ public JavassistInstrumentor(ClassPool classPool)
{
- wicketComponentDirectory = getWicketComponentDirectory(servletContext);
+ this.classPool = classPool;
}
-
- public void instrument() throws NotFoundException, CannotCompileException, ClassNotFoundException
+
+ public CtClass instrumentClass(String className) throws NotFoundException, CannotCompileException
{
- if (wicketComponentDirectory == null)
- {
- log.warn("No wicket components found to give Seam super powers to");
- classLoader = Thread.currentThread().getContextClassLoader();
- return;
- }
- ClassLoader parent = Thread.currentThread().getContextClassLoader();
- classPool = new ClassPool();
- classLoader = new WicketClassLoader(parent, classPool, classes, wicketComponentDirectory);
- classPool.insertClassPath(wicketComponentDirectory.getAbsolutePath());
- classPool.insertClassPath(new LoaderClassPath(parent));
-
- if (wicketComponentDirectory.exists())
- {
- // Scan for classes
- handleDirectory(wicketComponentDirectory, null);
- }
-
- // Ensure classes are instantiated, and create metadata
- for (String className : classes)
- {
- Class clazz = classLoader.loadClass(className);
- new WicketComponent(clazz);
- }
- }
-
- private static File getWicketComponentDirectory(ServletContext servletContext)
- {
- String path = servletContext.getRealPath(DEFAULT_WICKET_COMPONENT_DIRECTORY_PATH);
- if (path==null) //WebLogic!
- {
- log.debug("Could not find path for " + DEFAULT_WICKET_COMPONENT_DIRECTORY_PATH);
- }
- else
- {
- File wicketComponentDir = new File(path);
- if (wicketComponentDir.exists())
- {
- return wicketComponentDir;
- }
- }
- return null;
- }
-
- private void handleDirectory(File file, String path) throws NotFoundException, CannotCompileException
- {
- log.debug("directory: " + file);
- for ( File child: file.listFiles() )
- {
- String newPath = path==null ? child.getName() : path + '/' + child.getName();
- if ( child.isDirectory() )
- {
- handleDirectory(child, newPath);
- }
- else
- {
- handleItem(newPath);
- }
- }
- }
-
- private void handleItem(String path) throws NotFoundException, CannotCompileException
- {
- if (path.endsWith(".class"))
- {
- String className = filenameToClassname(path);
- instrumentClass(className, classPool);
- }
- }
-
- protected static String filenameToClassname(String filename)
- {
- return filename.substring( 0, filename.lastIndexOf(".class") )
- .replace('/', '.').replace('\\', '.');
- }
-
- private void instrumentClass(String className, ClassPool classPool) throws NotFoundException, CannotCompileException
- {
log.debug("Instrumenting " + className);
CtClass implementation = classPool.get(className);
if (isInstrumentable(implementation))
{
CtClass handlerClass = classPool.get(WicketHandler.class.getName());
+ CtClass componentClass = classPool.get(WicketComponent.class.getName());
CtField handlerField = new CtField(handlerClass, "handler", implementation);
Initializer handlerInitializer = Initializer.byCall(handlerClass, "create");
implementation.addField(handlerField, handlerInitializer);
+ CtField wicketComponentField = new CtField(componentClass,"component",implementation);
+ wicketComponentField.setModifiers(Modifier.STATIC);
+ Initializer componentInit = Initializer.byExpr("new org.jboss.seam.wicket.WicketComponent(" + className + ".class)");
+ implementation.addField(wicketComponentField,componentInit);
+
+
CtClass exception = classPool.get(Exception.class.getName());
CtClass instrumentedComponent = classPool.get(InstrumentedComponent.class.getName());
@@ -171,14 +87,9 @@
}
}
}
- classes.add(implementation.getName());
-
+ return implementation;
}
- public ClassLoader getClassLoader()
- {
- return classLoader;
- }
private static String createBody(CtClass clazz, CtMethod method, CtMethod newMethod) throws NotFoundException
{
Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketClassLoader.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketClassLoader.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketClassLoader.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -1,40 +1,90 @@
package org.jboss.seam.wicket.ioc;
+import static org.jboss.seam.deployment.ClassDescriptor.filenameToClassname;
+
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.List;
+import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.Loader;
+import javassist.LoaderClassPath;
+import javassist.NotFoundException;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
public class WicketClassLoader extends Loader
{
+ private static LogProvider log = Logging.getLogProvider(WicketClassLoader.class);
+
private List<String> classes;
private File wicketComponentDirectory;
+ private ClassPool classPool;
+ private ClassLoader parentLoader;
- public WicketClassLoader(List<String> classes, File wicketComponentDirectory)
+ private JavassistInstrumentor instrumentor;
+
+
+ public WicketClassLoader(ClassLoader parent, ClassPool classPool, File wicketComponentDirectory)
{
- super();
- this.classes = classes;
+ super(parent, classPool);
+ this.classes = new ArrayList<String>();
this.wicketComponentDirectory = wicketComponentDirectory;
+ this.classPool = classPool;
+ this.parentLoader = parent;
+ this.instrumentor = new JavassistInstrumentor(classPool);
}
- public WicketClassLoader(ClassLoader parent, ClassPool cp, List<String> classes, File wicketComponentDirectory)
+ public WicketClassLoader instrument() throws NotFoundException, CannotCompileException, ClassNotFoundException
{
- super(parent, cp);
- this.classes = classes;
- this.wicketComponentDirectory = wicketComponentDirectory;
+ classPool.insertClassPath(wicketComponentDirectory.getAbsolutePath());
+ classPool.insertClassPath(new LoaderClassPath(parentLoader));
+
+ // Scan for classes
+ if (wicketComponentDirectory.exists())
+ {
+ handleDirectory(wicketComponentDirectory, null);
+ }
+
+ // Ensure classes' static initializers have run, to register the classes
+ // with WicketComponent
+ for (String className : classes)
+ {
+ loadClass(className);
+ }
+ return this;
}
- public WicketClassLoader(ClassPool cp, List<String> classes, File wicketComponentDirectory)
+ private void handleDirectory(File file, String path) throws NotFoundException, CannotCompileException
{
- super(cp);
- this.classes = classes;
- this.wicketComponentDirectory = wicketComponentDirectory;
+ log.trace("directory: " + file);
+ for (File child : file.listFiles())
+ {
+ String newPath = path == null ? child.getName() : path + '/' + child.getName();
+ if (child.isDirectory())
+ {
+ handleDirectory(child, newPath);
+ }
+ else
+ {
+ handleItem(newPath);
+ }
+ }
}
+ private void handleItem(String path) throws NotFoundException, CannotCompileException
+ {
+ if (path.endsWith(".class"))
+ {
+ classes.add(instrumentor.instrumentClass(filenameToClassname(path)).getName());
+ }
+ }
+
@Override
protected Class loadClassByDelegation(String name) throws ClassNotFoundException
{
@@ -76,5 +126,4 @@
}
}
}
-
}
Added: trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketInstrumentationTask.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketInstrumentationTask.java (rev 0)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketInstrumentationTask.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -0,0 +1,89 @@
+package org.jboss.seam.wicket.ioc;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.LoaderClassPath;
+
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.FileSet;
+import org.apache.tools.ant.types.Path;
+
+public class WicketInstrumentationTask extends Task
+{
+ Path buildPath;
+
+ public Path getBuildPath()
+ {
+ return buildPath;
+ }
+
+ public void setBuildPath(Path buildPath)
+ {
+ this.buildPath = buildPath;
+ }
+
+ public void addClasspath(Path path)
+ {
+ buildPath = path;
+ }
+
+ private File outputDirectory;
+
+ public File getOutputDirectory()
+ {
+ return outputDirectory;
+ }
+
+ public void setOutputDirectory(File outputDirectory)
+ {
+ this.outputDirectory = outputDirectory;
+ }
+
+ private FileSet fileset;
+
+ public void addFileset(FileSet fileset)
+ {
+ this.fileset = fileset;
+ }
+
+ @Override
+ public void execute()
+ {
+ try
+ {
+
+ ClassPool classPool = new ClassPool();
+ classPool.insertClassPath(new LoaderClassPath(getProject().createClassLoader(buildPath)));
+
+ List<CtClass> instrumentedClasses = new ArrayList<CtClass>();
+
+ JavassistInstrumentor instrumentor = new JavassistInstrumentor(classPool);
+
+ for (String file : fileset.getDirectoryScanner(getProject()).getIncludedFiles())
+ {
+ if (file.endsWith(".class"))
+ {
+ instrumentedClasses.add(instrumentor.instrumentClass(filenameToClassname(file)));
+ }
+ }
+
+ for (CtClass clazz : instrumentedClasses)
+ {
+ clazz.writeFile(outputDirectory.getPath());
+ }
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected static String filenameToClassname(String filename)
+ {
+ return filename.substring(0, filename.lastIndexOf(".class")).replace('/', '.').replace('\\', '.');
+ }
+}
Property changes on: trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketInstrumentationTask.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/src/wicket/org/jboss/seam/wicket/web/WicketFilterInstantiator.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/web/WicketFilterInstantiator.java 2008-11-03 18:42:24 UTC (rev 9482)
+++ trunk/src/wicket/org/jboss/seam/wicket/web/WicketFilterInstantiator.java 2008-11-03 23:10:46 UTC (rev 9483)
@@ -4,11 +4,14 @@
package org.jboss.seam.wicket.web;
import static org.jboss.seam.annotations.Install.BUILT_IN;
+import static org.jboss.seam.util.Resources.getRealFile;
+import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javassist.CannotCompileException;
+import javassist.ClassPool;
import javassist.NotFoundException;
import javax.servlet.Filter;
@@ -22,11 +25,11 @@
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
-import org.jboss.seam.util.Reflections;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
import org.jboss.seam.web.FilterConfigWrapper;
import org.jboss.seam.wicket.WebApplication;
-import org.jboss.seam.wicket.SeamWebApplication;
-import org.jboss.seam.wicket.ioc.JavassistInstrumentor;
+import org.jboss.seam.wicket.ioc.WicketClassLoader;
@Name("org.jboss.seam.wicket.web.wicketFilterInstantiator")
@Install(precedence = BUILT_IN, classDependencies={"org.apache.wicket.Application"})
@@ -35,6 +38,9 @@
public class WicketFilterInstantiator
{
+ public static String DEFAULT_WICKET_COMPONENT_DIRECTORY_PATH = "WEB-INF/wicket";
+ private static LogProvider log = Logging.getLogProvider(WicketFilterInstantiator.class);
+
@Unwrap
public Filter unrwap()
{
@@ -49,9 +55,18 @@
Map<String, String> parameters = new HashMap<String, String>();
try
{
- JavassistInstrumentor javassistInstrumentor = new JavassistInstrumentor(filterConfig.getServletContext());
- javassistInstrumentor.instrument();
- classLoader = javassistInstrumentor.getClassLoader();
+
+ ClassLoader parent = Thread.currentThread().getContextClassLoader();
+ File dir = getRealFile(filterConfig.getServletContext(), DEFAULT_WICKET_COMPONENT_DIRECTORY_PATH);
+ if (dir == null)
+ {
+ log.warn("No wicket components directory specified to give Seam super powers to");
+ this.classLoader = parent;
+ }
+ else
+ {
+ this.classLoader = new WicketClassLoader(Thread.currentThread().getContextClassLoader(), new ClassPool(), dir).instrument();
+ }
}
catch (NotFoundException e)
{
More information about the seam-commits
mailing list