Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.tools.eclipse.util.status.StatusUtil;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.File;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -113,8 +114,15 @@ private static IPath getFinalArtifactPath(IProject project) throws CoreException

String buildDirectory = mavenProject.getBuild().getDirectory();
String finalName = mavenProject.getBuild().getFinalName();
String finalArtifactPath = buildDirectory + "/" + finalName + "." + mavenProject.getPackaging();
return new Path(finalArtifactPath);
String finalNamePath = buildDirectory + "/" + finalName;

String oneJarArtifactPath = finalNamePath + ".one-jar.jar";
if (new File(oneJarArtifactPath).exists()) {
return new Path(oneJarArtifactPath);
} else {
String finalArtifactPath = finalNamePath + "." + mavenProject.getPackaging();
return new Path(finalArtifactPath);
}
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void testMaterializeAppEngineStandardFiles_pomXmlIfEnablingMaven()
public void testMaterializeAppEngineFlexFiles()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setServiceName("database-service");
IFile mostImportant = CodeTemplates.materializeAppEngineFlexFiles(project, config, monitor);
validateNonConfigFiles(mostImportant, "http://xmlns.jcp.org/xml/ns/javaee",
"http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd", "3.1");
Expand All @@ -124,19 +125,63 @@ public void testMaterializeAppEngineFlexFiles_pomXmlIfEnablingMaven()
validatePomXml();
}

@Test
public void testMaterializeAppEngineFlexJarFiles()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");
config.setServiceName("database-service");

IFile mostImportant = CodeTemplates.materializeAppEngineFlexJarFiles(project, config, monitor);

validateFlexJarNonConfigFiles(mostImportant);
validateAppYaml();
validatePomXml();
}

@Test
public void testMaterializeAppEngineFlexJarFiles_mainClassSet()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");
config.setPackageName("com.example");

IFile mostImportant = CodeTemplates.materializeAppEngineFlexJarFiles(project, config, monitor);
validateMainClassInPomXml("com.example", mostImportant);
}

@Test
public void testMaterializeAppEngineFlexJarFiles_defaultPackageMainClassSet()
throws CoreException, ParserConfigurationException, SAXException, IOException {
AppEngineProjectConfig config = new AppEngineProjectConfig();
config.setUseMaven("my.project.group.id", "my-project-artifact-id", "98.76.54");

IFile mostImportant = CodeTemplates.materializeAppEngineFlexJarFiles(project, config, monitor);
validateMainClassInPomXml(null /* expectedPackage */, mostImportant);
}

private void validateMainClassInPomXml(String expectedPackage, IFile mostImportant)
throws ParserConfigurationException, SAXException, IOException, CoreException {
String mainClassName = mostImportant.getFullPath().removeFileExtension().lastSegment();

IFile pomXml = project.getFile("pom.xml");
Element root = buildDocument(pomXml).getDocumentElement();
Element mainClass = (Element) root.getElementsByTagName("mainClass").item(0);
if (expectedPackage == null) {
Assert.assertEquals(mainClassName, mainClass.getTextContent());
} else {
Assert.assertEquals(expectedPackage + "." + mainClassName, mainClass.getTextContent());
}
}

private void validateNonConfigFiles(IFile mostImportant,
String webXmlNamespace, String webXmlSchemaUrl, String servletVersion)
throws ParserConfigurationException, SAXException, IOException, CoreException {
IFolder src = project.getFolder("src");
IFolder main = src.getFolder("main");
IFolder java = main.getFolder("java");
IFile servlet = java.getFile("HelloAppEngine.java");
IFile servlet = project.getFile("src/main/java/HelloAppEngine.java");
Assert.assertTrue(servlet.exists());
Assert.assertEquals(servlet, mostImportant);

IFolder webapp = main.getFolder("webapp");
IFolder webinf = webapp.getFolder("WEB-INF");
IFile webXml = webinf.getFile("web.xml");
IFile webXml = project.getFile("src/main/webapp/WEB-INF/web.xml");
Element root = buildDocument(webXml).getDocumentElement();
Assert.assertEquals("web-app", root.getNodeName());
Assert.assertEquals(webXmlNamespace, root.getNamespaceURI());
Expand All @@ -148,18 +193,29 @@ private void validateNonConfigFiles(IFile mostImportant,
Assert.assertEquals("HelloAppEngine", servletClass.getTextContent());
}

IFile htmlFile = webapp.getFile("index.html");
IFile htmlFile = project.getFile("src/main/webapp/index.html");
Element html = buildDocument(htmlFile).getDocumentElement();
Assert.assertEquals("html", html.getNodeName());

IFolder test = src.getFolder("test");
IFolder testJava = test.getFolder("java");
IFile servletTest = testJava.getFile("HelloAppEngineTest.java");
IFile servletTest = project.getFile("src/test/java/HelloAppEngineTest.java");
Assert.assertTrue(servletTest.exists());
IFile mockServletResponse = testJava.getFile("MockHttpServletResponse.java");
IFile mockServletResponse = project.getFile("src/test/java/MockHttpServletResponse.java");
Assert.assertTrue(mockServletResponse.exists());
}

private void validateFlexJarNonConfigFiles(IFile mostImportant) {
IFile main = project.getFile("src/main/java/HelloAppEngineMain.java");
Assert.assertTrue(main.exists());
Assert.assertEquals(main, mostImportant);
IFile handler = project.getFile("src/main/java/HelloAppEngineHandler.java");
Assert.assertTrue(handler.exists());

IFile handlerTest = project.getFile("src/test/java/HelloAppEngineHandlerTest.java");
Assert.assertTrue(handlerTest.exists());
IFile mockHttpExchange = project.getFile("src/test/java/MockHttpExchange.java");
Assert.assertTrue(mockHttpExchange.exists());
}

private void validateAppEngineWebXml(AppEngineRuntime runtime)
throws ParserConfigurationException, SAXException, IOException, CoreException {
IFolder webinf = project.getFolder("src/main/webapp/WEB-INF");
Expand Down Expand Up @@ -188,15 +244,14 @@ private void validateAppEngineWebXml(AppEngineRuntime runtime)
}

private void validateAppYaml() throws IOException, CoreException {
IFolder appengineFolder = project.getFolder("src/main/appengine");
Assert.assertTrue(appengineFolder.exists());
IFile appYaml = appengineFolder.getFile("app.yaml");
IFile appYaml = project.getFile("src/main/appengine/app.yaml");
Assert.assertTrue(appYaml.exists());

try (BufferedReader reader = new BufferedReader(
new InputStreamReader(appYaml.getContents(), StandardCharsets.UTF_8))) {
Assert.assertEquals("runtime: java", reader.readLine());
Assert.assertEquals("env: flex", reader.readLine());
Assert.assertEquals("service: database-service", reader.readLine());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static IFile materializeAppEngineFlexFiles(IProject project, AppEnginePro
return materialize(project, config, false /* isStandardProject */, monitor);
}

public static IFile materializeAppEngineFlexJarFiles(IProject project,
AppEngineProjectConfig config, IProgressMonitor monitor) throws CoreException {
return materializeFlexJar(project, config, monitor);
}

/**
* Creates files for a sample App Engine project in the supplied Eclipse project.
*
Expand Down Expand Up @@ -96,6 +101,20 @@ private static IFile materialize(IProject project, AppEngineProjectConfig config
return hello;
}

private static IFile materializeFlexJar(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, "Generating code", 30);

IFile hello = createFlexJarJavaSourceFiles(project, config, subMonitor.newChild(20));

boolean isStandardProject = false;
createAppEngineWebXmlOrAppYaml(project, config, isStandardProject, subMonitor.newChild(5));

createFlexJarPomXml(project, config, subMonitor.newChild(5));

return hello;
}

private static IFile createJavaSourceFiles(IProject project, AppEngineProjectConfig config,
boolean isStandardProject, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 15);
Expand All @@ -119,15 +138,44 @@ private static IFile createJavaSourceFiles(IProject project, AppEngineProjectCon
mainPackageFolder, properties, subMonitor.newChild(5));

createChildFile("HelloAppEngineTest.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_TEST_TEMPLATE, testPackageFolder,
properties, subMonitor.newChild(5));
Templates.HELLO_APPENGINE_TEST_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));
createChildFile("MockHttpServletResponse.java", //$NON-NLS-1$
Templates.MOCK_HTTPSERVLETRESPONSE_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));

return hello;
}

private static IFile createFlexJarJavaSourceFiles(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 20);

String packageName = config.getPackageName();
String packagePath = packageName.replace('.', '/');
IFolder mainPackageFolder = project.getFolder("src/main/java/" + packagePath); //$NON-NLS-1$
IFolder testPackageFolder = project.getFolder("src/test/java/" + packagePath); //$NON-NLS-1$

Map<String, String> properties = new HashMap<>();
properties.put("package", Strings.nullToEmpty(packageName)); //$NON-NLS-1$

IFile hello = createChildFile("HelloAppEngineMain.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_MAIN_TEMPLATE,
mainPackageFolder, properties, subMonitor.newChild(5));
createChildFile("HelloAppEngineHandler.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_HANDLER_TEMPLATE,
mainPackageFolder, properties, subMonitor.newChild(5));

createChildFile("HelloAppEngineHandlerTest.java", //$NON-NLS-1$
Templates.HELLO_APPENGINE_HANDLER_TEST_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));
createChildFile("MockHttpExchange.java", //$NON-NLS-1$
Templates.MOCKHTTPEXCHANGE_TEMPLATE,
testPackageFolder, properties, subMonitor.newChild(5));

return hello;
}

private static void createAppEngineWebXmlOrAppYaml(IProject project,
AppEngineProjectConfig config, boolean isStandardProject, IProgressMonitor monitor)
throws CoreException {
Expand All @@ -143,8 +191,7 @@ private static void createAppEngineWebXmlOrAppYaml(IProject project,

if (isStandardProject) {
IFolder webInf = project.getFolder("src/main/webapp/WEB-INF"); //$NON-NLS-1$
createChildFile("appengine-web.xml", //$NON-NLS-1$
Templates.APPENGINE_WEB_XML_TEMPLATE,
createChildFile("appengine-web.xml", Templates.APPENGINE_WEB_XML_TEMPLATE, //$NON-NLS-1$
webInf, properties, monitor);
} else {
IFolder appengine = project.getFolder("src/main/appengine"); //$NON-NLS-1$
Expand Down Expand Up @@ -212,6 +259,18 @@ private static void createPomXml(IProject project, AppEngineProjectConfig config
}
}

private static void createFlexJarPomXml(IProject project, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
Map<String, String> properties = new HashMap<>();
properties.put("projectGroupId", config.getMavenGroupId()); //$NON-NLS-1$
properties.put("projectArtifactId", config.getMavenArtifactId()); //$NON-NLS-1$
properties.put("projectVersion", config.getMavenVersion()); //$NON-NLS-1$
properties.put("package", config.getPackageName());

createChildFile("pom.xml", Templates.POM_XML_FLEX_JAR_TEMPLATE, //$NON-NLS-1$
project, properties, monitor);
}

@VisibleForTesting
static IFile createChildFile(String name, String template, IContainer parent,
Map<String, String> values, IProgressMonitor monitor) throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public IStatus validateDependencies() {
@Override
public CreateAppEngineWtpProject getAppEngineProjectCreationOperation(
AppEngineProjectConfig config, IAdaptable uiInfoAdapter) {
return new CreateAppEngineFlexWtpProject(config, uiInfoAdapter, repositoryService);
return new CreateAppEngineFlexJarProject(config, uiInfoAdapter, repositoryService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.tools.eclipse.appengine.newproject.flex;

import com.google.cloud.tools.eclipse.appengine.facets.AppEngineFlexJarFacet;
import com.google.cloud.tools.eclipse.appengine.libraries.repository.ILibraryRepositoryService;
import com.google.cloud.tools.eclipse.appengine.newproject.AppEngineProjectConfig;
import com.google.cloud.tools.eclipse.appengine.newproject.CodeTemplates;
import com.google.cloud.tools.eclipse.appengine.newproject.CreateAppEngineWtpProject;
import com.google.cloud.tools.eclipse.appengine.newproject.Messages;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;

public class CreateAppEngineFlexJarProject extends CreateAppEngineWtpProject {

CreateAppEngineFlexJarProject(AppEngineProjectConfig config, IAdaptable uiInfoAdapter,
ILibraryRepositoryService repositoryService) {
super(config, uiInfoAdapter, repositoryService);
}

@Override
public void addAppEngineFacet(IFacetedProject newProject, IProgressMonitor monitor)
throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor,
Messages.getString("add.appengine.flex.jar.facet"), 100);

AppEngineFlexJarFacet.installAppEngineFacet(
newProject, true /* installDependentFacets */, subMonitor.newChild(100));
}

@Override
public String getDescription() {
return Messages.getString("creating.app.engine.flex.project"); //$NON-NLS-1$
}

@Override
public IFile createAndConfigureProjectContent(IProject newProject, AppEngineProjectConfig config,
IProgressMonitor monitor) throws CoreException {
IFile mostImportantFile = CodeTemplates.materializeAppEngineFlexJarFiles(newProject, config,
monitor);
return mostImportantFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ project.location.exists: Project location already exists: {0}
app.engine.service=App Engine service:
add.appengine.standard.facet=Adding App Engine Standard Facet
add.appengine.flex.war.facet=Adding App Engine Flexible WAR Facet
add.appengine.flex.jar.facet=Adding App Engine Flexible JAR Facet
app.engine.standard.project.runtimetype=Java version:

app.engine.flex.project=App Engine Flexible Project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@

public class Templates {
public static final String APPENGINE_WEB_XML_TEMPLATE = "appengine-web.xml.ftl";
public static final String HELLO_APPENGINE_TEMPLATE = "HelloAppEngine.java.ftl";
public static final String APP_YAML_TEMPLATE = "app.yaml.ftl";

public static final String INDEX_HTML_TEMPLATE = "index.html.ftl";
public static final String WEB_XML_TEMPLATE = "web.xml.ftl";

public static final String HELLO_APPENGINE_TEMPLATE = "HelloAppEngine.java.ftl";
public static final String HELLO_APPENGINE_TEST_TEMPLATE = "HelloAppEngineTest.java.ftl";
public static final String MOCK_HTTPSERVLETRESPONSE_TEMPLATE = "MockHttpServletResponse.java.ftl";
public static final String APP_YAML_TEMPLATE = "app.yaml.ftl";

public static final String HELLO_APPENGINE_MAIN_TEMPLATE = "HelloAppEngineMain.java.ftl";
public static final String HELLO_APPENGINE_HANDLER_TEMPLATE = "HelloAppEngineHandler.java.ftl";
public static final String HELLO_APPENGINE_HANDLER_TEST_TEMPLATE =
"HelloAppEngineHandlerTest.java.ftl";
public static final String MOCKHTTPEXCHANGE_TEMPLATE = "MockHttpExchange.java.ftl";

public static final String POM_XML_STANDARD_TEMPLATE = "pom.xml.standard.ftl";
public static final String POM_XML_FLEX_TEMPLATE = "pom.xml.flex.ftl";
public static final String POM_XML_FLEX_JAR_TEMPLATE = "pom.xml.flex.jar.ftl";

private static Configuration configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<#if package != "">package ${package};

</#if>import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class HelloAppEngineHandler implements HttpHandler {

@Override
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Hello App Engine!";
httpExchange.sendResponseHeaders(200, response.length());
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(response.getBytes(StandardCharsets.UTF_8));
}
}
}
Loading