KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > splash > SplashTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.splash;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
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 /**
34  * Creates a splash screen. The splash screen is displayed
35  * for the duration of the build and includes a handy progress bar as
36  * well. Use in conjunction with the sound task to provide interest
37  * whilst waiting for your builds to complete...
38  * @since Ant1.5
39  */

40 public class SplashTask extends Task {
41
42     private String JavaDoc imgurl = null;
43     private String JavaDoc proxy = null;
44     private String JavaDoc user = null;
45     private String JavaDoc password = null;
46     private String JavaDoc port = "80";
47     private int showDuration = 5000;
48     private boolean useProxy = false;
49
50     private static SplashScreen splash = null;
51
52     /**
53      * A URL pointing to an image to display; optional, default antlogo.gif
54      * from the classpath.
55      * @param imgurl the url string pointing to the image
56      */

57     public void setImageURL(String JavaDoc imgurl) {
58         this.imgurl = imgurl;
59     }
60
61     /**
62      * flag to enable proxy settings; optional, deprecated : consider
63      * using <setproxy> instead
64      * @param useProxy if ture, enable proxy settings
65      * @deprecated since 1.5.x.
66      * Use org.apache.tools.ant.taskdefs.optional.SetProxy
67      */

68     public void setUseproxy(boolean useProxy) {
69         this.useProxy = useProxy;
70     }
71
72     /**
73      * name of proxy; optional.
74      * @param proxy the name of the proxy host
75      */

76     public void setProxy(String JavaDoc proxy) {
77         this.proxy = proxy;
78     }
79
80     /**
81      * Proxy port; optional, default 80.
82      * @param port the proxy port
83      */

84     public void setPort(String JavaDoc port) {
85         this.port = port;
86     }
87
88     /**
89      * Proxy user; optional, default =none.
90      * @param user the proxy user
91      */

92     public void setUser(String JavaDoc user) {
93         this.user = user;
94     }
95
96     /**
97      * Proxy password; required if <tt>user</tt> is set.
98      * @param password the proxy password
99      */

100     public void setPassword(String JavaDoc 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     /**
115      * Execute the task.
116      * @throws BuildException on error
117      */

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 JavaDoc in = null;
128
129         if (imgurl != null) {
130             try {
131                 URLConnection JavaDoc 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 JavaDoc url = new URL JavaDoc(imgurl);
142
143                     conn = url.openConnection();
144                     if (user != null && user.length() > 0) {
145                         // converted from sun internal classes to
146
// new Base64Converter
147
// utility class extracted from Get task
148
String JavaDoc 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 JavaDoc url = new URL JavaDoc(imgurl);
160                     conn = url.openConnection();
161                 }
162                 conn.setDoInput(true);
163                 conn.setDoOutput(false);
164
165                 in = conn.getInputStream();
166
167                 // Catch everything - some of the above return nulls,
168
// throw exceptions or generally misbehave
169
// in the event of a problem etc
170

171             } catch (Throwable JavaDoc 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 JavaDoc 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 JavaDoc din = new DataInputStream JavaDoc(in);
192             try {
193                 ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
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 JavaDoc img = new ImageIcon JavaDoc(bout.toByteArray());
203                     splash = new SplashScreen(img);
204                     success = true;
205                 } catch (Throwable JavaDoc e) {
206                     logHeadless(e);
207                 }
208             } catch (Exception JavaDoc e) {
209                 throw new BuildException(e);
210             } finally {
211                 try {
212                     din.close();
213                 } catch (IOException JavaDoc ioe) {
214                     // swallow if there was an error before so that
215
// original error will be passed up
216
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 JavaDoc 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 JavaDoc e) {
237                 // Ignore Exception
238
}
239         }
240     }
241
242     private void logHeadless(Throwable JavaDoc 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