001: /*
002: * $Id: RuntimeModule.java,v 1.52 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.runtime;
011:
012: import anvil.core.Any;
013: import anvil.core.AnyBinary;
014: import anvil.core.AnyString;
015: import anvil.core.Array;
016: import anvil.core.io.AnyOutputStream;
017: import anvil.Log;
018: import anvil.script.Context;
019: import anvil.script.Function;
020: import anvil.script.Module;
021: import anvil.script.StackFrame;
022: import anvil.java.lang.ThreadPool;
023: import java.io.ByteArrayOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.util.Locale;
027: import java.util.TimeZone;
028:
029: ///
030: /// @module anvil.runtime
031: /// Provides access to runtime related operations, such as
032: /// output redirection and type inspection.
033: public class RuntimeModule {
034:
035: public static final anvil.core.RuntimePermission CAN_LOG = new anvil.core.RuntimePermission(
036: "anvil.runtime.log", false);
037: public static final anvil.core.RuntimePermission CAN_LOGERROR = new anvil.core.RuntimePermission(
038: "anvil.runtime.logError", false);
039:
040: /// @function getNamespace
041: /// Returns namespace with given name, or null.
042: /// @synopsis Namespace getNamespace(string name)
043: public static final Any getNamespace(Context context, String name) {
044: return context.getNS(name);
045: }
046:
047: /// @function getLibraryRoot
048: /// Returns the root namespace of loaded native libraries.
049: /// @synopsis Type getLibraryRoot()
050: public static final Any getLibraryRoot(Context context) {
051: return new AnyType(context.getModules());
052: }
053:
054: /// @function write
055: /// Writes (prints) data to output.
056: /// @synopsis void write(binary binary, int offset, int length)
057: /// @synopsis void write(object text)
058: public static final Object[] p_write = { null, "value", "*offset",
059: new Integer(0), "*length", null };
060:
061: public static final Any write(Context context, Any value,
062: int offset, Any alength) {
063: switch (value.typeOf()) {
064: case Any.IS_NULL:
065: case Any.IS_UNDEFINED:
066: break;
067:
068: case Any.IS_BINARY:
069: byte[] array = value.toBinary();
070: int size = value.sizeOf();
071:
072: if (offset < 0) {
073: offset = 0;
074: } else if (offset >= size) {
075: break;
076: }
077:
078: if (alength == null) {
079: context.print(array, offset, size - offset);
080: } else {
081: int length = alength.toInt();
082: if (length <= 0) {
083: break;
084: } else if (offset + length > size) {
085: length = size - offset;
086: }
087: context.print(array, offset, length);
088: }
089: break;
090:
091: default:
092: context.print(value.toString());
093: }
094: return Any.TRUE;
095: }
096:
097: /// @function push
098: /// Sets the execution output stream.
099: /// @synopsis void push()
100: /// @synopsis void push(OutputStream output)
101: /// @param output Output stream where to direct the output, if
102: /// omitted memory stream is used
103: public static final Object[] p_push = { null, "*stream", null };
104:
105: public static final Any push(Context context, Any stream) {
106: OutputStream output = null;
107: if (stream != null) {
108: if (stream instanceof AnyOutputStream) {
109: output = (OutputStream) stream.toObject();
110: } else {
111: throw context.BadParameter("OutputStream expected");
112: }
113: } else {
114: output = new ByteArrayOutputStream();
115: }
116: context.pushOutputStream(output);
117: return Any.TRUE;
118: }
119:
120: /// @function pop
121: /// Removes previous execution output stream.
122: /// @synopsis void pop()
123: /// @throws IOError if an IO error occred
124: public static final Any pop(Context context) {
125: try {
126: OutputStream o = context.popOutputStream();
127: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
128: o.close();
129: }
130: return Any.TRUE;
131: } catch (IOException e) {
132: throw context.exception(e);
133: }
134: }
135:
136: /// @function getContentLength
137: /// Returns the content length in memory output stream.
138: /// @synopsis int getContentLength()
139: public static final Any getContentLength(Context context) {
140: OutputStream o = context.getOutputStream();
141: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
142: return Any.create(((ByteArrayOutputStream) o).size());
143: } else {
144: return Any.NULL;
145: }
146: }
147:
148: /// @function getContent
149: /// Returns the content from memory output stream.
150: /// @synopsis string getContent()
151: public static final Any getContent(Context context) {
152: OutputStream o = context.getOutputStream();
153: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
154: return new AnyString(((ByteArrayOutputStream) o).toString());
155: } else {
156: return Any.NULL;
157: }
158: }
159:
160: /// @function getBinaryContent
161: /// Returns the content from memory output stream.
162: /// @synopsis binary getBinaryContent()
163: public static final Any getBinaryContent(Context context) {
164: OutputStream o = context.getOutputStream();
165: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
166: return new AnyBinary(((ByteArrayOutputStream) o)
167: .toByteArray());
168: } else {
169: return Any.NULL;
170: }
171: }
172:
173: /// @function getOutput
174: /// Returns the output stream to write to.
175: /// @synopsis OutputStream getOutput()
176: public static final Any getOutput(Context context) {
177: OutputStream o = context.getOutputStream();
178: if (o != null) {
179: return new anvil.core.io.AnyOutputStream(o);
180: } else {
181: return Any.NULL;
182: }
183: }
184:
185: /// @function reset
186: /// Clears the content in memory output stream.
187: /// @synopsis void reset()
188: public static final Any reset(Context context) {
189: OutputStream o = context.getOutputStream();
190: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
191: ((ByteArrayOutputStream) o).reset();
192: return Any.TRUE;
193: } else {
194: return Any.FALSE;
195: }
196: }
197:
198: /// @function flush
199: /// Writes the of memory output stream to previous output stream in stack.
200: /// @synopsis void flush()
201: /// @throws IOError if an IO error occred
202: public static final Any flush(Context context) {
203: OutputStream o = context.getOutputStream();
204: if ((o != null) && (o instanceof ByteArrayOutputStream)) {
205: OutputStream p = context.peekOutputStream();
206: try {
207: ((ByteArrayOutputStream) o).writeTo(p);
208: return Any.TRUE;
209: } catch (IOException e) {
210: throw context.exception(e);
211: }
212: } else {
213: return Any.FALSE;
214: }
215: }
216:
217: /// @function log
218: /// Logs messages.
219: /// @synopsis void log(messages...)
220: public static final Any log(Context context, Any[] messages) {
221: context.checkAccess(CAN_LOG);
222: if (messages != null) {
223: StringBuffer buffer = new StringBuffer();
224: int n = messages.length;
225: for (int i = 0; i < n; i++) {
226: buffer.append(messages[i].toString());
227: }
228: context.log().info(buffer.toString());
229: }
230: return Any.TRUE;
231: }
232:
233: /// @function logError
234: /// Logs error messages.
235: /// @synopsis void logError(messages...)
236: public static final Any logError(Context context, Any[] messages) {
237: context.checkAccess(CAN_LOGERROR);
238: if (messages != null) {
239: StringBuffer buffer = new StringBuffer();
240: int n = messages.length;
241: for (int i = 0; i < n; i++) {
242: buffer.append(messages[i].toString());
243: }
244: context.log().error(buffer.toString());
245: }
246: return Any.TRUE;
247: }
248:
249: /// @function getLanguage
250: /// Returns the language setting.
251: /// This setting is the default language for
252: /// operations needing language, but didn't
253: /// receive language explicitly.
254: /// @synopsis string getLanguage()
255: public static final Any getLanguage(Context context) {
256: return Any.create(context.getLanguage());
257: }
258:
259: /// @function setLanguage
260: /// Locale: sets the language.
261: /// @synopsis void setLanguage(string language)
262: public static final Any setLanguage(Context context, String language) {
263: context.setLanguage(language);
264: return Any.TRUE;
265: }
266:
267: /// @function setLocale
268: /// Locale: sets the locale.
269: /// This setting is the default locale for
270: /// operations needing it, but didn't
271: /// receive locale explicitly.
272: /// @synopsis void setLocale(string locale)
273: public static final Any setLocale(Context context, String locale) {
274: context.setLocale(new Locale(context.getLanguage(), locale));
275: return Any.TRUE;
276: }
277:
278: /// @function setTimeZone
279: /// Sets the default timezone.
280: /// @synopsis void setTimeZone(string timezone)
281: public static final Any setTimeZone(Context context, String timezone) {
282: context.setTimeZone(TimeZone.getTimeZone(timezone));
283: return Any.TRUE;
284: }
285:
286: /// @function sleep
287: /// Ceases the execution of current thread for given amount of milliseconds.
288: /// @synopsis void sleep(int millis)
289: /// @throws Interrupted If sleeping was interrupted
290: public static final Any sleep(Context context, int millis) {
291: try {
292: if (millis < 0) {
293: millis = 0;
294: }
295: Thread.sleep(millis);
296: } catch (InterruptedException e) {
297: throw context.Interrupted(e.getMessage());
298: }
299: return Any.TRUE;
300: }
301:
302: /// @function cease
303: /// Temporarily ceases the execution of current thread so that
304: /// other can execute.
305: /// @synopsis void cease()
306: public static final Any cease() {
307: Thread.yield();
308: return Any.TRUE;
309: }
310:
311: /// @function setTracing
312: /// Enables or disables stack tracing. If enabled
313: /// will output notifications on log
314: /// when functions are entered or leaved.
315: /// @synopsis void setTracing(boolean enabled)
316: public static final Object[] p_setTracing = { null, "enable" };
317:
318: public static final Any setTracing(Context context, boolean enable) {
319: if (enable) {
320: context.enableTracing();
321: } else {
322: context.disableTracing();
323: }
324: return Any.NULL;
325: }
326:
327: /// @function enableTracing
328: /// Enables stack tracing.
329: /// @synopsis void enableTracing()
330: public static final Any enableTracing(Context context) {
331: context.enableTracing();
332: return Any.NULL;
333: }
334:
335: /// @function disableTracing
336: /// Disables stack tracing.
337: /// @synopsis void disableTracing()
338: public static final Any disableTracing(Context context) {
339: context.disableTracing();
340: return Any.NULL;
341: }
342:
343: public static final anvil.script.compiler.NativeNamespace __module__ = new anvil.script.compiler.NativeNamespace(
344: "runtime", RuntimeModule.class,
345: new Class[] { anvil.core.runtime.stack.StackModule.class,
346: AnyFunction.class, AnyType.class, AnyDoc.class,
347: AnyScope.class, AnyNamespace.class,
348: AnyPermission.class, AnyStackTraceElement.class,
349: AnyThread.class, AnyThreadPool.class, },
350: //DOC{{
351: ""
352: + "\n"
353: + " @module anvil.runtime\n"
354: + " Provides access to runtime related operations, such as \n"
355: + " output redirection and type inspection.\n"
356: + " @function getNamespace\n"
357: + " Returns namespace with given name, or null.\n"
358: + " @synopsis Namespace getNamespace(string name)\n"
359: + " @function getLibraryRoot\n"
360: + " Returns the root namespace of loaded native libraries.\n"
361: + " @synopsis Type getLibraryRoot()\n"
362: + " @function write\n"
363: + " Writes (prints) data to output.\n"
364: + " @synopsis void write(binary binary, int offset, int length)\n"
365: + " @synopsis void write(object text)\n"
366: + " @function push\n"
367: + " Sets the execution output stream.\n"
368: + " @synopsis void push()\n"
369: + " @synopsis void push(OutputStream output) \n"
370: + " @param output Output stream where to direct the output, if\n"
371: + " omitted memory stream is used\n"
372: + " @function pop\n"
373: + " Removes previous execution output stream.\n"
374: + " @synopsis void pop()\n"
375: + " @function getContentLength\n"
376: + " Returns the content length in memory output stream.\n"
377: + " @synopsis int getContentLength()\n"
378: + " @function getContent\n"
379: + " Returns the content from memory output stream.\n"
380: + " @synopsis string getContent()\n"
381: + " @function getBinaryContent\n"
382: + " Returns the content from memory output stream.\n"
383: + " @synopsis binary getBinaryContent()\n"
384: + " @function getOutput\n"
385: + " Returns the output stream to write to.\n"
386: + " @synopsis OutputStream getOutput()\n"
387: + " @function reset\n"
388: + " Clears the content in memory output stream.\n"
389: + " @synopsis void reset()\n"
390: + " @function flush\n"
391: + " Writes the of memory output stream to previous output stream in stack.\n"
392: + " @synopsis void flush()\n"
393: + " @function log\n"
394: + " Logs messages.\n"
395: + " @synopsis void log(messages...)\n"
396: + " @function logError\n"
397: + " Logs error messages.\n"
398: + " @synopsis void logError(messages...)\n"
399: + " @function getLanguage\n"
400: + " Returns the language setting.\n"
401: + " This setting is the default language for\n"
402: + " operations needing language, but didn't \n"
403: + " receive language explicitly.\n"
404: + " @synopsis string getLanguage()\n"
405: + " @function setLanguage\n"
406: + " Locale: sets the language.\n"
407: + " @synopsis void setLanguage(string language)\n"
408: + " @function setLocale\n"
409: + " Locale: sets the locale.\n"
410: + " This setting is the default locale for\n"
411: + " operations needing it, but didn't \n"
412: + " receive locale explicitly.\n"
413: + " @synopsis void setLocale(string locale)\n"
414: + " @function setTimeZone\n"
415: + " Sets the default timezone.\n"
416: + " @synopsis void setTimeZone(string timezone)\n"
417: + " @function sleep\n"
418: + " Ceases the execution of current thread for given amount of milliseconds.\n"
419: + " @synopsis void sleep(int millis)\n"
420: + " @throws Interrupted If sleeping was interrupted\n"
421: + " @function cease\n"
422: + " Temporarily ceases the execution of current thread so that\n"
423: + " other can execute.\n"
424: + " @synopsis void cease()\n"
425: + " @function setTracing\n"
426: + " Enables or disables stack tracing. If enabled\n"
427: + " will output notifications on log\n"
428: + " when functions are entered or leaved.\n"
429: + " @synopsis void setTracing(boolean enabled)\n"
430: + " @function enableTracing\n"
431: + " Enables stack tracing.\n"
432: + " @synopsis void enableTracing()\n"
433: + " @function disableTracing\n"
434: + " Disables stack tracing.\n"
435: + " @synopsis void disableTracing()\n"
436: //}}DOC
437: );
438:
439: }
|