4b50f188ba3ece79efe5be7d0336272449871521
[nestedvm.git] / src / org / ibex / nestedvm / util / Platform.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.nestedvm.util;
6
7 import java.io.*;
8 import java.nio.channels.*;
9 import java.net.*;
10 import java.util.*;
11
12 import java.text.DateFormatSymbols;
13
14 /*
15  GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk11.<init>
16  GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk12.<init>
17  GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk13.<init>
18  GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk14.<init>
19 */
20
21 public abstract class Platform {
22     Platform() { }
23     private static final Platform p;
24     
25     static {
26         float version;
27         try {
28             if(getProperty("java.vm.name").equals("SableVM"))
29                 version = 1.2f;
30             else
31                 version = Float.valueOf(getProperty("java.specification.version")).floatValue();
32         } catch(Exception e) {
33             System.err.println("WARNING: " + e + " while trying to find jvm version -  assuming 1.1");
34             version = 1.1f;
35         }
36         String platformClass;
37         if(version >= 1.4f) platformClass = "Jdk14";
38         else if(version >= 1.3f) platformClass = "Jdk13";
39         else if(version >= 1.2f) platformClass = "Jdk12";
40         else if(version >= 1.1f) platformClass = "Jdk11";
41         else throw new Error("JVM Specification version: " + version + " is too old. (see org.ibex.util.Platform to add support)");
42         
43         try {
44             p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
45         } catch(Exception e) {
46             e.printStackTrace();
47             throw new Error("Error instansiating platform class");
48         }
49     }
50     
51     public static String getProperty(String key) {
52         try {
53             return System.getProperty(key);
54         } catch(SecurityException e) {
55             return null;
56         }
57     }
58     
59     
60     abstract boolean _atomicCreateFile(File f) throws IOException;
61     public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
62
63     abstract Seekable.Lock _lockFile(Seekable s, RandomAccessFile raf, long pos, long size, boolean shared) throws IOException;
64     public static Seekable.Lock lockFile(Seekable s, RandomAccessFile raf, long pos, long size, boolean shared) throws IOException {
65         return p._lockFile(s, raf, pos, size, shared); }
66     
67     abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
68     public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
69     
70     abstract void _socketSetKeepAlive(Socket s, boolean on) throws SocketException;
71     public static void socketSetKeepAlive(Socket s, boolean on) throws SocketException { p._socketSetKeepAlive(s,on); }
72     
73     abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
74     public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
75     
76     abstract String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l);
77     public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); }
78     public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); }
79     
80     abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException;
81     public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); }
82     
83     abstract File[] _listRoots();
84     public static File[] listRoots() { return p._listRoots(); }
85     
86     abstract File _getRoot(File f);
87     public static File getRoot(File f) { return p._getRoot(f); }
88     
89     static class Jdk11 extends Platform {
90         boolean _atomicCreateFile(File f) throws IOException {
91             // This is not atomic, but its the best we can do on jdk 1.1
92             if(f.exists()) return false;
93             new FileOutputStream(f).close();
94             return true;
95         }
96         Seekable.Lock _lockFile(Seekable s, RandomAccessFile raf, long p, long size, boolean shared) throws IOException {
97             throw new IOException("file locking requires jdk 1.4+");
98         }
99         void _socketHalfClose(Socket s, boolean output) throws IOException {
100             throw new IOException("half closing sockets not supported");
101         }
102         InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
103             if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
104             return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
105         }
106         void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
107             if(on) throw new SocketException("keepalive not supported");
108         }
109         String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
110             String[][] zs  = new DateFormatSymbols(l).getZoneStrings();
111             String id = tz.getID();
112             for(int i=0;i<zs.length;i++)
113                 if(zs[i][0].equals(id))
114                     return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
115             StringBuffer sb = new StringBuffer("GMT");
116             int off = tz.getRawOffset() / 1000;
117             if(off < 0) { sb.append("-"); off = -off; }
118             else sb.append("+");
119             sb.append(off/3600); off = off%3600;
120             if(off > 0) sb.append(":").append(off/60); off=off%60;
121             if(off > 0) sb.append(":").append(off);
122             return sb.toString();
123         }
124         
125         RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
126             new FileOutputStream(f).close();
127             return new RandomAccessFile(f,mode);
128         }
129         
130         File[] _listRoots() {
131             String[] rootProps = new String[]{"java.home","java.class.path","java.library.path","java.io.tmpdir","java.ext.dirs","user.home","user.dir" };
132             Hashtable known = new Hashtable();
133             for(int i=0;i<rootProps.length;i++) {
134                 String prop = getProperty(rootProps[i]);
135                 if(prop == null) continue;
136                 for(;;) {
137                     String path = prop;
138                     int p;
139                     if((p = prop.indexOf(File.pathSeparatorChar)) != -1) {
140                         path = prop.substring(0,p);
141                         prop = prop.substring(p+1);
142                     }
143                     File root = getRoot(new File(path));
144                     //System.err.println(rootProps[i] + ": " + path + " -> " + root);
145                     known.put(root,Boolean.TRUE);
146                     if(p == -1) break;
147                 }
148             }
149             File[] ret = new File[known.size()];
150             int i=0;
151             for(Enumeration e = known.keys();e.hasMoreElements();)
152                 ret[i++] = (File) e.nextElement();
153             return ret;
154         }
155         
156         File _getRoot(File f) {
157             if(!f.isAbsolute()) f = new File(f.getAbsolutePath());
158             String p;
159             while((p = f.getParent()) != null) f = new File(p);
160             if(f.getPath().length() == 0) f = new File("/"); // work around a classpath bug
161             return f;
162         }
163     }
164     
165     static class Jdk12 extends Jdk11 {
166         boolean _atomicCreateFile(File f) throws IOException {
167             return f.createNewFile();
168         }
169         
170         String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
171             return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
172         }
173         
174         RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
175             RandomAccessFile raf = new RandomAccessFile(f,mode);
176             raf.setLength(0);
177             return raf;
178         }
179         
180         File[] _listRoots() { return File.listRoots(); }
181     }
182     
183     static class Jdk13 extends Jdk12 {
184         void _socketHalfClose(Socket s, boolean output) throws IOException {
185             if(output) s.shutdownOutput();
186             else s.shutdownInput();
187         }
188         
189         void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
190             s.setKeepAlive(on);
191         }
192     }
193     
194     static class Jdk14 extends Jdk13 {
195         InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); } 
196
197         Seekable.Lock _lockFile(Seekable s, RandomAccessFile r, long pos, long size, boolean shared) throws IOException {
198             FileLock flock;
199             try {
200                 flock = pos == 0 && size == 0 ? r.getChannel().lock() :
201                     r.getChannel().tryLock(pos, size, shared);
202             } catch (OverlappingFileLockException e) { flock = null; }
203             if (flock == null) return null; // region already locked
204             return new Jdk14FileLock(s, flock);
205         }
206     }
207
208     private static final class Jdk14FileLock extends Seekable.Lock {
209         private final Seekable s;
210         private final FileLock l;
211
212         Jdk14FileLock(Seekable sk, FileLock flock) { s = sk; l = flock; }
213         public Seekable seekable() { return s; }
214         public boolean isShared() { return l.isShared(); }
215         public boolean isValid() { return l.isValid(); }
216         public void release() throws IOException { l.release(); }
217         public long position() { return l.position(); }
218         public long size() { return l.size(); }
219         public String toString() { return l.toString(); }
220     }
221 }