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 info.hupel.isabelle.api.*;
17 import info.hupel.isabelle.japi.*;
18 import info.hupel.isabelle.setup.*;
20 import java.awt.BorderLayout;
21 import java.awt.Color;
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;
35 import javax.swing.JFrame;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTextArea;
40 import org.apache.log4j.Logger;
43 * Bridge main class: initializes connection to SML, starts auxillary threads, handles logging mechanism
47 class BridgeMain implements Serializable {
49 private static final long serialVersionUID = 507L;
50 private static final Logger logger = Logger.getLogger(BridgeMain.class.getName());
51 private JFrame log_frame_;
55 JScrollPane scroll_pane_;
56 private Thread thread_sml_;
57 private BridgeLogger bridge_logger_;
58 private BufferedReader sml_reader_;
60 public int ignore_output_ = 0;
61 private boolean restoring_; // true = bridge is in restoring_ phase
62 private JSystem connection_to_kernel_;
65 * Constructor used to set up the bridge main program
67 * @param log_path_: path_ to initialization file
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");
74 if (logger.isDebugEnabled()) {
75 logger.debug("BridgeMain log_path =" + log_path);
77 Integer.valueOf(BridgeMainPaths.BRIDGE_MAIN_PORT);
79 bridge_logger_ = new BridgeLogger(log_path);
82 //*TTY*/startThreadsFirstTime();
83 /*PIDE*/log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
84 //see libisabelle/modules/examples/src/main/java/edu/tum/cs/isabelle/examples/Hello_PIDE.java
86 * https://hg.risc.uni-linz.ac.at/wneuper/isac/file/84f1ca6a6dd9/isac-java/src/java/isac/bridge/BridgeMain.java#l85
87 85 log(1, "-->ISA: try connection with ISABELLE_HOME=\"" + isabelle_home + "\"...");
88 86 Setup setup = new Setup(Paths.get(isabelle_home), JPlatform.guess(),
89 87 new Version("2015"), Setup.defaultPackageName());
90 88 Environment env = JSetup.makeEnvironment(setup); // ohne Duration
91 89 Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
92 90 JSystem sys = JSystem.create(env, config);
94 93 connection_to_kernel_ = sys;
95 94 log(1, "<--ISA: connection established");
98 package info.hupel.isabelle.examples.java;
100 import java.nio.file.Path;
101 import java.nio.file.Paths;
102 import java.util.Arrays;
103 import info.hupel.isabelle.api.*;
104 import info.hupel.isabelle.japi.*;
105 import info.hupel.isabelle.setup.*;
107 public class Hello_PIDE {
109 public static void main(String args[]) {
110 JResources res = JResources.dumpIsabelleResources();
111 Configuration config = Configuration.simple("Protocol");
112 Environment env = JSetup.makeEnvironment(JSetup.defaultSetup(new Version.Stable("2017")), res);
113 JSystem sys = JSystem.create(env, config);
114 String response = sys.invoke(Operations.HELLO, "world");
115 System.out.println(response);
120 JResources res = JResources.dumpIsabelleResources();
121 Path path = java.nio.file.Paths.get
122 ("/home/wneuper/.isabelle/isabisac/heaps/polyml-5.7.1_x86-linux/libisabelle_Isac");
123 Configuration config = Configuration.simple("Protocol");
124 Environment env = JSetup.makeEnvironment(JSetup.defaultSetup(new Version.Stable("2018")), res);
125 JSystem sys = JSystem.create(env, config);
127 /*PIDE*/connection_to_kernel_ = sys;
128 /*PIDE*/log(1, "<--ISA: connection established");
132 protected JSystem getConnectionToKernel(){
133 return connection_to_kernel_;
135 private void setUpBridgeLog() {
136 if (BridgeMainPaths.SHOW_GUI_LOGGER) {
137 log_frame_ = new JFrame("Java<->SML");
138 log_frame_.setLocation(700, 250);
139 panel_ = new JPanel();
140 panel_.setSize(200, 300);
141 panel_.setLayout(new BorderLayout());
142 panel_.setBackground(Color.white);
144 text_area_ = new JTextArea();
145 text_area_.setFont(new Font("Monospaced", 0, 11));
146 scroll_pane_ = new JScrollPane(text_area_,
147 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
148 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
149 panel_.add("Center", scroll_pane_);
150 log_frame_.getContentPane().add(panel_);
152 log_frame_.setTitle("BridgeLog Java <=> SML");
153 WindowListener l = new WindowAdapter() {
154 public void windowClosing(WindowEvent e) {
158 log_frame_.addWindowListener(l);
160 log_frame_.setSize(300, 500);
161 log_frame_.setVisible(true);
165 // Here goes the future load balancing functionality
166 // public void startNewKernel() {
167 // implement load balancing
169 protected void finalize() throws Throwable {
170 super.finalize(); // gosling p.47
171 // Objects created in run method are finalized when
172 // program terminates and thread exits
173 while (thread_sml_.isAlive()) {
174 thread_sml_.interrupt();
175 log(1, "waiting for SML thread to quit..");
177 bridge_logger_.log(1, "finished");
178 bridge_logger_.close();
182 * Log a message with a specified severity level
184 * @param level severity level
185 * @param msg the message to be logged
187 public void log(int level, String msg) {
188 if (BridgeMainPaths.LOG_TO_FILE) {
189 this.bridge_logger_.log(level, msg);
191 if (BridgeMainPaths.SHOW_GUI_LOGGER) {
192 this.text_area_.append(msg + "\n");
196 public BufferedReader getSmlReader() {
197 //if (logger.isDebugEnabled())
198 // logger.debug("getSmlReader x");
202 public String getPath() {
206 public void setRMI(BridgeRMI bridgeRMI) {
207 if (logger.isDebugEnabled()) {
208 logger.debug("setRMI: bridge_rmi_=" + bridgeRMI);
210 bridgeRMI.setRmiID(1); // only one BridgeRMI used so far
213 boolean isRestoring() {
218 * @param args commandline arguments (exactly 4 expected: _iniPath _host _port _dtdPath)
221 public static void main(String[] args) {
223 System.out.println("Starting Bridge...");
225 String ini_load_result = BridgeMainPaths.loadFromFile(args[0]);
227 if (ini_load_result != null) {
228 ini_load_result += BridgeMainPaths.loadFromResource(args[0]);
231 if (ini_load_result != null) {
232 System.err.println(ini_load_result);
236 // initialize bridge main part
237 BridgeMain bridge = new BridgeMain(BridgeMainPaths.LOG_PATH, BridgeMainPaths.ISABELLE_HOME);
239 // Use ObjectManagerRMISocketFactory
241 RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory(BridgeMainPaths.BRIDGE_RMI_PORT));
243 // setup rmi connection to to the clients
244 BridgeRMI rmi = new BridgeRMI(bridge, BridgeMainPaths.HOST,
245 BridgeMainPaths.BRIDGE_MAIN_PORT, BridgeMainPaths.DTD_PATH);
247 // System.out.println( rmi );
250 } catch (RemoteException e) {
251 System.out.println("Could not instantiate RMI: exiting");
253 } catch (IOException e) {
254 System.out.println("Exception: " + e.getMessage());