Skip to content

Commit

Permalink
updated handling of module properties files
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Apr 13, 2017
1 parent df8a21e commit 4a649e4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ gradle-app.setting
mimetypes.properties
routemaster.properties

modules
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ repositories {
dependencies {
runtime(group: 'synapticloop', name: 'routemaster', version: '2.2.0', ext: 'jar')
runtime(group: 'synapticloop', name: 'routemaster', version: '2.2.1', ext: 'jar')
compile(group: 'synapticloop', name: 'routemaster', version: '2.2.0', ext: 'jar')
compile(group: 'synapticloop', name: 'routemaster', version: '2.2.1', ext: 'jar')
}
Expand All @@ -254,9 +254,9 @@ or, more simply for versions of gradle greater than 2.1
dependencies {
runtime 'synapticloop:routemaster:2.2.0'
runtime 'synapticloop:routemaster:2.2.1'
compile 'synapticloop:routemaster:2.2.0'
compile 'synapticloop:routemaster:2.2.1'
}
Expand All @@ -270,7 +270,7 @@ dependencies {
<dependency>
<groupId>synapticloop</groupId>
<artifactId>routemaster</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
<type>jar</type>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
id "synapticloop.documentr" version "2.9.0"
}

version = '2.2.0'
version = '2.2.1'

group = 'synapticloop'
archivesBaseName = 'routemaster'
Expand Down
105 changes: 85 additions & 20 deletions src/main/java/synapticloop/nanohttpd/router/RouteMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,13 +33,16 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;

import synapticloop.nanohttpd.handler.Handler;
Expand Down Expand Up @@ -117,6 +121,11 @@ public static void initialise(File rootDir) {
}
}

/**
* Dynamically load any modules that exist within the 'modules' directory
*
* @param properties the properties from the default routemaster.properties
*/
private static void loadModules(Properties properties) {
// look in the modules directory
File modulesDirectory = new File(rootDir.getAbsolutePath() + "/modules/");
Expand All @@ -141,39 +150,76 @@ public boolean accept(File dir, String name) {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
} catch (Exception ex) {
logFatal("Could not load any modules, exception message was: " + ex.getMessage());
return;
}

method.setAccessible(true);
for (String module : moduleList) {
logInfo("Found potential module in file '" + module + "'.");

File file = new File(rootDir + "/modules/" + module);
try {
JarFile jarFile = new JarFile(file);
ZipEntry zipEntry = jarFile.getEntry("routemaster.properties");
if(null != zipEntry) {
URL url = file.toURI().toURL();
method.invoke(classLoader, url);
// assuming that the above works - read the properties from the
// jar file
readProperties(properties, jarFile, zipEntry);
if(!modules.contains(module)) {
modules.add(module);

// try and find the <module>-<version>.jar.properties file which will
// over-ride the routemaster.properties entry in the jar file

boolean loadedOverrideProperties = false;

String moduleName = getModuleName(module);
File overridePropertiesFile = new File(rootDir + "/modules/" + moduleName + ".properties");
if(overridePropertiesFile.exists() && overridePropertiesFile.canRead()) {
logInfo("[ " + module + " ] Found an over-ride .properties file '" + moduleName + ".properties'.");
try {
Properties mergeProperties = new Properties();
mergeProperties.load(new FileReader(overridePropertiesFile));
Iterator<Object> iterator = mergeProperties.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
if(properties.containsKey(key)) {
logWarn("[ " + module + " ] Routemaster already has a property with key '" + key + "', over-writing...");
}

String value = mergeProperties.getProperty(key);
properties.setProperty(key, value);

logInfo("[ " + module + " ] Adding property key '" + key + "', value '" + value + "'");
}
} else {
logWarn("Could not find '/routemaster.properties' in file '" + module + "'.");
loadedOverrideProperties = true;
} catch (IOException ex) {
logFatal("Could not load modules message was: " + ex.getMessage());
}
}

jarFile.close();
} catch (Exception ex) {
logFatal("Could not load modules message was: " + ex.getMessage());
try {
JarFile jarFile = new JarFile(file);
ZipEntry zipEntry = jarFile.getEntry(moduleName + ".properties");
if(null != zipEntry) {
URL url = file.toURI().toURL();
method.invoke(classLoader, url);

if(!loadedOverrideProperties) {
// assuming that the above works - read the properties from the
// jar file
readProperties(module, properties, jarFile, zipEntry);
}

if(!modules.contains(module)) {
modules.add(module);
}

} else {
logWarn("[ " + module + " ] Could not find '/" + moduleName + ".properties' in file '" + module + "'.");
}

jarFile.close();
} catch (Exception ex) {
logFatal("Could not load modules message was: " + ex.getMessage());
}
}
}
}
}
}

private static void readProperties(Properties properties, JarFile jarFile, ZipEntry zipEntry) throws IOException {
private static void readProperties(String module, Properties properties, JarFile jarFile, ZipEntry zipEntry) throws IOException {
InputStream input = jarFile.getInputStream(zipEntry);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(isr);
Expand All @@ -191,16 +237,28 @@ private static void readProperties(Properties properties, JarFile jarFile, ZipEn
String key = split[0].trim();
String value = split[1].trim();
if(properties.containsKey(key)) {
logWarn("Routemaster already has a property with key '" + key + "', over-writing...");
logWarn("[ " + module + " ] Routemaster already has a property with key '" + key + "', over-writing...");
}

properties.setProperty(key, value);
logInfo("Adding property key '" + key + "', value '" + value + "'");
logInfo("[ " + module + " ] Adding property key '" + key + "', value '" + value + "'");
}
}
}
reader.close();
}

private static String getModuleName(String module) {
Pattern r = Pattern.compile("(.*)-\\d+\\.\\d+.*\\.jar");
Matcher m = r.matcher(module);
if(m.matches()) {
return(m.group(1));
}

int lastIndexOf = module.lastIndexOf(".");
return(module.substring(0, lastIndexOf));
}

private static void parseOptionsAndRoutes(Properties properties) {
// now parse the properties
parseOptions(properties);
Expand Down Expand Up @@ -469,4 +527,11 @@ private static Response getErrorResponse(File rootDir, IHTTPSession httpSession,
* @return the handler cache
*/
public static Map<String, Handler> getHandlerCache() { return (handlerCache); }

/**
* Get the set of modules that have been registered with the routemaster
*
* @return the set of modules that have been registered
*/
public static Set<String> getModules() { return(modules); }
}

0 comments on commit 4a649e4

Please sign in to comment.