| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.ant; |
| 5 | |
| 6 | import java.io.File; |
| 7 | import java.io.FileNotFoundException; |
| 8 | import java.io.FileOutputStream; |
| 9 | import java.io.IOException; |
| 10 | import java.net.MalformedURLException; |
| 11 | import java.net.URL; |
| 12 | |
| 13 | import org.apache.tools.ant.BuildException; |
| 14 | import org.sourceforge.xradar.resolver.XRadarStreamHandlerFactory; |
| 15 | |
| 16 | /** |
| 17 | * |
| 18 | * @author Romain PELISSE, belaran@gmail.com |
| 19 | * |
| 20 | */ |
| 21 | public class RadarLoaderTask extends AbstractXRadarAntTask { |
| 22 | |
| 23 | private String resource; |
| 24 | private String target; |
| 25 | |
| 26 | /** |
| 27 | * @return the resourceName |
| 28 | */ |
| 29 | public String getResource() { |
| 30 | return resource; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param resource the resourceName to set |
| 35 | */ |
| 36 | public void setResource(String resource) { |
| 37 | this.resource = resource; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void execute() throws BuildException { |
| 42 | // Registering the new resolver to handle xradar protocol |
| 43 | registerURLResolver(); |
| 44 | URL resourceURL = validate(); |
| 45 | // Extracting the resource from the URL provided |
| 46 | extracting(resourceURL); |
| 47 | } |
| 48 | |
| 49 | private void extracting(URL resourceURL) { |
| 50 | LoaderHelper helper = new LoaderHelper(super.isOnline()); |
| 51 | try { |
| 52 | helper.extractFrom(resourceURL.openStream(), new FileOutputStream(new File(target))); |
| 53 | } catch (FileNotFoundException e) { |
| 54 | throw new BuildException(e); |
| 55 | } catch (IOException e) { |
| 56 | throw new BuildException(e); |
| 57 | } catch (Exception e) { |
| 58 | throw new BuildException(e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private void registerURLResolver() { |
| 63 | // FIXME: This exception catching is ugly !!!!!! |
| 64 | // FIXME: Should be moved inside AbstractProcess |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | // Adding to JVM our own URL resolver to resolve |
| 69 | // the 'xradar' protocol in stylesheet |
| 70 | URL.setURLStreamHandlerFactory(new XRadarStreamHandlerFactory()); |
| 71 | } |
| 72 | catch ( java.lang.Error e) |
| 73 | { |
| 74 | //FIXME: this so ugly, it actually requires an other fixme ! |
| 75 | // Only if we run into this error we should not take care of the exception |
| 76 | if ( ! "class java.lang.Error".equals(e.getClass()) && |
| 77 | ! "factory already defined".equals(e.getMessage()) ) |
| 78 | { |
| 79 | throw e; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | private URL validate() throws BuildException { |
| 86 | if ( this.resource == null || "".equals(this.resource) ) |
| 87 | throw new BuildException("resourceName is a required attribute"); |
| 88 | if ( this.target == null || "".equals(this.target) ) |
| 89 | throw new BuildException("target is a required attribute"); |
| 90 | else |
| 91 | { |
| 92 | File file = new File(this.target); |
| 93 | if ( file.exists() && ! file.canWrite() ) |
| 94 | throw new BuildException("target attribute value must be a writable."); |
| 95 | } |
| 96 | try { |
| 97 | return new URL(this.resource); |
| 98 | } catch (MalformedURLException e){ |
| 99 | throw new BuildException(e); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Very small test... |
| 105 | * @param args |
| 106 | */ |
| 107 | public static void main(String[] args) { |
| 108 | RadarLoaderTask task = new RadarLoaderTask(); |
| 109 | task.setResource("xradar://resources/tools.xml"); |
| 110 | task.setTarget("target/tools.xml"); |
| 111 | task.execute(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return the target |
| 116 | */ |
| 117 | public String getTarget() { |
| 118 | return target; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param target the target to set |
| 123 | */ |
| 124 | public void setTarget(String target) { |
| 125 | this.target = target; |
| 126 | } |
| 127 | |
| 128 | } |