Skip to content
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
23 changes: 21 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
<jdk.version>1.6</jdk.version>
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
<spring.version>3.2.6.RELEASE</spring.version>
<spring.version>4.3.4.RELEASE</spring.version>
<spring-boot.version>1.4.2.RELEASE</spring-boot.version>
</properties>

<organization>
Expand Down Expand Up @@ -118,6 +119,18 @@
<version>${spring.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand All @@ -128,7 +141,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -155,6 +168,12 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/org/mixer2/spring/boot/Mixer2AutoConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.mixer2.spring.boot;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mixer2.Mixer2Engine;
import org.mixer2.spring.webmvc.Mixer2XhtmlViewResolver;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;

import java.util.List;

@Configuration
@ConditionalOnClass(Mixer2Engine.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(Mixer2Properties.class)
public class Mixer2AutoConfiguration {

private static Log log = LogFactory.getLog(Mixer2AutoConfiguration.class);

@Bean
@ConditionalOnMissingBean
public Mixer2Engine mixer2Engine() {
return new Mixer2Engine();
}

@Configuration
@ConditionalOnClass(ViewResolver.class)
public static class Mixer2ViewConfiguration {

private final Mixer2Engine mixer2Engine;
private final Mixer2Properties properties;
private final BeanFactory beanFactory;

@Autowired
public Mixer2ViewConfiguration(Mixer2Engine mixer2Engine, Mixer2Properties properties, BeanFactory beanFactory) {
this.mixer2Engine = mixer2Engine;
this.properties = properties;
this.beanFactory = beanFactory;
}

@Bean
@ConditionalOnMissingBean
public Mixer2XhtmlViewResolver mixer2XhtmlViewResolver() {
Mixer2XhtmlViewResolver resolver = new Mixer2XhtmlViewResolver();
resolver.setOrder(properties.getOrder());
resolver.setMixer2Engine(this.mixer2Engine);
resolver.setPrefix(properties.getPrefix());
resolver.setSuffix(properties.getSuffix());
if (properties.getViewBasePackage() != null) {
resolver.setBasePackage(properties.getViewBasePackage());
} else if (AutoConfigurationPackages.has(beanFactory)) {
List<String> packages = AutoConfigurationPackages.get(beanFactory);
if (packages.size() == 1) {
String viewBasePackage = packages.get(0) + ".web.view";
resolver.setBasePackage(viewBasePackage);
if (log.isDebugEnabled()) {
log.debug("Apply '" + viewBasePackage + "' to the base package of view.");
}
} else {
if (log.isDebugEnabled()) {
log.debug("Skip auto-detecting base package of view because found multiple base packages. found base packages : " + packages);
}
}
}
if (properties.getViewClassNameSuffix() != null) {
resolver.setClassNameSuffix(properties.getViewClassNameSuffix());
}
if (properties.getContentType() != null) {
resolver.setContentType(properties.getContentType());
}
if (properties.getDocType() != null) {
resolver.setDocType(properties.getDocType());
}
if (properties.getReturnNullIfTemplateFileNotFound() != null) {
resolver.setReturnNullIfTemplateFileNotFound(properties.getReturnNullIfTemplateFileNotFound());
}
if (properties.getRaiseErrorIfViewClassNotFound() != null) {
resolver.setRaiseErrorIfViewClassNotFound(properties.getRaiseErrorIfViewClassNotFound());
}
if (properties.getCache() != null) {
resolver.setCache(properties.getCache());
}
if (resolver.isCache()) {
if (properties.getCacheLimit() != null) {
resolver.setCacheLimit(properties.getCacheLimit());
}
if (properties.getCacheUnresolved() != null) {
resolver.setCacheUnresolved(properties.getCacheUnresolved());
}
}
return resolver;
}

}

}
132 changes: 132 additions & 0 deletions src/main/java/org/mixer2/spring/boot/Mixer2Properties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.mixer2.spring.boot;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.Serializable;

@ConfigurationProperties(prefix = "mixer2")
public class Mixer2Properties implements Serializable {

private static final long serialVersionUID = -8282705230257476884L;

private int order = 1;

private String prefix = "classpath:/m2mockup/templates/";

private String suffix = ".html";

private String viewBasePackage;

private String viewClassNameSuffix;

private String docType;

private String contentType;

private Boolean returnNullIfTemplateFileNotFound;

private Boolean raiseErrorIfViewClassNotFound;

private Integer cacheLimit;

private Boolean cacheUnresolved;

private Boolean cache;

public int getOrder() {
return order;
}

public void setOrder(int order) {
this.order = order;
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}

public String getViewBasePackage() {
return viewBasePackage;
}

public void setViewBasePackage(String viewBasePackage) {
this.viewBasePackage = viewBasePackage;
}

public String getViewClassNameSuffix() {
return viewClassNameSuffix;
}

public void setViewClassNameSuffix(String viewClassNameSuffix) {
this.viewClassNameSuffix = viewClassNameSuffix;
}

public String getDocType() {
return docType;
}

public void setDocType(String docType) {
this.docType = docType;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public Boolean getReturnNullIfTemplateFileNotFound() {
return returnNullIfTemplateFileNotFound;
}

public void setReturnNullIfTemplateFileNotFound(Boolean returnNullIfTemplateFileNotFound) {
this.returnNullIfTemplateFileNotFound = returnNullIfTemplateFileNotFound;
}

public Boolean getRaiseErrorIfViewClassNotFound() {
return raiseErrorIfViewClassNotFound;
}

public void setRaiseErrorIfViewClassNotFound(Boolean raiseErrorIfViewClassNotFound) {
this.raiseErrorIfViewClassNotFound = raiseErrorIfViewClassNotFound;
}

public Integer getCacheLimit() {
return cacheLimit;
}

public void setCacheLimit(Integer cacheLimit) {
this.cacheLimit = cacheLimit;
}

public Boolean getCacheUnresolved() {
return cacheUnresolved;
}

public void setCacheUnresolved(Boolean cacheUnresolved) {
this.cacheUnresolved = cacheUnresolved;
}

public Boolean getCache() {
return cache;
}

public void setCache(Boolean cache) {
this.cache = cache;
}

}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mixer2.spring.boot.Mixer2AutoConfiguration
Loading