1 29 package com.eaio.nativecall; 30 31 import java.io.IOException ; 32 33 import sun.misc.ServiceConfigurationError; 34 35 import com.eaio.util.lang.NativeLoader; 36 37 62 public abstract class NativeCall { 63 64 69 private int lastErrorCode; 70 71 78 private int functionHandle, moduleHandle; 79 80 85 private String function; 86 87 92 private String module; 93 94 97 private static native void initIDs(); 98 99 102 private static boolean initialized = false; 103 104 121 public static synchronized void init() 122 throws 123 IOException , 124 SecurityException , 125 UnsatisfiedLinkError , 126 ServiceConfigurationError, 127 UnsupportedOperationException { 128 if (!initialized) { 129 Verifiers.init(); 130 if (Verifiers.getInstance() == null) { 131 throw new UnsupportedOperationException (); 132 } 133 new NativeLoader("NativeCall").load(); 134 initIDs(); 135 initialized = true; 136 } 137 } 138 139 149 public NativeCall(String function) 150 throws IllegalArgumentException , NullPointerException { 151 this(Verifiers.getInstance().getDefaultModule(), function); 152 } 153 154 164 public NativeCall(String module, String function) 165 throws IllegalArgumentException , NullPointerException { 166 Verifier v = Verifiers.getInstance(); 167 this.function = v.verifyFunctionName(function); 168 this.module = v.verifyModuleName(module); 169 if (!initHandles()) { 170 if (lastErrorCode != 0) { 171 throw new IllegalArgumentException (getLastError()); 172 } 173 throw new IllegalArgumentException (); 174 } 175 } 176 177 183 private native boolean initHandles(); 184 185 192 public final int getLastErrorCode() { 193 return lastErrorCode; 194 } 195 196 203 public final native String getLastError(); 204 205 214 public native synchronized void destroy(); 215 216 225 protected void check(Object [] params) throws ClassCastException { 226 if (params == null) { 227 return; 228 } 229 for (int i = 0; i < params.length; ++i) { 230 checkParam(params[i]); 231 if (params[i] instanceof String ) { 232 params[i] = 233 Verifiers.getInstance().handleString( 234 (String ) params[i], 235 module, 236 function); 237 } 238 } 239 } 240 241 247 protected void checkParam(Object o) throws ClassCastException { 248 if (o == null 249 || o instanceof Boolean 250 || o instanceof Integer 251 || o instanceof byte[] 252 || o instanceof char[] 253 || o instanceof String ) { 254 return; 255 } 256 if (o instanceof Holder) { 257 checkParam(((Holder) o).get()); 258 return; 259 } 260 throw new ClassCastException (o.getClass().getName()); 261 } 262 263 272 public boolean equals(Object obj) { 273 if (this == obj) { 274 return true; 275 } 276 if (!(obj instanceof NativeCall)) { 277 return false; 278 } 279 if (!getClass().getName().equals(obj.getClass().getName())) { 280 return false; 281 } 282 NativeCall c = (NativeCall) obj; 283 return module.equals(c.module) && function.equals(c.function); 284 } 285 286 293 public int hashCode() { 294 int out = function.hashCode(); 295 out ^= module.hashCode(); 296 return out; 297 } 298 299 304 protected void finalize() throws Throwable { 305 try { 306 destroy(); 307 } 308 finally { 309 super.finalize(); 310 } 312 } 313 314 321 public final String toString() { 322 return toStringBuffer(null).toString(); 323 } 324 325 332 public StringBuffer toStringBuffer(StringBuffer in) { 333 if (in == null) { 334 in = new StringBuffer (64); 335 } 336 else { 337 in.ensureCapacity(in.length() + 64); 338 } 339 in.append("{ "); 340 int idx = getClass().getName().lastIndexOf("."); 341 if (idx > -1) { 342 in.append(getClass().getName().substring(++idx)); 343 } 344 else { 345 in.append(getClass().getName()); 346 } 347 in.append(": module = "); 348 in.append(module); 349 in.append(", function = "); 350 in.append(function); 351 in.append(" }"); 352 return in; 353 } 354 355 } 356 | Popular Tags |