Source Code Cross Referenced for Main.java in  » 6.0-JDK-Modules » j2me » sun » applet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » sun.applet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)Main.java	1.21 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package sun.applet;
029:
030:        import java.io.BufferedInputStream;
031:        import java.io.File;
032:        import java.io.FileInputStream;
033:        import java.io.FileOutputStream;
034:        import java.io.IOException;
035:        import java.lang.reflect.Method;
036:        import java.lang.reflect.InvocationTargetException;
037:        import java.net.URL;
038:        import java.net.MalformedURLException;
039:        import java.util.Enumeration;
040:        import java.util.Properties;
041:        import java.util.Vector;
042:
043:        /**
044:         * The main entry point into AppletViewer.
045:         */
046:        public class Main {
047:            /**
048:             * The file which contains all of the AppletViewer specific properties.
049:             */
050:            static File theUserPropertiesFile;
051:            /**
052:             * The default key/value pairs for the required user-specific properties.
053:             */
054:            static final String[][] avDefaultUserProps = {
055:                    // There's a bootstrapping problem here.  If we don't have a proxyHost,
056:                    // then we will not be able to connect to a URL outside the firewall;
057:                    // however, it's not possible to set the proxyHost without starting
058:                    // AppletViewer.  This problem existed before the re-write.
059:                    { "http.proxyHost", "" }, { "http.proxyPort", "80" },
060:                    { "package.restrict.access.sun", "true" } };
061:            static {
062:                File userHome = new File(System.getProperty("user.home"));
063:                // make sure we can write to this location
064:                userHome.canWrite();
065:                theUserPropertiesFile = new File(userHome, ".appletviewer");
066:            }
067:            // i18n
068:            private static AppletMessageHandler amh = new AppletMessageHandler(
069:                    "appletviewer");
070:            /**
071:             * Member variables set according to options passed in to AppletViewer.
072:             */
073:            private boolean debugFlag = false;
074:            private boolean helpFlag = false;
075:            private String encoding = null;
076:            private boolean noSecurityFlag = false;
077:            private static boolean cmdLineTestFlag = false;
078:            /**
079:             * The list of valid URLs passed in to AppletViewer.
080:             */
081:            private static Vector urlList = new Vector(1);
082:            // This is used in init().  Getting rid of this is desirable but depends
083:            // on whether the property that uses it is necessary/standard.
084:            public static final String theVersion = "1.3";
085:
086:            /**
087:             * The main entry point into AppletViewer.
088:             */
089:            public static void main(String[] args) {
090:                Main m = new Main();
091:                int ret = m.run(args);
092:                // Exit immediately if we got some sort of error along the way.
093:                // For debugging purposes, if we have passed in "-XcmdLineTest" we
094:                // force a premature exit.
095:                if ((ret != 0) || (cmdLineTestFlag))
096:                    System.exit(ret);
097:            }
098:
099:            private int run(String[] args) {
100:                // DECODE ARGS
101:                try {
102:                    if (args.length == 0) {
103:                        usage();
104:                        return 0;
105:                    }
106:                    for (int i = 0; i < args.length;) {
107:                        int j = decodeArg(args, i);
108:                        if (j == 0) {
109:                            throw new ParseException(lookup(
110:                                    "main.err.unrecognizedarg", args[i]));
111:                        }
112:                        i += j;
113:                    }
114:                } catch (ParseException e) {
115:                    System.err.println(e.getMessage());
116:                    return 1;
117:                }
118:                // CHECK ARGUMENTS
119:                if (helpFlag) {
120:                    usage();
121:                    return 0;
122:                }
123:                if (urlList.size() == 0) {
124:                    System.err.println(lookup("main.err.inputfile"));
125:                    return 1;
126:                }
127:                if (debugFlag) {
128:                    // START A DEBUG SESSION
129:                    // Given the current architecture, we will end up decoding the
130:                    // arguments again, but at least we are guaranteed to have
131:                    // arguments which are valid.
132:                    return invokeDebugger(args);
133:                }
134:                // INSTALL THE SECURITY MANAGER (if necessary)
135:                if (!noSecurityFlag && (System.getSecurityManager() == null))
136:                    init();
137:                // LAUNCH APPLETVIEWER FOR EACH URL
138:                for (int i = 0; i < urlList.size(); i++) {
139:                    try {
140:                        // NOTE: this parsing method should be changed/fixed so that
141:                        // it doesn't do both parsing of the html file and launching of
142:                        // the AppletPanel
143:                        AppletViewer
144:                                .parse((URL) urlList.elementAt(i), encoding);
145:                    } catch (IOException e) {
146:                        System.err
147:                                .println(lookup("main.err.io", e.getMessage()));
148:                        return 1;
149:                    }
150:                }
151:                return 0;
152:            }
153:
154:            private static void usage() {
155:                System.out.println(lookup("usage"));
156:            }
157:
158:            /**
159:             * Decode a single argument in an array and return the number of elements
160:             * used.
161:             *
162:             * @param args The array of arguments.
163:             * @param i    The argument to decode.
164:             * @return     The number of array elements used when the argument was
165:             *             decoded.
166:             * @exception ParseException
167:             *             Thrown when there is a problem with something in the
168:             *             argument array.
169:             */
170:            private int decodeArg(String[] args, int i) throws ParseException {
171:                String arg = args[i];
172:                int argc = args.length;
173:                if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
174:                    helpFlag = true;
175:                    return 1;
176:                } else if ("-encoding".equals(arg) && (i < argc - 1)) {
177:                    if (encoding != null)
178:                        throw new ParseException(lookup("main.err.dupoption",
179:                                arg));
180:                    encoding = args[++i];
181:                    return 2;
182:                } else if ("-debug".equals(arg)) {
183:                    debugFlag = true;
184:                    return 1;
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:                    noSecurityFlag = true;
194:                    return 1;
195:                } else if ("-XcmdLineTest".equals(arg)) {
196:                    // This is an internal flag which should be used for command-line
197:                    // testing.  It instructs AppletViewer to force a premature exit
198:                    // immediately after the applet has been launched.
199:                    cmdLineTestFlag = true;
200:                    return 1;
201:                } else if (arg.startsWith("-")) {
202:                    throw new ParseException(lookup("main.err.unsupportedopt",
203:                            arg));
204:                } else {
205:                    // we found what we hope is a url
206:                    URL url = parseURL(arg);
207:                    if (url != null) {
208:                        urlList.addElement(url);
209:                        return 1;
210:                    }
211:                }
212:                return 0;
213:            }
214:
215:            /**
216:             * Following the relevant RFC, construct a valid URL based on the passed in
217:             * string.
218:             *
219:             * @param url  A string which represents either a relative or absolute URL.
220:             * @return     A URL when the passed in string can be interpreted according
221:             *             to the RFC.  <code>null</code> otherwise.
222:             * @exception ParseException
223:             *             Thrown when we are unable to construct a proper URL from the
224:             *             passed in string.
225:             */
226:            private URL parseURL(String url) throws ParseException {
227:                URL u = null;
228:                try {
229:                    if (url.indexOf(':') <= 1) {
230:                        // we were passed in a relative URL or an absolute URL on a
231:                        // win32 machine
232:                        u = new File(System.getProperty("user.dir"), url)
233:                                .toURL();
234:                    } else {
235:                        if (url.startsWith("file:")
236:                                && url.replace(File.separatorChar, '/')
237:                                        .indexOf('/') == -1) {
238:                            // We were passed in a relative "file" URL, like this:
239:                            //     "file:index.html".
240:                            // Prepend current directory location.
241:                            String fname = url.substring("file:".length());
242:                            if (fname.length() > 0) {
243:                                u = new File(System.getProperty("user.dir"),
244:                                        fname).toURL();
245:                            } else {
246:                                u = new URL(url);
247:                            }
248:                        } else {
249:                            u = new URL(url);
250:                        }
251:                    }
252:                } catch (MalformedURLException e) {
253:                    throw new ParseException(lookup("main.err.badurl", url, e
254:                            .getMessage()));
255:                }
256:                return u;
257:            }
258:
259:            /**
260:             * Invoke the debugger with the arguments passed in to appletviewer.
261:             *
262:             * @param args The arguments passed into the debugger.
263:             * @return     <code>0</code> if the debugger is invoked successfully,
264:             *             <code>1</code> otherwise.
265:             */
266:            private int invokeDebugger(String[] args) {
267:                // CONSTRUCT THE COMMAND LINE
268:                String[] newArgs = new String[args.length + 1];
269:                int current = 0;
270:                // Add a -classpath argument that prevents 
271:                // the debugger from launching appletviewer with the default of
272:                // ".". appletviewer's classpath should never contain valid
273:                // classes since they will result in security exceptions.
274:                // Ideally, the classpath should be set to "", but the VM won't
275:                // allow an empty classpath, so a phony directory name is used.  
276:                String phonyDir = System.getProperty("java.home")
277:                        + File.separator + "phony";
278:                newArgs[current++] = "-Djava.class.path=" + phonyDir;
279:                // Appletviewer's main class is the debuggee
280:                newArgs[current++] = "sun.applet.Main";
281:                // Append all the of the original appletviewer arguments, 
282:                // leaving out the "-debug" option.
283:                for (int i = 0; i < args.length; i++) {
284:                    if (!("-debug".equals(args[i]))) {
285:                        newArgs[current++] = args[i];
286:                    }
287:                }
288:                // LAUNCH THE DEBUGGER
289:                // Reflection is used for two reasons:
290:                // 1) The debugger classes are on classpath and thus must be loaded
291:                // by the application class loader. (Currently, appletviewer are 
292:                // loaded through the boot class path out of rt.jar.) 
293:                // 2) Reflection removes any build dependency between appletviewer 
294:                // and jdb.
295:                try {
296:                    Class c = Class.forName(
297:                            "com.sun.tools.example.debug.tty.TTY", true,
298:                            ClassLoader.getSystemClassLoader());
299:                    Method m = c.getDeclaredMethod("main",
300:                            new Class[] { String[].class });
301:                    m.invoke(null, new Object[] { newArgs });
302:                } catch (ClassNotFoundException cnfe) {
303:                    System.err.println(lookup("main.debug.cantfinddebug"));
304:                    return 1;
305:                } catch (NoSuchMethodException nsme) {
306:                    System.err.println(lookup("main.debug.cantfindmain"));
307:                    return 1;
308:                } catch (InvocationTargetException ite) {
309:                    System.err.println(lookup("main.debug.exceptionindebug"));
310:                    return 1;
311:                } catch (IllegalAccessException iae) {
312:                    System.err.println(lookup("main.debug.cantaccess"));
313:                    return 1;
314:                }
315:                return 0;
316:            }
317:
318:            private void init() {
319:                // GET APPLETVIEWER USER-SPECIFIC PROPERTIES
320:                Properties avProps = getAVProps();
321:                // ADD OTHER RANDOM PROPERTIES
322:                // TODO: need to revisit why these are here, is there some
323:                // standard for what is available?
324:
325:                // Define a number of standard properties
326:                avProps.put("acl.read", "+");
327:                avProps.put("acl.read.default", "");
328:                avProps.put("acl.write", "+");
329:                avProps.put("acl.write.default", "");
330:                // Standard browser properties
331:                avProps.put("browser", "sun.applet.AppletViewer");
332:                avProps.put("browser.version", "1.06");
333:                avProps.put("browser.vendor", "Sun Microsystems Inc.");
334:                avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v"
335:                        + theVersion);
336:                // Define which packages can be extended by applets
337:                // NOTE: probably not needed, not checked in AppletSecurity
338:                avProps.put("package.restrict.definition.java", "true");
339:                avProps.put("package.restrict.definition.sun", "true");
340:                // Define which properties can be read by applets.
341:                // A property named by "key" can be read only when its twin
342:                // property "key.applet" is true.  The following ten properties
343:                // are open by default.  Any other property can be explicitly
344:                // opened up by the browser user by calling appletviewer with
345:                // -J-Dkey.applet=true
346:                avProps.put("java.version.applet", "true");
347:                avProps.put("java.vendor.applet", "true");
348:                avProps.put("java.vendor.url.applet", "true");
349:                avProps.put("java.class.version.applet", "true");
350:                avProps.put("os.name.applet", "true");
351:                avProps.put("os.version.applet", "true");
352:                avProps.put("os.arch.applet", "true");
353:                avProps.put("file.separator.applet", "true");
354:                avProps.put("path.separator.applet", "true");
355:                avProps.put("line.separator.applet", "true");
356:                // Read in the System properties.  If something is going to be
357:                // over-written, warn about it.
358:                Properties sysProps = System.getProperties();
359:                for (Enumeration e = sysProps.propertyNames(); e
360:                        .hasMoreElements();) {
361:                    String key = (String) e.nextElement();
362:                    String val = (String) sysProps.getProperty(key);
363:                    String oldVal;
364:                    if ((oldVal = (String) avProps.setProperty(key, val)) != null)
365:                        System.err.println(lookup("main.warn.prop.overwrite",
366:                                key, oldVal, val));
367:                }
368:                // INSTALL THE PROPERTY LIST
369:                System.setProperties(avProps);
370:                // Create and install the security manager
371:                if (!noSecurityFlag) {
372:                    System.setSecurityManager(new AppletSecurity());
373:                } else {
374:                    System.err.println(lookup("main.nosecmgr"));
375:                }
376:                // TODO: Create and install a socket factory!
377:            }
378:
379:            /**
380:             * Read the AppletViewer user-specific properties.  Typically, these
381:             * properties should reside in the file $USER/.appletviewer.  If this file
382:             * does not exist, one will be created.  Information for this file will
383:             * be gleaned from $USER/.hotjava/properties.  If that file does not exist,
384:             * then default values will be used.
385:             *
386:             * @return     A Properties object containing all of the AppletViewer
387:             *             user-specific properties.
388:             */
389:            private Properties getAVProps() {
390:                Properties avProps = new Properties();
391:                File dotAV = theUserPropertiesFile;
392:                if (dotAV.exists()) {
393:                    // we must have already done the conversion
394:                    if (dotAV.canRead()) {
395:                        // just read the file
396:                        avProps = getAVProps(dotAV);
397:                    } else {
398:                        // send out warning and use defaults
399:                        System.err.println(lookup("main.warn.cantreadprops",
400:                                dotAV.toString()));
401:                        avProps = setDefaultAVProps();
402:                    }
403:                } else {
404:                    // create the $USER/.appletviewer file
405:
406:                    // see if $USER/.hotjava/properties exists
407:                    File userHome = new File(System.getProperty("user.home"));
408:                    File dotHJ = new File(userHome, ".hotjava");
409:                    dotHJ = new File(dotHJ, "properties");
410:                    if (dotHJ.exists()) {
411:                        // just read the file
412:                        avProps = getAVProps(dotHJ);
413:                    } else {
414:                        // send out warning and use defaults
415:                        System.err.println(lookup("main.warn.cantreadprops",
416:                                dotHJ.toString()));
417:                        avProps = setDefaultAVProps();
418:                    }
419:                    // SAVE THE FILE
420:                    try {
421:                        FileOutputStream out = new FileOutputStream(dotAV);
422:                        avProps.store(out, lookup("main.prop.store"));
423:                        out.close();
424:                    } catch (IOException e) {
425:                        System.err.println(lookup("main.err.prop.cantsave",
426:                                dotAV.toString()));
427:                    }
428:                }
429:                return avProps;
430:            }
431:
432:            /**
433:             * Set the AppletViewer user-specific properties to be the default values.
434:             *
435:             * @return     A Properties object containing all of the AppletViewer
436:             *             user-specific properties, set to the default values.
437:             */
438:            private Properties setDefaultAVProps() {
439:                Properties avProps = new Properties();
440:                for (int i = 0; i < avDefaultUserProps.length; i++) {
441:                    avProps.setProperty(avDefaultUserProps[i][0],
442:                            avDefaultUserProps[i][1]);
443:                }
444:                return avProps;
445:            }
446:
447:            /**
448:             * Given a file, find only the properties that are setable by AppletViewer.
449:             *
450:             * @param inFile A Properties file from which we select the properties of
451:             *             interest.
452:             * @return     A Properties object containing all of the AppletViewer
453:             *             user-specific properties.
454:             */
455:            private Properties getAVProps(File inFile) {
456:                Properties avProps = new Properties();
457:                // read the file
458:                Properties tmpProps = new Properties();
459:                try {
460:                    FileInputStream in = new FileInputStream(inFile);
461:                    tmpProps.load(new BufferedInputStream(in));
462:                    in.close();
463:                } catch (IOException e) {
464:                    System.err.println(lookup("main.err.prop.cantread", inFile
465:                            .toString()));
466:                }
467:                // pick off the properties we care about
468:                for (int i = 0; i < avDefaultUserProps.length; i++) {
469:                    String value = tmpProps
470:                            .getProperty(avDefaultUserProps[i][0]);
471:                    if (value != null) {
472:                        // the property exists in the file, so replace the default
473:                        avProps.setProperty(avDefaultUserProps[i][0], value);
474:                    } else {
475:                        // just use the default
476:                        avProps.setProperty(avDefaultUserProps[i][0],
477:                                avDefaultUserProps[i][1]);
478:                    }
479:                }
480:                return avProps;
481:            }
482:
483:            /**
484:             * Methods for easier i18n handling.
485:             */
486:
487:            private static String lookup(String key) {
488:                return amh.getMessage(key);
489:            }
490:
491:            private static String lookup(String key, String arg0) {
492:                return amh.getMessage(key, arg0);
493:            }
494:
495:            private static String lookup(String key, String arg0, String arg1) {
496:                return amh.getMessage(key, arg0, arg1);
497:            }
498:
499:            private static String lookup(String key, String arg0, String arg1,
500:                    String arg2) {
501:                return amh.getMessage(key, arg0, arg1, arg2);
502:            }
503:
504:            class ParseException extends RuntimeException {
505:                public ParseException(String msg) {
506:                    super (msg);
507:                }
508:
509:                public ParseException(Throwable t) {
510:                    super (t.getMessage());
511:                    this .t = t;
512:                }
513:
514:                Throwable t = null;
515:            }
516:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.