Skip to content

Commit

Permalink
Merge pull request #7 from johnjcool/poc
Browse files Browse the repository at this point in the history
use Reflections instead of AssetLocator
  • Loading branch information
l0rdn1kk0n committed Jan 21, 2014
2 parents 439d02a + 4e9ca47 commit 9e1f45c
Show file tree
Hide file tree
Showing 17 changed files with 887 additions and 628 deletions.
73 changes: 39 additions & 34 deletions library/pom.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>de.agilecoders.wicket.webjars</groupId>
<artifactId>wicket-webjars-parent</artifactId>
<version>0.3.4-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<parent>
<groupId>de.agilecoders.wicket.webjars</groupId>
<artifactId>wicket-webjars-parent</artifactId>
<version>0.3.4-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>wicket-webjars</artifactId>
<packaging>jar</packaging>
<name>library</name>
<artifactId>wicket-webjars</artifactId>
<packaging>jar</packaging>
<name>library</name>

<dependencies>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
</dependency>

<!-- LOGGING DEPENDENCIES - LOG4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- THIRD PARTY DEPENDENCIES -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<groupId>org.jboss</groupId>
<artifactId>jboss-vfs</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${mvn.build.java.version}</source>
<target>${mvn.build.java.version}</target>
<compilerVersion>${mvn.build.java.version}</compilerVersion>
<encoding>${project.build.sourceEncoding}</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
<!-- TESTING DEPENDENCIES -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.agilecoders.wicket.webjars.collectors;

import java.net.URL;
import java.util.Collection;
import java.util.regex.Pattern;

/**
* TODO miha: document class purpose
*
* @author miha
*/
public interface AssetPathCollector {

boolean accept(URL url);

Collection<String> collect(URL url, Pattern filterExpr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package de.agilecoders.wicket.webjars.collectors;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class FileAssetPathCollector extends ProtocolAwareAssetPathCollector {
private static final int MAX_DIRECTORY_DEPTH = 5;

private final String pathPrefix;

public FileAssetPathCollector(final String pathPrefix) {
super("file");

this.pathPrefix = pathPrefix;
}

@Override
public Collection<String> collect(URL url, Pattern filterExpr) {
final File file;
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}

return listFiles(file, filterExpr);
}

/**
* Recursively search all directories for relative file paths matching `filterExpr`.
*
* @param file
* @param filterExpr
* @return
*/
private Set<String> listFiles(final File file, final Pattern filterExpr) {
final Set<String> aggregatedChildren = new HashSet<String>();
aggregateChildren(file, file, aggregatedChildren, filterExpr, 0);
return aggregatedChildren;
}

private void aggregateChildren(final File rootDirectory, final File file, final Set<String> aggregatedChildren, final Pattern filterExpr, final int level) {
if (file != null && file.isDirectory()) {
if (level > MAX_DIRECTORY_DEPTH) {
throw new IllegalStateException("Got deeper than " + MAX_DIRECTORY_DEPTH + " levels while searching " + rootDirectory);
}

File[] files = file.listFiles();

if (files != null) {
for (final File child : files) {
aggregateChildren(rootDirectory, child, aggregatedChildren, filterExpr, level + 1);
}
}
} else if (file != null) {
aggregateFile(file, aggregatedChildren, filterExpr);
}
}

private void aggregateFile(final File file, final Set<String> aggregatedChildren, final Pattern filterExpr) {
final String path = file.getPath().replace('\\', '/');
final String relativePath = path.substring(path.indexOf(pathPrefix));
if (filterExpr.matcher(relativePath).matches()) {
aggregatedChildren.add(relativePath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.agilecoders.wicket.webjars.collectors;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

public class JarAssetPathCollector extends ProtocolAwareAssetPathCollector {

public JarAssetPathCollector() {
super("jar");
}

protected JarAssetPathCollector(final String protocol) {
super(protocol);
}

protected JarAssetPathCollector(final String... protocols) {
super(protocols);
}

@Override
public Collection<String> collect(URL url, Pattern filterExpr) {
final JarFile jarFile = newJarFile(url);
final Set<String> assetPaths = new HashSet<String>();

final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String assetPathCandidate = entry.getName();
if (!entry.isDirectory() && filterExpr.matcher(assetPathCandidate).matches()) {
assetPaths.add(assetPathCandidate);
}
}

return assetPaths;
}

protected JarFile newJarFile(final URL url) {
try {
final String path = url.getPath();
final File file = new File(URI.create(path.substring(0, path.indexOf("!"))));

return new JarFile(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.agilecoders.wicket.webjars.collectors;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

public abstract class ProtocolAwareAssetPathCollector implements AssetPathCollector {

private final List<String> protocols;

protected ProtocolAwareAssetPathCollector(final String protocol) {
this.protocols = Arrays.asList(protocol);
}

protected ProtocolAwareAssetPathCollector(final String... protocols) {
this.protocols = Arrays.asList(protocols);
}

@Override
public boolean accept(URL url) {
if (url == null) {
return false;
}
for (String protocol : protocols) {
if (protocol.equalsIgnoreCase(url.getProtocol())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.agilecoders.wicket.webjars.collectors;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jboss.vfs.VirtualFile;
import org.reflections.vfs.Vfs;

import de.agilecoders.wicket.webjars.vfs.ExtendedUrlTypeVFS;

/**
* Adds support of vfs protocol to webjars.
*
* @author miha
*/
public class VfsJarAssetPathCollector extends JarAssetPathCollector {
private static final Pattern PATTERN = Pattern.compile("/([^/]*\\.jar)");

/**
* Construct.
*/
public VfsJarAssetPathCollector() {
super("vfs", "vfszip", "vfsfile");
addDefaultUrlTypes();
}

@Override
protected JarFile newJarFile(URL url) {
try {
URLConnection conn = url.openConnection();
VirtualFile vf = (VirtualFile) conn.getContent();
File contentsFile = vf.getPhysicalFile();
String c = contentsFile.getPath().replace('\\', '/');

final String jarName = toJarName(url);
final String pathToJar = c.substring(0, c.indexOf("/contents/"));

return new JarFile(new File(pathToJar, jarName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String toJarName(URL url) throws FileNotFoundException {
final String path = url.getPath();

Matcher m = PATTERN.matcher(path);
if (m.find()) {
return m.group(1);
}

throw new FileNotFoundException(url.getPath());
}

private static AtomicBoolean added = new AtomicBoolean(false);

private static void addDefaultUrlTypes() {
if (!added.getAndSet(true)) {
Vfs.addDefaultURLTypes(new ExtendedUrlTypeVFS());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package de.agilecoders.wicket.webjars.util;

import de.agilecoders.wicket.webjars.collectors.AssetPathCollector;
import de.agilecoders.wicket.webjars.util.file.WebjarsResourceFinder;

import org.apache.wicket.Application;
import org.apache.wicket.util.file.IResourceFinder;
import org.apache.wicket.util.lang.Args;
import org.webjars.AssetPathCollector;
import org.webjars.VfsAwareWebJarAssetLocator;
import org.webjars.WebJarAssetLocator;

import java.util.List;

Expand All @@ -23,7 +24,7 @@ public final class WicketWebjars {
* @param collectorArr the collectors to register
*/
public static void registerCollector(AssetPathCollector... collectorArr) {
VfsAwareWebJarAssetLocator.registerCollector(collectorArr);
WebJarAssetLocator.registerCollector(collectorArr);
}

/**
Expand Down
Loading

0 comments on commit 9e1f45c

Please sign in to comment.