11package org .dataflowanalysis .standalone ;
22
3- import java .awt .Desktop ;
4- import java .io .BufferedReader ;
5- import java .io .File ;
63import java .util .Scanner ;
7- import java .io .InputStreamReader ;
8- import java .net .URI ;
9- import java .net .URL ;
10- import org .eclipse .core .runtime .Path ;
11- import java .time .Duration ;
12- import java .util .Iterator ;
13- import java .util .regex .Matcher ;
14- import java .util .regex .Pattern ;
15- import java .util .stream .Collectors ;
16- import java .util .stream .Stream ;
174
18- import org .eclipse .core .runtime .FileLocator ;
19- import org .eclipse .core .runtime .IPath ;
20- import org .eclipse .core .runtime .Platform ;
5+ import org .dataflowanalysis .standalone .frontend .FrontendUtils ;
6+ import org .dataflowanalysis .standalone .websocket .WebSocketServerUtils ;
217import org .eclipse .equinox .app .IApplication ;
228import org .eclipse .equinox .app .IApplicationContext ;
23- import org .eclipse .jetty .server .Server ;
24- import org .eclipse .jetty .server .ServerConnector ;
25- import org .eclipse .jetty .servlet .ServletContextHandler ;
26- import org .eclipse .jetty .websocket .server .config .JettyWebSocketServletContainerInitializer ;
27- import org .osgi .framework .Bundle ;
289
2910public class Application implements IApplication {
30- private static String editorPath = "WebEditor/" ;
31-
32- private static BufferedReader reader ;
33-
34- private static Process frontEnd ;
35- private static Thread webSocketServer ;
36- private static final String npmCommand = System .getProperty ("os.name" ).toLowerCase ().contains ("win" ) ? "npm.cmd" : "npm" ;
37-
38-
3911 @ Override
40- public Object start (IApplicationContext context ) throws Exception {
41-
12+ public Object start (IApplicationContext context ) throws Exception {
4213 try {
43- Bundle bundle = Platform .getBundle ("org.dataflowanalysis.standalone" );
44-
45- IPath path = new Path ("" );
46-
47- URL url = FileLocator .find (bundle , path , null );
48- URL fileUrl = FileLocator .toFileURL (url );
49-
50- editorPath = fileUrl .getPath ().toString () + "WebEditor/" ;
51-
52- System .out .println (editorPath );
53-
54- runCommand ("git init" , editorPath );
55- runCommand (npmCommand + " install" , editorPath );
56-
57- Thread frontEnd = new Thread (() -> startFrontendServer ());
58- webSocketServer = new Thread (() -> startWebSocketServer ());
59- webSocketServer .start ();
60- frontEnd .start ();
14+
15+ Thread frontend = FrontendUtils .startFrontendServerForApplication ();
16+ Thread webSocketServer = WebSocketServerUtils .startWebSocketServer ();
6117
6218
6319 Scanner scanner = new Scanner (System .in );
@@ -68,15 +24,15 @@ public Object start(IApplicationContext context) throws Exception {
6824 // Loop until user types "exit"
6925
7026
71- while (frontEnd .isAlive () || webSocketServer .isAlive ()) {
27+ while (frontend .isAlive () || webSocketServer .isAlive ()) {
7228 while (!input .equalsIgnoreCase ("exit" )) {
7329 input = scanner .nextLine ();
7430 }
75- EventEndpoint . shutDownFrontEnd ();
76- return IApplication . EXIT_OK ;
77-
31+ FrontendUtils . stopFrontendServer ();
32+ scanner . close () ;
33+ return IApplication . EXIT_OK ;
7834 };
79-
35+ scanner . close ();
8036 } catch (Exception e ) {
8137 e .printStackTrace ();
8238 }
@@ -85,140 +41,6 @@ public Object start(IApplicationContext context) throws Exception {
8541
8642 @ Override
8743 public void stop () {
88- EventEndpoint .shutDownFrontEnd ();
89- }
90-
91- private static void startWebSocketServer () {
92- try {
93- var server = new Server ();
94- var connector = new ServerConnector (server );
95- server .addConnector (connector );
96-
97- // Setup the basic application "context" for this application at "/"
98- // This is also known as the handler tree (in jetty speak)
99- ServletContextHandler context = new ServletContextHandler (ServletContextHandler .SESSIONS );
100- context .setContextPath ("/" );
101- server .setHandler (context );
102-
103- // Configure specific websocket behavior
104- JettyWebSocketServletContainerInitializer .configure (context , (servletContext , wsContainer ) ->
105- {
106- // Configure default max size
107- wsContainer .setMaxTextMessageSize (Long .MAX_VALUE );
108- wsContainer .setIdleTimeout (Duration .ofMinutes (20 ));
109-
110- // Add websockets
111- wsContainer .addMapping ("/events/*" , EventEndpoint .class );
112- });
113- connector .setPort (3000 );
114- server .start ();
115- server .join ();
116-
117-
118- } catch (Exception e ) {
119- // TODO Auto-generated catch block
120- e .printStackTrace ();
121- }
122- }
123-
124- private static void startFrontendServer () {
125- try {
126- String command = npmCommand + " run dev" ;
127- System .out .println (command );
128-
129- // Split the command into arguments for ProcessBuilder
130- String [] commandParts = command .split (" " );
131-
132- ProcessBuilder builder = new ProcessBuilder (commandParts );
133-
134- builder .directory (new File (editorPath ));// Inherit the I/O of the current process to show the server logs
135-
136-
137- // Start the process
138- frontEnd = builder .start ();
139- var processReader = new BufferedReader (new InputStreamReader (frontEnd .getInputStream ()));
140-
141- while (frontEnd .isAlive ()) {
142- var line = processReader .readLine ();
143- if ( line != null && line .contains ("local" )) {
144- openBrowser (line );
145- }
146- }
147- // Wait for the process to finish (optional, or you can log/process the output)
148- int exitCode = frontEnd .waitFor ();
149- System .out .println ("Process exited with code: " + exitCode );
150- } catch (Exception e ) {
151- e .printStackTrace ();
152- }
44+ FrontendUtils .stopFrontendServer ();
15345 }
154-
155-
156-
157- // Method to run the command using ProcessBuilder
158- private static void runCommand (String command , String projectPath ) {
159- try {
160- System .out .println (command );
161-
162- // Split the command into arguments for ProcessBuilder
163- String [] commandParts = command .split (" " );
164-
165- ProcessBuilder builder = new ProcessBuilder (commandParts );
166-
167-
168- builder .directory (new File (projectPath ));// Inherit the I/O of the current process to show the server logs
169-
170-
171- // Start the process
172- Process process = builder .start ();
173- var processReader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
174- if (command .startsWith ("npm.cmd run" )) {
175- while (process .isAlive ()) {
176- var line = processReader .readLine ();
177- if (line .contains ("Local" )) {
178- openBrowser (line );
179- }
180- }
181- }
182- // Wait for the process to finish (optional, or you can log/process the output)
183- int exitCode = process .waitFor ();
184- System .out .println ("Process exited with code: " + exitCode );
185- } catch (Exception e ) {
186- e .printStackTrace ();
187- }
188- }
189-
190- private static void openBrowser (String full ) {
191- try {
192- String ansiRegex = "\\ u001B\\ [[;\\ d]*m" ;
193- String cleanedLine = full .replaceAll (ansiRegex , "" );
194-
195- // Define a regex to extract the port number
196- String regex = "http://localhost:(\\ d+)/" ;
197- Pattern pattern = Pattern .compile (regex );
198- Matcher matcher = pattern .matcher (cleanedLine );
199-
200- // Check if the pattern matches and extract the port
201- if (matcher .find ()) {
202- var url2 = new URI (matcher .group (0 ));
203- // Check if Desktop is supported
204- if (Desktop .isDesktopSupported ()) {
205- // Get the desktop instance and open the URL in the default browser
206- Desktop desktop = Desktop .getDesktop ();
207- if (desktop .isSupported (Desktop .Action .BROWSE )) {
208- desktop .browse (url2 );
209- } else {
210- System .out .println ("Browsing not supported on this platform." );
211- }
212- } else {
213- System .out .println ("Desktop is not supported on this platform." );
214- } // Extract the first matching group (the port number)
215- } else {
216- System .out .println ("No port found in the input." );
217- }
218- } catch (Exception e ) {
219- // TODO Auto-generated catch block
220- e .printStackTrace ();
221- }
222- }
223-
22446}
0 commit comments