isac-java/src/java/isac/bridge/BridgeMain.java
author Andreas Hujber <a.hujber@gmx.at>
Wed, 08 Feb 2017 13:15:26 +0100
changeset 4974 0f75aafd3216
parent 4971 73c49c8a38cf
child 5229 6bf0e95981e3
permissions -rw-r--r--
remove Warning: unused Code
     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 edu.tum.cs.isabelle.api.Configuration;
    17 import edu.tum.cs.isabelle.api.Environment;
    18 import edu.tum.cs.isabelle.api.Version;
    19 import edu.tum.cs.isabelle.japi.JPlatform;
    20 import edu.tum.cs.isabelle.japi.JSetup;
    21 import edu.tum.cs.isabelle.japi.JSystem;
    22 import edu.tum.cs.isabelle.setup.Setup;
    23 
    24 import java.awt.BorderLayout;
    25 import java.awt.Color;
    26 import java.awt.Font;
    27 import java.awt.event.WindowAdapter;
    28 import java.awt.event.WindowEvent;
    29 import java.awt.event.WindowListener;
    30 import java.io.BufferedReader;
    31 import java.io.IOException;
    32 import java.io.Serializable;
    33 import java.nio.file.Paths;
    34 import java.rmi.RemoteException;
    35 import java.rmi.server.RMISocketFactory;
    36 
    37 import javax.swing.JFrame;
    38 import javax.swing.JPanel;
    39 import javax.swing.JScrollPane;
    40 import javax.swing.JTextArea;
    41 
    42 import org.apache.log4j.Logger;
    43 
    44 /**
    45  * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
    46  *
    47  * @author rgradisc
    48  */
    49 class BridgeMain implements Serializable {
    50 
    51     private static final long serialVersionUID = 507L;
    52     private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
    53     private JFrame log_frame_;
    54     private String path_;
    55     JPanel panel_;
    56     JTextArea text_area_;
    57     JScrollPane scroll_pane_;
    58     private Thread thread_sml_;
    59     private BridgeLogger bridge_logger_;
    60     private BufferedReader sml_reader_;
    61 
    62     public int ignore_output_ = 0;
    63     private boolean restoring_; // true = bridge is in restoring_ phase
    64     private JSystem connection_to_kernel_;
    65 
    66     /**
    67      * Constructor used to set up the bridge main program
    68      *
    69      * @param log_path_: path_ to initialization file
    70      */
    71     BridgeMain(String log_path, String isabelle_home) {
    72         // hack for suppressing warning messages
    73         // (no longer nescessary with j2sdk1.4.2)
    74         // System.setProperty("java.util.prefs.syncInterval", "20000000000");
    75 
    76         if (logger.isDebugEnabled()) {
    77             logger.debug("BridgeMain log_path =" + log_path);
    78         }
    79         Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
    80 
    81         bridge_logger_ = new BridgeLogger(log_path);
    82         new ClientList();
    83         setUpBridgeLog();
    84         //*TTY*/startThreadsFirstTime();
    85         /*PIDE*/log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
    86         Setup setup = new Setup(Paths.get(isabelle_home), JPlatform.guess(),
    87           new Version("2015"), Setup.defaultPackageName());
    88         Environment env = JSetup.makeEnvironment(setup); // ohne Duration
    89         Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
    90         JSystem sys = JSystem.create(env, config);
    91 
    92         
    93         /*PIDE*/connection_to_kernel_ = sys;
    94         /*PIDE*/log(1, "<--ISA: connection established");
    95 
    96     }
    97 
    98     protected JSystem getConnectionToKernel(){
    99       return connection_to_kernel_;
   100     }
   101     private void setUpBridgeLog() {
   102         if (BridgeMainPaths.SHOW_GUI_LOGGER) {
   103             log_frame_ = new JFrame("Java<->SML");
   104             log_frame_.setLocation(700, 250);
   105             panel_ = new JPanel();
   106             panel_.setSize(200, 300);
   107             panel_.setLayout(new BorderLayout());
   108             panel_.setBackground(Color.white);
   109 
   110             text_area_ = new JTextArea();
   111             text_area_.setFont(new Font("Monospaced", 0, 11));
   112             scroll_pane_ = new JScrollPane(text_area_,
   113                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
   114                     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   115             panel_.add("Center", scroll_pane_);
   116             log_frame_.getContentPane().add(panel_);
   117 
   118             log_frame_.setTitle("BridgeLog Java <=> SML");
   119             WindowListener l = new WindowAdapter() {
   120                 public void windowClosing(WindowEvent e) {
   121                     System.exit(0);
   122                 }
   123             };
   124             log_frame_.addWindowListener(l);
   125             log_frame_.pack();
   126             log_frame_.setSize(300, 500);
   127             log_frame_.setVisible(true);
   128         }
   129     }
   130 
   131     // Here goes the future load balancing functionality
   132     // public void startNewKernel() {
   133     // implement load balancing
   134     // }
   135     protected void finalize() throws Throwable {
   136         super.finalize(); // gosling p.47
   137         // Objects created in run method are finalized when
   138         // program terminates and thread exits
   139         while (thread_sml_.isAlive()) {
   140             thread_sml_.interrupt();
   141             log(1, "waiting for SML thread to quit..");
   142         }
   143         bridge_logger_.log(1, "finished");
   144         bridge_logger_.close();
   145     }
   146 
   147     /**
   148      * Log a message with a specified severity level
   149      *
   150      * @param level severity level
   151      * @param msg the message to be logged
   152      */
   153     public void log(int level, String msg) {
   154         if (BridgeMainPaths.LOG_TO_FILE) {
   155             this.bridge_logger_.log(level, msg);
   156         }
   157         if (BridgeMainPaths.SHOW_GUI_LOGGER) {
   158             this.text_area_.append(msg + "\n");
   159         }
   160     }
   161 
   162     public BufferedReader getSmlReader() {
   163         //if (logger.isDebugEnabled())
   164         //    logger.debug("getSmlReader x");
   165         return sml_reader_;
   166     }
   167 
   168     public String getPath() {
   169         return path_;
   170     }
   171 
   172     public void setRMI(BridgeRMI bridgeRMI) {
   173         if (logger.isDebugEnabled()) {
   174             logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
   175         }
   176         bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
   177     }
   178 
   179     boolean isRestoring() {
   180         return restoring_;
   181     }
   182 
   183     /**
   184      * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
   185      * @see BridgeMain()
   186      */
   187     public static void main(String[] args) {
   188 
   189         System.out.println("Starting Bridge...");
   190 
   191         String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
   192 
   193         if (ini_load_result != null) {
   194             ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
   195         }
   196 
   197         if (ini_load_result != null) {
   198             System.err.println(ini_load_result);
   199             System.exit(1);
   200         }
   201 
   202         // initialize bridge main part
   203         BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
   204         try {
   205             // Use ObjectManagerRMISocketFactory
   206             // 6666 = dummy
   207             RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
   208 
   209             // setup rmi connection to to the clients
   210             BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
   211                     BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
   212 
   213 //			System.out.println( rmi );
   214 
   215             bridge.setRMI(rmi);
   216         } catch (RemoteException e) {
   217             System.out.println("Could not instantiate RMI: exiting");
   218             e.printStackTrace();
   219         } catch (IOException e) {
   220             System.out.println("Exception: " + e.getMessage());
   221             e.printStackTrace();
   222         }
   223     }
   224 }