Merge pull request #1419 from eclipse/cd_issue1410

[#1410] added java stub support for web
This commit is contained in:
Christian Dietrich 2020-03-16 16:27:46 +01:00 committed by GitHub
commit 62fa1c581f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 696 additions and 314 deletions

View file

@ -675,78 +675,165 @@ class WebIntegrationFragment extends AbstractXtextGeneratorFragment {
}
protected def void generateServerLauncher() {
fileAccessFactory.createXtendFile(grammar.serverLauncherClass, '''
/**
* 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 «grammar.serverLauncherClass.simpleName» {
def static void main(String[] args) {
val server = new «'org.eclipse.jetty.server.Server'.typeRef»(new «'java.net.InetSocketAddress'.typeRef»('localhost', 8080))
server.handler = new «'org.eclipse.jetty.webapp.WebAppContext'.typeRef» => [
resourceBase = '«projectConfig.web.assets.path.replace(projectConfig.web.root.path + "/", "")»'
welcomeFiles = #["index.html"]
contextPath = "/"
configurations = #[
new «'org.eclipse.jetty.annotations.AnnotationConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.WebXmlConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.MetaInfConfiguration'.typeRef»
if (codeConfig.isPreferXtendStubs) {
fileAccessFactory.createXtendFile(grammar.serverLauncherClass, '''
/**
* 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 «grammar.serverLauncherClass.simpleName» {
def static void main(String[] args) {
val server = new «'org.eclipse.jetty.server.Server'.typeRef»(new «'java.net.InetSocketAddress'.typeRef»('localhost', 8080))
server.handler = new «'org.eclipse.jetty.webapp.WebAppContext'.typeRef» => [
resourceBase = '«projectConfig.web.assets.path.replace(projectConfig.web.root.path + "/", "")»'
welcomeFiles = #["index.html"]
contextPath = "/"
configurations = #[
new «'org.eclipse.jetty.annotations.AnnotationConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.WebXmlConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef»,
new «'org.eclipse.jetty.webapp.MetaInfConfiguration'.typeRef»
]
setAttribute(«'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef».CONTAINER_JAR_PATTERN, '.*/«projectConfig.web.name.replace('.', '\\\\.')»/.*,.*\\.jar')
setInitParameter("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false")
]
setAttribute(«'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef».CONTAINER_JAR_PATTERN, '.*/«projectConfig.web.name.replace('.', '\\\\.')»/.*,.*\\.jar')
setInitParameter("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false")
]
val log = new «'org.eclipse.jetty.util.log.Slf4jLog'.typeRef»(«grammar.serverLauncherClass.simpleName».name)
try {
server.start
log.info('Server started ' + server.getURI + '...')
new Thread[
log.info('Press enter to stop the server...')
val key = System.in.read
if (key != -1) {
server.stop
} else {
log.warn('Console input is not available. In order to stop the server, you need to cancel process manually.')
}
].start
server.join
} catch (Exception exception) {
log.warn(exception.message)
System.exit(1)
val log = new «'org.eclipse.jetty.util.log.Slf4jLog'.typeRef»(«grammar.serverLauncherClass.simpleName».name)
try {
server.start
log.info('Server started ' + server.getURI + '...')
new Thread[
log.info('Press enter to stop the server...')
val key = System.in.read
if (key != -1) {
server.stop
} else {
log.warn('Console input is not available. In order to stop the server, you need to cancel process manually.')
}
].start
server.join
} catch (Exception exception) {
log.warn(exception.message)
System.exit(1)
}
}
}
}
''').writeTo(projectConfig.web.src)
''').writeTo(projectConfig.web.src)
} else {
fileAccessFactory.createJavaFile(grammar.serverLauncherClass, '''
/**
* 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/
*/
public class «grammar.serverLauncherClass.simpleName» {
public static void main(String[] args) {
«'org.eclipse.jetty.server.Server'.typeRef» server = new «'org.eclipse.jetty.server.Server'.typeRef»(new «'java.net.InetSocketAddress'.typeRef»("localhost", 8080));
«'org.eclipse.jetty.webapp.WebAppContext'.typeRef» ctx = new «'org.eclipse.jetty.webapp.WebAppContext'.typeRef»();
ctx.setResourceBase("WebRoot");
ctx.setWelcomeFiles(new String[] {"index.html"});
ctx.setContextPath("/");
ctx.setConfigurations(new «'org.eclipse.jetty.webapp.Configuration'.typeRef»[] {
new «'org.eclipse.jetty.annotations.AnnotationConfiguration'.typeRef»(),
new «'org.eclipse.jetty.webapp.WebXmlConfiguration'.typeRef»(),
new «'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef»(),
new «'org.eclipse.jetty.webapp.MetaInfConfiguration'.typeRef»()
});
ctx.setAttribute(«'org.eclipse.jetty.webapp.WebInfConfiguration'.typeRef».CONTAINER_JAR_PATTERN,
".*/«projectConfig.web.name.replace('.', '\\\\.')»/.*,.*\\.jar");
ctx.setInitParameter("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
server.setHandler(ctx);
«'org.eclipse.jetty.util.log.Slf4jLog'.typeRef» log = new «'org.eclipse.jetty.util.log.Slf4jLog'.typeRef»(«grammar.serverLauncherClass.simpleName».class.getName());
try {
server.start();
log.info("Server started " + server.getURI() + "...");
new Thread() {
public void run() {
try {
log.info("Press enter to stop the server...");
int key = System.in.read();
if (key != -1) {
server.stop();
} else {
log.warn(
"Console input is not available. In order to stop the server, you need to cancel process manually.");
}
} catch (Exception e) {
log.warn(e);
}
}
}.start();
server.join();
} catch (Exception exception) {
log.warn(exception.getMessage());
System.exit(1);
}
}
}
''').writeTo(projectConfig.web.src)
}
}
protected def void generateServlet() {
fileAccessFactory.createXtendFile(grammar.servletClass, '''
/**
* Deploy this class into a servlet container to enable DSL-specific services.
*/
«IF useServlet3Api»
@«new TypeReference("javax.servlet.annotation.WebServlet")»(name = 'XtextServices', urlPatterns = '/xtext-service/*')
«ENDIF»
class «grammar.servletClass.simpleName» extends «'org.eclipse.xtext.web.servlet.XtextServlet'.typeRef» {
«DisposableRegistry» disposableRegistry
override init() {
super.init()
val injector = new «grammar.webSetup»().createInjectorAndDoEMFRegistration()
disposableRegistry = injector.getInstance(«DisposableRegistry»)
}
override destroy() {
if (disposableRegistry !== null) {
disposableRegistry.dispose()
disposableRegistry = null
if (codeConfig.isPreferXtendStubs) {
fileAccessFactory.createXtendFile(grammar.servletClass, '''
/**
* Deploy this class into a servlet container to enable DSL-specific services.
*/
«IF useServlet3Api»
@«new TypeReference("javax.servlet.annotation.WebServlet")»(name = 'XtextServices', urlPatterns = '/xtext-service/*')
«ENDIF»
class «grammar.servletClass.simpleName» extends «'org.eclipse.xtext.web.servlet.XtextServlet'.typeRef» {
«DisposableRegistry» disposableRegistry
override init() {
super.init()
val injector = new «grammar.webSetup»().createInjectorAndDoEMFRegistration()
disposableRegistry = injector.getInstance(«DisposableRegistry»)
}
super.destroy()
override destroy() {
if (disposableRegistry !== null) {
disposableRegistry.dispose()
disposableRegistry = null
}
super.destroy()
}
}
}
''').writeTo(projectConfig.web.src)
''').writeTo(projectConfig.web.src)
} else {
fileAccessFactory.createJavaFile(grammar.servletClass, '''
/**
* Deploy this class into a servlet container to enable DSL-specific services.
*/
«IF useServlet3Api»
@«new TypeReference("javax.servlet.annotation.WebServlet")»(name = "XtextServices", urlPatterns = "/xtext-service/*")
«ENDIF»
public class «grammar.servletClass.simpleName» extends «'org.eclipse.xtext.web.servlet.XtextServlet'.typeRef» {
«DisposableRegistry» disposableRegistry;
public void init() throws «'javax.servlet.ServletException'.typeRef» {
super.init();
«'com.google.inject.Injector'.typeRef» injector = new «grammar.webSetup»().createInjectorAndDoEMFRegistration();
this.disposableRegistry = injector.getInstance(«DisposableRegistry».class);
}
public void destroy() {
if (disposableRegistry != null) {
disposableRegistry.dispose();
disposableRegistry = null;
}
super.destroy();
}
}
''').writeTo(projectConfig.web.src)
}
}
protected def void generateWebXml() {

View file

@ -1520,261 +1520,556 @@ public class WebIntegrationFragment extends AbstractXtextGeneratorFragment {
}
protected void generateServerLauncher() {
TypeReference _serverLauncherClass = this.getServerLauncherClass(this.getGrammar());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* This program starts an HTTP server for testing the web integration of your DSL.");
_builder.newLine();
_builder.append(" ");
_builder.append("* Just execute it and point a web browser to http://localhost:8080/");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("class ");
String _simpleName = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("def static void main(String[] args) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val server = new ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.jetty.server.Server");
_builder.append(_typeRef, "\t\t");
_builder.append("(new ");
TypeReference _typeRef_1 = TypeReference.typeRef("java.net.InetSocketAddress");
_builder.append(_typeRef_1, "\t\t");
_builder.append("(\'localhost\', 8080))");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("server.handler = new ");
TypeReference _typeRef_2 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebAppContext");
_builder.append(_typeRef_2, "\t\t");
_builder.append(" => [");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("resourceBase = \'");
String _path = WebIntegrationFragment.this.getProjectConfig().getWeb().getAssets().getPath();
String _path_1 = WebIntegrationFragment.this.getProjectConfig().getWeb().getRoot().getPath();
String _plus = (_path_1 + "/");
String _replace = _path.replace(_plus, "");
_builder.append(_replace, "\t\t\t");
_builder.append("\'");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("welcomeFiles = #[\"index.html\"]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("contextPath = \"/\"");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("configurations = #[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_3 = TypeReference.typeRef("org.eclipse.jetty.annotations.AnnotationConfiguration");
_builder.append(_typeRef_3, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_4 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebXmlConfiguration");
_builder.append(_typeRef_4, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_5 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_5, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_6 = TypeReference.typeRef("org.eclipse.jetty.webapp.MetaInfConfiguration");
_builder.append(_typeRef_6, "\t\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAttribute(");
TypeReference _typeRef_7 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_7, "\t\t\t");
_builder.append(".CONTAINER_JAR_PATTERN, \'.*/");
String _replace_1 = WebIntegrationFragment.this.getProjectConfig().getWeb().getName().replace(".", "\\\\.");
_builder.append(_replace_1, "\t\t\t");
_builder.append("/.*,.*\\\\.jar\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("setInitParameter(\"org.mortbay.jetty.servlet.Default.useFileMappedBuffer\", \"false\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val log = new ");
TypeReference _typeRef_8 = TypeReference.typeRef("org.eclipse.jetty.util.log.Slf4jLog");
_builder.append(_typeRef_8, "\t\t");
_builder.append("(");
String _simpleName_1 = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName_1, "\t\t");
_builder.append(".name)");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.start");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.info(\'Server started \' + server.getURI + \'...\')");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("new Thread[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("log.info(\'Press enter to stop the server...\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("val key = System.in.read");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (key != -1) {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("server.stop");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("log.warn(\'Console input is not available. In order to stop the server, you need to cancel process manually.\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("].start");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.join");
_builder.newLine();
_builder.append("\t\t");
_builder.append("} catch (Exception exception) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.warn(exception.message)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("System.exit(1)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createXtendFile(_serverLauncherClass, _client).writeTo(this.getProjectConfig().getWeb().getSrc());
boolean _isPreferXtendStubs = this.codeConfig.isPreferXtendStubs();
if (_isPreferXtendStubs) {
TypeReference _serverLauncherClass = this.getServerLauncherClass(this.getGrammar());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* This program starts an HTTP server for testing the web integration of your DSL.");
_builder.newLine();
_builder.append(" ");
_builder.append("* Just execute it and point a web browser to http://localhost:8080/");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("class ");
String _simpleName = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("def static void main(String[] args) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val server = new ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.jetty.server.Server");
_builder.append(_typeRef, "\t\t");
_builder.append("(new ");
TypeReference _typeRef_1 = TypeReference.typeRef("java.net.InetSocketAddress");
_builder.append(_typeRef_1, "\t\t");
_builder.append("(\'localhost\', 8080))");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("server.handler = new ");
TypeReference _typeRef_2 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebAppContext");
_builder.append(_typeRef_2, "\t\t");
_builder.append(" => [");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("resourceBase = \'");
String _path = WebIntegrationFragment.this.getProjectConfig().getWeb().getAssets().getPath();
String _path_1 = WebIntegrationFragment.this.getProjectConfig().getWeb().getRoot().getPath();
String _plus = (_path_1 + "/");
String _replace = _path.replace(_plus, "");
_builder.append(_replace, "\t\t\t");
_builder.append("\'");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("welcomeFiles = #[\"index.html\"]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("contextPath = \"/\"");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("configurations = #[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_3 = TypeReference.typeRef("org.eclipse.jetty.annotations.AnnotationConfiguration");
_builder.append(_typeRef_3, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_4 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebXmlConfiguration");
_builder.append(_typeRef_4, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_5 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_5, "\t\t\t\t");
_builder.append(",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t\t");
_builder.append("new ");
TypeReference _typeRef_6 = TypeReference.typeRef("org.eclipse.jetty.webapp.MetaInfConfiguration");
_builder.append(_typeRef_6, "\t\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAttribute(");
TypeReference _typeRef_7 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_7, "\t\t\t");
_builder.append(".CONTAINER_JAR_PATTERN, \'.*/");
String _replace_1 = WebIntegrationFragment.this.getProjectConfig().getWeb().getName().replace(".", "\\\\.");
_builder.append(_replace_1, "\t\t\t");
_builder.append("/.*,.*\\\\.jar\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("setInitParameter(\"org.mortbay.jetty.servlet.Default.useFileMappedBuffer\", \"false\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val log = new ");
TypeReference _typeRef_8 = TypeReference.typeRef("org.eclipse.jetty.util.log.Slf4jLog");
_builder.append(_typeRef_8, "\t\t");
_builder.append("(");
String _simpleName_1 = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName_1, "\t\t");
_builder.append(".name)");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.start");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.info(\'Server started \' + server.getURI + \'...\')");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("new Thread[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("log.info(\'Press enter to stop the server...\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("val key = System.in.read");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (key != -1) {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("server.stop");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("log.warn(\'Console input is not available. In order to stop the server, you need to cancel process manually.\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("].start");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.join");
_builder.newLine();
_builder.append("\t\t");
_builder.append("} catch (Exception exception) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.warn(exception.message)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("System.exit(1)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createXtendFile(_serverLauncherClass, _client).writeTo(this.getProjectConfig().getWeb().getSrc());
} else {
TypeReference _serverLauncherClass_1 = this.getServerLauncherClass(this.getGrammar());
StringConcatenationClient _client_1 = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* This program starts an HTTP server for testing the web integration of your DSL.");
_builder.newLine();
_builder.append(" ");
_builder.append("* Just execute it and point a web browser to http://localhost:8080/");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("public class ");
String _simpleName = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("public static void main(String[] args) {");
_builder.newLine();
_builder.append("\t\t");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.jetty.server.Server");
_builder.append(_typeRef, "\t\t");
_builder.append(" server = new ");
TypeReference _typeRef_1 = TypeReference.typeRef("org.eclipse.jetty.server.Server");
_builder.append(_typeRef_1, "\t\t");
_builder.append("(new ");
TypeReference _typeRef_2 = TypeReference.typeRef("java.net.InetSocketAddress");
_builder.append(_typeRef_2, "\t\t");
_builder.append("(\"localhost\", 8080));");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
TypeReference _typeRef_3 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebAppContext");
_builder.append(_typeRef_3, "\t\t");
_builder.append(" ctx = new ");
TypeReference _typeRef_4 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebAppContext");
_builder.append(_typeRef_4, "\t\t");
_builder.append("();");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("ctx.setResourceBase(\"WebRoot\");");
_builder.newLine();
_builder.append("\t\t");
_builder.append("ctx.setWelcomeFiles(new String[] {\"index.html\"});");
_builder.newLine();
_builder.append("\t\t");
_builder.append("ctx.setContextPath(\"/\");");
_builder.newLine();
_builder.append("\t\t");
_builder.append("ctx.setConfigurations(new ");
TypeReference _typeRef_5 = TypeReference.typeRef("org.eclipse.jetty.webapp.Configuration");
_builder.append(_typeRef_5, "\t\t");
_builder.append("[] {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("new ");
TypeReference _typeRef_6 = TypeReference.typeRef("org.eclipse.jetty.annotations.AnnotationConfiguration");
_builder.append(_typeRef_6, "\t\t\t");
_builder.append("(),");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("new ");
TypeReference _typeRef_7 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebXmlConfiguration");
_builder.append(_typeRef_7, "\t\t\t");
_builder.append("(),");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("new ");
TypeReference _typeRef_8 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_8, "\t\t\t");
_builder.append("(),");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("new ");
TypeReference _typeRef_9 = TypeReference.typeRef("org.eclipse.jetty.webapp.MetaInfConfiguration");
_builder.append(_typeRef_9, "\t\t\t");
_builder.append("()");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("});");
_builder.newLine();
_builder.append("\t\t");
_builder.append("ctx.setAttribute(");
TypeReference _typeRef_10 = TypeReference.typeRef("org.eclipse.jetty.webapp.WebInfConfiguration");
_builder.append(_typeRef_10, "\t\t");
_builder.append(".CONTAINER_JAR_PATTERN,");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("\".*/");
String _replace = WebIntegrationFragment.this.getProjectConfig().getWeb().getName().replace(".", "\\\\.");
_builder.append(_replace, "\t\t\t");
_builder.append("/.*,.*\\\\.jar\");");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("ctx.setInitParameter(\"org.mortbay.jetty.servlet.Default.useFileMappedBuffer\", \"false\");");
_builder.newLine();
_builder.append("\t\t");
_builder.append("server.setHandler(ctx);");
_builder.newLine();
_builder.append("\t\t");
TypeReference _typeRef_11 = TypeReference.typeRef("org.eclipse.jetty.util.log.Slf4jLog");
_builder.append(_typeRef_11, "\t\t");
_builder.append(" log = new ");
TypeReference _typeRef_12 = TypeReference.typeRef("org.eclipse.jetty.util.log.Slf4jLog");
_builder.append(_typeRef_12, "\t\t");
_builder.append("(");
String _simpleName_1 = WebIntegrationFragment.this.getServerLauncherClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName_1, "\t\t");
_builder.append(".class.getName());");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.start();");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.info(\"Server started \" + server.getURI() + \"...\");");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("new Thread() {");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("public void run() {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("log.info(\"Press enter to stop the server...\");");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("int key = System.in.read();");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("if (key != -1) {");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t");
_builder.append("server.stop();");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t");
_builder.append("log.warn(");
_builder.newLine();
_builder.append("\t\t\t\t\t\t\t\t\t");
_builder.append("\"Console input is not available. In order to stop the server, you need to cancel process manually.\");");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("} catch (Exception e) {");
_builder.newLine();
_builder.append("\t\t\t\t\t\t");
_builder.append("log.warn(e);");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("}.start();");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("server.join();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("} catch (Exception exception) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("log.warn(exception.getMessage());");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("System.exit(1);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createJavaFile(_serverLauncherClass_1, _client_1).writeTo(this.getProjectConfig().getWeb().getSrc());
}
}
protected void generateServlet() {
TypeReference _servletClass = this.getServletClass(this.getGrammar());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Deploy this class into a servlet container to enable DSL-specific services.");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
{
if (WebIntegrationFragment.this.useServlet3Api) {
_builder.append("@");
TypeReference _typeReference = new TypeReference("javax.servlet.annotation.WebServlet");
_builder.append(_typeReference);
_builder.append("(name = \'XtextServices\', urlPatterns = \'/xtext-service/*\')");
_builder.newLineIfNotEmpty();
boolean _isPreferXtendStubs = this.codeConfig.isPreferXtendStubs();
if (_isPreferXtendStubs) {
TypeReference _servletClass = this.getServletClass(this.getGrammar());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Deploy this class into a servlet container to enable DSL-specific services.");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
{
if (WebIntegrationFragment.this.useServlet3Api) {
_builder.append("@");
TypeReference _typeReference = new TypeReference("javax.servlet.annotation.WebServlet");
_builder.append(_typeReference);
_builder.append("(name = \'XtextServices\', urlPatterns = \'/xtext-service/*\')");
_builder.newLineIfNotEmpty();
}
}
_builder.append("class ");
String _simpleName = WebIntegrationFragment.this.getServletClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.xtext.web.servlet.XtextServlet");
_builder.append(_typeRef);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append(DisposableRegistry.class, "\t");
_builder.append(" disposableRegistry");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override init() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("super.init()");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val injector = new ");
TypeReference _webSetup = WebIntegrationFragment.this._xtextGeneratorNaming.getWebSetup(WebIntegrationFragment.this.getGrammar());
_builder.append(_webSetup, "\t\t");
_builder.append("().createInjectorAndDoEMFRegistration()");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("disposableRegistry = injector.getInstance(");
_builder.append(DisposableRegistry.class, "\t\t");
_builder.append(")");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override destroy() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (disposableRegistry !== null) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry.dispose()");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry = null");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("super.destroy()");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
_builder.append("class ");
String _simpleName = WebIntegrationFragment.this.getServletClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.xtext.web.servlet.XtextServlet");
_builder.append(_typeRef);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append(DisposableRegistry.class, "\t");
_builder.append(" disposableRegistry");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override init() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("super.init()");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val injector = new ");
TypeReference _webSetup = WebIntegrationFragment.this._xtextGeneratorNaming.getWebSetup(WebIntegrationFragment.this.getGrammar());
_builder.append(_webSetup, "\t\t");
_builder.append("().createInjectorAndDoEMFRegistration()");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("disposableRegistry = injector.getInstance(");
_builder.append(DisposableRegistry.class, "\t\t");
_builder.append(")");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override destroy() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (disposableRegistry !== null) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry.dispose()");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry = null");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("super.destroy()");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createXtendFile(_servletClass, _client).writeTo(this.getProjectConfig().getWeb().getSrc());
};
this.fileAccessFactory.createXtendFile(_servletClass, _client).writeTo(this.getProjectConfig().getWeb().getSrc());
} else {
TypeReference _servletClass_1 = this.getServletClass(this.getGrammar());
StringConcatenationClient _client_1 = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Deploy this class into a servlet container to enable DSL-specific services.");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
{
if (WebIntegrationFragment.this.useServlet3Api) {
_builder.append("@");
TypeReference _typeReference = new TypeReference("javax.servlet.annotation.WebServlet");
_builder.append(_typeReference);
_builder.append("(name = \"XtextServices\", urlPatterns = \"/xtext-service/*\")");
_builder.newLineIfNotEmpty();
}
}
_builder.append("public class ");
String _simpleName = WebIntegrationFragment.this.getServletClass(WebIntegrationFragment.this.getGrammar()).getSimpleName();
_builder.append(_simpleName);
_builder.append(" extends ");
TypeReference _typeRef = TypeReference.typeRef("org.eclipse.xtext.web.servlet.XtextServlet");
_builder.append(_typeRef);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append(DisposableRegistry.class, "\t");
_builder.append(" disposableRegistry;");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public void init() throws ");
TypeReference _typeRef_1 = TypeReference.typeRef("javax.servlet.ServletException");
_builder.append(_typeRef_1, "\t");
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("super.init();");
_builder.newLine();
_builder.append("\t\t");
TypeReference _typeRef_2 = TypeReference.typeRef("com.google.inject.Injector");
_builder.append(_typeRef_2, "\t\t");
_builder.append(" injector = new ");
TypeReference _webSetup = WebIntegrationFragment.this._xtextGeneratorNaming.getWebSetup(WebIntegrationFragment.this.getGrammar());
_builder.append(_webSetup, "\t\t");
_builder.append("().createInjectorAndDoEMFRegistration();");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("this.disposableRegistry = injector.getInstance(");
_builder.append(DisposableRegistry.class, "\t\t");
_builder.append(".class);");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public void destroy() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (disposableRegistry != null) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry.dispose();");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("disposableRegistry = null;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("super.destroy();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
}
};
this.fileAccessFactory.createJavaFile(_servletClass_1, _client_1).writeTo(this.getProjectConfig().getWeb().getSrc());
}
}
protected void generateWebXml() {