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