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;
8 import java.nio.channels.*;
12 import java.text.DateFormatSymbols;
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>
21 public abstract class Platform {
23 private static final Platform p;
28 if(getProperty("java.vm.name").equals("SableVM"))
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");
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)");
44 p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
45 } catch(Exception e) {
47 throw new Error("Error instansiating platform class");
51 public static String getProperty(String key) {
53 return System.getProperty(key);
54 } catch(SecurityException e) {
60 abstract boolean _atomicCreateFile(File f) throws IOException;
61 public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
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); }
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); }
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); }
73 abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
74 public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
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()); }
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); }
83 abstract File[] _listRoots();
84 public static File[] listRoots() { return p._listRoots(); }
86 abstract File _getRoot(File f);
87 public static File getRoot(File f) { return p._getRoot(f); }
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();
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+");
99 void _socketHalfClose(Socket s, boolean output) throws IOException {
100 throw new IOException("half closing sockets not supported");
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));
106 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
107 if(on) throw new SocketException("keepalive not supported");
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; }
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();
125 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
126 new FileOutputStream(f).close();
127 return new RandomAccessFile(f,mode);
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;
139 if((p = prop.indexOf(File.pathSeparatorChar)) != -1) {
140 path = prop.substring(0,p);
141 prop = prop.substring(p+1);
143 File root = getRoot(new File(path));
144 //System.err.println(rootProps[i] + ": " + path + " -> " + root);
145 known.put(root,Boolean.TRUE);
149 File[] ret = new File[known.size()];
151 for(Enumeration e = known.keys();e.hasMoreElements();)
152 ret[i++] = (File) e.nextElement();
156 File _getRoot(File f) {
157 if(!f.isAbsolute()) f = new File(f.getAbsolutePath());
159 while((p = f.getParent()) != null) f = new File(p);
160 if(f.getPath().length() == 0) f = new File("/"); // work around a classpath bug
165 static class Jdk12 extends Jdk11 {
166 boolean _atomicCreateFile(File f) throws IOException {
167 return f.createNewFile();
170 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
171 return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
174 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
175 RandomAccessFile raf = new RandomAccessFile(f,mode);
180 File[] _listRoots() { return File.listRoots(); }
183 static class Jdk13 extends Jdk12 {
184 void _socketHalfClose(Socket s, boolean output) throws IOException {
185 if(output) s.shutdownOutput();
186 else s.shutdownInput();
189 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
194 static class Jdk14 extends Jdk13 {
195 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); }
197 Seekable.Lock _lockFile(Seekable s, RandomAccessFile r, long pos, long size, boolean shared) throws IOException {
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);
208 private static final class Jdk14FileLock implements Seekable.Lock {
209 private final Seekable s;
210 private final FileLock l;
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(); }