From be4f78978faba3d3ceb88df02a7f93a2e09ff1e0 Mon Sep 17 00:00:00 2001 From: Kenji Hosokawa Date: Tue, 3 Aug 2021 18:42:39 +0900 Subject: Initial commit Bug-AGL: SPEC-4033 Signed-off-by: Kenji Hosokawa --- .../rba/tool/editor/endpoint/ServerLauncher.xtend | 213 +++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend (limited to 'rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend') diff --git a/rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend b/rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend new file mode 100644 index 0000000..d689919 --- /dev/null +++ b/rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend @@ -0,0 +1,213 @@ +/* + * generated by Xtext 2.13.0 + */ +package rba.tool.editor.endpoint + +import java.io.IOException +import java.net.InetSocketAddress +import java.nio.file.NoSuchFileException +import org.eclipse.emf.common.util.URI +import org.eclipse.emf.common.util.WrappedException +import org.eclipse.jetty.annotations.AnnotationConfiguration +import org.eclipse.jetty.client.HttpClient +import org.eclipse.jetty.server.Server +import org.eclipse.jetty.webapp.MetaInfConfiguration +import org.eclipse.jetty.webapp.WebAppContext +import org.eclipse.jetty.webapp.WebInfConfiguration +import org.eclipse.jetty.webapp.WebXmlConfiguration +import rba.tool.editor.endpoint.server.persistence.RBAModelResourceBaseProviderImpl + +import java.io.File; +import org.eclipse.jetty.util.log.*; +import java.nio.charset.StandardCharsets; +import java.io.OutputStreamWriter +import java.io.FileOutputStream +import java.io.BufferedReader +import java.io.InputStreamReader +import java.io.FileInputStream +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +/** + * This program starts an HTTP server for testing the web integration of your DSL. + * Just execute it and point a web browser to http://localhost:8080/ + */ +class ServerLauncher { + + private static String HOST_NAME = 'localhost'; + private static int PORT_NUMBER = 18080; + private static String GENERATE_REQUEST_URL = 'http://%s:%d/xtext-service/generate-all?resource=%s'; + + def static void main(String[] args) { + var successFile = new File(new File(ServerLauncher.getProtectionDomain().getCodeSource().getLocation().toURI()).parent + "/" + "success") + if(successFile.exists) + { + successFile.delete + } + System.setProperty("org.eclipse.jetty.util.log.class", NoLogger.name); + + val server = new Server(new InetSocketAddress(HOST_NAME, PORT_NUMBER)) + server.handler = new WebAppContext => [ + resourceBase = '.' + contextPath = "/" + configurations = #[ + new AnnotationConfiguration, + new WebXmlConfiguration, + new WebInfConfiguration, + new MetaInfConfiguration + ] + setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, '.*/rba\\.tool\\.editor\\.web/.*,.*\\.jar') + setInitParameter("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false") + ] + + try { + var outputDirStr = new File(ServerLauncher.getProtectionDomain().getCodeSource().getLocation().toURI()).parent; + if(args.length > 1) { + outputDirStr = args.get(1); + } + + var outputDir = new File(outputDirStr); + if (!outputDir.exists()) { + System.err.println("No such file or directory: " + outputDirStr) + System.exit(1) + return; + } + + server.start + + if (args !== null && args.length > 0) { + doRequest(args.get(0)) + } + + server.stop + server.join + + var successFile2 = new File(new File(ServerLauncher.getProtectionDomain().getCodeSource().getLocation().toURI()).parent + "/" + "success") + if(!successFile2.exists) + { + System.exit(1); + } + successFile2.delete + + var srcDir = new File(ServerLauncher.getProtectionDomain().getCodeSource().getLocation().toURI()).parent; + var jsonFile = new File(srcDir + "/template-gen/RBAModel.json"); + var tmpJsonFile = new File(srcDir + "/template-gen/RBAModel.json.tmp"); + jsonFile.renameTo(tmpJsonFile); + + var newJsonFile = new File(srcDir + "/template-gen/RBAModel.json"); + + var in = new BufferedReader(new InputStreamReader(new FileInputStream(tmpJsonFile))); + var writer = new OutputStreamWriter(new FileOutputStream(newJsonFile), StandardCharsets.UTF_8); + var line = ""; + while((line = in.readLine()) !== null) { + writer.write(line); + if(in.ready()){ + writer.write("\n"); + } + } + writer.flush(); + writer.close(); + in.close(); + + tmpJsonFile.delete; + + var targetFile = new File(outputDirStr + "/RBAModel.json"); + if(newJsonFile != targetFile) { + jsonFile.renameTo(targetFile); + } + + System.exit(0); + + } catch (Exception exception) { + System.err.println(exception.message) + System.exit(1) + } + } + + def static void doRequest(String resourceId) { + try { + val uri = URI.createURI(RBAModelResourceBaseProviderImpl.slashify(resourceId, true)) + if (uri === null) + throw new IOException('The requested resource does not exist.') + + val client = new HttpClient(); + client.start(); + val String resourceId_URLEnc = URLEncoder.encode(resourceId, "UTF-8"); + val res = client.GET(String.format(GENERATE_REQUEST_URL, HOST_NAME, PORT_NUMBER, resourceId_URLEnc)) + client.stop() + } catch (NoSuchFileException exception) { + exception.printStackTrace + System.err.format("%s: no such file or directory%n", resourceId); + throw exception.cause + } catch (IOException exception) { + System.err.format("%s%n", exception); + throw exception.cause + } catch (WrappedException exception) { + throw exception.cause + } + } +} + +class NoLogger implements Logger { + + override debug(Throwable thrown) { + return + } + + override debug(String arg0, Object... arg1) { + return + } + + override debug(String arg0, long arg1) { + return + } + + override debug(String arg0, Throwable arg1) { + return + } + + override getLogger(String name) { + return this + } + + override getName() { + return "nothing" + } + + override ignore(Throwable ignored) { + return + } + + override info(Throwable thrown) { + return + } + + override info(String arg0, Object... arg1) { + return + } + + override info(String arg0, Throwable arg1) { + return + } + + override isDebugEnabled() { + return false + } + + override setDebugEnabled(boolean enabled) { + return + } + + override warn(Throwable thrown) { + return + } + + override warn(String arg0, Object... arg1) { + return + } + + override warn(String arg0, Throwable arg1) { + return + } + +} -- cgit 1.2.3-korg