diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-30 22:32:28 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-30 22:32:28 +1200 |
commit | 09ab10ae55444bbd5e09a82fbb41d2f4903a9029 (patch) | |
tree | d19fe32f98ac758b435a49b277d67bbb4a5fd8e7 /src | |
parent | 23de67f6f8fe2406729c8a97fee662c940165288 (diff) | |
download | traccar-server-09ab10ae55444bbd5e09a82fbb41d2f4903a9029.tar.gz traccar-server-09ab10ae55444bbd5e09a82fbb41d2f4903a9029.tar.bz2 traccar-server-09ab10ae55444bbd5e09a82fbb41d2f4903a9029.zip |
Fix dynamic class loading (fix #1283)
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/ServerManager.java | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index d0ff203fc..de167b9d2 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -15,11 +15,16 @@ */ package org.traccar; -import java.io.BufferedReader; -import java.io.InputStreamReader; +import java.io.File; +import java.net.URI; +import java.net.URL; +import java.net.URLDecoder; import java.sql.SQLException; +import java.util.Enumeration; import java.util.LinkedList; import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import org.jboss.netty.bootstrap.ConnectionlessBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; @@ -30,19 +35,36 @@ public class ServerManager { public void init() throws Exception { - ClassLoader cl = ServerManager.class.getClassLoader(); - String pack = "org/traccar/protocol"; - String dottedPackage = pack.replaceAll("[/]", "."); + List<String> names = new LinkedList<String>(); + String packageName = "org.traccar.protocol"; + String packagePath = packageName.replace('.', '/'); + URL packageUrl = Thread.currentThread().getContextClassLoader().getResource(packagePath); - BufferedReader reader = new BufferedReader(new InputStreamReader(cl.getResourceAsStream(pack))); - String line = null; - while ((line = reader.readLine()) != null) { - if (line.endsWith(".class")) { - Class protocolClass = Class.forName(dottedPackage + "." + line.substring(0, line.lastIndexOf('.'))); - if (BaseProtocol.class.isAssignableFrom(protocolClass)) { - initProtocolServer((BaseProtocol) protocolClass.newInstance()); + if (packageUrl.getProtocol().equals("jar")) { + String jarFileName = URLDecoder.decode(packageUrl.getFile(), "UTF-8"); + JarFile jf = new JarFile(jarFileName.substring(5, jarFileName.indexOf("!"))); + + Enumeration<JarEntry> jarEntries = jf.entries(); + while(jarEntries.hasMoreElements()){ + String entryName = jarEntries.nextElement().getName(); + if (entryName.startsWith(packagePath) && entryName.length() > packagePath.length() + 5) { + names.add(entryName.substring(packagePath.length() + 1, entryName.lastIndexOf('.'))); } } + } else { + File folder = new File(new URI(packageUrl.toString())); + File[] files = folder.listFiles(); + for (File actual: files) { + String entryName = actual.getName(); + names.add(entryName.substring(0, entryName.lastIndexOf('.'))); + } + } + + for (String name : names) { + Class protocolClass = Class.forName(packageName + '.' + name); + if (BaseProtocol.class.isAssignableFrom(protocolClass)) { + initProtocolServer((BaseProtocol) protocolClass.newInstance()); + } } initProtocolDetector(); |