-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from johnjcool/poc
use Reflections instead of AssetLocator
- Loading branch information
Showing
17 changed files
with
887 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
library/src/main/java/de/agilecoders/wicket/webjars/collectors/AssetPathCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
72 changes: 72 additions & 0 deletions
72
library/src/main/java/de/agilecoders/wicket/webjars/collectors/FileAssetPathCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
library/src/main/java/de/agilecoders/wicket/webjars/collectors/JarAssetPathCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...c/main/java/de/agilecoders/wicket/webjars/collectors/ProtocolAwareAssetPathCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
library/src/main/java/de/agilecoders/wicket/webjars/collectors/VfsJarAssetPathCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.