1 18 19 package org.apache.tools.ant.taskdefs.optional.splash; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.DataInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import javax.swing.ImageIcon ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.util.Base64Converter; 32 33 40 public class SplashTask extends Task { 41 42 private String imgurl = null; 43 private String proxy = null; 44 private String user = null; 45 private String password = null; 46 private String port = "80"; 47 private int showDuration = 5000; 48 private boolean useProxy = false; 49 50 private static SplashScreen splash = null; 51 52 57 public void setImageURL(String imgurl) { 58 this.imgurl = imgurl; 59 } 60 61 68 public void setUseproxy(boolean useProxy) { 69 this.useProxy = useProxy; 70 } 71 72 76 public void setProxy(String proxy) { 77 this.proxy = proxy; 78 } 79 80 84 public void setPort(String port) { 85 this.port = port; 86 } 87 88 92 public void setUser(String user) { 93 this.user = user; 94 } 95 96 100 public void setPassword(String password) { 101 this.password = password; 102 } 103 104 109 public void setShowduration(int duration) { 110 this.showDuration = duration; 111 } 112 113 114 118 public void execute() throws BuildException { 119 if (splash != null) { 120 splash.setVisible(false); 121 getProject().removeBuildListener(splash); 122 splash.dispose(); 123 splash = null; 124 } 125 126 log("Creating new SplashScreen", Project.MSG_VERBOSE); 127 InputStream in = null; 128 129 if (imgurl != null) { 130 try { 131 URLConnection conn = null; 132 133 if (useProxy && (proxy != null && proxy.length() > 0) 134 && (port != null && port.length() > 0)) { 135 136 log("Using proxied Connection", Project.MSG_DEBUG); 137 System.getProperties().put("http.proxySet", "true"); 138 System.getProperties().put("http.proxyHost", proxy); 139 System.getProperties().put("http.proxyPort", port); 140 141 URL url = new URL (imgurl); 142 143 conn = url.openConnection(); 144 if (user != null && user.length() > 0) { 145 String encodedcreds = 149 new Base64Converter().encode(user + ":" + password); 150 conn.setRequestProperty("Proxy-Authorization", 151 encodedcreds); 152 } 153 154 } else { 155 System.getProperties().put("http.proxySet", "false"); 156 System.getProperties().put("http.proxyHost", ""); 157 System.getProperties().put("http.proxyPort", ""); 158 log("Using Direction HTTP Connection", Project.MSG_DEBUG); 159 URL url = new URL (imgurl); 160 conn = url.openConnection(); 161 } 162 conn.setDoInput(true); 163 conn.setDoOutput(false); 164 165 in = conn.getInputStream(); 166 167 171 } catch (Throwable ioe) { 172 log("Unable to download image, trying default Ant Logo", 173 Project.MSG_DEBUG); 174 log("(Exception was \"" + ioe.getMessage() + "\"", 175 Project.MSG_DEBUG); 176 } 177 } 178 179 if (in == null) { 180 ClassLoader cl = SplashTask.class.getClassLoader(); 181 if (cl != null) { 182 in = cl.getResourceAsStream("images/ant_logo_large.gif"); 183 } else { 184 in = ClassLoader 185 .getSystemResourceAsStream("images/ant_logo_large.gif"); 186 } 187 } 188 189 boolean success = false; 190 if (in != null) { 191 DataInputStream din = new DataInputStream (in); 192 try { 193 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 194 int data; 195 while ((data = din.read()) != -1) { 196 bout.write((byte) data); 197 } 198 199 log("Got ByteArray, creating splash", Project.MSG_DEBUG); 200 201 try { 202 ImageIcon img = new ImageIcon (bout.toByteArray()); 203 splash = new SplashScreen(img); 204 success = true; 205 } catch (Throwable e) { 206 logHeadless(e); 207 } 208 } catch (Exception e) { 209 throw new BuildException(e); 210 } finally { 211 try { 212 din.close(); 213 } catch (IOException ioe) { 214 if (success) { 217 throw new BuildException(ioe); 218 } 219 } 220 } 221 } else { 222 try { 223 splash = new SplashScreen("Image Unavailable."); 224 success = true; 225 } catch (Throwable e) { 226 logHeadless(e); 227 } 228 } 229 230 if (success) { 231 splash.setVisible(true); 232 splash.toFront(); 233 getProject().addBuildListener(splash); 234 try { 235 Thread.sleep(showDuration); 236 } catch (InterruptedException e) { 237 } 239 } 240 } 241 242 private void logHeadless(Throwable e) { 243 log("failed to display SplashScreen, caught " 244 + e.getClass().getName() + " with message: " + e.getMessage(), 245 Project.MSG_WARN); 246 } 247 } 248 | Popular Tags |