aboutsummaryrefslogtreecommitdiffstats
path: root/rba.tool.editor.endpoint/src/rba/tool/editor/endpoint/ServerLauncher.xtend
blob: d689919d99f7fa1f206ea905b7504efca15bcdb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
	}
	
}