KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecSystem


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

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 /** System codecs */
43
44 public class CodecSystem extends Codec
45 {
46     private static int _uniqueId = 0;
47     protected static final Map __lock = new HashMap();
48
49     /** waits on "object" for given wait time. If given "object" is a string, then an internal lock object (an object array of length 1) is used, and the signal object sent by notifyAll is returned. Otherwise, given object is used as a lock and returned as-is. */
50
51     public static Object JavaDoc wait(Object JavaDoc object, Object JavaDoc wait) throws Exception JavaDoc
52     {
53         Long JavaDoc longWait = TypeUtil.isLong(wait);
54         Object JavaDoc lock = object;
55         Object JavaDoc[] internalLock = null;
56
57         if (object instanceof String JavaDoc)
58         {
59             synchronized(__lock)
60             {
61                 lock = __lock.get(object);
62
63                 if (lock != null)
64                 {
65                     internalLock = (Object JavaDoc[])lock;
66                 }
67                 else
68                 {
69                     internalLock = new Object JavaDoc[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     /** notifies all threads waiting on a lock. "object" is used the same way as in wait(). If object is a string, then sends given signal back via internal lock. */
95
96     public static Object JavaDoc notifyAll(Object JavaDoc object, Object JavaDoc signal)
97     {
98         Object JavaDoc lock = object;
99         Object JavaDoc[] internalLock = null;
100
101         if (object instanceof String JavaDoc)
102         {
103             synchronized(__lock)
104             {
105                 lock = __lock.remove(object);
106
107                 internalLock = (Object JavaDoc[])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     /** sleeps for given number of milliseconds */
128
129     public static Object JavaDoc sleep(Object JavaDoc object) throws Exception JavaDoc
130     {
131         return CodecThread.sleep(object);
132     }
133
134     /** yields to other thread */
135
136     public static Object JavaDoc yield()
137     {
138         return CodecThread.yield();
139     }
140
141     /** invokes a java method */
142
143     public static Object JavaDoc invoke(Object JavaDoc mapObject) throws Exception JavaDoc
144     {
145         NestedMap map = new NestedMap(mapObject);
146
147         return TypeUtil.invoke(map);
148     }
149
150     /** invokes a java method on object */
151
152     public static Object JavaDoc invoke(Object JavaDoc object, Object JavaDoc mapObject) throws Exception JavaDoc
153     {
154         NestedMap map = new NestedMap(mapObject);
155
156         return TypeUtil.invoke(object, map);
157     }
158
159     /** obtain an unique ID */
160
161     public static synchronized Object JavaDoc uniqueId() throws Exception JavaDoc
162     {
163         return new Integer JavaDoc(_uniqueId ++);
164     }
165
166     /** obtain some runtime info */
167
168     public static Object JavaDoc runtime(Object JavaDoc object)
169     {
170         NestedMap map = TypeUtil.isNestedMap(object);
171
172         Runtime JavaDoc 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 JavaDoc haltValue = TypeUtil.isInteger(map.get("halt"));
185
186         if (haltValue != null)
187         {
188             runtime.halt(haltValue.intValue());
189         }
190
191         Integer JavaDoc exitValue = TypeUtil.isInteger(map.get("exit"));
192
193         if (exitValue != null)
194         {
195             runtime.exit(exitValue.intValue());
196         }
197
198         Boolean JavaDoc traceInstructionsValue = TypeUtil.isBoolean(map.get("traceInstruactions"));
199
200         if (traceInstructionsValue != null)
201         {
202             runtime.traceInstructions(traceInstructionsValue.booleanValue());
203         }
204
205         Boolean JavaDoc 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 JavaDoc(runtime.freeMemory()));
213         map.put("maxMemory", new Long JavaDoc(runtime.maxMemory()));
214         map.put("totalMemory", new Long JavaDoc(runtime.totalMemory()));
215         map.put("availableProcessors", new Long JavaDoc(runtime.availableProcessors()));
216         
217         return map;
218     }
219
220     public static Object JavaDoc runtime() throws Exception JavaDoc
221     {
222         return runtime(null);
223     }
224
225     /** set system property **/
226
227     public static Object JavaDoc setProperty(Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
228     {
229         return System.setProperty(key.toString(), value.toString());
230     }
231
232     /** set system properties - map version **/
233
234     public static Object JavaDoc setProperties(Object JavaDoc object) throws Exception JavaDoc
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     /** get system property **/
254
255     public static Object JavaDoc getProperty(Object JavaDoc key) throws Exception JavaDoc
256     {
257         return System.getProperty(key.toString());
258     }
259
260     /** get system properties - map version **/
261
262     public static Object JavaDoc getProperties() throws Exception JavaDoc
263     {
264         return TypeUtil.isNestedMap(System.getProperties()).exportParams();
265     }
266     
267     /** current system time in milliseconds */
268
269     public static Object JavaDoc currentTimeMillis() throws Exception JavaDoc
270     {
271         return new Long JavaDoc(System.currentTimeMillis());
272     }
273
274     /** current time as date */
275
276     public static Object JavaDoc currentTime() throws Exception JavaDoc
277     {
278         return new Date(System.currentTimeMillis());
279     }
280
281     /** creates a cache */
282
283     public static Object JavaDoc createCache(Object JavaDoc spec) throws Exception JavaDoc
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 JavaDoc 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     /** retrieves a static variable */
304
305     public static Object JavaDoc getStatic(Object JavaDoc classSpec, Object JavaDoc varSpec) throws Exception JavaDoc
306     {
307         Class JavaDoc classObject = null;
308
309         if (classSpec instanceof Class JavaDoc)
310         {
311             classObject = (Class JavaDoc)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 JavaDoc getStatic(Object JavaDoc spec) throws Exception JavaDoc
322     {
323         String JavaDoc specString = spec.toString();
324         int lastDotIndex = specString.lastIndexOf('.');
325         String JavaDoc classSpec = specString.substring(0, lastDotIndex);
326         String JavaDoc varSpec = specString.substring(lastDotIndex + 1);
327         return getStatic(classSpec, varSpec);
328     }
329 }
330
Popular Tags