1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.splash;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.DataInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.URL;
  23. import java.net.URLConnection;
  24. import javax.swing.ImageIcon;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.Task;
  28. /**
  29. * Creates a splash screen. The splash screen is displayed
  30. * for the duration of the build and includes a handy progress bar as
  31. * well. Use in conjunction with the sound task to provide interest
  32. * whilst waiting for your builds to complete...
  33. * @since Ant1.5
  34. */
  35. public class SplashTask extends Task {
  36. private String imgurl = null;
  37. private String proxy = null;
  38. private String user = null;
  39. private String password = null;
  40. private String port = "80";
  41. private int showDuration = 5000;
  42. private boolean useProxy = false;
  43. private static SplashScreen splash = null;
  44. /**
  45. * A URL pointing to an image to display; optional, default antlogo.gif
  46. * from the classpath.
  47. */
  48. public void setImageURL(String imgurl) {
  49. this.imgurl = imgurl;
  50. }
  51. /**
  52. * flag to enable proxy settings; optional, deprecated : consider
  53. * using <setproxy> instead
  54. * @deprecated use org.apache.tools.ant.taskdefs.optional.SetProxy
  55. */
  56. public void setUseproxy(boolean useProxy) {
  57. this.useProxy = useProxy;
  58. }
  59. /**
  60. * name of proxy; optional.
  61. */
  62. public void setProxy(String proxy) {
  63. this.proxy = proxy;
  64. }
  65. /**
  66. * Proxy port; optional, default 80.
  67. */
  68. public void setPort(String port) {
  69. this.port = port;
  70. }
  71. /**
  72. * Proxy user; optional, default =none.
  73. */
  74. public void setUser(String user) {
  75. this.user = user;
  76. }
  77. /**
  78. * Proxy password; required if <tt>user</tt> is set.
  79. */
  80. public void setPassword(String password) {
  81. this.password = password;
  82. }
  83. /**
  84. * how long to show the splash screen in milliseconds,
  85. * optional; default 5000 ms.
  86. */
  87. public void setShowduration(int duration) {
  88. this.showDuration = duration;
  89. }
  90. public void execute() throws BuildException {
  91. if (splash != null) {
  92. splash.setVisible(false);
  93. getProject().removeBuildListener(splash);
  94. splash.dispose();
  95. splash = null;
  96. }
  97. log("Creating new SplashScreen", Project.MSG_VERBOSE);
  98. InputStream in = null;
  99. if (imgurl != null) {
  100. try {
  101. URLConnection conn = null;
  102. if (useProxy && (proxy != null && proxy.length() > 0)
  103. && (port != null && port.length() > 0)) {
  104. log("Using proxied Connection", Project.MSG_DEBUG);
  105. System.getProperties().put("http.proxySet", "true");
  106. System.getProperties().put("http.proxyHost", proxy);
  107. System.getProperties().put("http.proxyPort", port);
  108. URL url = new URL(imgurl);
  109. conn = url.openConnection();
  110. if (user != null && user.length() > 0) {
  111. String encodedcreds =
  112. new sun.misc.BASE64Encoder().encode((new String(user + ":" + password)).getBytes());
  113. conn.setRequestProperty("Proxy-Authorization",
  114. encodedcreds);
  115. }
  116. } else {
  117. System.getProperties().put("http.proxySet", "false");
  118. System.getProperties().put("http.proxyHost", "");
  119. System.getProperties().put("http.proxyPort", "");
  120. log("Using Direction HTTP Connection", Project.MSG_DEBUG);
  121. URL url = new URL(imgurl);
  122. conn = url.openConnection();
  123. }
  124. conn.setDoInput(true);
  125. conn.setDoOutput(false);
  126. in = conn.getInputStream();
  127. // Catch everything - some of the above return nulls,
  128. // throw exceptions or generally misbehave
  129. // in the event of a problem etc
  130. } catch (Throwable ioe) {
  131. log("Unable to download image, trying default Ant Logo",
  132. Project.MSG_DEBUG);
  133. log("(Exception was \"" + ioe.getMessage() + "\"",
  134. Project.MSG_DEBUG);
  135. }
  136. }
  137. if (in == null) {
  138. ClassLoader cl = SplashTask.class.getClassLoader();
  139. if (cl != null) {
  140. in = cl.getResourceAsStream("images/ant_logo_large.gif");
  141. } else {
  142. in = ClassLoader
  143. .getSystemResourceAsStream("images/ant_logo_large.gif");
  144. }
  145. }
  146. boolean success = false;
  147. if (in != null) {
  148. DataInputStream din = new DataInputStream(in);
  149. try {
  150. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  151. int data;
  152. while ((data = din.read()) != -1) {
  153. bout.write((byte) data);
  154. }
  155. log("Got ByteArray, creating splash", Project.MSG_DEBUG);
  156. try {
  157. ImageIcon img = new ImageIcon(bout.toByteArray());
  158. splash = new SplashScreen(img);
  159. success = true;
  160. } catch (Throwable e) {
  161. logHeadless(e);
  162. }
  163. } catch (Exception e) {
  164. throw new BuildException(e);
  165. } finally {
  166. try {
  167. din.close();
  168. } catch (IOException ioe) {
  169. // swallow if there was an error before so that
  170. // original error will be passed up
  171. if (success) {
  172. throw new BuildException(ioe);
  173. }
  174. }
  175. }
  176. } else {
  177. try {
  178. splash = new SplashScreen("Image Unavailable.");
  179. success = true;
  180. } catch (Throwable e) {
  181. logHeadless(e);
  182. }
  183. }
  184. if (success) {
  185. splash.setVisible(true);
  186. splash.toFront();
  187. getProject().addBuildListener(splash);
  188. try {
  189. Thread.sleep(showDuration);
  190. } catch (InterruptedException e) {
  191. }
  192. }
  193. }
  194. private void logHeadless(Throwable e) {
  195. log("failed to display SplashScreen, caught "
  196. + e.getClass().getName() + " with message: " + e.getMessage(),
  197. Project.MSG_WARN);
  198. }
  199. }