1 /* |
|
2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package sun.jkernel; |
|
26 |
|
27 import java.io.*; |
|
28 |
|
29 /** |
|
30 * Invoked by DownloadManager to begin (in a new JRE) the process of downloading |
|
31 * all remaining JRE components in the background. A mutex is used to ensure |
|
32 * that only one BackgroundDownloader can be active at a time. |
|
33 * |
|
34 */ |
|
35 public class BackgroundDownloader { |
|
36 public static final String BACKGROUND_DOWNLOAD_PROPERTY = "kernel.background.download"; |
|
37 // relative to the bundle directory |
|
38 public static final String PID_PATH = "tmp" + File.separator + "background.pid"; |
|
39 |
|
40 // Time to wait before beginning to download components. Gives the JRE |
|
41 // which spawned this one a chance to get its downloads going. |
|
42 private static final int WAIT_TIME = 10000; |
|
43 |
|
44 private static Mutex backgroundMutex; |
|
45 |
|
46 static synchronized Mutex getBackgroundMutex() { |
|
47 if (backgroundMutex == null) |
|
48 backgroundMutex = Mutex.create(DownloadManager.MUTEX_PREFIX + "background"); |
|
49 return backgroundMutex; |
|
50 } |
|
51 |
|
52 private static void doBackgroundDownloads() { |
|
53 if (DownloadManager.isJREComplete()) |
|
54 return; |
|
55 if (getBackgroundMutex().acquire(0)) { // give up and exit immediately if we can't acquire mutex |
|
56 try { |
|
57 writePid(); |
|
58 Thread.sleep(WAIT_TIME); |
|
59 DownloadManager.doBackgroundDownloads(false); |
|
60 DownloadManager.performCompletionIfNeeded(); |
|
61 } |
|
62 catch (InterruptedException e) { |
|
63 } |
|
64 finally { |
|
65 getBackgroundMutex().release(); |
|
66 } |
|
67 } |
|
68 else { |
|
69 System.err.println("Unable to acquire background download mutex."); |
|
70 System.exit(1); |
|
71 } |
|
72 } |
|
73 |
|
74 |
|
75 /** |
|
76 * Writes the current process ID to a file, so that the uninstaller can |
|
77 * find and kill this process if needed. |
|
78 */ |
|
79 private static void writePid() { |
|
80 try { |
|
81 File pid = new File(DownloadManager.getBundlePath(), PID_PATH); |
|
82 pid.getParentFile().mkdirs(); |
|
83 PrintStream out = new PrintStream(new FileOutputStream(pid)); |
|
84 pid.deleteOnExit(); |
|
85 out.println(DownloadManager.getCurrentProcessId()); |
|
86 out.close(); |
|
87 } |
|
88 catch (IOException e) { |
|
89 e.printStackTrace(); |
|
90 System.exit(1); |
|
91 } |
|
92 } |
|
93 |
|
94 |
|
95 /** |
|
96 * Reads from an InputStream until exhausted, writing all data to the |
|
97 * specified OutputStream. |
|
98 */ |
|
99 private static void send(InputStream in, OutputStream out) |
|
100 throws IOException { |
|
101 int c; |
|
102 byte[] buffer = new byte[2048]; |
|
103 while ((c = in.read(buffer)) > 0) |
|
104 out.write(buffer, 0, c); |
|
105 } |
|
106 |
|
107 /* |
|
108 * Returns the value of the BACKGROUND_DOWNLOAD_PROPERTY. |
|
109 * Checks if system property has been set first |
|
110 * then checks if registry key to disable background download |
|
111 * has been set. |
|
112 */ |
|
113 public static boolean getBackgroundDownloadProperty(){ |
|
114 /* |
|
115 * Check registry key value |
|
116 */ |
|
117 boolean bgDownloadEnabled = getBackgroundDownloadKey(); |
|
118 |
|
119 /* |
|
120 * Check system property - it should override the registry |
|
121 * key value. |
|
122 */ |
|
123 if (System.getProperty(BACKGROUND_DOWNLOAD_PROPERTY) != null){ |
|
124 bgDownloadEnabled = Boolean.valueOf( |
|
125 System.getProperty(BACKGROUND_DOWNLOAD_PROPERTY)); |
|
126 } |
|
127 return bgDownloadEnabled; |
|
128 |
|
129 } |
|
130 |
|
131 // This method is to retrieve the value of registry key |
|
132 // that disables background download. |
|
133 static native boolean getBackgroundDownloadKey(); |
|
134 |
|
135 |
|
136 static void startBackgroundDownloads() { |
|
137 if (!getBackgroundDownloadProperty()){ |
|
138 // If getBackgroundDownloadProperty() returns false |
|
139 // we're doing the downloads from this VM; we don't want to |
|
140 // spawn another one |
|
141 return; |
|
142 } |
|
143 |
|
144 // if System.err isn't initialized yet, it means the charsets aren't |
|
145 // available yet and we're going to run into trouble down below. Wait |
|
146 // until it's ready. |
|
147 while (System.err == null) { |
|
148 try { |
|
149 Thread.sleep(1000); |
|
150 } |
|
151 catch (InterruptedException e) { |
|
152 return; |
|
153 } |
|
154 } |
|
155 |
|
156 try { |
|
157 String args = "-D" + BACKGROUND_DOWNLOAD_PROPERTY + "=false -Xmx256m"; |
|
158 String backgroundDownloadURL = DownloadManager.getBaseDownloadURL(); |
|
159 |
|
160 // only set KERNEL_DOWNLOAD_URL_PROPERTY if we override |
|
161 // the default download url |
|
162 if (backgroundDownloadURL != null && |
|
163 backgroundDownloadURL.equals( |
|
164 DownloadManager.DEFAULT_DOWNLOAD_URL) == false) { |
|
165 args += " -D" + DownloadManager.KERNEL_DOWNLOAD_URL_PROPERTY + |
|
166 "=" + backgroundDownloadURL; |
|
167 }; |
|
168 args += " sun.jkernel.BackgroundDownloader"; |
|
169 final Process jvm = Runtime.getRuntime().exec("\"" + new File(System.getProperty("java.home"), "bin" + |
|
170 File.separator + "java.exe") + "\" " + args); |
|
171 Thread outputReader = new Thread("kernelOutputReader") { |
|
172 public void run() { |
|
173 try { |
|
174 InputStream in = jvm.getInputStream(); |
|
175 send(in, new PrintStream(new ByteArrayOutputStream())); |
|
176 } |
|
177 catch (IOException e) { |
|
178 e.printStackTrace(); |
|
179 } |
|
180 } |
|
181 }; |
|
182 outputReader.setDaemon(true); |
|
183 outputReader.start(); |
|
184 |
|
185 Thread errorReader = new Thread("kernelErrorReader") { |
|
186 public void run() { |
|
187 try { |
|
188 InputStream in = jvm.getErrorStream(); |
|
189 send(in, new PrintStream(new ByteArrayOutputStream())); |
|
190 } |
|
191 catch (IOException e) { |
|
192 e.printStackTrace(); |
|
193 } |
|
194 } |
|
195 }; |
|
196 errorReader.setDaemon(true); |
|
197 errorReader.start(); |
|
198 } |
|
199 catch (Exception e) { |
|
200 e.printStackTrace(); |
|
201 // TODO: error handling |
|
202 } |
|
203 } |
|
204 |
|
205 |
|
206 public static void main(String[] arg) { |
|
207 doBackgroundDownloads(); |
|
208 } |
|
209 } |
|