isac-java/src/java/isac/bridge/BridgeMain.java
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 19 Dec 2018 13:19:34 +0100
changeset 5237 ee17f1b81a7f
parent 5236 bdd3733fa7db
child 5239 b4e3883d7b66
permissions -rw-r--r--
tuned
     1 /**
     2  * Created on Aug 13, 2003
     3  *
     4  * @author Richard Gradischnegg RG
     5  */
     6 package isac.bridge;
     7 
     8 /**
     9  * BridgeMain: main class for bridge, does initialization and provides logging delegate and utility functionality.
    10  *
    11  * @author Richard Gradischnegg RG
    12  * @version 1.1
    13  */
    14 import isac.util.BridgeMainPaths;
    15 import isac.util.FixedPortRMISocketFactory;
    16 import info.hupel.isabelle.api.*;
    17 import info.hupel.isabelle.japi.*;
    18 import info.hupel.isabelle.setup.*;
    19 
    20 import java.awt.BorderLayout;
    21 import java.awt.Color;
    22 import java.awt.Font;
    23 import java.awt.event.WindowAdapter;
    24 import java.awt.event.WindowEvent;
    25 import java.awt.event.WindowListener;
    26 import java.io.BufferedReader;
    27 import java.io.IOException;
    28 import java.io.Serializable;
    29 import java.nio.file.Path;
    30 import java.nio.file.Paths;
    31 import java.rmi.RemoteException;
    32 import java.rmi.server.RMISocketFactory;
    33 import java.util.Arrays;
    34 
    35 import javax.swing.JFrame;
    36 import javax.swing.JPanel;
    37 import javax.swing.JScrollPane;
    38 import javax.swing.JTextArea;
    39 
    40 import org.apache.log4j.Logger;
    41 
    42 /**
    43  * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
    44  *
    45  * @author rgradisc
    46  */
    47 class BridgeMain implements Serializable {
    48 
    49     private static final long serialVersionUID = 507L;
    50     private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
    51     private JFrame log_frame_;
    52     private String path_;
    53     JPanel panel_;
    54     JTextArea text_area_;
    55     JScrollPane scroll_pane_;
    56     private Thread thread_sml_;
    57     private BridgeLogger bridge_logger_;
    58     private BufferedReader sml_reader_;
    59 
    60     public int ignore_output_ = 0;
    61     private boolean restoring_; // true = bridge is in restoring_ phase
    62     private JSystem connection_to_kernel_;
    63 
    64     /**
    65      * Constructor used to set up the bridge main program
    66      *
    67      * @param log_path_: path_ to initialization file
    68      */
    69     BridgeMain(String log_path, String isabelle_home) {
    70         // hack for suppressing warning messages
    71         // (no longer nescessary with j2sdk1.4.2)
    72         // System.setProperty("java.util.prefs.syncInterval", "20000000000");
    73 
    74         if (logger.isDebugEnabled()) {
    75             logger.debug("BridgeMain log_path =" + log_path);
    76         }
    77         Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
    78 
    79         bridge_logger_ = new BridgeLogger(log_path);
    80         new ClientList();
    81         setUpBridgeLog();
    82         //*TTY*/startThreadsFirstTime();
    83 /* cp from last working version:
    84  *  https://hg.risc.uni-linz.ac.at/wneuper/isac/file/84f1ca6a6dd9/isac-java/src/java/isac/bridge/BridgeMain.java#l85
    85     85         log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
    86     86         Setup setup = new Setup(Paths.get(isabelle_home), JPlatform.guess(),
    87     87           new Version("2015"), Setup.defaultPackageName());
    88     88         Environment env = JSetup.makeEnvironment(setup); // without Duration
    89     89         Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
    90     90         JSystem sys = JSystem.create(env, config);
    91     92         
    92     93         connection_to_kernel_ = sys;
    93     94         log(1, "<--ISA: connection established");
    94  */
    95 /* cp from downloaded libisabelle, current version:
    96 package info.hupel.isabelle.examples.java;
    97 
    98 import java.nio.file.Path;
    99 import java.nio.file.Paths;
   100 import java.util.Arrays;
   101 import info.hupel.isabelle.api.*;
   102 import info.hupel.isabelle.japi.*;
   103 import info.hupel.isabelle.setup.*;
   104 
   105 public class Hello_PIDE {
   106 
   107   public static void main(String args[]) {
   108     JResources res = JResources.dumpIsabelleResources();
   109     Configuration config = Configuration.simple("Protocol");
   110     Environment env = JSetup.makeEnvironment(JSetup.defaultSetup(new Version.Stable("2017")), res);
   111     JSystem sys = JSystem.create(env, config);
   112     String response = sys.invoke(Operations.HELLO, "world");
   113     System.out.println(response);
   114     sys.dispose();
   115   }
   116 }
   117  */
   118         /*PIDE*/log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
   119 
   120         //these two values shall come from BridgeMail.properties
   121         Path path = java.nio.file.Paths.get   //
   122           ("/home/wneuper/.isabelle/isabisac/heaps/polyml-5.7.1_x86-linux/libisabelle_Isac");
   123         String vers = "2018";
   124         
   125         JResources res = JResources.dumpIsabelleResources();
   126         Setup setup = new Setup(path, JPlatform.guess(), new Version.Stable(vers));
   127         Environment env = JSetup.makeEnvironment(setup, res);
   128         //Configuration config = Configuration.fromBuiltin("libisabelle_Isac"); //fromBuiltin is undefined
   129         JSystem sys = JSystem.create(env, config);
   130 
   131         /*PIDE*/connection_to_kernel_ = sys;
   132         /*PIDE*/log(1, "<--ISA: connection established");
   133 
   134     }
   135 
   136     protected JSystem getConnectionToKernel(){
   137       return connection_to_kernel_;
   138     }
   139     private void setUpBridgeLog() {
   140         if (BridgeMainPaths.SHOW_GUI_LOGGER) {
   141             log_frame_ = new JFrame("Java<->SML");
   142             log_frame_.setLocation(700, 250);
   143             panel_ = new JPanel();
   144             panel_.setSize(200, 300);
   145             panel_.setLayout(new BorderLayout());
   146             panel_.setBackground(Color.white);
   147 
   148             text_area_ = new JTextArea();
   149             text_area_.setFont(new Font("Monospaced", 0, 11));
   150             scroll_pane_ = new JScrollPane(text_area_,
   151                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
   152                     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   153             panel_.add("Center", scroll_pane_);
   154             log_frame_.getContentPane().add(panel_);
   155 
   156             log_frame_.setTitle("BridgeLog Java <=> SML");
   157             WindowListener l = new WindowAdapter() {
   158                 public void windowClosing(WindowEvent e) {
   159                     System.exit(0);
   160                 }
   161             };
   162             log_frame_.addWindowListener(l);
   163             log_frame_.pack();
   164             log_frame_.setSize(300, 500);
   165             log_frame_.setVisible(true);
   166         }
   167     }
   168 
   169     // Here goes the future load balancing functionality
   170     // public void startNewKernel() {
   171     // implement load balancing
   172     // }
   173     protected void finalize() throws Throwable {
   174         super.finalize(); // gosling p.47
   175         // Objects created in run method are finalized when
   176         // program terminates and thread exits
   177         while (thread_sml_.isAlive()) {
   178             thread_sml_.interrupt();
   179             log(1, "waiting for SML thread to quit..");
   180         }
   181         bridge_logger_.log(1, "finished");
   182         bridge_logger_.close();
   183     }
   184 
   185     /**
   186      * Log a message with a specified severity level
   187      *
   188      * @param level severity level
   189      * @param msg the message to be logged
   190      */
   191     public void log(int level, String msg) {
   192         if (BridgeMainPaths.LOG_TO_FILE) {
   193             this.bridge_logger_.log(level, msg);
   194         }
   195         if (BridgeMainPaths.SHOW_GUI_LOGGER) {
   196             this.text_area_.append(msg + "\n");
   197         }
   198     }
   199 
   200     public BufferedReader getSmlReader() {
   201         //if (logger.isDebugEnabled())
   202         //    logger.debug("getSmlReader x");
   203         return sml_reader_;
   204     }
   205 
   206     public String getPath() {
   207         return path_;
   208     }
   209 
   210     public void setRMI(BridgeRMI bridgeRMI) {
   211         if (logger.isDebugEnabled()) {
   212             logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
   213         }
   214         bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
   215     }
   216 
   217     boolean isRestoring() {
   218         return restoring_;
   219     }
   220 
   221     /**
   222      * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
   223      * @see BridgeMain()
   224      */
   225     public static void main(String[] args) {
   226 
   227         System.out.println("Starting Bridge...");
   228 
   229         String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
   230 
   231         if (ini_load_result != null) {
   232             ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
   233         }
   234 
   235         if (ini_load_result != null) {
   236             System.err.println(ini_load_result);
   237             System.exit(1);
   238         }
   239 
   240         // initialize bridge main part
   241         BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
   242         try {
   243             // Use ObjectManagerRMISocketFactory
   244             // 6666 = dummy
   245             RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
   246 
   247             // setup rmi connection to to the clients
   248             BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
   249                     BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
   250 
   251 //			System.out.println( rmi );
   252 
   253             bridge.setRMI(rmi);
   254         } catch (RemoteException e) {
   255             System.out.println("Could not instantiate RMI: exiting");
   256             e.printStackTrace();
   257         } catch (IOException e) {
   258             System.out.println("Exception: " + e.getMessage());
   259             e.printStackTrace();
   260         }
   261     }
   262 }