Skip to content

Commit

Permalink
Restructure: split wrapping of application and component in separate …
Browse files Browse the repository at this point in the history
…packages. This should also ensure the code is modular enough to allow clean external extensions.
  • Loading branch information
coekie committed Mar 30, 2012
1 parent 47866aa commit b06c5ff
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 147 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Expand Up @@ -32,6 +32,10 @@
<id>snicoll</id>
<name>Stephane Nicoll</name>
</developer>
<developer>
<id>wco</id>
<name>Wouter Coekaerts</name>
</developer>
</developers>

<properties>
Expand Down
96 changes: 37 additions & 59 deletions src/main/java/com/bsb/common/vaadin/embed/EmbedVaadinConfig.java
Expand Up @@ -37,7 +37,6 @@
* <li><tt>context.path</tt>: to specify the context path of the deployed application</li>
* <li><tt>context.rootDir</tt>: to specify the root directory of the web application</li>
* <li><tt>server.await</tt>: to specify if the thread should block when the server has started</li>
* <li><tt>vaadin.theme</tt>: to specify the theme to use for the vaadin application</li>
* <li><tt>vaadin.widgetSet</tt>: to specify the widgetSet to use for the vaadin application</li>
* </ul>
*
Expand Down Expand Up @@ -65,11 +64,6 @@ public class EmbedVaadinConfig implements Serializable {
*/
public static final boolean DEFAULT_WAITING = true;

/**
* The default theme if none is set.
*/
public static final String DEFAULT_THEME = "reindeer";

/**
* Do not start the browser by default.
*/
Expand All @@ -81,12 +75,16 @@ public class EmbedVaadinConfig implements Serializable {
private File contextRootDirectory;
private boolean waiting;

private String theme;
private String widgetSet;

private boolean openBrowser;

private EmbedVaadinConfig(Properties properties) {
/**
* Creates a new instance using the configuration in the given {@link Properties}
*
* @param properties configuration properties
*/
public EmbedVaadinConfig(Properties properties) {
port = Integer.valueOf(properties.getProperty("server.port", String.valueOf(DEFAULT_PORT)));
contextPath = properties.getProperty("context.path", DEFAULT_CONTEXT_PATH);
final String contextBase = properties.getProperty("context.rootDir");
Expand All @@ -97,7 +95,6 @@ private EmbedVaadinConfig(Properties properties) {
}
waiting = Boolean.valueOf(properties.getProperty("server.await", String.valueOf(DEFAULT_WAITING)));

theme = properties.getProperty("vaadin.theme", DEFAULT_THEME);
widgetSet = properties.getProperty("vaadin.widgetSet");

openBrowser = Boolean.valueOf(properties.getProperty("open.browser", String.valueOf(DEFAULT_START_BROWSER)));
Expand All @@ -113,43 +110,15 @@ private EmbedVaadinConfig(Properties properties) {
*
* @param clone the instance to clone
*/
public EmbedVaadinConfig(EmbedVaadinConfig clone) {
protected EmbedVaadinConfig(EmbedVaadinConfig clone) {
this.port = clone.port;
this.contextPath = clone.contextPath;
this.contextRootDirectory = clone.contextRootDirectory;
this.waiting = clone.waiting;
this.theme = clone.theme;
this.widgetSet = clone.widgetSet;
this.openBrowser = clone.openBrowser;
}

/**
* Loads a configuration file from the default {@link #CONFIG_LOCATION config location}.
* <p/>
* If no file is found, only the standard default are available.
*
* @return a new instance with the default settings, potentially customized by an
* external properties file at the default location
* @see #CONFIG_LOCATION
*/
public static EmbedVaadinConfig load() {
return new EmbedVaadinConfig(loadProperties(CONFIG_LOCATION, false));
}


/**
* Loads a configuration file from the specified location. Fails if the
* specified <tt>path</tt> does not exist.
*
* @param path the location of a properties file in the classpath
* @return a new instance with the default settings customized by an
* external properties file at the specified location
* @throws IllegalStateException if no such properties file is found
*/
public static EmbedVaadinConfig load(String path) {
return new EmbedVaadinConfig(loadProperties(path, true));
}

/**
* Creates a new instance with the default settings, i.e. does not attempt
* to load customized settings from the {@link #CONFIG_LOCATION config location}.
Expand Down Expand Up @@ -211,17 +180,6 @@ public boolean isWaiting() {
return waiting;
}

/**
* Returns the vaadin theme to use for the application. Only taken into account
* when a wrapper application is created for a component.
*
* @return the theme to use
* @see #DEFAULT_THEME
*/
public String getTheme() {
return theme;
}

/**
* Returns the vaadin widgetSet to use for the application. Returns <tt>null</tt>
* if no specific widgetSet is configured and the default one should be used.
Expand All @@ -241,31 +199,27 @@ public boolean shouldOpenBrowser() {
return openBrowser;
}

protected void setPort(int port) {
void setPort(int port) {
this.port = port;
}

protected void setContextPath(String contextPath) {
void setContextPath(String contextPath) {
this.contextPath = contextPath;
}

protected void setContextRootDirectory(File contextRootDirectory) {
void setContextRootDirectory(File contextRootDirectory) {
this.contextRootDirectory = contextRootDirectory;
}

protected void setWaiting(boolean waiting) {
void setWaiting(boolean waiting) {
this.waiting = waiting;
}

protected void setTheme(String theme) {
this.theme = theme;
}

protected void setWidgetSet(String widgetSet) {
void setWidgetSet(String widgetSet) {
this.widgetSet = widgetSet;
}

protected void setOpenBrowser(boolean openBrowser) {
void setOpenBrowser(boolean openBrowser) {
this.openBrowser = openBrowser;
}

Expand All @@ -276,6 +230,30 @@ private void validate() {
}
}

/**
* Loads a configuration file from the default {@link #CONFIG_LOCATION config location}.
* <p/>
* If no file is found, only the standard default are available.
*
* @return Properties loaded from the default locations, or empty Properties if the file doesn't exist.
* @see #CONFIG_LOCATION
*/
public static Properties loadProperties() {
return loadProperties(CONFIG_LOCATION, false);
}

/**
* Loads a configuration file from the specified location. Fails if the
* specified <tt>path</tt> does not exist.
*
* @param path the location of a properties file in the classpath
* @return Properties Properties loaded from the <code>.properties</code> file at the specified location
* @throws IllegalStateException if no such properties file is found
*/
public static Properties loadProperties(String path) {
return loadProperties(path, true);
}

private static Properties loadProperties(String path, boolean failIfNotFound) {
try {
final Properties properties = new Properties();
Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.bsb.common.vaadin.embed;

import java.io.File;
import java.util.Properties;

/**
* A basic builder for an {@link EmbedVaadinServer}.
Expand All @@ -25,21 +26,10 @@
public abstract class EmbedVaadinServerBuilder<B extends EmbedVaadinServerBuilder<B, S>,
S extends EmbedVaadinServer> {

private EmbedVaadinConfig config;

/**
* Creates a new instance, initializing the properties with a default
* {@link EmbedVaadinConfig} instance.
*
* @param loadDefault <tt>true</tt> to initialize the builder with the default config
* @see EmbedVaadinConfig#load()
* Creates a new instance
*/
protected EmbedVaadinServerBuilder(boolean loadDefault) {
if (loadDefault) {
this.config = EmbedVaadinConfig.load();
} else {
this.config = EmbedVaadinConfig.defaultConfig();
}
protected EmbedVaadinServerBuilder() {
}

/**
Expand All @@ -57,26 +47,34 @@ protected EmbedVaadinServerBuilder(boolean loadDefault) {
public abstract S build();

/**
* Applies the content of the specified {@link EmbedVaadinConfig}.
* Loads a configuration file from the specified location. Fails if the
* specified <tt>path</tt> does not exist.
*
* @param config the configuration to use
* @param path the location of a properties file in the classpath
* @return this
* @throws IllegalStateException if no such properties file is found
*/
public B withConfig(EmbedVaadinConfig config) {
assertNotNull(config, "embed config could not be null.");
this.config = new EmbedVaadinConfig(config);

public B withConfigPath(String path) {
withConfigProperties(EmbedVaadinConfig.loadProperties(path));
return self();
}

/**
* Loads configuration from the specified {@link Properties}, usually read from a <code>.properties</code> file.
*
* @param properties configuration properties
* @return this
*/
public abstract B withConfigProperties(Properties properties);

/**
* Specifies the HTTP port to use.
*
* @param httpPort the http port
* @return this
*/
public B withHttpPort(int httpPort) {
this.config.setPort(httpPort);
getConfig().setPort(httpPort);
return self();
}

Expand All @@ -92,11 +90,11 @@ public B withContextPath(String contextPath) {

// Special handling so that / can be used for the root context as well
if (contextPath.equals("/") || contextPath.trim().equals("")) {
this.config.setContextPath("");
getConfig().setContextPath("");
} else if (!contextPath.startsWith("/")) {
this.config.setContextPath("/" + contextPath);
getConfig().setContextPath("/" + contextPath);
} else {
this.config.setContextPath(contextPath);
getConfig().setContextPath(contextPath);
}
return self();
}
Expand All @@ -120,7 +118,7 @@ public B withContextRootDirectory(File webappRootDirectory) {
throw new IllegalArgumentException("the specified value ["
+ webappRootDirectory.getAbsolutePath() + "] is not a directory!");
}
this.config.setContextRootDirectory(webappRootDirectory);
getConfig().setContextRootDirectory(webappRootDirectory);
return self();
}

Expand Down Expand Up @@ -152,7 +150,7 @@ public B withContextRootDirectory(String relativeDirectory) {
* @return this
*/
public B withWidgetSet(String widgetSet) {
this.config.setWidgetSet(widgetSet);
getConfig().setWidgetSet(widgetSet);
return self();
}

Expand All @@ -167,7 +165,7 @@ public B withWidgetSet(String widgetSet) {
* @return this
*/
public B wait(boolean shouldWait) {
this.config.setWaiting(shouldWait);
getConfig().setWaiting(shouldWait);
return self();
}

Expand All @@ -179,7 +177,7 @@ public B wait(boolean shouldWait) {
* @return this
*/
public B openBrowser(boolean open) {
this.config.setOpenBrowser(open);
getConfig().setOpenBrowser(open);
return self();
}

Expand All @@ -194,18 +192,15 @@ public S start() {
return server;
}


// Helpers for concrete builders

/**
* Returns the underling {@link EmbedVaadinConfig} that this instance is managing.
* Must be implemented by a concrete builder because the type of configuration can be different.
*
* @return the configuration
*/
protected EmbedVaadinConfig getConfig() {
return config;
}
protected abstract EmbedVaadinConfig getConfig();

// Helpers for concrete builders

protected void assertNotNull(Object value, String message) {
if (value == null) {
Expand Down
Expand Up @@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bsb.common.vaadin.embed;
package com.bsb.common.vaadin.embed.application;

import com.bsb.common.vaadin.embed.AbstractEmbedVaadinTomcat;
import com.bsb.common.vaadin.embed.EmbedVaadinConfig;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.ApplicationServlet;
import org.apache.catalina.Wrapper;
Expand Down
@@ -1,7 +1,12 @@
package com.bsb.common.vaadin.embed;
package com.bsb.common.vaadin.embed.application;

import com.bsb.common.vaadin.embed.EmbedVaadinConfig;
import com.bsb.common.vaadin.embed.EmbedVaadinServer;
import com.bsb.common.vaadin.embed.EmbedVaadinServerBuilder;
import com.vaadin.Application;

import java.util.Properties;

/**
* A builder for a server embedding an application.
*
Expand All @@ -10,11 +15,13 @@
public class EmbedVaadinApplication extends EmbedVaadinServerBuilder<EmbedVaadinApplication, EmbedVaadinServer> {

private final Class<? extends Application> applicationClass;
private EmbedVaadinConfig config;

public EmbedVaadinApplication(Class<? extends Application> applicationClass) {
super(true);
super();
assertNotNull(applicationClass, "applicationClass could not be null.");
this.applicationClass = applicationClass;
withConfigProperties(EmbedVaadinConfig.loadProperties());
}

/**
Expand All @@ -35,4 +42,15 @@ protected EmbedVaadinApplication self() {
public EmbedVaadinServer build() {
return new ApplicationBasedEmbedVaadinTomcat(getConfig(), getApplicationClass());
}

@Override
public EmbedVaadinApplication withConfigProperties(Properties properties) {
this.config = new EmbedVaadinConfig(properties);
return self();
}

@Override
protected EmbedVaadinConfig getConfig() {
return config;
}
}

0 comments on commit b06c5ff

Please sign in to comment.