aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/Tool.java
blob: e0f6a83baad8c7e5c218ce1721405b6cb372bd2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * @(#) Tool.java 
 *
 * @author 	Sven Goethel 
 */
 
package gl4java.utils;

import java.util.*;
import java.lang.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.lang.reflect.*;
import java.security.*;

public class Tool {

  public static final boolean DEBUG = false;

/**
  * A few Component methods for Java-VM access
  */

  public static final String getVersion()
  {
  	String version = null;

	try {
		version = java.lang.System.getProperty("java.version");
	}
	catch ( SecurityException s)
	{
		version=(s.getMessage()==null)?s.toString():s.getMessage();
		System.out.println(version);
	}

	return version;
  }

  public static final int getVersionMajor()
  {
  	String jvmVersion = getVersion();
	int jvmVersionMajor = 0;
	int i0 = 0;
	int i1 = jvmVersion.indexOf(".", i0);
        String strhlp = null;
	if(i1>0)
	{
	    strhlp = jvmVersion.substring(i0,i1);
	    try {
	      jvmVersionMajor = Integer.valueOf(strhlp).intValue();
	    } catch (Exception e) 
	    {System.out.println("Not a number: "+strhlp+" ("+jvmVersion+")");}
	}
	return jvmVersionMajor;
  }

  public static final int getVersionMinor()
  {
  	String jvmVersion = getVersion();
	int jvmVersionMinor = 0;
	int i0 = 0;
	int i1 = jvmVersion.indexOf(".", i0);
        String strhlp = null;
	i0 = i1+1;
	i1 = jvmVersion.indexOf(".", i0);
	if( i1 < 0 )
		i1 = jvmVersion.length(); // no 2nd dot, no bug version number

	if( 0<i0 && i0<i1 )
	{
	    strhlp = jvmVersion.substring(i0,i1);
	    try {
	      jvmVersionMinor = Integer.valueOf(strhlp).intValue();
	    } catch (Exception e) 
	    {System.out.println("Not a number: "+strhlp+" ("+jvmVersion+")");}
	}
	return jvmVersionMinor;
  }

  public static String getVendor()
  {
  	String vendor = null;

	try {
		vendor = java.lang.System.getProperty("java.vendor");
	}
	catch ( SecurityException s)
	{
		vendor=(s.getMessage()==null)?s.toString():s.getMessage();
		System.out.println(vendor);
	}

	return vendor;

  }

  public static String getOSName()
  {
  	String osname = null;

	try {
		osname = java.lang.System.getProperty("os.name");
	}
	catch ( SecurityException s)
	{
		osname=(s.getMessage()==null)?s.toString():s.getMessage();
		System.out.println(osname);
	}

	return osname;

  }

  public static boolean isNetscapesVM() 
  { String vendor=getVendor(); 
    return vendor!=null && vendor.indexOf("Netscape")>=0; }

  public static boolean isSunsVM() 
  { String vendor=getVendor(); 
    return vendor!=null && vendor.indexOf("Sun")>=0; }

  public static boolean isMicrosoftsVM() 
  { String vendor=getVendor(); 
    return vendor!=null && vendor.indexOf("Microsoft")>=0; }

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

/**
  * A few Component methods for easy awt-hierarchy querys
  */

  public static Point getAbsoluteCoord(Component co)
  {
	Object obj = co;
        Point absCoord = co.getLocation();
        Point p = null;

	while (obj instanceof Component) {
		Container cont = ((Component)obj).getParent();
		if( cont != null ) {
			p = cont.getLocation();
			absCoord.x+=p.x;
			absCoord.y+=p.y;
		}
		if( cont instanceof Window) {
			obj=null;
		} else obj=cont;
	}
	return absCoord;
  }	

    public  static Window getWindow(Component co)
    {
        Window f = null;
        Object obj = co;

        while (obj instanceof Component) {
                Container cont = ((Component)obj).getParent();
                if( cont instanceof Window) {
                        f=(Window)cont;
			obj=cont; // continue seeking for frame or dialog
                } else if( cont instanceof Frame) {
                        f=(Window)cont;
                        obj=null;
                } else if( cont instanceof Dialog) {
                        f=(Window)cont;
                        obj=null;
                } else obj=cont;
        }
        return f;
    }
          
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

/**
   * A few find methods to seek for Component(Shadow)s
  */


  public static boolean isInSameWindow(Component co1, Component co2)
  {	
  	if(co1==null || co2==null ) return false;

        Window f1 = getWindow(co1);
        Window f2 = getWindow(co2);

	if(f1!=null && f2!=null && f1.equals(f2))
		return true;

	return false;	
  }


/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

  
  public static final native long tmpfileCreate();
  public static final native boolean tmpfileClose(long filed);
  public static final native int tmpfileWrite(long filed, byte[] data, int bytes);
  public static final native int tmpfileRead(long filed, byte[] data, int bytes);
  public static final native void tmpfileRewind(long filed);

  public static final long copyResource2TempFile
  		(Object resourceOrigin, URL urlOrigin, String uri)
  {
		URL url = null;
		InputStream in = null;
		File destFile = null;
		String fname;
  		boolean isNetscapeJvm = isNetscapesVM(); 
		int jvmVersionMajor = getVersionMajor();
		int jvmVersionMinor = getVersionMinor();

		long filed = tmpfileCreate();
		if(filed==0) 
		{
		    System.err.println("GL4Java-Tool: can not open temporary file");
		    return 0;
	        }

		if(isNetscapeJvm)
		{
		  System.out.println("Netscape JVM try to get Privileges");
		  try {
			Class privmgr = 
				Class.forName("netscape.security.PrivilegeManager");
			Class[] parameterTypes = new Class[1];
			parameterTypes[0] = Class.forName("java.lang.String");
			Method m = privmgr.getMethod("enablePrivilege",parameterTypes);
			Object args[] = new Object[1];
			args[0] = (Object)(new String("UniversalPropertyRead"));
			m.invoke(privmgr,args);
			args[0] = (Object)(new String("UniversalConnect"));
			m.invoke(privmgr,args);
			args[0] = (Object)(new String("UniversalFdRead"));
			m.invoke(privmgr,args);
			System.out.println("Netscape-Privilege: enabled UniversalPropertyRead, UniversalConnect, UniversalFdRead priv.");
		  } catch (Exception ex) 
		  { 
			System.out.println("Not enabled Netscape-Privilege: UniversalPropertyRead, UniversalConnect, UniversalFdRead priv.");
		  }
		}

		if( jvmVersionMajor>=2 ||
		    (jvmVersionMajor==1 && jvmVersionMinor>=2)
		  )
		{
		   final String f_uri = uri;
		   final Object f_resourceOrigin = resourceOrigin;

		   in = (InputStream)
		     AccessController.doPrivileged(new PrivilegedAction() {
		      public Object run() 
		      {
		        String fname = null;
			InputStream in = null;

			if(f_resourceOrigin!=null)
			{
				fname = "fonts/"+f_uri;
				try
				{
				    in = f_resourceOrigin.getClass().getResourceAsStream(fname);
				    if(in!=null && DEBUG)
					    System.out.println("<found by resource>: "+f_uri);
				}
				catch (Exception ex) {
				    in  = null;
				    if(DEBUG)
				    {
					ex.printStackTrace();
				    }
				}
			}

			if(in==null)
			{
				fname = "gl4java/utils/glf/fonts/"+f_uri;
				try
				{
				    in = ClassLoader.getSystemResourceAsStream(fname);
				    if(in!=null && DEBUG)
					    System.out.println("<found by sys resource>: "+f_uri);
				}
				catch (Exception ex) {
				    in  = null;
				    if(DEBUG)
				    {
					ex.printStackTrace();
				    }
				}
			}
			return in;
		      }
	             });
	        } else {
			if(resourceOrigin!=null)
			{
				fname = "fonts/"+uri;
				try
				{
				    in = resourceOrigin.getClass().getResourceAsStream(fname);
				    if(in!=null && DEBUG)
					    System.out.println("<found by resource>: "+uri);
				}
				catch (Exception ex) {
				    url = null;
				    in  = null;
				    if(DEBUG)
				    {
					ex.printStackTrace();
				    }
				}
			}

			if(in==null)
			{
				fname = "gl4java/utils/glf/fonts/"+uri;
				try
				{
				    in = ClassLoader.getSystemResourceAsStream(fname);
				    if(in!=null && DEBUG)
					    System.out.println("<found by sys resource>: "+uri);
				}
				catch (Exception ex) {
				    url = null;
				    in  = null;
				    if(DEBUG)
				    {
					ex.printStackTrace();
				    }
				}
			}
		}

		if(in==null)
		{
			try
			{
			    if(urlOrigin!=null)
				    url = new URL(urlOrigin, uri);
			    else
				    url = new URL(uri);
			    if(url!=null && DEBUG)
			        System.out.println("<found by URL>: "+url);
			    if(url!=null)
				    in = url.openStream();
			}
			catch (Exception e) {
			    url = null;
			    if(DEBUG)
			    {
				e.printStackTrace();
			    }
			}
		}

	        boolean ok = true;
	        int bytesreadtotal=0, byteswrittentotal=0;

		if( in != null )
		{
			// Copy the data from the source to the destination.
			try
			{
			    byte[] buf = new byte[4096];
			    int bytesread, tmp;

			    while (ok && (bytesread = in.read(buf,0,buf.length)) >= 0)
			    {
				if (bytesread < 1) continue;
				bytesreadtotal+=bytesread;
			        // Write the data to the destination file.
  				if ( (tmp=tmpfileWrite(filed, buf, bytesread))
				     != bytesread ) 
				     ok = false;

			        byteswrittentotal+=tmp;
			    }
			}
			catch (Exception e0)
			{ ok = false;      // Should never happen!		
		          if(DEBUG) e0.printStackTrace();
			}

			if(ok) {
				tmpfileRewind(filed);
			} else {
				tmpfileClose(filed);
				filed=0;
				System.err.println("GL4Java-Tool: write temp file error ("+bytesreadtotal+"r / "+byteswrittentotal+" w)");
			}
			if(DEBUG)
			{
			   System.out.println("bytes read: "+bytesreadtotal+
			                      ", bytes written: "+
					      byteswrittentotal);
		        }
		} else ok = false;

		try {
			if (in != null) in.close();
		} catch (Exception e) {
			in = null;
		        if(DEBUG) e.printStackTrace();
		}

		if(in==null)
		{
		    System.err.println("GL4Java-Tool: could not find uri: "+uri);
		}

		if (in==null && DEBUG) 
		{
			String errMsg = "ERROR: could not get resource "+uri;
			System.out.println(errMsg);
			if(DEBUG)
			{
				System.out.println("given parameters:\n"+
					" ResObj:"+resourceOrigin+"\n"+
					" urlOrigin:"+urlOrigin+"\n"+
					" uri:"+uri+"\n");
				Exception e = new Exception();
				e.printStackTrace();
			}
		} 

		return filed;
	}

}