001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.splash;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.DataInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.URL;
026: import java.net.URLConnection;
027: import javax.swing.ImageIcon;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.Project;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.util.Base64Converter;
032:
033: /**
034: * Creates a splash screen. The splash screen is displayed
035: * for the duration of the build and includes a handy progress bar as
036: * well. Use in conjunction with the sound task to provide interest
037: * whilst waiting for your builds to complete...
038: * @since Ant1.5
039: */
040: public class SplashTask extends Task {
041:
042: private String imgurl = null;
043: private String proxy = null;
044: private String user = null;
045: private String password = null;
046: private String port = "80";
047: private int showDuration = 5000;
048: private boolean useProxy = false;
049:
050: private static SplashScreen splash = null;
051:
052: /**
053: * A URL pointing to an image to display; optional, default antlogo.gif
054: * from the classpath.
055: * @param imgurl the url string pointing to the image
056: */
057: public void setImageURL(String imgurl) {
058: this .imgurl = imgurl;
059: }
060:
061: /**
062: * flag to enable proxy settings; optional, deprecated : consider
063: * using <setproxy> instead
064: * @param useProxy if ture, enable proxy settings
065: * @deprecated since 1.5.x.
066: * Use org.apache.tools.ant.taskdefs.optional.SetProxy
067: */
068: public void setUseproxy(boolean useProxy) {
069: this .useProxy = useProxy;
070: }
071:
072: /**
073: * name of proxy; optional.
074: * @param proxy the name of the proxy host
075: */
076: public void setProxy(String proxy) {
077: this .proxy = proxy;
078: }
079:
080: /**
081: * Proxy port; optional, default 80.
082: * @param port the proxy port
083: */
084: public void setPort(String port) {
085: this .port = port;
086: }
087:
088: /**
089: * Proxy user; optional, default =none.
090: * @param user the proxy user
091: */
092: public void setUser(String user) {
093: this .user = user;
094: }
095:
096: /**
097: * Proxy password; required if <tt>user</tt> is set.
098: * @param password the proxy password
099: */
100: public void setPassword(String password) {
101: this .password = password;
102: }
103:
104: /**
105: * how long to show the splash screen in milliseconds,
106: * optional; default 5000 ms.
107: * @param duration the spash duration in milliseconds
108: */
109: public void setShowduration(int duration) {
110: this .showDuration = duration;
111: }
112:
113: /**
114: * Execute the task.
115: * @throws BuildException on error
116: */
117: public void execute() throws BuildException {
118: if (splash != null) {
119: splash.setVisible(false);
120: getProject().removeBuildListener(splash);
121: splash.dispose();
122: splash = null;
123: }
124:
125: log("Creating new SplashScreen", Project.MSG_VERBOSE);
126: InputStream in = null;
127:
128: if (imgurl != null) {
129: try {
130: URLConnection conn = null;
131:
132: if (useProxy && (proxy != null && proxy.length() > 0)
133: && (port != null && port.length() > 0)) {
134:
135: log("Using proxied Connection", Project.MSG_DEBUG);
136: System.getProperties().put("http.proxySet", "true");
137: System.getProperties().put("http.proxyHost", proxy);
138: System.getProperties().put("http.proxyPort", port);
139:
140: URL url = new URL(imgurl);
141:
142: conn = url.openConnection();
143: if (user != null && user.length() > 0) {
144: // converted from sun internal classes to
145: // new Base64Converter
146: // utility class extracted from Get task
147: String encodedcreds = new Base64Converter()
148: .encode(user + ":" + password);
149: conn.setRequestProperty("Proxy-Authorization",
150: encodedcreds);
151: }
152:
153: } else {
154: System.getProperties()
155: .put("http.proxySet", "false");
156: System.getProperties().put("http.proxyHost", "");
157: System.getProperties().put("http.proxyPort", "");
158: log("Using Direction HTTP Connection",
159: Project.MSG_DEBUG);
160: URL url = new URL(imgurl);
161: conn = url.openConnection();
162: }
163: conn.setDoInput(true);
164: conn.setDoOutput(false);
165:
166: in = conn.getInputStream();
167:
168: // Catch everything - some of the above return nulls,
169: // throw exceptions or generally misbehave
170: // in the event of a problem etc
171:
172: } catch (Throwable ioe) {
173: log(
174: "Unable to download image, trying default Ant Logo",
175: Project.MSG_DEBUG);
176: log("(Exception was \"" + ioe.getMessage() + "\"",
177: Project.MSG_DEBUG);
178: }
179: }
180:
181: if (in == null) {
182: ClassLoader cl = SplashTask.class.getClassLoader();
183: if (cl != null) {
184: in = cl
185: .getResourceAsStream("images/ant_logo_large.gif");
186: } else {
187: in = ClassLoader
188: .getSystemResourceAsStream("images/ant_logo_large.gif");
189: }
190: }
191:
192: boolean success = false;
193: if (in != null) {
194: DataInputStream din = new DataInputStream(in);
195: try {
196: ByteArrayOutputStream bout = new ByteArrayOutputStream();
197: int data;
198: while ((data = din.read()) != -1) {
199: bout.write((byte) data);
200: }
201:
202: log("Got ByteArray, creating splash", Project.MSG_DEBUG);
203:
204: try {
205: ImageIcon img = new ImageIcon(bout.toByteArray());
206: splash = new SplashScreen(img);
207: success = true;
208: } catch (Throwable e) {
209: logHeadless(e);
210: }
211: } catch (Exception e) {
212: throw new BuildException(e);
213: } finally {
214: try {
215: din.close();
216: } catch (IOException ioe) {
217: // swallow if there was an error before so that
218: // original error will be passed up
219: if (success) {
220: throw new BuildException(ioe);
221: }
222: }
223: }
224: } else {
225: try {
226: splash = new SplashScreen("Image Unavailable.");
227: success = true;
228: } catch (Throwable e) {
229: logHeadless(e);
230: }
231: }
232:
233: if (success) {
234: splash.setVisible(true);
235: splash.toFront();
236: getProject().addBuildListener(splash);
237: try {
238: Thread.sleep(showDuration);
239: } catch (InterruptedException e) {
240: // Ignore Exception
241: }
242: }
243: }
244:
245: private void logHeadless(Throwable e) {
246: log("failed to display SplashScreen, caught "
247: + e.getClass().getName() + " with message: "
248: + e.getMessage(), Project.MSG_WARN);
249: }
250: }
|