Server configuration refactoring. Server initialization support.

This commit is contained in:
robin shine 2013-09-08 21:59:19 +08:00
parent b5e6caff21
commit 5920de5c9d
10 changed files with 227 additions and 59 deletions

View File

@ -6,6 +6,7 @@ import java.util.Properties;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.hibernate.cfg.Configuration;
@ -32,7 +33,7 @@ public class ConfigurationProvider implements Provider<Configuration> {
@Inject
public ConfigurationProvider(PluginManager pluginManager, NamingStrategy namingStrategy,
@Nullable @Hibernate Properties hibernateProperties) {
@Nullable @Named("hibernate") Properties hibernateProperties) {
this.pluginManager = pluginManager;
this.namingStrategy = namingStrategy;
this.hibernateProperties = hibernateProperties;

View File

@ -1,16 +0,0 @@
package com.pmease.commons.hibernate;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import com.google.inject.BindingAnnotation;
@Retention(RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@BindingAnnotation
public @interface Hibernate {
}

View File

@ -9,6 +9,7 @@ import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import com.pmease.commons.hibernate.dao.DefaultGeneralDao;
import com.pmease.commons.hibernate.dao.GeneralDao;
@ -23,7 +24,7 @@ public class HibernateModule extends AbstractPluginModule {
// Use an optional binding here in case our client does not like to
// start persist service provided by this plugin
bind(Properties.class).annotatedWith(Hibernate.class).toProvider(Providers.<Properties>of(null));
bind(Properties.class).annotatedWith(Names.named("hibernate")).toProvider(Providers.<Properties>of(null));
bind(NamingStrategy.class).to(ImprovedNamingStrategy.class);
bind(PersistService.class).to(PersistServiceImpl.class);

View File

@ -1,11 +1,15 @@
package com.pmease.gitop.core;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.inject.Inject;
import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -17,6 +21,7 @@ import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.ClassUtils;
import com.pmease.gitop.core.manager.InitManager;
import com.pmease.gitop.core.model.ModelLocator;
import com.pmease.gitop.core.setting.ServerConfig;
public class Gitop extends AbstractPlugin {
@ -24,10 +29,14 @@ public class Gitop extends AbstractPlugin {
private final InitManager initManager;
private final ServerConfig serverConfig;
private List<ManualConfig> manualConfigs;
public Gitop(InitManager initManager) {
@Inject
public Gitop(ServerConfig serverConfig, InitManager initManager) {
this.initManager = initManager;
this.serverConfig = serverConfig;
}
@Override
@ -86,8 +95,18 @@ public class Gitop extends AbstractPlugin {
}
}
private String guessServerUrl() {
return "http://localhost:8080";
public String guessServerUrl() {
String hostName;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
if (serverConfig.getHttpPort() != 0)
return "http://" + hostName + ":" + serverConfig.getHttpPort();
else
return "https://" + hostName + ":" + serverConfig.getSslConfig().getPort();
}
/**

View File

@ -0,0 +1,32 @@
package com.pmease.gitop.core.setting;
public interface ServerConfig {
/**
* Get http port configured for the server.
* <p>
* @return
* http port of the server, or <i>0</i> if http port is not defined.
* In case http port is not defined, {@link #getSslConfig()} must
* not return <i>null</i>
*/
int getHttpPort();
/**
* Get ssl config of the server.
* <p>
* @return
* ssl config of the server, or <i>null</i> if ssl setting is not defined.
* In case ssl setting is not defined, {@link #getHttpPort()} must not
* return <i>0</i>
*/
SslConfig getSslConfig();
/**
* Get web session timeout in seconds.
* <p>
* @return
* web session timeout in seconds
*/
int getSessionTimeout();
}

View File

@ -0,0 +1,11 @@
package com.pmease.gitop.core.setting;
public interface SslConfig {
int getPort();
String getKeystorePath();
String getKeystorePassword();
String getKeystoreKeyPassword();
}

View File

@ -0,0 +1,82 @@
package com.pmease.gitop.product;
import java.io.File;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import com.pmease.commons.bootstrap.Bootstrap;
import com.pmease.commons.util.StringUtils;
import com.pmease.gitop.core.setting.ServerConfig;
import com.pmease.gitop.core.setting.SslConfig;
@Singleton
public class DefaultServerConfig implements ServerConfig {
private int httpPort;
private int sessionTimeout;
private SslConfig sslConfig;
@Inject
public DefaultServerConfig(@Named("server") Properties props) {
String httpPortStr = props.getProperty("httpPort");
if (StringUtils.isNotBlank(httpPortStr)) {
httpPort = Integer.parseInt(httpPortStr);
}
String httpsPortStr = props.getProperty("httpsPort");
if (StringUtils.isNotBlank(httpsPortStr)) {
SslConfigBean sslConfigBean = new SslConfigBean();
sslConfigBean.setPort(Integer.parseInt(httpsPortStr));
String keystorePath = props.getProperty("sslKeystorePath");
if (StringUtils.isBlank(keystorePath))
keystorePath = "sample.keystore";
String keystorePassword = props.getProperty("sslKeystorePassword");
if (StringUtils.isBlank(keystorePassword))
keystorePassword = "123456";
String keystoreKeyPassword = props.getProperty("sslKeystoreKeyPassword");
if (StringUtils.isBlank(keystoreKeyPassword))
keystoreKeyPassword = "123456";
File keystoreFile = new File(keystorePath);
if (!keystoreFile.isAbsolute())
keystoreFile = new File(Bootstrap.getConfDir(), keystorePath);
sslConfigBean.setKeyStorePath(keystoreFile.getAbsolutePath());
sslConfigBean.setKeyStorePassword(keystorePassword);
sslConfigBean.setKeyStoreKeyPassword(keystoreKeyPassword);
sslConfig = sslConfigBean;
}
if (httpPort == 0 && sslConfig == null)
throw new RuntimeException("Either httpPort or httpsPort or both should be enabled.");
String sessionTimeout = props.getProperty("sessionTimeout");
if (StringUtils.isNotBlank(sessionTimeout))
this.sessionTimeout = Integer.parseInt(sessionTimeout);
else
throw new RuntimeException("sessionTimeout is not specified.");
}
@Override
public int getHttpPort() {
return httpPort;
}
@Override
public SslConfig getSslConfig() {
return sslConfig;
}
@Override
public int getSessionTimeout() {
return sessionTimeout;
}
}

View File

@ -2,7 +2,6 @@ package com.pmease.gitop.product;
import java.io.File;
import java.util.Collection;
import java.util.Properties;
import javax.inject.Inject;
@ -20,20 +19,20 @@ import com.pmease.commons.jetty.FileAssetServlet;
import com.pmease.commons.jetty.extensionpoints.ServerConfigurator;
import com.pmease.commons.jetty.extensionpoints.ServletContextConfigurator;
import com.pmease.commons.loader.AbstractPlugin;
import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.StringUtils;
import com.pmease.gitop.core.setting.ServerConfig;
import com.pmease.gitop.core.setting.SslConfig;
public class Product extends AbstractPlugin {
private static final Logger logger = LoggerFactory.getLogger(Product.class);
private final Properties serverProps;
private final ServerConfig serverConfig;
public static final String NAME = "Gitop";
@Inject
public Product() {
serverProps = FileUtils.loadProperties(new File(Bootstrap.getConfDir(), "server.properties"));
public Product(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
@Override
@ -44,49 +43,30 @@ public class Product extends AbstractPlugin {
@SuppressWarnings("deprecation")
@Override
public void configure(Server server) {
SocketConnector connector = new SocketConnector();
String httpPort = serverProps.getProperty("httpPort");
if (StringUtils.isNotBlank(httpPort)) {
connector.setPort(Integer.parseInt(httpPort));
if (serverConfig.getHttpPort() != 0) {
SocketConnector connector = new SocketConnector();
connector.setPort(serverConfig.getHttpPort());
server.addConnector(connector);
}
String httpsPort = serverProps.getProperty("httpsPort");
if (StringUtils.isNotBlank(httpsPort)) {
SslConfig sslConfig = serverConfig.getSslConfig();
if (sslConfig != null) {
SslSocketConnector sslConnector = new SslSocketConnector();
sslConnector.setPort(Integer.parseInt(httpsPort));
sslConnector.setPort(sslConfig.getPort());
String keystorePath = serverProps.getProperty("sslKeystorePath");
if (StringUtils.isBlank(keystorePath))
keystorePath = "sample.keystore";
String keystorePassword = serverProps.getProperty("sslKeystorePassword");
if (StringUtils.isBlank(keystorePassword))
keystorePassword = "123456";
String keystoreKeyPassword = serverProps.getProperty("sslKeystoreKeyPassword");
if (StringUtils.isBlank(keystoreKeyPassword))
keystoreKeyPassword = "123456";
File keystoreFile = new File(keystorePath);
if (!keystoreFile.isAbsolute())
keystoreFile = new File(Bootstrap.getConfDir(), keystorePath);
sslConnector.setKeystore(keystoreFile.getAbsolutePath());
sslConnector.setPassword(keystorePassword);
sslConnector.setKeyPassword(keystoreKeyPassword);
sslConnector.setKeystore(sslConfig.getKeystorePath());
sslConnector.setPassword(sslConfig.getKeystorePassword());
sslConnector.setKeyPassword(sslConfig.getKeystoreKeyPassword());
server.addConnector(sslConnector);
}
if (StringUtils.isBlank(httpPort) && StringUtils.isBlank(httpsPort))
throw new RuntimeException("Either httpPort or httpsPort or both should be enabled.");
}
},
new ServletContextConfigurator() {
@Override
public void configure(ServletContextHandler context) {
int sessionTimeout = Integer.valueOf(serverProps.getProperty("sessionTimeout"));
context.getSessionHandler().getSessionManager().setMaxInactiveInterval(sessionTimeout);
context.getSessionHandler().getSessionManager().setMaxInactiveInterval(serverConfig.getSessionTimeout());
/*
* Configure a servlet to serve contents under site folder. Site folder can be used

View File

@ -3,12 +3,13 @@ package com.pmease.gitop.product;
import java.io.File;
import java.util.Properties;
import com.google.inject.name.Names;
import com.pmease.commons.bootstrap.Bootstrap;
import com.pmease.commons.hibernate.Hibernate;
import com.pmease.commons.loader.AbstractPlugin;
import com.pmease.commons.loader.AbstractPluginModule;
import com.pmease.commons.loader.AppName;
import com.pmease.commons.util.FileUtils;
import com.pmease.gitop.core.setting.ServerConfig;
public class ProductModule extends AbstractPluginModule {
@ -20,7 +21,13 @@ public class ProductModule extends AbstractPluginModule {
Properties hibernateProps = FileUtils.loadProperties(
new File(Bootstrap.installDir, "conf/hibernate.properties"));
bind(Properties.class).annotatedWith(Hibernate.class).toInstance(hibernateProps);
bind(Properties.class).annotatedWith(Names.named("hibernate")).toInstance(hibernateProps);
Properties serverProps = FileUtils.loadProperties(
new File(Bootstrap.installDir, "conf/server.properties"));
bind(Properties.class).annotatedWith(Names.named("server")).toInstance(serverProps);
bind(ServerConfig.class).to(DefaultServerConfig.class);
}
@Override

View File

@ -0,0 +1,51 @@
package com.pmease.gitop.product;
import com.pmease.gitop.core.setting.SslConfig;
class SslConfigBean implements SslConfig {
private int port;
private String keyStorePath;
private String keyStorePassword;
private String keyStoreKeyPassword;
@Override
public int getPort() {
return port;
}
@Override
public String getKeystorePath() {
return keyStorePath;
}
@Override
public String getKeystorePassword() {
return keyStorePassword;
}
@Override
public String getKeystoreKeyPassword() {
return keyStoreKeyPassword;
}
public void setPort(int port) {
this.port = port;
}
public void setKeyStorePath(String keyStorePath) {
this.keyStorePath = keyStorePath;
}
public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword = keyStorePassword;
}
public void setKeyStoreKeyPassword(String keyStoreKeyPassword) {
this.keyStoreKeyPassword = keyStoreKeyPassword;
}
}