Revision: 19980 http://sourceforge.net/p/gate/code/19980 Author: markagreenwood Date: 2017-01-25 06:39:54 +0000 (Wed, 25 Jan 2017) Log Message: ----------- tieded up the url handler code
Modified Paths: -------------- gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java gate/branches/sawdust2/gate-core/src/main/java/gate/creole/CreoleAnnotationHandler.java gate/branches/sawdust2/gate-core/src/main/java/sun/net/www/protocol/creole/Handler.java Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java 2017-01-25 02:23:54 UTC (rev 19979) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java 2017-01-25 06:39:54 UTC (rev 19980) @@ -52,7 +52,6 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; -import org.apache.maven.model.License; import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuilderFactory; import org.apache.maven.model.building.DefaultModelBuildingRequest; @@ -92,78 +91,91 @@ protected static final Logger log = Logger.getLogger(Plugin.class); - static { + private static class URLHandlerFactory implements URLStreamHandlerFactory { - if(!Plugin.class.getClassLoader() - .equals(ClassLoader.getSystemClassLoader())) { - // we only need to register the factory if the handler classes are not - // already on the system classloader + private URLStreamHandlerFactory delegate; + + public URLHandlerFactory() { + delegate = null; + } + + public URLHandlerFactory(URLStreamHandlerFactory current) { + this.delegate = current; + } + + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + if(delegate != null) { + // if there was already a factory then delegate to it first to see + // if it can handle the protocol + URLStreamHandler handler = + delegate.createURLStreamHandler(protocol); - log.debug("registering URLStreamHandlerFactory"); + // if so our work here is done + if(handler != null) return handler; + } - Field factoryField = null; + // build up the normal classname from the protocol + String className = "sun.net.www.protocol." + protocol + ".Handler"; try { - // get the factory field from java.net.URL via reflection - factoryField = URL.class.getDeclaredField("factory"); + // try to load the class + Class<?> handler = Class.forName(className); - // it's usually package protected but we need access to it - factoryField.setAccessible(true); + // create and return an instance of the protocol handler + return (URLStreamHandler)handler.newInstance(); - // get the current factory out of the protected field. This will be null - // if a factory hasn't already been set - URLStreamHandlerFactory currentFactory = - (URLStreamHandlerFactory)factoryField.get(null); + } catch(ClassNotFoundException | InstantiationException + | IllegalAccessException e) { + // skip over this to the null return; + } - // now we define out custom factory - URLStreamHandlerFactory newFactory = new URLStreamHandlerFactory() { + // either there is no handler for the protocol or something went + // wrong, either way just return null + return null; + } + + } + + static { - @Override - public URLStreamHandler createURLStreamHandler(String protocol) { - if(currentFactory != null) { - // if there was already a factory then delegate to it first to see - // if it can handle the protocol - URLStreamHandler handler = - currentFactory.createURLStreamHandler(protocol); + if(!Plugin.class.getClassLoader() + .equals(ClassLoader.getSystemClassLoader())) { + // we only need to register the factory if the handler classes are not + // already on the system classloader - // if so our work here is done - if(handler != null) return handler; - } + log.debug("registering URLStreamHandlerFactory"); + + try { + URL.setURLStreamHandlerFactory(new URLHandlerFactory()); + } + catch (Error error) { - // build up the normal classname from the protocol - String className = "sun.net.www.protocol." + protocol + ".Handler"; + Field factoryField = null; - try { - // try to load the class - Class<?> handler = Class.forName(className); + try { + // get the factory field from java.net.URL via reflection + factoryField = URL.class.getDeclaredField("factory"); - // create and return an instance of the protocol handler - return (URLStreamHandler)handler.newInstance(); + // it's usually package protected but we need access to it + factoryField.setAccessible(true); - } catch(ClassNotFoundException | InstantiationException - | IllegalAccessException e) { - // skip over this to the null return; - } + URLStreamHandlerFactory delegate = (URLStreamHandlerFactory)factoryField.get(null); - // either there is no handler for the protocol or something went - // wrong, either way just return null - return null; - } - }; + // make sure the factory field is set to null so that setting it won't + // cause an exception + factoryField.set(null, null); - // make sure the factory field is set to null so that setting it won't - // cause an exception - factoryField.set(null, null); - - // register our deligating factory - URL.setURLStreamHandlerFactory(newFactory); - } catch(NoSuchFieldException | IllegalAccessException e) { - //this is a problem as it means our custom protocol handlers aren't going to get used - e.printStackTrace(); - } finally { - // make sure we put the field back the way it was before we started - if(factoryField != null) factoryField.setAccessible(false); - } + // register our delegating factory + URL.setURLStreamHandlerFactory(new URLHandlerFactory(delegate)); + } catch(NoSuchFieldException | IllegalAccessException e) { + //this is a problem as it means our custom protocol handlers aren't going to get used + e.printStackTrace(); + } finally { + // make sure we put the field back the way it was before we started + if(factoryField != null) factoryField.setAccessible(false); + } + } } } @@ -209,6 +221,11 @@ result = prime * result + ((baseURL == null) ? 0 : baseURL.hashCode()); return result; } + + @Override + public String toString() { + return getName(); + } @Override public boolean equals(Object obj) { @@ -504,7 +521,6 @@ this.version = version; name = group+":"+artifact+":"+version; - //TODO move calculation of baseURL into here } @Override @@ -608,7 +624,7 @@ artifactRequest); baseURL = new URI("creole://"+group+";"+artifact+";"+version+"/"); - + artifactURL = new URL("jar:" + artifactResult.getArtifact().getFile().toURI().toURL() @@ -654,7 +670,6 @@ .toExternalForm()); if(ar.getArtifact().equals(artifactResult.getArtifact())) { - System.out.println("scanning"); jarElement.setAttribute("SCAN", "true"); } @@ -682,15 +697,14 @@ if (model.getName() != null && !model.getName().trim().equals("")) name = model.getName(); - System.out.println(model.getOrganization().getName()); + /*System.out.println(model.getOrganization().getName()); for (org.apache.maven.model.Repository r : model.getRepositories()) { System.out.println(r.getName()); } for (License l : model.getLicenses()) { System.out.println(l.getName()); - } + }*/ - return jdomDoc; } Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/creole/CreoleAnnotationHandler.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/creole/CreoleAnnotationHandler.java 2017-01-25 02:23:54 UTC (rev 19979) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/creole/CreoleAnnotationHandler.java 2017-01-25 06:39:54 UTC (rev 19980) @@ -198,7 +198,6 @@ throws MalformedURLException { if("JAR".equals(jdomElt.getName())) { URL url = new URL(plugin.getBaseURL(), jdomElt.getTextTrim()); - System.out.println("adding: "+url); try { java.io.InputStream s = url.openStream(); s.close(); Modified: gate/branches/sawdust2/gate-core/src/main/java/sun/net/www/protocol/creole/Handler.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/sun/net/www/protocol/creole/Handler.java 2017-01-25 02:23:54 UTC (rev 19979) +++ gate/branches/sawdust2/gate-core/src/main/java/sun/net/www/protocol/creole/Handler.java 2017-01-25 06:39:54 UTC (rev 19980) @@ -26,7 +26,7 @@ Set<Plugin> plugins = Gate.getCreoleRegister().getPlugins(); for (Plugin registered : plugins) { - System.out.println(registered.getName()); + if (registered.equals(plugin)) { try { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs