1 | /*
|
---|
2 | * Misc.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | package org.greenstone.util;
|
---|
21 |
|
---|
22 | import java.util.HashMap;
|
---|
23 | import java.util.Set;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.Iterator;
|
---|
26 | import java.util.Properties;
|
---|
27 | import java.io.InputStream;
|
---|
28 | import java.io.BufferedReader;
|
---|
29 | import java.io.InputStreamReader;
|
---|
30 | import java.io.IOException;
|
---|
31 | import java.net.HttpURLConnection;
|
---|
32 | import java.net.URL;
|
---|
33 | import java.net.URLConnection;
|
---|
34 |
|
---|
35 | /** contains miscellaneous functions */
|
---|
36 | public class Misc {
|
---|
37 |
|
---|
38 | // Can initialise static final vars on declaration or in static initialisation code block
|
---|
39 | // http://stackoverflow.com/questions/2339932/java-can-final-variables-be-initialized-in-static-initialization-block
|
---|
40 | // Initialise object member final vars on declaration or in constructors
|
---|
41 | public static final String NEWLINE;
|
---|
42 |
|
---|
43 | // Before Java 7, no System.lineSeparator() or System.getProperty("line.separator")
|
---|
44 | // And on local linux, am compiling with JDK 6, so need this.
|
---|
45 | // http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character
|
---|
46 |
|
---|
47 | static {
|
---|
48 | // http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime
|
---|
49 | // https://www.tutorialspoint.com/java/lang/package_getspecificationversion.htm
|
---|
50 |
|
---|
51 | double java_version = Double.parseDouble(System.getProperty("java.specification.version"));
|
---|
52 | if(java_version >= 1.7) { // https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
|
---|
53 | NEWLINE = System.getProperty("line.separator");
|
---|
54 | } else {
|
---|
55 | NEWLINE = isWindows() ? "\r\n" : "\n";
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static void printHash(HashMap map) {
|
---|
60 | Set entries = map.entrySet();
|
---|
61 | Iterator i = entries.iterator();
|
---|
62 | while (i.hasNext()) {
|
---|
63 | Map.Entry m = (Map.Entry)i.next();
|
---|
64 | String name = (String)m.getKey();
|
---|
65 | String value = (String)m.getValue();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Handy function to display the list of calling functions by
|
---|
72 | * printing out the stack trace even when you don't have an exception
|
---|
73 | */
|
---|
74 | static public void printStackTrace() {
|
---|
75 | // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
|
---|
76 |
|
---|
77 | //Thread.dumpStack(); // looks too much like an exception, though the newlines separating each function call is handy
|
---|
78 | //new Exception().printStackTrace(); // outputs in the format of an exception too
|
---|
79 | //System.err.println("\n@@@@ stacktrace:\n" + Arrays.toString(Thread.currentThread().getStackTrace()) + "\n"); // outputs all in one line
|
---|
80 |
|
---|
81 | System.err.println("\n@@@@ stacktrace:");
|
---|
82 | StackTraceElement[] els = new Throwable().getStackTrace(); // starts at index 1, which is this function
|
---|
83 | //StackTraceElement[] els = Thread.currentThread().getStackTrace(); starts at index 0, "java.lang.Thread.getStackTrace()"
|
---|
84 | for(StackTraceElement ste : els) {
|
---|
85 | System.err.println(" " + ste);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Handy function to display the parent of the calling function
|
---|
91 | * (the function that called the function that called printCaller())
|
---|
92 | */
|
---|
93 | static public void printCaller() {
|
---|
94 | int parent = 1;
|
---|
95 | // this function printCaller() itself adds another layer on the callstack since
|
---|
96 | // it calls the overloaded method, so need to add 1 more to parent
|
---|
97 | printCaller(parent++);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Handy function to display the nth ancestor of the calling function
|
---|
102 | * where ancestor=0 would be the calling function itself
|
---|
103 | */
|
---|
104 | static public void printCaller(int ancestor) {
|
---|
105 | // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
|
---|
106 |
|
---|
107 | // Thread.currentThread().getStackTrace() starts at index 0: "java.lang.Thread.getStackTrace()"
|
---|
108 | // index 1 will be this method (printCaller) and index 2 will be the calling function itself who wants
|
---|
109 | // to know who called it. So need to at least start at index 3 to get informative caller information
|
---|
110 |
|
---|
111 | StackTraceElement[] callstack = Thread.currentThread().getStackTrace();
|
---|
112 | StackTraceElement requestor = callstack[2]; // the calling function, the function that called this one
|
---|
113 | StackTraceElement caller_requested = callstack[ancestor+3]; // the function requested
|
---|
114 | System.err.println("@@@ Function " + requestor + " called by:\n "
|
---|
115 | + caller_requested + " at " + ancestor + " ancestors back");
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /** Method to determine if the host system is Windows based.
|
---|
120 | * @return a boolean which is true if the platform is Windows,
|
---|
121 | * false otherwise
|
---|
122 | */
|
---|
123 | public static boolean isWindows() {
|
---|
124 | Properties props = System.getProperties();
|
---|
125 | String os_name = props.getProperty("os.name","");
|
---|
126 | if(os_name.startsWith("Windows")) {
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 | return false;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static boolean isWindows9x() {
|
---|
133 | Properties props = System.getProperties();
|
---|
134 | String os_name = props.getProperty("os.name","");
|
---|
135 | if(os_name.startsWith("Windows") && os_name.indexOf("9") != -1) {
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Method to determine if the host system is MacOS based.
|
---|
142 | * @return a boolean which is true if the platform is MacOS, false otherwise
|
---|
143 | */
|
---|
144 | public static boolean isMac() {
|
---|
145 | Properties props = System.getProperties();
|
---|
146 | String os_name = props.getProperty("os.name","");
|
---|
147 | if(os_name.startsWith("Mac OS")) {
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public static String getGsdlOS() {
|
---|
154 | if (isWindows()) {
|
---|
155 | return "windows";
|
---|
156 | }
|
---|
157 | if (isMac()) {
|
---|
158 | return "darwin";
|
---|
159 | }
|
---|
160 | return "linux";
|
---|
161 | }
|
---|
162 |
|
---|
163 | public static boolean isBigEndian() {
|
---|
164 |
|
---|
165 | if (System.getProperty("sun.cpu.endian").equals("big")) {
|
---|
166 | return true;
|
---|
167 | }
|
---|
168 | return false;
|
---|
169 |
|
---|
170 | }
|
---|
171 |
|
---|
172 | public static BufferedReader makeHttpConnection(String url_string)
|
---|
173 | throws java.net.MalformedURLException, java.io.IOException {
|
---|
174 | BufferedReader reader = null;
|
---|
175 | URL url = new URL(url_string);
|
---|
176 | HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
---|
177 | InputStream input = connection.getInputStream();
|
---|
178 | reader = new BufferedReader(new InputStreamReader(input));
|
---|
179 | return reader;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static void main(String [] args) {
|
---|
183 | isBigEndian();
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|