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);
93 /*PIDE*/connection_to_kernel_ = sys;
94 /*PIDE*/log(1, "<--ISA: connection established");
98 protected JSystem getConnectionToKernel(){
99 return connection_to_kernel_;
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);
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_);
118 log_frame_.setTitle("BridgeLog Java <=> SML");
119 WindowListener l = new WindowAdapter() {
120 public void windowClosing(WindowEvent e) {
124 log_frame_.addWindowListener(l);
126 log_frame_.setSize(300, 500);
127 log_frame_.setVisible(true);
131 // Here goes the future load balancing functionality
132 // public void startNewKernel() {
133 // implement load balancing
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..");
143 bridge_logger_.log(1, "finished");
144 bridge_logger_.close();
148 * Log a message with a specified severity level
150 * @param level severity level
151 * @param msg the message to be logged
153 public void log(int level, String msg) {
154 if (BridgeMainPaths.LOG_TO_FILE) {
155 this.bridge_logger_.log(level, msg);
157 if (BridgeMainPaths.SHOW_GUI_LOGGER) {
158 this.text_area_.append(msg + "\n");
162 public BufferedReader getSmlReader() {
163 //if (logger.isDebugEnabled())
164 // logger.debug("getSmlReader x");
168 public String getPath() {
172 public void setRMI(BridgeRMI bridgeRMI) {
173 if (logger.isDebugEnabled()) {
174 logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
176 bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
179 boolean isRestoring() {
184 * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
187 public static void main(String[] args) {
189 System.out.println("Starting Bridge...");
191 String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
193 if (ini_load_result != null) {
194 ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
197 if (ini_load_result != null) {
198 System.err.println(ini_load_result);
202 // initialize bridge main part
203 BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
205 // Use ObjectManagerRMISocketFactory
207 RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
209 // setup rmi connection to to the clients
210 BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
211 BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
213 // System.out.println( rmi );
216 } catch (RemoteException e) {
217 System.out.println("Could not instantiate RMI: exiting");
219 } catch (IOException e) {
220 System.out.println("Exception: " + e.getMessage());