1 34 35 36 package com.micronova.util.codec; 37 38 import java.util.*; 39 import java.util.regex.*; 40 import com.micronova.util.*; 41 42 43 44 public class CodecSystem extends Codec 45 { 46 private static int _uniqueId = 0; 47 protected static final Map __lock = new HashMap(); 48 49 50 51 public static Object wait(Object object, Object wait) throws Exception 52 { 53 Long longWait = TypeUtil.isLong(wait); 54 Object lock = object; 55 Object [] internalLock = null; 56 57 if (object instanceof String ) 58 { 59 synchronized(__lock) 60 { 61 lock = __lock.get(object); 62 63 if (lock != null) 64 { 65 internalLock = (Object [])lock; 66 } 67 else 68 { 69 internalLock = new Object [1]; 70 lock = internalLock; 71 __lock.put(object, lock); 72 } 73 } 74 } 75 76 if (lock != null) 77 { 78 long waitTime = (longWait != null) ? (longWait.longValue()) : 0L; 79 80 synchronized(lock) 81 { 82 lock.wait(waitTime); 83 84 if (internalLock != null) 85 { 86 object = internalLock[0]; 87 } 88 } 89 } 90 91 return object; 92 } 93 94 95 96 public static Object notifyAll(Object object, Object signal) 97 { 98 Object lock = object; 99 Object [] internalLock = null; 100 101 if (object instanceof String ) 102 { 103 synchronized(__lock) 104 { 105 lock = __lock.remove(object); 106 107 internalLock = (Object [])lock; 108 } 109 } 110 111 if (lock != null) 112 { 113 synchronized(lock) 114 { 115 if (internalLock != null) 116 { 117 internalLock[0] = signal; 118 } 119 120 lock.notifyAll(); 121 } 122 } 123 124 return object; 125 } 126 127 128 129 public static Object sleep(Object object) throws Exception 130 { 131 return CodecThread.sleep(object); 132 } 133 134 135 136 public static Object yield() 137 { 138 return CodecThread.yield(); 139 } 140 141 142 143 public static Object invoke(Object mapObject) throws Exception 144 { 145 NestedMap map = new NestedMap(mapObject); 146 147 return TypeUtil.invoke(map); 148 } 149 150 151 152 public static Object invoke(Object object, Object mapObject) throws Exception 153 { 154 NestedMap map = new NestedMap(mapObject); 155 156 return TypeUtil.invoke(object, map); 157 } 158 159 160 161 public static synchronized Object uniqueId() throws Exception 162 { 163 return new Integer (_uniqueId ++); 164 } 165 166 167 168 public static Object runtime(Object object) 169 { 170 NestedMap map = TypeUtil.isNestedMap(object); 171 172 Runtime runtime = Runtime.getRuntime(); 173 174 if (map.get("gc") != null) 175 { 176 runtime.gc(); 177 } 178 179 if (map.get("runFinalization") != null) 180 { 181 runtime.runFinalization(); 182 } 183 184 Integer haltValue = TypeUtil.isInteger(map.get("halt")); 185 186 if (haltValue != null) 187 { 188 runtime.halt(haltValue.intValue()); 189 } 190 191 Integer exitValue = TypeUtil.isInteger(map.get("exit")); 192 193 if (exitValue != null) 194 { 195 runtime.exit(exitValue.intValue()); 196 } 197 198 Boolean traceInstructionsValue = TypeUtil.isBoolean(map.get("traceInstruactions")); 199 200 if (traceInstructionsValue != null) 201 { 202 runtime.traceInstructions(traceInstructionsValue.booleanValue()); 203 } 204 205 Boolean traceMethodCallsValue = TypeUtil.isBoolean(map.get("traceMethodCalls")); 206 207 if (traceMethodCallsValue != null) 208 { 209 runtime.traceMethodCalls(traceMethodCallsValue.booleanValue()); 210 } 211 212 map.put("freeMemory", new Long (runtime.freeMemory())); 213 map.put("maxMemory", new Long (runtime.maxMemory())); 214 map.put("totalMemory", new Long (runtime.totalMemory())); 215 map.put("availableProcessors", new Long (runtime.availableProcessors())); 216 217 return map; 218 } 219 220 public static Object runtime() throws Exception 221 { 222 return runtime(null); 223 } 224 225 226 227 public static Object setProperty(Object key, Object value) throws Exception 228 { 229 return System.setProperty(key.toString(), value.toString()); 230 } 231 232 233 234 public static Object setProperties(Object object) throws Exception 235 { 236 if (object != null) 237 { 238 NestedMap map = TypeUtil.isNestedMap(object); 239 240 Iterator iterator = map.exportParams().entrySet().iterator(); 241 242 while (iterator.hasNext()) 243 { 244 Map.Entry entry = (Map.Entry)iterator.next(); 245 246 System.setProperty(entry.getKey().toString(), entry.getValue().toString()); 247 } 248 } 249 250 return object; 251 } 252 253 254 255 public static Object getProperty(Object key) throws Exception 256 { 257 return System.getProperty(key.toString()); 258 } 259 260 261 262 public static Object getProperties() throws Exception 263 { 264 return TypeUtil.isNestedMap(System.getProperties()).exportParams(); 265 } 266 267 268 269 public static Object currentTimeMillis() throws Exception 270 { 271 return new Long (System.currentTimeMillis()); 272 } 273 274 275 276 public static Object currentTime() throws Exception 277 { 278 return new Date(System.currentTimeMillis()); 279 } 280 281 282 283 public static Object createCache(Object spec) throws Exception 284 { 285 NestedMap specMap = new NestedMap(spec); 286 287 int initialCapacity = specMap.getInt("initialCapacity", 16); 288 float loadFactor = specMap.getFloat("loadFactor", 0.75f); 289 String type = specMap.getString("type", "LRU"); 290 int maxSize = specMap.getInt("maxSize", 16); 291 long lifespan = specMap.getLong("lifespan", -1L); 292 293 if (lifespan < 0) 294 { 295 return new Cache(initialCapacity, loadFactor, (type.equals("LRU")), maxSize); 296 } 297 else 298 { 299 return new TimedCache(initialCapacity, loadFactor, (type.equals("LRU")), maxSize, lifespan); 300 } 301 } 302 303 304 305 public static Object getStatic(Object classSpec, Object varSpec) throws Exception 306 { 307 Class classObject = null; 308 309 if (classSpec instanceof Class ) 310 { 311 classObject = (Class )classSpec; 312 } 313 else 314 { 315 classObject = Class.forName(classSpec.toString()); 316 } 317 318 return TypeUtil.invoke(null, classObject, "." + varSpec.toString(), null, null); 319 } 320 321 public static Object getStatic(Object spec) throws Exception 322 { 323 String specString = spec.toString(); 324 int lastDotIndex = specString.lastIndexOf('.'); 325 String classSpec = specString.substring(0, lastDotIndex); 326 String varSpec = specString.substring(lastDotIndex + 1); 327 return getStatic(classSpec, varSpec); 328 } 329 } 330 | Popular Tags |