Package anvil.core.runtime

Source Code of anvil.core.runtime.RuntimeModule

/*
* $Id: RuntimeModule.java,v 1.52 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime;

import anvil.core.Any;
import anvil.core.AnyBinary;
import anvil.core.AnyString;
import anvil.core.Array;
import anvil.core.io.AnyOutputStream;
import anvil.Log;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.Module;
import anvil.script.StackFrame;
import anvil.java.lang.ThreadPool;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.TimeZone;

///
/// @module anvil.runtime
/// Provides access to runtime related operations, such as
/// output redirection and type inspection.
public class RuntimeModule
{

  public static final anvil.core.RuntimePermission CAN_LOG = new anvil.core.RuntimePermission("anvil.runtime.log", false);
  public static final anvil.core.RuntimePermission CAN_LOGERROR = new anvil.core.RuntimePermission("anvil.runtime.logError", false);


  /// @function getNamespace
  /// Returns namespace with given name, or null.
  /// @synopsis Namespace getNamespace(string name)
  public static final Any getNamespace(Context context, String name)
  {
    return context.getNS(name);
  }


  /// @function getLibraryRoot
  /// Returns the root namespace of loaded native libraries.
  /// @synopsis Type getLibraryRoot()
  public static final Any getLibraryRoot(Context context)
  {
    return new AnyType(context.getModules());
  }


  /// @function write
  /// Writes (prints) data to output.
  /// @synopsis void write(binary binary, int offset, int length)
  /// @synopsis void write(object text)
  public static final Object[] p_write = { null, "value", "*offset", new Integer(0), "*length", null };
  public static final Any write(Context context, Any value, int offset, Any alength)
  {
    switch(value.typeOf()) {
    case Any.IS_NULL:
    case Any.IS_UNDEFINED:
      break;
     
    case Any.IS_BINARY:
      byte[] array = value.toBinary();
      int size = value.sizeOf();

      if (offset < 0) {
        offset = 0;
      } else if (offset >= size) {
        break;
      }

      if (alength == null) {
        context.print(array, offset, size - offset);
      } else {
        int length = alength.toInt();
        if (length <= 0) {
          break;
        } else if (offset + length > size) {
          length = size - offset;
        }
        context.print(array, offset, length);
      }
      break;
     
    default:
      context.print(value.toString());
    }
    return Any.TRUE;
  }


  /// @function push
  /// Sets the execution output stream.
  /// @synopsis void push()
  /// @synopsis void push(OutputStream output)
  /// @param output Output stream where to direct the output, if
  /// omitted memory stream is used
  public static final Object[] p_push = { null, "*stream", null };
  public static final Any push(Context context, Any stream)
  {
    OutputStream output = null;
    if (stream != null) {
      if (stream instanceof AnyOutputStream) {
        output = (OutputStream)stream.toObject();
      } else {
        throw context.BadParameter("OutputStream expected");
      }
    } else {
      output = new ByteArrayOutputStream();
    }
    context.pushOutputStream(output);
    return Any.TRUE;
  }


  /// @function pop
  /// Removes previous execution output stream.
  /// @synopsis void pop()
  /// @throws IOError if an IO error occred
  public static final Any pop(Context context)
  {
    try {
      OutputStream o = context.popOutputStream();
      if ((o != null) && (o instanceof ByteArrayOutputStream)) {
        o.close();
      }
      return Any.TRUE;
    } catch (IOException e) {
      throw context.exception(e);
    }
  }


  /// @function getContentLength
  /// Returns the content length in memory output stream.
  /// @synopsis int getContentLength()
  public static final Any getContentLength(Context context)
  {
    OutputStream o = context.getOutputStream();
    if ((o != null) && (o instanceof ByteArrayOutputStream)) {
      return Any.create(((ByteArrayOutputStream)o).size());
    } else {
      return Any.NULL;
    }
  }


  /// @function getContent
  /// Returns the content from memory output stream.
  /// @synopsis string getContent()
  public static final Any getContent(Context context)
  {
    OutputStream o = context.getOutputStream();
    if ((o != null) && (o instanceof ByteArrayOutputStream)) {
      return new AnyString(((ByteArrayOutputStream)o).toString());
    } else {
      return Any.NULL;
   
  }


  /// @function getBinaryContent
  /// Returns the content from memory output stream.
  /// @synopsis binary getBinaryContent()
  public static final Any getBinaryContent(Context context)
  {
    OutputStream o = context.getOutputStream();
    if ((o != null) && (o instanceof ByteArrayOutputStream)) {
      return new AnyBinary(((ByteArrayOutputStream)o).toByteArray());
    } else {
      return Any.NULL;
   
  }


  /// @function getOutput
  /// Returns the output stream to write to.
  /// @synopsis OutputStream getOutput()
  public static final Any getOutput(Context context)
  {
    OutputStream o = context.getOutputStream();
    if (o != null) {
      return new anvil.core.io.AnyOutputStream(o);
    } else {
      return Any.NULL;
   
  }

  /// @function reset
  /// Clears the content in memory output stream.
  /// @synopsis void reset()
  public static final Any reset(Context context)
  {
    OutputStream o = context.getOutputStream();
    if ((o != null) && (o instanceof ByteArrayOutputStream)) {
      ((ByteArrayOutputStream)o).reset();
      return Any.TRUE;
    } else {
      return Any.FALSE;
   
  }
 

  /// @function flush
  /// Writes the of memory output stream to previous output stream in stack.
  /// @synopsis void flush()
  /// @throws IOError if an IO error occred
  public static final Any flush(Context context)
  {
    OutputStream o = context.getOutputStream();
    if ((o != null) && (o instanceof ByteArrayOutputStream)) {
      OutputStream p = context.peekOutputStream();
      try {
        ((ByteArrayOutputStream)o).writeTo(p);
        return Any.TRUE;
      } catch (IOException e) {
        throw context.exception(e);
      }
    } else {
      return Any.FALSE;
   
  }


  /// @function log
  /// Logs messages.
  /// @synopsis void log(messages...)
  public static final Any log(Context context, Any[] messages)
  {
    context.checkAccess(CAN_LOG);
    if (messages != null) {
      StringBuffer buffer = new StringBuffer();
      int n = messages.length;
      for(int i=0; i<n; i++) {
        buffer.append(messages[i].toString());
      }
      context.log().info(buffer.toString());
    }
    return Any.TRUE;
  }


  /// @function logError
  /// Logs error messages.
  /// @synopsis void logError(messages...)
  public static final Any logError(Context context, Any[] messages)
  {
    context.checkAccess(CAN_LOGERROR);
    if (messages != null) {
      StringBuffer buffer = new StringBuffer();
      int n = messages.length;
      for(int i=0; i<n; i++) {
        buffer.append(messages[i].toString());
      }
      context.log().error(buffer.toString());
    }
    return Any.TRUE;
  }


  /// @function getLanguage
  /// Returns the language setting.
  /// This setting is the default language for
  /// operations needing language, but didn't
  /// receive language explicitly.
  /// @synopsis string getLanguage()
  public static final Any getLanguage(Context context)
  {
    return Any.create(context.getLanguage());
  }


  /// @function setLanguage
  /// Locale: sets the language.
  /// @synopsis void setLanguage(string language)
  public static final Any setLanguage(Context context, String language)
  {
    context.setLanguage(language);
    return Any.TRUE;
  }



  /// @function setLocale
  /// Locale: sets the locale.
  /// This setting is the default locale for
  /// operations needing it, but didn't
  /// receive locale explicitly.
  /// @synopsis void setLocale(string locale)
  public static final Any setLocale(Context context, String locale)
  {
    context.setLocale(new Locale(context.getLanguage(), locale));
    return Any.TRUE;
  }


  /// @function setTimeZone
  /// Sets the default timezone.
  /// @synopsis void setTimeZone(string timezone)
  public static final Any setTimeZone(Context context, String timezone)
  {
    context.setTimeZone(TimeZone.getTimeZone(timezone));
    return Any.TRUE;
  }


  /// @function sleep
  /// Ceases the execution of current thread for given amount of milliseconds.
  /// @synopsis void sleep(int millis)
  /// @throws Interrupted If sleeping was interrupted
  public static final Any sleep(Context context, int millis)
  {
    try {
      if (millis < 0) {
        millis = 0;
      }
      Thread.sleep(millis);
    } catch (InterruptedException e) {
      throw context.Interrupted(e.getMessage());
    }
    return Any.TRUE;
  }


  /// @function cease
  /// Temporarily ceases the execution of current thread so that
  /// other can execute.
  /// @synopsis void cease()
  public static final Any cease()
  {
    Thread.yield();
    return Any.TRUE;
  }
 

  /// @function setTracing
  /// Enables or disables stack tracing. If enabled
  /// will output notifications on log
  /// when functions are entered or leaved.
  /// @synopsis void setTracing(boolean enabled)
  public static final Object[] p_setTracing = { null, "enable" };
  public static final Any setTracing(Context context, boolean enable)
  {
    if (enable) {
      context.enableTracing();
    } else {
      context.disableTracing();
    }
    return Any.NULL;
  }


  /// @function enableTracing
  /// Enables stack tracing.
  /// @synopsis void enableTracing()
  public static final Any enableTracing(Context context)
  {
    context.enableTracing();
    return Any.NULL;
  }


  /// @function disableTracing
  /// Disables stack tracing.
  /// @synopsis void disableTracing()
  public static final Any disableTracing(Context context)
  {
    context.disableTracing();
    return Any.NULL;
  }


  public static final anvil.script.compiler.NativeNamespace __module__ =
    new anvil.script.compiler.NativeNamespace(
      "runtime",
      RuntimeModule.class,
      new Class[] {
        anvil.core.runtime.stack.StackModule.class,
        AnyFunction.class,
        AnyType.class,
        AnyDoc.class,
        AnyScope.class,
        AnyNamespace.class,
        AnyPermission.class,
        AnyStackTraceElement.class,
        AnyThread.class,
        AnyThreadPool.class,
      },
      //DOC{{
    ""+
      "\n" +
      " @module anvil.runtime\n" +
      " Provides access to runtime related operations, such as \n" +
      " output redirection and type inspection.\n" +
      " @function getNamespace\n" +
      " Returns namespace with given name, or null.\n" +
      " @synopsis Namespace getNamespace(string name)\n" +
      " @function getLibraryRoot\n" +
      " Returns the root namespace of loaded native libraries.\n" +
      " @synopsis Type getLibraryRoot()\n" +
      " @function write\n" +
      " Writes (prints) data to output.\n" +
      " @synopsis void write(binary binary, int offset, int length)\n" +
      " @synopsis void write(object text)\n" +
      " @function push\n" +
      " Sets the execution output stream.\n" +
      " @synopsis void push()\n" +
      " @synopsis void push(OutputStream output) \n" +
      " @param output Output stream where to direct the output, if\n" +
      " omitted memory stream is used\n" +
      " @function pop\n" +
      " Removes previous execution output stream.\n" +
      " @synopsis void pop()\n" +
      " @function getContentLength\n" +
      " Returns the content length in memory output stream.\n" +
      " @synopsis int getContentLength()\n" +
      " @function getContent\n" +
      " Returns the content from memory output stream.\n" +
      " @synopsis string getContent()\n" +
      " @function getBinaryContent\n" +
      " Returns the content from memory output stream.\n" +
      " @synopsis binary getBinaryContent()\n" +
      " @function getOutput\n" +
      " Returns the output stream to write to.\n" +
      " @synopsis OutputStream getOutput()\n" +
      " @function reset\n" +
      " Clears the content in memory output stream.\n" +
      " @synopsis void reset()\n" +
      " @function flush\n" +
      " Writes the of memory output stream to previous output stream in stack.\n" +
      " @synopsis void flush()\n" +
      " @function log\n" +
      " Logs messages.\n" +
      " @synopsis void log(messages...)\n" +
      " @function logError\n" +
      " Logs error messages.\n" +
      " @synopsis void logError(messages...)\n" +
      " @function getLanguage\n" +
      " Returns the language setting.\n" +
      " This setting is the default language for\n" +
      " operations needing language, but didn't \n" +
      " receive language explicitly.\n" +
      " @synopsis string getLanguage()\n" +
      " @function setLanguage\n" +
      " Locale: sets the language.\n" +
      " @synopsis void setLanguage(string language)\n" +
      " @function setLocale\n" +
      " Locale: sets the locale.\n" +
      " This setting is the default locale for\n" +
      " operations needing it, but didn't \n" +
      " receive locale explicitly.\n" +
      " @synopsis void setLocale(string locale)\n" +
      " @function setTimeZone\n" +
      " Sets the default timezone.\n" +
      " @synopsis void setTimeZone(string timezone)\n" +
      " @function sleep\n" +
      " Ceases the execution of current thread for given amount of milliseconds.\n" +
      " @synopsis void sleep(int millis)\n" +
      " @throws Interrupted If sleeping was interrupted\n" +
      " @function cease\n" +
      " Temporarily ceases the execution of current thread so that\n" +
      " other can execute.\n" +
      " @synopsis void cease()\n" +
      " @function setTracing\n" +
      " Enables or disables stack tracing. If enabled\n" +
      " will output notifications on log\n" +
      " when functions are entered or leaved.\n" +
      " @synopsis void setTracing(boolean enabled)\n" +
      " @function enableTracing\n" +
      " Enables stack tracing.\n" +
      " @synopsis void enableTracing()\n" +
      " @function disableTracing\n" +
      " Disables stack tracing.\n" +
      " @synopsis void disableTracing()\n"
    //}}DOC
    );


}


 
TOP

Related Classes of anvil.core.runtime.RuntimeModule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.