Skip to content

Commit

Permalink
scripts classpath fix, scriptFileTest add
Browse files Browse the repository at this point in the history
  • Loading branch information
creepid committed May 8, 2016
1 parent a676398 commit 6272417
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ public ScriptingNetworkManager(String scriptFolder) {

@Override
public void init() {
File[] scripts = null;
String[] scriptNames = null;
try {
scripts = FileHelper.getFiles(scriptFolder, new ScriptFileFilter());
scriptNames = FileHelper.getFiles(scriptFolder, new ScriptFileFilter());
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
if (scripts != null && scripts.length != 0) {

for (int i = 0; i < scripts.length; i++) {
File script = scripts[i];
if (scriptNames != null && scriptNames.length != 0) {
for (int i = 0; i < scriptNames.length; i++) {
ScriptSource scriptSource = ScriptSourceFactory.createScriptResource(
scriptFolder + File.separator + script.getName());
scriptFolder + File.separator + scriptNames[i]);

ScriptableDevicePrototype prototype = new ScriptableDevicePrototype();
prototype.bind(scriptSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
*
Expand Down Expand Up @@ -90,30 +94,103 @@ public static String getExtension(String fileName) {
return extension;
}

public static File[] getFiles(String folderName, FilenameFilter filter) throws IOException {
String context = folderName.split(":")[0] + ":";
String folderWithoutContext = folderName.split(":")[1];
/**
* List directory contents for a resource folder. Not recursive.
* This is basically a brute-force implementation.
* Works for regular files and also JARs.
*
* @author Greg Briggs
* @param clazz Any java class that lives in the same place as the resources you want.
* @param path Should end with "/", but not start with one.
* @return Just the name of each member item, not the full paths.
* @throws URISyntaxException
* @throws IOException
*/
private static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException {
if (path == null){
throw new NullPointerException("Path must not be null!");
}
String correctedPath = (!path.endsWith(File.separator)) ? path + File.separator : path;
URL dirURL = clazz.getClassLoader().getResource(correctedPath);
if (dirURL != null && dirURL.getProtocol().equals("file")) {
/* A file path: easy enough */
return new File(dirURL.toURI()).list();
}

if (dirURL == null) {
/*
* In case of a jar file, we can't actually find a directory.
* Have to assume the same jar as clazz.
*/
String me = clazz.getName().replace(".", "/")+".class";
dirURL = clazz.getClassLoader().getResource(me);
}

if (dirURL.getProtocol().equals("jar")) {
/* A JAR path */
String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
while(entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith(path)) { //filter according to the path
String entry = name.substring(path.length());
int checkSubdir = entry.indexOf("/");
if (checkSubdir >= 0) {
// if it is a subdirectory, we just return the directory name
entry = entry.substring(0, checkSubdir);
}
result.add(entry);
}
}
return result.toArray(new String[result.size()]);
}

throw new UnsupportedOperationException("Cannot list files for URL "+dirURL);
}

/**
* Getting the names of files in folderPath
*
* @param folderPath - folder in classpath, absolute or relative path
* @param filter - filter to select files
* @return the name of each member item, not the full paths.
* @throws IOException
*/
public static String[] getFiles(String folderPath, FilenameFilter filter) throws IOException {
String context = folderPath.split(":")[0] + ":";
String folderWithoutContext = folderPath.split(":")[1];

File folder = null;
//if folder path is absolute or relative
if (context.equalsIgnoreCase("file:")) {
folder = new File(folderWithoutContext);
if (!folder.exists()) {
throw new IOException("Folder [" + folder.getAbsolutePath() + "] doesnt exist");
throw new IOException("Folder [" + folder.getAbsolutePath() + "] doesn't exist");
}
File[] filteredFiles = folder.listFiles(filter);
String[] filteredFileNames = new String[filteredFiles.length];
for (int i = 0; i < filteredFiles.length; i++) {
filteredFileNames[i] = filteredFiles[i].getName();
}
return filteredFileNames;
//folder is in classpath
} else if (context.equalsIgnoreCase("classpath:")) {
URL url = FileHelper.class.getResource(folderWithoutContext);
try {
folder = new File(url.toURI());
String[] fileNames = getResourceListing(FileHelper.class, folderWithoutContext);
List<String> filteredFileNames = new ArrayList<>(fileNames.length);
for (int i = 0; i < fileNames.length; i++) {
if (filter.accept(null, fileNames[i])){
filteredFileNames.add(fileNames[i]);
}
}
return filteredFileNames.toArray(new String[filteredFileNames.size()]);
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}

if (folder != null) {
File[] scripts = folder.listFiles(filter);
return scripts;
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.easysmarthouse.scripting;

import junit.framework.TestCase;
import org.junit.Test;

import static org.junit.Assert.*;

public class ScriptFileFilterTest {

@Test
public void testAccept() throws Exception {
System.out.println("***** accept *****");

ScriptFileFilter filter = new ScriptFileFilter();
assertTrue(filter.accept(null, "script.js"));
assertFalse(filter.accept(null, "abc.txt"));
}
}
2 changes: 1 addition & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<filtering>false</filtering>
<directory>src/main/resources</directory>
<includes>
<include>*.*</include>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>*.properties</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ public void setScriptFolder(String scriptFolder) {
this.scriptFolder = scriptFolder;
}

private String findAddress(File scriptFile) {
String content = FileHelper.readFile(scriptFile.getPath());
return DeviceScriptUtil.getDeviceAddress(content);
private String findAddress(String scriptContent) {
return DeviceScriptUtil.getDeviceAddress(scriptContent);
}

@Override
Expand All @@ -88,17 +87,16 @@ public void afterPropertiesSet() throws Exception {
}

try {
File[] scriptFiles = FileHelper.getFiles(scriptFolder, new ScriptFileFilter());
if (scriptFiles == null || scriptFiles.length == 0) {
String[] scriptNames = FileHelper.getFiles(scriptFolder, new ScriptFileFilter());
if (scriptNames == null || scriptNames.length == 0) {
return;
}

for (int i = 0; i < scriptFiles.length; i++) {
File scriptFile = scriptFiles[i];
String deviceAddress = findAddress(scriptFile);
for (int i = 0; i < scriptNames.length; i++) {
ScriptSource scriptSource = ScriptSourceFactory.createScriptResource(
scriptFolder + File.separator + scriptNames[i]);
String deviceAddress = findAddress(scriptSource.getScript());
if (deviceAddress != null) {
ScriptSource scriptSource = ScriptSourceFactory.createScriptResource(
scriptFolder + File.separator + scriptFile.getName());
scriptSources.put(deviceAddress, scriptSource);
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/src/main/resources/scripting-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</bean>

<bean id="scriptDecoratorAdvice" class="net.easysmarthouse.service.scripting.ScriptDecoratorAdvice" >
<property name="scriptFolder" value="file:src\main\resources\scripts"/>
<property name="scriptFolder" value="classpath:scripts"/>
</bean>

</beans>

0 comments on commit 6272417

Please sign in to comment.