001    /*
002     * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    
027    package com.sun.tools.javah;
028    
029    import java.io.File;
030    import java.io.FileInputStream;
031    import java.io.IOException;
032    import java.util.ResourceBundle;
033    import java.text.MessageFormat;
034    import java.util.MissingResourceException;
035    
036    /**
037     * Messages, verbose and error handling support.
038     *
039     * For errors, the failure modes are:
040     *      error -- User did something wrong
041     *      bug   -- Bug has occurred in javah
042     *      fatal -- We can't even find resources, so bail fast, don't localize
043     *
044     */
045    public class Util {
046    
047        /*
048         * Help for verbosity.
049         */
050        public static boolean verbose = false;
051    
052        public static void log(String s) {
053            System.out.println(s);
054        }
055    
056    
057        /*
058         * Help for loading localized messages.
059         */
060        private static ResourceBundle m;
061    
062        private static void initMessages() {
063            try {
064                m=ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
065            } catch (MissingResourceException mre) {
066                fatal("Error loading resources.  Please file a bug report.", mre);
067            }
068        }
069    
070        public static String getText(String key) {
071            return getText(key, null, null);
072        }
073    
074        private static String getText(String key, String a1, String a2){
075            if (m == null)
076                initMessages();
077            try {
078                return MessageFormat.format(m.getString(key),
079                                            new Object[] { a1, a2 });
080            } catch (MissingResourceException e) {
081                fatal("Key " + key + " not found in resources.", e);
082            }
083            return null; /* dead code */
084        }
085    
086        /*
087         * Usage message.
088         */
089        public static void usage(int exitValue) {
090            if (exitValue == 0) {
091                System.out.println(getText("usage"));
092            } else {
093                System.err.println(getText("usage"));
094            }
095            System.exit(exitValue);
096        }
097    
098        public static void version() {
099            System.out.println(getText("javah.version",
100                                       System.getProperty("java.version"), null));
101            System.exit(0);
102        }
103    
104        /*
105         * Failure modes.
106         */
107        public static void bug(String key) {
108            bug(key, null);
109        }
110    
111        public static void bug(String key, Exception e) {
112            if (e != null)
113                e.printStackTrace();
114            System.err.println(getText(key));
115            System.err.println(getText("bug.report"));
116            System.exit(11);
117        }
118    
119        public static void error(String key) {
120            error(key, null);
121        }
122    
123        public static void error(String key, String a1) {
124            error(key, a1, null);
125        }
126    
127        public static void error(String key, String a1, String a2) {
128            error(key, a1, a2, false);
129        }
130    
131        public static void error(String key, String a1, String a2,
132                                 boolean showUsage) {
133            System.err.println("Error: " + getText(key, a1, a2));
134            if (showUsage)
135                usage(15);
136            System.exit(15);
137        }
138    
139    
140        private static void fatal(String msg) {
141            fatal(msg, null);
142        }
143    
144        private static void fatal(String msg, Exception e) {
145            if (e != null) {
146                e.printStackTrace();
147            }
148            System.err.println(msg);
149            System.exit(10);
150        }
151    
152        /*
153         * Support for platform specific things in javah, such as pragma
154         * directives, exported symbols etc.
155         */
156        static private ResourceBundle platform = null;
157    
158        /*
159         * Set when platform has been initialized.
160         */
161        static private boolean platformInit = false;
162    
163        static String getPlatformString(String key) {
164            if (!platformInit) {
165                initPlatform();
166                platformInit = true;
167            }
168            if (platform == null)
169                return null;
170            try {
171                return platform.getString(key);
172            } catch (MissingResourceException mre) {
173                return null;
174            }
175        }
176    
177        private static void initPlatform() {
178            String os = System.getProperty("os.name");
179            if (os.startsWith("Windows")) {
180                os = "win32";
181            } else if (os.indexOf("Linux") >= 0) {
182                os = "Linux";
183            } else if (os.indexOf("FreeBSD") >= 0) {
184                os = "FreeBSD";
185            } else if (os.indexOf("Darwin") >= 0) {
186                os = "Darwin";
187            } else if (os.indexOf("NetBSD") >= 0) {
188                os = "NetBSD";
189            } else if (os.indexOf("OpenBSD") >= 0) {
190                os = "OpenBSD";
191            }
192            String arch = System.getProperty("os.arch");
193            String resname = "com.sun.tools.javah.resources." + os + "_" + arch;
194            try {
195                platform=ResourceBundle.getBundle(resname);
196            } catch (MissingResourceException mre) {
197                // fatal("Error loading resources.  Please file a bug report.", mre);
198            }
199        }
200    }