Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddedrespondfactory threading issue #16

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@
<!-- This should be removed when the googlecode repositories are migrated to the standard Nexus OSS repository infrastructure -->
<distributionManagement>
<snapshotRepository>
<id>google-snapshots</id>
<name>Sonatype OSS Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/google-snapshots</url>
<id>${snapshots.id}</id>
<name>${snapshots.name}</name>
<url>${snapshots.url}</url>
</snapshotRepository>
<repository>
<id>google-with-staging</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.sitebricks.example;

import com.google.sitebricks.headless.Reply;
import com.google.sitebricks.http.Get;

public class PageWithReply {

@Get
public Object get() {
return Reply.saying().status(678);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.runtime.directive.VelocimacroProxy;

/**
* @author Dhanji R. Prasanna ([email protected])
*/
Expand Down Expand Up @@ -82,6 +84,7 @@ private void bindExplicitly() {
at("/repeat").show(Repeat.class);
at("/showif").show(ShowIf.class);
at("/dynamic.js").show(DynamicJs.class);
at("/pageWithReply").show(PageWithReply.class);

at("/conversion").show(Conversion.class);

Expand Down Expand Up @@ -109,6 +112,7 @@ private void bindExplicitly() {

// templating by extension
at("/template").show(DecoratedPage.class);
at("/velocitySample").show(VelocitySample.class);

embed(HelloWorld.class).as("Hello");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.google.sitebricks.example;

import com.google.sitebricks.http.Get;

public class VelocitySample {

public static final String MSG = "Yaba Daba Doo!";

@Get
public void get() {
System.out.println("velocity sample. woot!");
}

public String getName() {
return "fred flinstone";
}

public String getMessage() {
return MSG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>for this test, the template will never be displayed because the page get method will always return a non-200 status code Reply</p>
</body>
</html>
11 changes: 11 additions & 0 deletions sitebricks-acceptance-tests/src/main/resources/VelocitySample.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>velocity sample</title>
</head>
<body>
<h1>velocity sample</h1>
<h2>$page.name</h2>
<h2>$page.message</h2>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.google.sitebricks.acceptance;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.testng.annotations.Test;

import com.google.sitebricks.acceptance.util.AcceptanceTest;

@Test(suiteName = AcceptanceTest.SUITE)
public class PageWithReplyAcceptanceTest {

public void shouldReturnCustomStatusCode() throws IOException {
URL url = new URL(AcceptanceTest.BASE_URL + "/pageWithReply");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int expected = 678;
int actual = connection.getResponseCode();

assert actual == expected : "expected custom response code '" + expected + "' but was '" + actual + "'";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.google.sitebricks.acceptance;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

import com.google.sitebricks.acceptance.page.VelocitySamplePage;
import com.google.sitebricks.acceptance.util.AcceptanceTest;

@Test(suiteName = AcceptanceTest.SUITE)
public class VelocitySampleAcceptanceTest {

public void shouldRenderDynamicTextFromVelocitySample() {
WebDriver driver = AcceptanceTest.createWebDriver();
VelocitySamplePage page = VelocitySamplePage.open(driver, "/velocitySample");

assertVelocitySampleContent(page);
}

private void assertVelocitySampleContent(VelocitySamplePage page) {
String title = page.getTitle();
assert title.equals("velocity sample") : title + " != \"velocity sample\"\n" + page.getContent();
assert page.hasMessage() : "did not have dynamic text";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.google.sitebricks.acceptance.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

import com.google.sitebricks.acceptance.util.AcceptanceTest;
import com.google.sitebricks.example.VelocitySample;

public class VelocitySamplePage {
private final WebDriver driver;

public VelocitySamplePage(WebDriver driver) {
this.driver = driver;
}

public static VelocitySamplePage open(WebDriver driver, String url) {
driver.get(AcceptanceTest.BASE_URL + url);
return PageFactory.initElements(driver, VelocitySamplePage.class);
}

public String getTitle() {
return driver.getTitle();
}

public boolean hasMessage() {
System.out.println(driver.getPageSource());
return driver.getPageSource().contains(VelocitySample.MSG);
}

public String getContent() {
return driver.getPageSource();
}
}
6 changes: 3 additions & 3 deletions sitebricks-async/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
standard Nexus OSS repository infrastructure -->
<distributionManagement>
<snapshotRepository>
<id>google-snapshots</id>
<name>Sonatype OSS Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/google-snapshots</url>
<id>${snapshots.id}</id>
<name>${snapshots.name}</name>
<url>${snapshots.url}</url>
</snapshotRepository>
<repository>
<id>google-with-staging</id>
Expand Down
22 changes: 19 additions & 3 deletions sitebricks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.sitebricks</groupId>
<artifactId>sitebricks-converter</artifactId>
Expand Down Expand Up @@ -136,9 +152,9 @@
<!-- This should be removed when the googlecode repositories are migrated to the standard Nexus OSS repository infrastructure -->
<distributionManagement>
<snapshotRepository>
<id>google-snapshots</id>
<name>Sonatype OSS Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/google-snapshots</url>
<id>${snapshots.id}</id>
<name>${snapshots.name}</name>
<url>${snapshots.url}</url>
</snapshotRepository>
<repository>
<id>google-with-staging</id>
Expand Down
4 changes: 3 additions & 1 deletion sitebricks/src/main/java/com/google/sitebricks/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String getText() {
}

public static enum Kind {
HTML, XML, FLAT, MVEL, FREEMARKER;
HTML, XML, FLAT, MVEL, FREEMARKER, VELOCITY;

/**
* Returns whether a given template should be treated as html, xml or flat
Expand All @@ -36,6 +36,8 @@ else if (template.endsWith(".mvel"))
return MVEL;
else if (template.endsWith(".fml"))
return FREEMARKER;
else if (template.endsWith(".vm"))
return VELOCITY;
else
return FLAT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TemplateLoader {
private final Provider<ServletContext> context;

private final String[] fileNameTemplates = new String[] { "%s.html", "%s.xhtml", "%s.xml",
"%s.txt", "%s.fml", "%s.mvel" };
"%s.txt", "%s.fml", "%s.mvel", "%s.vm" };

@Inject
public TemplateLoader(Provider<ServletContext> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface Compilers {
Renderable compileMvel(Class<?> page, String template);

Renderable compileFreemarker( Class<?> page, String text );

Renderable compileVelocity(Class<?> page, String template);

/**
* Performs static analysis of the given page class to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.google.sitebricks.compiler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.sitebricks.Bricks;
Expand All @@ -9,17 +13,16 @@
import com.google.sitebricks.Template;
import com.google.sitebricks.TemplateLoader;
import com.google.sitebricks.compiler.template.MvelTemplateCompiler;
import com.google.sitebricks.compiler.template.VelocityContextProvider;
import com.google.sitebricks.compiler.template.VelocityEngineProvider;
import com.google.sitebricks.compiler.template.VelocityTemplateCompiler;
import com.google.sitebricks.compiler.template.freemarker.FreemarkerTemplateCompiler;
import com.google.sitebricks.headless.Reply;
import com.google.sitebricks.rendering.Decorated;
import com.google.sitebricks.rendering.control.WidgetRegistry;
import com.google.sitebricks.routing.PageBook;
import com.google.sitebricks.routing.SystemMetrics;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

/**
* A factory for internal template compilers.
*
Expand All @@ -32,15 +35,20 @@ class StandardCompilers implements Compilers {
private final SystemMetrics metrics;
private final Map<String, Class<? extends Annotation>> httpMethods;
private final TemplateLoader loader;
private final VelocityEngineProvider velocityEngineProvider;
private final VelocityContextProvider velocityContextProvider;

@Inject
public StandardCompilers(WidgetRegistry registry, PageBook pageBook, SystemMetrics metrics,
@Bricks Map<String, Class<? extends Annotation>> httpMethods, TemplateLoader loader) {
@Bricks Map<String, Class<? extends Annotation>> httpMethods, TemplateLoader loader, VelocityEngineProvider velocityEngineProvider,
VelocityContextProvider velocityContextProvider) {
this.registry = registry;
this.pageBook = pageBook;
this.metrics = metrics;
this.httpMethods = httpMethods;
this.loader = loader;
this.velocityEngineProvider = velocityEngineProvider;
this.velocityContextProvider = velocityContextProvider;
}

public Renderable compileXml(Class<?> page, String template) {
Expand All @@ -67,6 +75,11 @@ public Renderable compileMvel(Class<?> page, String template) {
public Renderable compileFreemarker( Class<?> page, String template ) {
return new FreemarkerTemplateCompiler(page).compile(template);
}

@Override
public Renderable compileVelocity(Class<?> page, String template) {
return new VelocityTemplateCompiler(velocityEngineProvider, velocityContextProvider).compile(template);
}

// TODO(dhanji): Feedback errors as return rather than throwing.
public void analyze(Class<?> page) {
Expand Down Expand Up @@ -151,8 +164,10 @@ public Renderable compile(Class<?> templateClass) {
case FREEMARKER:
widget = compileFreemarker(templateClass, template.getText());
break;
case VELOCITY:
widget = compileVelocity(templateClass, template.getText());
break;
}
return widget;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.google.sitebricks.compiler.template;

import javax.inject.Provider;
import javax.inject.Singleton;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.tools.ToolManager;

public class VelocityContextProvider implements Provider<VelocityContext> {

@Override
@Singleton
public VelocityContext get() {
try {
ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("velocity-tools.xml");
return new VelocityContext(velocityToolManager.createContext());
} catch (RuntimeException e) {
return new VelocityContext();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.google.sitebricks.compiler.template;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.inject.Singleton;

import org.apache.velocity.app.VelocityEngine;

import com.google.inject.Provider;


public class VelocityEngineProvider implements Provider<VelocityEngine> {

@Override
@Singleton
public VelocityEngine get() {
Properties properties = new Properties();
try {
InputStream propertyStream = getClass().getResourceAsStream("/velocity.properties");
if (propertyStream != null)
properties.load(propertyStream);
} catch (IOException e) {
throw new RuntimeException(e);
}

VelocityEngine velocityEngine = new VelocityEngine(properties);
return velocityEngine;
}

}
Loading