Author: abelevich
Date: 2009-01-19 05:39:05 -0500 (Mon, 19 Jan 2009)
New Revision: 12327
Modified:
trunk/sandbox/cdk/maven-resource-dependency-plugin/pom.xml
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/JarResourceScanner.java
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/handler/ComponentsHandler.java
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/mojo/CreateScriptMojo.java
Log:
Modified: trunk/sandbox/cdk/maven-resource-dependency-plugin/pom.xml
===================================================================
--- trunk/sandbox/cdk/maven-resource-dependency-plugin/pom.xml 2009-01-19 10:23:28 UTC
(rev 12326)
+++ trunk/sandbox/cdk/maven-resource-dependency-plugin/pom.xml 2009-01-19 10:39:05 UTC
(rev 12327)
@@ -24,8 +24,8 @@
<version>2.0</version>
</dependency>
<dependency>
- <groupId>org.apache.maven</groupId>
- <artifactId>maven-project</artifactId>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
@@ -54,7 +54,7 @@
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
- <version>1.0.4</version>
+ <version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
@@ -92,6 +92,17 @@
<version>2.0.8</version>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>commons-digester</groupId>
+ <artifactId>commons-digester</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.8.0</version>
+ </dependency>
+
</dependencies>
</project>
Modified:
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/JarResourceScanner.java
===================================================================
---
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/JarResourceScanner.java 2009-01-19
10:23:28 UTC (rev 12326)
+++
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/JarResourceScanner.java 2009-01-19
10:39:05 UTC (rev 12327)
@@ -16,7 +16,7 @@
private FileObject baseFile;
- private Set result = new HashSet();
+ private Set <FileObject> result = new HashSet <FileObject>();
private String [] patterns;
@@ -72,11 +72,11 @@
}
}
- public Set getResult() {
+ public Set <FileObject> getResult() {
return result;
}
- public void setResult(Set result) {
+ public void setResult(Set <FileObject> result) {
this.result = result;
}
}
Modified:
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/handler/ComponentsHandler.java
===================================================================
---
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/handler/ComponentsHandler.java 2009-01-19
10:23:28 UTC (rev 12326)
+++
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/handler/ComponentsHandler.java 2009-01-19
10:39:05 UTC (rev 12327)
@@ -1,6 +1,6 @@
package org.richfaces.cdk.rd.handler;
-import java.util.Collection;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -9,7 +9,6 @@
import org.richfaces.cdk.rd.Component;
import org.richfaces.cdk.rd.Components;
import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
@@ -18,48 +17,16 @@
*/
public class ComponentsHandler extends DefaultHandler{
-
-// private Set <PrefixMapping> prefixList = new HashSet<PrefixMapping>();
-//
-// private Set <String> userNamespaces = new HashSet<String>();
-//
-// public static String RICH_DEFAULT = "http://richfaces.org/rich";
-//
-// public static String A4J_DEFAULT = "http://richfaces.org/a4j";
-
private Log log;
private List <Components> components;
private String tempString;
- private Set <String> scripts = new HashSet<String>();
+ private List <String> scripts = new ArrayList<String>();
- private Set <String> styles = new HashSet<String>();
+ private List <String> styles = new ArrayList<String>();
-// @Override
-// public void startElement(String uri, String localName, String name, Attributes
attributes) throws SAXException {
-// for (PrefixMapping prefix: prefixList) {
-//
-// String prefixUri = prefix.getUri();
-// String qname = prefix.getPrefix();
-//
-// if(uri.equals(prefixUri) && name.startsWith(qname)) {
-// getResult().add(localName);
-// }
-// }
-// }
-
-// @Override
-// public void startPrefixMapping(String prefix, String uri) throws SAXException {
-// getLog().info("Process mapping: prefix: " + prefix + " uri: " +
uri );
-//
-// if (userNamespaces != null && userNamespaces.contains(uri)) {
-// PrefixMapping prefixMapping = new PrefixMapping(prefix, uri);
-// addPrefixMapping(prefixMapping);
-// }
-
-// }
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
tempString = new String(ch, start, length);
@@ -67,32 +34,48 @@
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
- for (Components library: components) {
- if(library.getNamespace().equals(uri)) {
- List <Component> components = library.getComponents();
- for(Component component : components ){
- if(localName.equals(component.getComponentName())) {
- collectScripts(component.getScripts());
- collectStyles(component.getScripts());
+ if(components != null) {
+ for (Components library: components) {
+ if(library.getNamespace().equals(uri)) {
+ List <Component> components = library.getComponents();
+ for(Component component : components ){
+ if(localName.equals(component.getComponentName())) {
+ collectScripts(component);
+ collectStyles(component);
+ }
}
}
}
}
}
- private void collectStyles(Collection <String> styles) {
- this.styles.addAll(styles);
+ private void collectStyles(Component component) {
+ for(String style : component.getStyles()) {
+ if(!this.styles.contains(style)) {
+ if (getLog() != null) {
+ getLog().info("collect styles from: " + component.getComponentName() +
" component");
+ }
+ this.styles.add(style);
+ }
+ }
}
- private void collectScripts(Collection <String> scripts) {
- this.scripts.addAll(scripts);
+ private void collectScripts(Component component) {
+ for(String script : component.getScripts()) {
+ if(!this.scripts.contains(script)) {
+ if (getLog() != null) {
+ getLog().info("collect script from: " + component.getComponentName() +
" component");
+ }
+ this.scripts.add(script);
+ }
+ }
}
- public Set <String> getStyles() {
+ public List <String> getStyles() {
return this.styles;
}
- public Set <String> getScripts() {
+ public List <String> getScripts() {
return this.scripts;
}
@@ -103,10 +86,6 @@
public void setComponents(List<Components> components) {
this.components = components;
}
-
- @Override
- public void fatalError(SAXParseException e) throws SAXException {
- }
public Log getLog() {
return log;
@@ -115,22 +94,5 @@
public void setLog(Log log) {
this.log = log;
}
-
-
-// public void addNamespaces(Collection <String>namespaces) {
-// this.userNamespaces.addAll(namespaces);
-// }
-//
-// public Collection <String> getNamespaces() {
-// if(userNamespaces.isEmpty() || !(userNamespaces.contains(RICH_DEFAULT) &&
userNamespaces.contains(A4J_DEFAULT))) {
-// userNamespaces.add(RICH_DEFAULT);
-// userNamespaces.add(A4J_DEFAULT);
-// }
-// return this.userNamespaces;
-// }
-//
-// public void addPrefixMapping(PrefixMapping prefixMapping) {
-// prefixList.add(prefixMapping);
-// }
-//
+
}
Modified:
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/mojo/CreateScriptMojo.java
===================================================================
---
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/mojo/CreateScriptMojo.java 2009-01-19
10:23:28 UTC (rev 12326)
+++
trunk/sandbox/cdk/maven-resource-dependency-plugin/src/main/java/org/richfaces/cdk/rd/mojo/CreateScriptMojo.java 2009-01-19
10:39:05 UTC (rev 12327)
@@ -22,38 +22,44 @@
package org.richfaces.cdk.rd.mojo;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
-import javax.xml.parsers.FactoryConfigurationError;
-import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.digester.Digester;
import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.model.Dependency;
-import org.apache.maven.model.Model;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.project.artifact.MavenMetadataSource;
+import org.codehaus.classworlds.ClassRealm;
+import org.codehaus.classworlds.ClassWorld;
import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.IOUtil;
import org.richfaces.cdk.rd.Component;
import org.richfaces.cdk.rd.Components;
import org.richfaces.cdk.rd.JarResourceScanner;
import org.richfaces.cdk.rd.handler.ComponentsHandler;
-import org.xml.sax.SAXException;
/**
* @author Anton Belevich
@@ -101,13 +107,13 @@
/**
* outputScriptDirectory
- * @parameter
+ * @parameter expression = "${basedir}/src/main/webapp"
*/
- private File outputScriptDirectory;
+ private File outputScriptDirectory ;
/**
* scriptPackName
- * @parameter;
+ * @parameter expression="custom-dependencies";
*/
private String scriptPackName;
@@ -130,27 +136,47 @@
*/
private String loadOrder;
+ /**
+ *
+ * @component
+ */
+ private ArtifactMetadataSource metadataSource;
+
+
public void execute() throws MojoExecutionException {
try {
- Artifact richfaces = getUIArifact(project);
- FileObject jarRichfaces = resolveArtifact(richfaces);
- FileObject [] configs = resolveConfigsFromJar(jarRichfaces);
- List components = processConfigs(configs, new ArrayList());
- Set <String> result = findComponents(webSourceDirectory, components);
-// Set result = selectScriptResources(richConfigs,components);
+ Artifact richfacesUI = createDependencyArtifact("richfaces-ui",
project.getVersion(), "jar");
+ Artifact richfacesImpl = createDependencyArtifact("richfaces-impl",
project.getVersion(), "jar");
+ List <FileObject> richfaces = new ArrayList<FileObject>();
+
+ FileObject jarRichfacesUI = resolveArtifact(richfacesUI);
+ richfaces.add(jarRichfacesUI);
+ FileObject jarRichfacesImpl = resolveArtifact(richfacesImpl);
+ richfaces.add(jarRichfacesImpl);
+
+ FileObject [] configs = resolveConfigsFromJar(jarRichfacesUI);
+ List <Components> components = processConfigs(configs, new ArrayList
<Components>());
+ ComponentsHandler handler = findComponents(webSourceDirectory, components);
+
+ List result = handler.getScripts();
+ mergeScripts(result, richfaces, outputScriptDirectory);
+
for (Iterator iterator = result.iterator(); iterator.hasNext();) {
getLog().info("collect: " + iterator.next());
}
+
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(),e);
}
}
- public List processConfigs(FileObject [] configs, List collector) {
+ public List <Components> processConfigs(FileObject [] configs, List collector) {
+
for (FileObject config: configs) {
+
Digester digester = new Digester();
digester.addObjectCreate("components", Components.class);
digester.addCallMethod("components/namespace", "setNamespace",
0);
@@ -159,19 +185,27 @@
digester.addCallMethod("components/component/scripts/script",
"addScript",0);
digester.addCallMethod("components/component/styles/styles",
"addStyle",0);
digester.addSetNext("components/component", "addComponent");
-
+ InputStream configInputStream = null;
+
try {
- collector.add(digester.parse(config.getContent().getInputStream()));
- } catch (SAXException e) {
+ configInputStream = config.getContent().getInputStream();
+ collector.add(digester.parse(configInputStream));
+
+ } catch (Exception e) {
getLog().error(e);
- } catch (IOException e) {
- getLog().error(e);
+ } finally {
+ try {
+ configInputStream.close();
+ } catch (IOException e) {
+ getLog().error(e);
+ }
}
+
}
return collector;
}
- public Set findComponents (File webSourceDir, List components) {
+ public ComponentsHandler findComponents (File webSourceDir, List <Components>
components) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(webSourceDir);
@@ -196,40 +230,32 @@
getLog().info("process file: " + file.getName());
try {
saxParser.parse(file, handler);
- } catch (SAXException e) {
- getLog().error(e);
- } catch (IOException e) {
- getLog().error(e);
- }
+ } catch (Exception e) {
+ getLog().error("Error process file: " + file.getAbsolutePath(), e);
+ }
}
}
- } catch (FactoryConfigurationError e) {
+ } catch (Exception e) {
getLog().error(e);
- } catch (ParserConfigurationException e) {
- getLog().error(e);
- } catch (SAXException e){
- getLog().error(e);
- }
+ }
- return handler.getScripts();
+ return handler;
}
- protected Artifact getUIArifact (MavenProject project) {
+ protected Artifact createDependencyArtifact(String id, String version, String type)
throws Exception{
- Model model = project.getModel();
- List <Dependency> dependencyList = model.getDependencies();
-
+ List <Dependency> dependencies = project.getDependencies();
Artifact artifact = null;
-
- for(Dependency dependency : dependencyList) {
- if(dependency.getArtifactId().equals("richfaces-ui") &&
dependency.getVersion().equals(model.getVersion()) &&
dependency.getType().equals("jar")) {
+
+ for(Dependency dependency : dependencies) {
+ if(dependency.getArtifactId().equals(id) &&
dependency.getVersion().equals(version) && dependency.getType().equals(type)) {
String groupId = dependency.getGroupId();
String artifactId = dependency.getArtifactId();
- String version = dependency.getVersion();
+ String artifactVersion = dependency.getVersion();
String scope = dependency.getScope();
- String type = dependency.getType();
- artifact = factory.createArtifact(groupId, artifactId, version, scope, type);
+ String artifactType = dependency.getType();
+ artifact = factory.createArtifact(groupId, artifactId, artifactVersion, scope,
artifactType);
break;
}
}
@@ -237,6 +263,19 @@
return artifact;
}
+ protected Set <Artifact> createProjectDependenciesArtifacts() {
+ ArtifactResolutionResult result = null;
+ try {
+ List <Dependency> dependencies = project.getDependencies();
+ Set <Artifact> artifacts = MavenMetadataSource.createArtifacts(factory,
dependencies, null, null, project);
+ result = resolver.resolveTransitively(artifacts, project.getArtifact(),
Collections.EMPTY_LIST, localRepository, metadataSource);
+ } catch (Exception e) {
+ getLog().error(e);
+ }
+ return result.getArtifacts();
+ }
+
+
protected FileObject resolveArtifact(Artifact artifact) {
FileObject jarFileObject = null;
@@ -254,9 +293,10 @@
try {
FileSystemManager manager = VFS.getManager();
jarFileObject = manager.resolveFile("jar://" + file.getAbsolutePath());
- } catch (IOException e) {
- getLog().error(e);
- }
+ } catch (FileSystemException e) {
+ getLog().error("Error during processing file: " + file.getAbsolutePath(),
e);
+ }
+
}
return jarFileObject;
}
@@ -277,6 +317,83 @@
return result;
}
- public void mergeScripts(Set <String> components) throws Exception{
+ public void mergeFiles(File dest, List <FileObject> jarFileObjects, String
scriptPath) throws Exception{
+
+ for(FileObject sourceFileObjects : jarFileObjects){
+
+ FileOutputStream out = new FileOutputStream(dest, true);
+ try {
+ FileObject scriptFileObject = sourceFileObjects.resolveFile(scriptPath);
+ if(scriptFileObject.exists()) {
+ InputStream in = scriptFileObject.getContent().getInputStream();
+ try {
+ IOUtil.copy(in, out);
+ if (true) {
+ out.write('\n');
+ }
+ } finally {
+ IOUtil.close(in);
+ in = null;
+ }
+ }
+ } finally {
+ IOUtil.close(out);
+ out = null;
+ }
+ }
}
+
+ protected void mergeScripts(List <String> scripts, List <FileObject>
jarFileObjects, File outputScriptDirectory) {
+
+ File assemblyScriptFile = new File(outputScriptDirectory, scriptPackName +
".js");
+ if(!assemblyScriptFile.exists()) {
+ try {
+ assemblyScriptFile.createNewFile();
+ } catch (IOException e) {
+ getLog().error("Error create file: " +
assemblyScriptFile.getAbsolutePath(), e);
+ }
+ }
+
+ for(String script: scripts) {
+ try {
+ if(script.endsWith(".js")) {
+ mergeFiles(assemblyScriptFile, jarFileObjects, script);
+ } else {
+
+ Set <Artifact> artifacts = createProjectDependenciesArtifacts();
+
+ //save current ContextClassloader
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+
+ //create a new classloading space
+ ClassWorld world = new ClassWorld();
+
+ //use the existing ContextClassLoader in a realm of the classloading space
+ ClassRealm realm = world.newRealm("org.richfaces.cdk",
Thread.currentThread().getContextClassLoader());
+
+ //create another realm for the app jars
+ ClassRealm jars = realm.createChildRealm("jar");
+ for(Artifact jar : artifacts ) {
+ jars.addConstituent(jar.getFile().toURL());
+ }
+
+ //make the child realm the ContextClassLoader
+ Thread.currentThread().setContextClassLoader(jars.getClassLoader());
+
+ //resolve framework script path
+ Class clazz = Thread.currentThread().getContextClassLoader().loadClass(script);
+ Object obj = clazz.newInstance();
+ Method method = clazz.getMethod("getPath", new Class [0]);
+ String path = (String) method.invoke(obj, new Object[0]);
+
+ // restore old ContextClassLoader
+ Thread.currentThread().setContextClassLoader(oldClassLoader);
+
+ mergeFiles(assemblyScriptFile, jarFileObjects, path);
+ }
+ } catch (Exception e) {
+ getLog().error(e);
+ }
+ }
+ }
}