58 // AppletViewer. This problem existed before the re-write.
59 {"http.proxyHost", ""},
60 {"http.proxyPort", "80"},
61 {"package.restrict.access.sun", "true"}
62 };
63
64 static {
65 File userHome = new File(System.getProperty("user.home"));
66 // make sure we can write to this location
67 userHome.canWrite();
68
69 theUserPropertiesFile = new File(userHome, ".appletviewer");
70 }
71
72 // i18n
73 private static AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
74
75 /**
76 * Member variables set according to options passed in to AppletViewer.
77 */
78 private boolean debugFlag = false;
79 private boolean helpFlag = false;
80 private String encoding = null;
81 private boolean noSecurityFlag = false;
82 private static boolean cmdLineTestFlag = false;
83
84 /**
85 * The list of valid URLs passed in to AppletViewer.
86 */
87 private static Vector<URL> urlList = new Vector<>(1);
88
89 // This is used in init(). Getting rid of this is desirable but depends
90 // on whether the property that uses it is necessary/standard.
91 public static final String theVersion = System.getProperty("java.version");
92
93 /**
94 * The main entry point into AppletViewer.
95 */
96 public static void main(String [] args) {
97 Main m = new Main();
98 int ret = m.run(args);
119 args[i]));
120 }
121 i += j;
122 }
123 } catch (ParseException e) {
124 System.err.println(e.getMessage());
125 return 1;
126 }
127
128 // CHECK ARGUMENTS
129 if (helpFlag) {
130 usage();
131 return 0;
132 }
133
134 if (urlList.size() == 0) {
135 System.err.println(lookup("main.err.inputfile"));
136 return 1;
137 }
138
139 if (debugFlag) {
140 // START A DEBUG SESSION
141 // Given the current architecture, we will end up decoding the
142 // arguments again, but at least we are guaranteed to have
143 // arguments which are valid.
144 return invokeDebugger(args);
145 }
146
147 // INSTALL THE SECURITY MANAGER (if necessary)
148 if (!noSecurityFlag && (System.getSecurityManager() == null))
149 init();
150
151 // LAUNCH APPLETVIEWER FOR EACH URL
152 for (int i = 0; i < urlList.size(); i++) {
153 try {
154 // XXX 5/17 this parsing method should be changed/fixed so that
155 // it doesn't do both parsing of the html file and launching of
156 // the AppletPanel
157 AppletViewer.parse(urlList.elementAt(i), encoding);
158 } catch (IOException e) {
159 System.err.println(lookup("main.err.io", e.getMessage()));
160 return 1;
161 }
162 }
163 return 0;
164 }
165
166 private static void usage() {
174 * @param args The array of arguments.
175 * @param i The argument to decode.
176 * @return The number of array elements used when the argument was
177 * decoded.
178 * @exception ParseException
179 * Thrown when there is a problem with something in the
180 * argument array.
181 */
182 private int decodeArg(String [] args, int i) throws ParseException {
183 String arg = args[i];
184 int argc = args.length;
185
186 if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
187 helpFlag = true;
188 return 1;
189 } else if ("-encoding".equals(arg) && (i < argc - 1)) {
190 if (encoding != null)
191 throw new ParseException(lookup("main.err.dupoption", arg));
192 encoding = args[++i];
193 return 2;
194 } else if ("-debug".equals(arg)) {
195 debugFlag = true;
196 return 1;
197 } else if ("-Xnosecurity".equals(arg)) {
198 // This is an undocumented (and, in the future, unsupported)
199 // flag which prevents AppletViewer from installing its own
200 // SecurityManager.
201
202 System.err.println();
203 System.err.println(lookup("main.warn.nosecmgr"));
204 System.err.println();
205
206 noSecurityFlag = true;
207 return 1;
208 } else if ("-XcmdLineTest".equals(arg)) {
209 // This is an internal flag which should be used for command-line
210 // testing. It instructs AppletViewer to force a premature exit
211 // immediately after the applet has been launched.
212 cmdLineTestFlag = true;
213 return 1;
214 } else if (arg.startsWith("-")) {
215 throw new ParseException(lookup("main.err.unsupportedopt", arg));
216 } else {
250 !(new File(url.substring(prefix.length())).isAbsolute()))
251 {
252 // relative file URL, like this "file:index.html"
253 // ensure that this file URL is absolute
254 // ParseUtil.fileToEncodedURL should be done last (see 6329251)
255 String path = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"))).getPath() +
256 url.substring(prefix.length());
257 u = new URL("file", "", path);
258 } else {
259 // appletviewer accepts only encoded urls
260 u = new URL(url);
261 }
262 } catch (MalformedURLException e) {
263 throw new ParseException(lookup("main.err.badurl",
264 url, e.getMessage()));
265 }
266
267 return u;
268 }
269
270 /**
271 * Invoke the debugger with the arguments passed in to appletviewer.
272 *
273 * @param args The arguments passed into the debugger.
274 * @return {@code 0} if the debugger is invoked successfully,
275 * {@code 1} otherwise.
276 */
277 private int invokeDebugger(String [] args) {
278 // CONSTRUCT THE COMMAND LINE
279 String [] newArgs = new String[args.length + 1];
280 int current = 0;
281
282 // Add a -classpath argument that prevents
283 // the debugger from launching appletviewer with the default of
284 // ".". appletviewer's classpath should never contain valid
285 // classes since they will result in security exceptions.
286 // Ideally, the classpath should be set to "", but the VM won't
287 // allow an empty classpath, so a phony directory name is used.
288 String phonyDir = System.getProperty("java.home") +
289 File.separator + "phony";
290 newArgs[current++] = "-Djava.class.path=" + phonyDir;
291
292 // Appletviewer's main class is the debuggee
293 newArgs[current++] = "sun.applet.Main";
294
295 // Append all the of the original appletviewer arguments,
296 // leaving out the "-debug" option.
297 for (int i = 0; i < args.length; i++) {
298 if (!("-debug".equals(args[i]))) {
299 newArgs[current++] = args[i];
300 }
301 }
302
303 // LAUNCH THE DEBUGGER
304 // Reflection is used for two reasons:
305 // 1) The debugger classes are on classpath and thus must be loaded
306 // by the application class loader. (Currently, appletviewer are
307 // loaded through the boot class path out of rt.jar.)
308 // 2) Reflection removes any build dependency between appletviewer
309 // and jdb.
310 try {
311 Class<?> c = Class.forName("com.sun.tools.example.debug.tty.TTY", true,
312 ClassLoader.getSystemClassLoader());
313 Method m = c.getDeclaredMethod("main",
314 new Class<?>[] { String[].class });
315 m.invoke(null, new Object[] { newArgs });
316 } catch (ClassNotFoundException cnfe) {
317 System.err.println(lookup("main.debug.cantfinddebug"));
318 return 1;
319 } catch (NoSuchMethodException nsme) {
320 System.err.println(lookup("main.debug.cantfindmain"));
321 return 1;
322 } catch (InvocationTargetException ite) {
323 System.err.println(lookup("main.debug.exceptionindebug"));
324 return 1;
325 } catch (IllegalAccessException iae) {
326 System.err.println(lookup("main.debug.cantaccess"));
327 return 1;
328 }
329 return 0;
330 }
331
332 private void init() {
333 // GET APPLETVIEWER USER-SPECIFIC PROPERTIES
334 Properties avProps = getAVProps();
335
336 // ADD OTHER RANDOM PROPERTIES
337 // XXX 5/18 need to revisit why these are here, is there some
338 // standard for what is available?
339
340 // Standard browser properties
341 avProps.put("browser", "sun.applet.AppletViewer");
342 avProps.put("browser.version", "1.06");
343 avProps.put("browser.vendor", "Oracle Corporation");
344 avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v" + theVersion);
345
346 // Define which packages can be extended by applets
347 // XXX 5/19 probably not needed, not checked in AppletSecurity
348 avProps.put("package.restrict.definition.java", "true");
349 avProps.put("package.restrict.definition.sun", "true");
350
351 // Define which properties can be read by applets.
|
58 // AppletViewer. This problem existed before the re-write.
59 {"http.proxyHost", ""},
60 {"http.proxyPort", "80"},
61 {"package.restrict.access.sun", "true"}
62 };
63
64 static {
65 File userHome = new File(System.getProperty("user.home"));
66 // make sure we can write to this location
67 userHome.canWrite();
68
69 theUserPropertiesFile = new File(userHome, ".appletviewer");
70 }
71
72 // i18n
73 private static AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
74
75 /**
76 * Member variables set according to options passed in to AppletViewer.
77 */
78 private boolean helpFlag = false;
79 private String encoding = null;
80 private boolean noSecurityFlag = false;
81 private static boolean cmdLineTestFlag = false;
82
83 /**
84 * The list of valid URLs passed in to AppletViewer.
85 */
86 private static Vector<URL> urlList = new Vector<>(1);
87
88 // This is used in init(). Getting rid of this is desirable but depends
89 // on whether the property that uses it is necessary/standard.
90 public static final String theVersion = System.getProperty("java.version");
91
92 /**
93 * The main entry point into AppletViewer.
94 */
95 public static void main(String [] args) {
96 Main m = new Main();
97 int ret = m.run(args);
118 args[i]));
119 }
120 i += j;
121 }
122 } catch (ParseException e) {
123 System.err.println(e.getMessage());
124 return 1;
125 }
126
127 // CHECK ARGUMENTS
128 if (helpFlag) {
129 usage();
130 return 0;
131 }
132
133 if (urlList.size() == 0) {
134 System.err.println(lookup("main.err.inputfile"));
135 return 1;
136 }
137
138 // INSTALL THE SECURITY MANAGER (if necessary)
139 if (!noSecurityFlag && (System.getSecurityManager() == null))
140 init();
141
142 // LAUNCH APPLETVIEWER FOR EACH URL
143 for (int i = 0; i < urlList.size(); i++) {
144 try {
145 // XXX 5/17 this parsing method should be changed/fixed so that
146 // it doesn't do both parsing of the html file and launching of
147 // the AppletPanel
148 AppletViewer.parse(urlList.elementAt(i), encoding);
149 } catch (IOException e) {
150 System.err.println(lookup("main.err.io", e.getMessage()));
151 return 1;
152 }
153 }
154 return 0;
155 }
156
157 private static void usage() {
165 * @param args The array of arguments.
166 * @param i The argument to decode.
167 * @return The number of array elements used when the argument was
168 * decoded.
169 * @exception ParseException
170 * Thrown when there is a problem with something in the
171 * argument array.
172 */
173 private int decodeArg(String [] args, int i) throws ParseException {
174 String arg = args[i];
175 int argc = args.length;
176
177 if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
178 helpFlag = true;
179 return 1;
180 } else if ("-encoding".equals(arg) && (i < argc - 1)) {
181 if (encoding != null)
182 throw new ParseException(lookup("main.err.dupoption", arg));
183 encoding = args[++i];
184 return 2;
185 } else if ("-Xnosecurity".equals(arg)) {
186 // This is an undocumented (and, in the future, unsupported)
187 // flag which prevents AppletViewer from installing its own
188 // SecurityManager.
189
190 System.err.println();
191 System.err.println(lookup("main.warn.nosecmgr"));
192 System.err.println();
193
194 noSecurityFlag = true;
195 return 1;
196 } else if ("-XcmdLineTest".equals(arg)) {
197 // This is an internal flag which should be used for command-line
198 // testing. It instructs AppletViewer to force a premature exit
199 // immediately after the applet has been launched.
200 cmdLineTestFlag = true;
201 return 1;
202 } else if (arg.startsWith("-")) {
203 throw new ParseException(lookup("main.err.unsupportedopt", arg));
204 } else {
238 !(new File(url.substring(prefix.length())).isAbsolute()))
239 {
240 // relative file URL, like this "file:index.html"
241 // ensure that this file URL is absolute
242 // ParseUtil.fileToEncodedURL should be done last (see 6329251)
243 String path = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"))).getPath() +
244 url.substring(prefix.length());
245 u = new URL("file", "", path);
246 } else {
247 // appletviewer accepts only encoded urls
248 u = new URL(url);
249 }
250 } catch (MalformedURLException e) {
251 throw new ParseException(lookup("main.err.badurl",
252 url, e.getMessage()));
253 }
254
255 return u;
256 }
257
258 private void init() {
259 // GET APPLETVIEWER USER-SPECIFIC PROPERTIES
260 Properties avProps = getAVProps();
261
262 // ADD OTHER RANDOM PROPERTIES
263 // XXX 5/18 need to revisit why these are here, is there some
264 // standard for what is available?
265
266 // Standard browser properties
267 avProps.put("browser", "sun.applet.AppletViewer");
268 avProps.put("browser.version", "1.06");
269 avProps.put("browser.vendor", "Oracle Corporation");
270 avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v" + theVersion);
271
272 // Define which packages can be extended by applets
273 // XXX 5/19 probably not needed, not checked in AppletSecurity
274 avProps.put("package.restrict.definition.java", "true");
275 avProps.put("package.restrict.definition.sun", "true");
276
277 // Define which properties can be read by applets.
|