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.
5 package org.ibex.nestedvm.util;
11 import java.text.DateFormatSymbols;
14 GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk11.<init>
15 GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk12.<init>
16 GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk13.<init>
17 GCCLASS_HINT: org.ibex.nestedvm.util.Platform.<clinit> org.ibex.nestedvm.util.Platform$Jdk14.<init>
20 public abstract class Platform {
22 private static final Platform p;
27 if(getProperty("java.vm.name").equals("SableVM"))
30 version = Float.valueOf(getProperty("java.specification.version")).floatValue();
31 } catch(Exception e) {
32 System.err.println("WARNING: " + e + " while trying to find jvm version - assuming 1.1");
36 if(version >= 1.4f) platformClass = "Jdk14";
37 else if(version >= 1.3f) platformClass = "Jdk13";
38 else if(version >= 1.2f) platformClass = "Jdk12";
39 else if(version >= 1.1f) platformClass = "Jdk11";
40 else throw new Error("JVM Specification version: " + version + " is too old. (see org.ibex.util.Platform to add support)");
43 p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
44 } catch(Exception e) {
46 throw new Error("Error instansiating platform class");
50 public static String getProperty(String key) {
52 return System.getProperty(key);
53 } catch(SecurityException e) {
59 abstract boolean _atomicCreateFile(File f) throws IOException;
60 public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
62 abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
63 public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
65 abstract void _socketSetKeepAlive(Socket s, boolean on) throws SocketException;
66 public static void socketSetKeepAlive(Socket s, boolean on) throws SocketException { p._socketSetKeepAlive(s,on); }
68 abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
69 public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
71 abstract String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l);
72 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); }
73 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); }
75 abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException;
76 public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); }
78 abstract File[] _listRoots();
79 public static File[] listRoots() { return p._listRoots(); }
81 abstract File _getRoot(File f);
82 public static File getRoot(File f) { return p._getRoot(f); }
84 static class Jdk11 extends Platform {
85 boolean _atomicCreateFile(File f) throws IOException {
86 // This is not atomic, but its the best we can do on jdk 1.1
87 if(f.exists()) return false;
88 new FileOutputStream(f).close();
91 void _socketHalfClose(Socket s, boolean output) throws IOException {
92 throw new IOException("half closing sockets not supported");
94 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
95 if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
96 return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
98 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
99 if(on) throw new SocketException("keepalive not supported");
101 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
102 String[][] zs = new DateFormatSymbols(l).getZoneStrings();
103 String id = tz.getID();
104 for(int i=0;i<zs.length;i++)
105 if(zs[i][0].equals(id))
106 return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
107 StringBuffer sb = new StringBuffer("GMT");
108 int off = tz.getRawOffset() / 1000;
109 if(off < 0) { sb.append("-"); off = -off; }
111 sb.append(off/3600); off = off%3600;
112 if(off > 0) sb.append(":").append(off/60); off=off%60;
113 if(off > 0) sb.append(":").append(off);
114 return sb.toString();
117 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
118 new FileOutputStream(f).close();
119 return new RandomAccessFile(f,mode);
122 File[] _listRoots() {
123 String[] rootProps = new String[]{"java.home","java.class.path","java.library.path","java.io.tmpdir","java.ext.dirs","user.home","user.dir" };
124 Hashtable known = new Hashtable();
125 for(int i=0;i<rootProps.length;i++) {
126 String prop = getProperty(rootProps[i]);
127 if(prop == null) continue;
131 if((p = prop.indexOf(File.pathSeparatorChar)) != -1) {
132 path = prop.substring(0,p);
133 prop = prop.substring(p+1);
135 File root = getRoot(new File(path));
136 //System.err.println(rootProps[i] + ": " + path + " -> " + root);
137 known.put(root,Boolean.TRUE);
141 File[] ret = new File[known.size()];
143 for(Enumeration e = known.keys();e.hasMoreElements();)
144 ret[i++] = (File) e.nextElement();
148 File _getRoot(File f) {
149 if(!f.isAbsolute()) f = new File(f.getAbsolutePath());
151 while((p = f.getParent()) != null) f = new File(p);
152 if(f.getPath().length() == 0) f = new File("/"); // work around a classpath bug
157 static class Jdk12 extends Jdk11 {
158 boolean _atomicCreateFile(File f) throws IOException {
159 return f.createNewFile();
162 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
163 return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
166 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
167 RandomAccessFile raf = new RandomAccessFile(f,mode);
172 File[] _listRoots() { return File.listRoots(); }
175 static class Jdk13 extends Jdk12 {
176 void _socketHalfClose(Socket s, boolean output) throws IOException {
177 if(output) s.shutdownOutput();
178 else s.shutdownInput();
181 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
186 static class Jdk14 extends Jdk13 {
187 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); }