Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #36 from hikage/master

Migration from XML configuration to Java Config
  • Loading branch information...
commit f9cadd021e8cbe322dfaa12b218b13b27fce06c4 2 parents 74d0540 + 16bdbbb
@jdubois jdubois authored
View
1  .gitignore
@@ -1,3 +1,4 @@
+.idea
tatami.iml
tatami.ipr
tatami.iws
View
6 pom.xml
@@ -30,6 +30,12 @@
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>1.0-3</version>
+ <exclusions>
+ <exclusion>
+ <artifactId>servlet-api</artifactId>
+ <groupId>javax.servlet</groupId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
View
26 src/main/java/fr/ippon/tatami/config/ApplicationConfiguration.java
@@ -0,0 +1,26 @@
+package fr.ippon.tatami.config;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.thrift.transport.TTransportException;
+import org.springframework.context.annotation.*;
+
+import javax.annotation.PostConstruct;
+import java.io.IOException;
+
+@Configuration
+@PropertySource("classpath:/META-INF/tatami/tatami.properties")
+@ComponentScan(basePackages = {"fr.ippon.tatami.application", "fr.ippon.tatami.repository", "fr.ippon.tatami.service"})
+@Import(value = {CacheConfiguration.class, CassandraConfiguration.class})
+@ImportResource({"classpath:META-INF/spring/applicationContext-security.xml"})
+public class ApplicationConfiguration {
+
+ private final Log log = LogFactory.getLog(ApplicationConfiguration.class);
+
+
+ @PostConstruct
+ public void initTatami() throws IOException, TTransportException {
+ log.info("Tatami started!");
+ }
+
+}
View
26 src/main/java/fr/ippon/tatami/config/CacheConfiguration.java
@@ -0,0 +1,26 @@
+package fr.ippon.tatami.config;
+
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.ehcache.EhCacheCacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@EnableCaching
+public class CacheConfiguration {
+
+ @Bean
+ public CacheManager cacheManager() {
+ EhCacheCacheManager cacheManager = new EhCacheCacheManager();
+ cacheManager.setCacheManager(ehCacheManager());
+ return cacheManager;
+ }
+
+ @Bean
+ public net.sf.ehcache.CacheManager ehCacheManager() {
+ return new net.sf.ehcache.CacheManager();
+ }
+
+
+}
View
36 .../tatami/application/ApplicationConfiguration.java → ...r/ippon/tatami/config/CassandraConfiguration.java
@@ -1,4 +1,4 @@
-package fr.ippon.tatami.application;
+package fr.ippon.tatami.config;
import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
@@ -14,14 +14,12 @@
import me.prettyprint.hom.EntityManagerImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.thrift.transport.TTransportException;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
-import javax.annotation.PostConstruct;
+import javax.inject.Inject;
import javax.persistence.EntityManager;
-import java.io.IOException;
import static fr.ippon.tatami.application.ColumnFamilyKeys.*;
@@ -31,21 +29,22 @@
* @author Julien Dubois
*/
@Configuration
-public class ApplicationConfiguration {
+public class CassandraConfiguration {
- private final Log log = LogFactory.getLog(ApplicationConfiguration.class);
+ private final Log log = LogFactory.getLog(CassandraConfiguration.class);
- @Value("${cassandra.host}")
- private String cassandraHost;
+ @Inject
+ Environment env;
- @Value("${cassandra.clusterName}")
- private String cassandraClusterName;
-
- @Value("${cassandra.keyspace}")
- private String cassandraKeyspace;
@Bean
public Keyspace keyspaceOperator() {
+
+ String cassandraHost = env.getProperty("cassandra.host");
+ String cassandraClusterName = env.getProperty("cassandra.clusterName");
+ String cassandraKeyspace = env.getProperty("cassandra.keyspace");
+
+
CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator(cassandraHost);
ThriftCluster cluster = new ThriftCluster(cassandraClusterName, cassandraHostConfigurator);
ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
@@ -75,7 +74,11 @@ public Keyspace keyspaceOperator() {
return HFactory.createKeyspace(cassandraKeyspace, cluster, consistencyLevelPolicy);
}
+
private void addColumnFamily(ThriftCluster cluster, String cfName) {
+
+ String cassandraKeyspace = env.getProperty("cassandra.keyspace");
+
ColumnFamilyDefinition cfd =
HFactory.createColumnFamilyDefinition(cassandraKeyspace, cfName);
cluster.addColumnFamily(cfd);
@@ -86,8 +89,5 @@ public EntityManager entityManager(Keyspace keyspace) {
return new EntityManagerImpl(keyspace, "fr.ippon.tatami.domain");
}
- @PostConstruct
- public void initTatami() throws IOException, TTransportException {
- log.info("Tatami started!");
- }
+
}
View
63 src/main/java/fr/ippon/tatami/config/DispatcherServletConfig.java
@@ -0,0 +1,63 @@
+package fr.ippon.tatami.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.View;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+import org.springframework.web.servlet.view.BeanNameViewResolver;
+import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
+import org.springframework.web.servlet.view.InternalResourceViewResolver;
+import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Configuration
+@ComponentScan("fr.ippon.tatami.web")
+@EnableWebMvc
+public class DispatcherServletConfig extends WebMvcConfigurerAdapter {
+
+ @Override
+ public void addViewControllers(ViewControllerRegistry registry) {
+ registry.addViewController("/").setViewName("home");
+ registry.addViewController("/login").setViewName("login");
+ registry.addViewController("/about").setViewName("about");
+
+ }
+
+ @Bean
+ public ViewResolver ContentNegotiatingViewResolver() {
+
+ ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
+
+ Map<String, String> mediaTypes = new HashMap<String, String>();
+ mediaTypes.put("html", "text/html");
+ mediaTypes.put("json", "application/json");
+ viewResolver.setMediaTypes(mediaTypes);
+
+ List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
+ viewResolvers.add(new BeanNameViewResolver());
+ InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
+ internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
+ internalResourceViewResolver.setSuffix(".jsp");
+ viewResolvers.add(internalResourceViewResolver);
+
+ viewResolver.setViewResolvers(viewResolvers);
+
+ List<View> defaultViews = new ArrayList<View>();
+ defaultViews.add(new MappingJacksonJsonView());
+ viewResolver.setDefaultViews(defaultViews);
+
+
+ return viewResolver;
+
+ }
+
+
+}
View
42 src/main/java/fr/ippon/tatami/config/WebInitializer.java
@@ -0,0 +1,42 @@
+package fr.ippon.tatami.config;
+
+import fr.ippon.tatami.web.monitoring.MonitoringFilter;
+import org.springframework.web.WebApplicationInitializer;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
+import org.springframework.web.filter.DelegatingFilterProxy;
+import org.springframework.web.servlet.DispatcherServlet;
+
+import javax.servlet.*;
+import java.util.EnumSet;
+
+public class WebInitializer implements WebApplicationInitializer {
+ @Override
+ public void onStartup(ServletContext servletContext) throws ServletException {
+
+ AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
+ rootContext.register(ApplicationConfiguration.class);
+ rootContext.refresh();
+
+ servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootContext);
+
+
+ AnnotationConfigWebApplicationContext dispatcherServletConfig = new AnnotationConfigWebApplicationContext();
+ dispatcherServletConfig.setParent(rootContext);
+ dispatcherServletConfig.register(DispatcherServletConfig.class);
+
+ ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfig));
+ dispatcherServlet.addMapping("/tatami/*");
+ dispatcherServlet.setLoadOnStartup(1);
+
+
+ FilterRegistration.Dynamic springSecurityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
+ EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
+ springSecurityFilter.addMappingForServletNames(disps, true, "dispatcher");
+
+ FilterRegistration.Dynamic monitoringFilter = servletContext.addFilter("monitoringFilter", new MonitoringFilter());
+ monitoringFilter.addMappingForUrlPatterns(disps, true, "/*");
+
+
+ }
+}
View
32 src/main/java/fr/ippon/tatami/web/TatamiController.java
@@ -1,32 +0,0 @@
-package fr.ippon.tatami.web;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-
-/**
- * Main tatami page.
- *
- * @author Julien Dubois
- */
-@Controller
-public class TatamiController {
-
- private final Log log = LogFactory.getLog(TatamiController.class);
-
- @RequestMapping("/login")
- public String welcome() {
- return "login";
- }
-
- @RequestMapping("/")
- public String tatami() {
- return "home";
- }
-
- @RequestMapping("/about")
- public String about() {
- return "about";
- }
-}
View
18 src/main/resources/META-INF/spring/applicationContext-cache.xml
@@ -1,18 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
-
- <cache:annotation-driven />
-
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
- <property name="cacheManager">
- <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
- </property>
- </bean>
-
-</beans>
View
2  src/main/resources/META-INF/spring/applicationContext-security.xml
@@ -16,7 +16,7 @@
<http pattern="/tatami/about" security="none"/>
<http auto-config="true" use-expressions="true">
- <intercept-url pattern="/**" access="isAuthenticated()" />
+ <intercept-url pattern="/**" access="isAuthenticated()"/>
<form-login
login-processing-url="/tatami/authentication"
login-page="/tatami/login"
View
16 src/main/resources/META-INF/spring/applicationContext.xml
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
-
- <context:property-placeholder location="classpath:/META-INF/tatami/tatami.properties"/>
-
- <context:component-scan base-package="fr.ippon.tatami.application, fr.ippon.tatami.repository, fr.ippon.tatami.service"/>
-
- <import resource="applicationContext-security.xml"/>
- <import resource="applicationContext-cache.xml"/>
-</beans>
View
40 src/main/webapp/WEB-INF/dispatcher-servlet.xml
@@ -1,40 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
-
- <context:component-scan base-package="fr.ippon.tatami.web"/>
-
- <mvc:annotation-driven/>
-
- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="mediaTypes">
- <map>
- <entry key="html" value="text/html"/>
- <entry key="json" value="application/json"/>
- </map>
- </property>
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </list>
- </property>
- <property name="defaultViews">
- <list>
- <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
- </list>
- </property>
- </bean>
-
-</beans>
View
59 src/main/webapp/WEB-INF/web.xml
@@ -1,59 +0,0 @@
-<web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0"
- metadata-complete="true">
-
- <!--
- Remove classpath scanning (from servlet 3.0) :
- metadata-complete="true" abvoe + empyt absolute ordering below
- -->
-
- <absolute-ordering>
- <!--
- Empty absolute ordering to desactivate classpath scanning.r()
- -->
- </absolute-ordering>
-
- <display-name>Tatami</display-name>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
- </context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <filter>
- <filter-name>monitoringFilter</filter-name>
- <filter-class>fr.ippon.tatami.web.monitoring.MonitoringFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>monitoringFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <servlet-name>dispatcher</servlet-name>
- </filter-mapping>
-
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/tatami/*</url-pattern>
- </servlet-mapping>
-</web-app>
Please sign in to comment.
Something went wrong with that request. Please try again.