2 * Created on Aug 13, 2003
4 * @author Richard Gradischnegg RG
9 * BridgeMain: main class for bridge, does initialization and provides logging delegate and utility functionality.
11 * @author Richard Gradischnegg RG
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;
24 import java.awt.BorderLayout;
25 import java.awt.Color;
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;
37 import javax.swing.JFrame;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTextArea;
42 import org.apache.log4j.Logger;
45 * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
49 class BridgeMain implements Serializable {
51 private static final long serialVersionUID = 507L;
52 private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
53 private JFrame log_frame_;
57 JScrollPane scroll_pane_;
58 private Thread thread_sml_;
59 private BridgeLogger bridge_logger_;
60 private BufferedReader sml_reader_;
62 public int ignore_output_ = 0;
63 private boolean restoring_; // true = bridge is in restoring_ phase
64 private JSystem connection_to_kernel_;
67 * Constructor used to set up the bridge main program
69 * @param log_path_: path_ to initialization file
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");
76 if (logger.isDebugEnabled()) {
77 logger.debug("BridgeMain log_path =" + log_path);
79 Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
81 bridge_logger_ = new BridgeLogger(log_path);
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);
92 /*PIDE*/connection_to_kernel_ = sys;
93 /*PIDE*/log(1, "<--ISA: connection established");
97 protected JSystem getConnectionToKernel(){
98 return connection_to_kernel_;
100 private void setUpBridgeLog() {
101 if (BridgeMainPaths.SHOW_GUI_LOGGER) {
102 log_frame_ = new JFrame("Java<->SML");
103 log_frame_.setLocation(700, 250);
104 panel_ = new JPanel();
105 panel_.setSize(200, 300);
106 panel_.setLayout(new BorderLayout());
107 panel_.setBackground(Color.white);
109 text_area_ = new JTextArea();
110 text_area_.setFont(new Font("Monospaced", 0, 11));
111 scroll_pane_ = new JScrollPane(text_area_,
112 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
113 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
114 panel_.add("Center", scroll_pane_);
115 log_frame_.getContentPane().add(panel_);
117 log_frame_.setTitle("BridgeLog Java <=> SML");
118 WindowListener l = new WindowAdapter() {
119 public void windowClosing(WindowEvent e) {
123 log_frame_.addWindowListener(l);
125 log_frame_.setSize(300, 500);
126 log_frame_.setVisible(true);
130 // Here goes the future load balancing functionality
131 // public void startNewKernel() {
132 // implement load balancing
134 protected void finalize() throws Throwable {
135 super.finalize(); // gosling p.47
136 // Objects created in run method are finalized when
137 // program terminates and thread exits
138 while (thread_sml_.isAlive()) {
139 thread_sml_.interrupt();
140 log(1, "waiting for SML thread to quit..");
142 bridge_logger_.log(1, "finished");
143 bridge_logger_.close();
147 * Log a message with a specified severity level
149 * @param level severity level
150 * @param msg the message to be logged
152 public void log(int level, String msg) {
153 if (BridgeMainPaths.LOG_TO_FILE) {
154 this.bridge_logger_.log(level, msg);
156 if (BridgeMainPaths.SHOW_GUI_LOGGER) {
157 this.text_area_.append(msg + "\n");
161 public BufferedReader getSmlReader() {
162 //if (logger.isDebugEnabled())
163 // logger.debug("getSmlReader x");
167 public String getPath() {
171 public void setRMI(BridgeRMI bridgeRMI) {
172 if (logger.isDebugEnabled()) {
173 logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
175 bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
178 boolean isRestoring() {
183 * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
186 public static void main(String[] args) {
188 System.out.println("Starting Bridge...");
190 String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
192 if (ini_load_result != null) {
193 ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
196 if (ini_load_result != null) {
197 System.err.println(ini_load_result);
201 // initialize bridge main part
202 BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
204 // Use ObjectManagerRMISocketFactory
206 RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
208 // setup rmi connection to to the clients
209 BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
210 BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
212 // System.out.println( rmi );
215 } catch (RemoteException e) {
216 System.out.println("Could not instantiate RMI: exiting");
218 } catch (IOException e) {
219 System.out.println("Exception: " + e.getMessage());