Package abstrasy.libraries.os

Source Code of abstrasy.libraries.os.External_SystemAdapter

package abstrasy.libraries.os;


import abstrasy.Bivalence;
import abstrasy.Hash;
import abstrasy.Interpreter;
import abstrasy.Node;

import abstrasy.externals.AExtCachable;
import abstrasy.externals.AExtClonable;

import abstrasy.interpreter.ORef;

import abstrasy.privates.Private_Execute;

import java.net.InetAddress;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/

public class External_SystemAdapter implements AExtClonable, AExtCachable {
   
    public External_SystemAdapter() {
    }
   
    public boolean equals(Object obj) {
        /*
         * Emulation d'un singleton...
         */
        return (this instanceof External_SystemAdapter);
    }
   
    public Node external_user_name(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("user.name"));
    }

    public Node external_user_language(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("user.language"));
    }

    public Node external_user_timezone(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("user.timezone"));
    }

    public Node external_user_country(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("user.country"));
    }

    public Node external_os_name(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("os.name"));
    }

    public Node external_os_version(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("os.version"));
    }

    public Node external_interpreter_version(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.version);
    }

    public Node external_interpreter_revision(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.revision);
    }

    public Node external_interpreter_heap_free(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.mySelf().getGLOBAL().getFree());
    }

    public Node external_interpreter_heap_used(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.mySelf().getGLOBAL().getUsed());
    }

    public Node external_interpreter_set_timeout(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 2);
        long old_to = Interpreter.mySelf().getExecutionTimeOut();
        long now_to = Interpreter.mySelf().getDeadlockTime();
        long start_to = now_to - old_to;
        old_to = (long) startAt.getSubNode(1, Node.TYPE_NUMBER).getNumber();
        now_to = start_to + old_to;
        Interpreter.mySelf().setExecutionTimeOut(old_to);
        Interpreter.mySelf().setDeadlockTime(now_to);
        return null;
    }

    public Node external_interpreter_get_timeout(Node startAt) throws Exception {
        return new Node(Interpreter.mySelf().getExecutionTimeOut());
    }

    public Node external_interpreter_get_deadlock_time(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.mySelf().getDeadlockTime());
    }

    public Node external_is_interpreter_timeout_check(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Interpreter.mySelf().isTimeOutCheck() ? Node.TRUE: Node.FALSE);
    }

    /*
    public Node external_is_interpreter_optimizing(Node startAt) throws Exception {
    startAt.isGoodArgsLenght(true, 1);
        return new Node(Interpreter.isOptimizing() ? Node.TRUE : Node.FALSE);
    }
    */

    public Node external_os_arch(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("os.arch"));
    }

    public Node external_java_runtime_name(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("java.runtime.name"));
    }

    public Node external_java_runtime_version(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(System.getProperty("java.runtime.version"));
    }

    public Node external_free_memory(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Runtime.getRuntime().freeMemory());
    }

    public Node external_available_memory(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        return new Node(Runtime.getRuntime().maxMemory());
    }

    public Node external_optimize_memory(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        System.runFinalization();
        System.gc();
        return null;
    }

    public Node external_available_cpu(Node startAt) throws Exception {
        return new Node(Runtime.getRuntime().availableProcessors());
    }

    public Node external_computer_name(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        String cname = "unknown";
        try {
            cname = InetAddress.getLocalHost().getHostName();
        }
        catch (Exception e) {
        }
        return new Node(cname);
    }

    public Node external_get_system_property(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 2);
        String id = startAt.getSubNode(1, Node.TYPE_STRING).getString();
        return new Node(System.getProperty(id));
    }

    public Node external_is_system_property_defined(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 2);
        String id = startAt.getSubNode(1, Node.TYPE_STRING).getString();
        return new Node(System.getProperties().containsKey((Object) id) ? Node.TRUE: Node.FALSE);
    }

    public Node external_set_system_property(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 3);
        String id = startAt.getSubNode(1, Node.TYPE_STRING).getString();
        String value = startAt.getSubNode(2, Node.TYPE_STRING).getString();
        return new Node(System.setProperty(id, value));
    }

    public Node external_system_properties(Node startAt) throws Exception {
        startAt.isGoodArgsCnt(1);
        Node ynode = Node.createHash();
        Hash hash=ynode.getHash();
        Properties sprop = System.getProperties();
        Enumeration<?> eprop = sprop.propertyNames();
        while (eprop.hasMoreElements()) {
            String oid = (String) eprop.nextElement();
            hash.store(new Node(oid), new Node(sprop.getProperty(oid)));
        }
        return ynode;
    }

    public Node external_env_properties(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 1);
        Node ynode = Node.createHash();
        Hash hash=ynode.getHash();
        Map<String,String> map = System.getenv();
        Set<String> keys = map.keySet();
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = map.get(key);
            hash.store(new Node(key), new Node(value));
        }
        return ynode;
    }

    public Node external_get_env_property(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 2);
        String id = startAt.getSubNode(1, Node.TYPE_STRING).getString();
        String env=System.getenv(id);
        return env==null ? Node.createNothing() : new Node(env);
    }

    public Node external_execute(Node startAt) throws Exception {
        startAt.isGoodArgsLength(true, 2);
        String command = startAt.getSubNode(1, Node.TYPE_STRING).getString();
        Private_Execute pexec = new Private_Execute(command);
        int res = pexec.exec();
        Node rnode = Node.createHash();
        Hash hash=rnode.getHash();
        hash.store(Node.createQSymbol("result-code"), new Node(res));
        hash.store(Node.createQSymbol("output"), new Node(pexec.getOutput()));
        hash.store(Node.createQSymbol("error"), new Node(pexec.getError()));
        return rnode;
    }

    public Object clone_my_self(Bivalence bival) {
        return this;
    }

    /*
     * ----------------------------------------------------------------------------
     *
     * Optimisation par cache d'instanciantion (mars 2012) rev 1.0-6321.0
     *
     * ----------------------------------------------------------------------------
     */


    private static ORef _optim_symbols_cache_ = new ORef();
   
    @Override
    public Object getSymbolsCache() {
        return _optim_symbols_cache_.getRef();
    }

    @Override
    public void setSymbolsCache(Object cache) {
        _optim_symbols_cache_.setRef(cache);
    }
}
TOP

Related Classes of abstrasy.libraries.os.External_SystemAdapter

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.