FREEMARKER-54: initial ConfigurationFactoryBean design
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/57270ecf Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/57270ecf Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/57270ecf Branch: refs/heads/3 Commit: 57270ecf85744c5c6ca566c89f21f721a7a9d59a Parents: dbbfe99 Author: Woonsan Ko <woon...@apache.org> Authored: Thu Jun 15 00:10:58 2017 -0400 Committer: Woonsan Ko <woon...@apache.org> Committed: Thu Jun 15 00:10:58 2017 -0400 ---------------------------------------------------------------------- freemarker-spring/build.gradle | 13 +-- .../spring/ConfigurationFactoryBean.java | 115 +++++++++++++++++++ .../spring/SpringConfigurationBuilder.java | 30 ----- .../spring/ConfigurationFactoryBeanTest.java | 74 ++++++++++++ 4 files changed, 194 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/57270ecf/freemarker-spring/build.gradle ---------------------------------------------------------------------- diff --git a/freemarker-spring/build.gradle b/freemarker-spring/build.gradle index fa32eac..d5f7e15 100644 --- a/freemarker-spring/build.gradle +++ b/freemarker-spring/build.gradle @@ -29,22 +29,19 @@ dependencies { def springVersion = "4.3.9.RELEASE" - compileOnly("org.springframework:spring-core:$springVersion") { + compile("org.springframework:spring-core:$springVersion") { exclude group: "commons-logging", module: "commons-logging" } - compileOnly("org.springframework:spring-beans:$springVersion") { + compile("org.springframework:spring-beans:$springVersion") { exclude group: "commons-logging", module: "commons-logging" } - compileOnly("org.springframework:spring-context:$springVersion") { + compile("org.springframework:spring-context:$springVersion") { exclude group: "commons-logging", module: "commons-logging" } - compileOnly("org.springframework:spring-context-support:$springVersion") { + compile("org.springframework:spring-web:$springVersion") { exclude group: "commons-logging", module: "commons-logging" } - compileOnly("org.springframework:spring-web:$springVersion") { - exclude group: "commons-logging", module: "commons-logging" - } - compileOnly("org.springframework:spring-webmvc:$springVersion") { + compile("org.springframework:spring-webmvc:$springVersion") { exclude group: "commons-logging", module: "commons-logging" } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/57270ecf/freemarker-spring/src/main/java/org/apache/freemarker/spring/ConfigurationFactoryBean.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/ConfigurationFactoryBean.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/ConfigurationFactoryBean.java new file mode 100644 index 0000000..ead823d --- /dev/null +++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/ConfigurationFactoryBean.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.freemarker.spring; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.freemarker.core.Configuration; +import org.apache.freemarker.core.Configuration.ExtendableBuilder; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.config.AbstractFactoryBean; + +/** + * Configuration factory bean to support Spring Framework applications. + */ +public class ConfigurationFactoryBean extends ExtendableBuilder<ConfigurationFactoryBean> + implements FactoryBean<Configuration>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean { + + private AbstractFactoryBean<Configuration> delegate; + + private Map<String, String> settings = new LinkedHashMap<>(); + + public ConfigurationFactoryBean() { + super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); + + delegate = new AbstractFactoryBean<Configuration>() { + + @Override + public Class<?> getObjectType() { + return Configuration.class; + } + + @Override + protected Configuration createInstance() throws Exception { + for (Map.Entry<String, String> entry : settings.entrySet()) { + setSetting(entry.getKey(), entry.getValue()); + } + + return build(); + } + + }; + } + + public Map<String, String> getSettings() { + return Collections.unmodifiableMap(settings); + } + + public void setSettings(Map<String, String> settings) { + this.settings.putAll(settings); + } + + @Override + public Configuration getObject() throws Exception { + return delegate.getObject(); + } + + @Override + public Class<?> getObjectType() { + return delegate.getObjectType(); + } + + public void setSingleton(boolean singleton) { + delegate.setSingleton(singleton); + } + + @Override + public boolean isSingleton() { + return delegate.isSingleton(); + } + + @Override + public void destroy() throws Exception { + delegate.destroy(); + } + + @Override + public void afterPropertiesSet() throws Exception { + delegate.afterPropertiesSet(); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + delegate.setBeanFactory(beanFactory); + } + + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + delegate.setBeanClassLoader(classLoader); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/57270ecf/freemarker-spring/src/main/java/org/apache/freemarker/spring/SpringConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/SpringConfigurationBuilder.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/SpringConfigurationBuilder.java deleted file mode 100644 index ac0d963..0000000 --- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/SpringConfigurationBuilder.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.spring; - -import org.apache.freemarker.core.Configuration.ExtendableBuilder; -import org.apache.freemarker.core.Version; - -public class SpringConfigurationBuilder extends ExtendableBuilder { - - public SpringConfigurationBuilder(Version incompatibleImprovements) { - super(incompatibleImprovements); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/57270ecf/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java new file mode 100644 index 0000000..7dcbcc5 --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.freemarker.spring; + +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.freemarker.core.Configuration; +import org.apache.freemarker.core.Configuration.ExtendableBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.context.support.GenericApplicationContext; + +public class ConfigurationFactoryBeanTest { + + private GenericApplicationContext appContext; + + @Before + public void setUp() throws Exception { + appContext = new GenericApplicationContext(); + } + + @After + public void tearDown() throws Exception { + if (appContext.isActive()) { + appContext.stop(); + appContext.destroy(); + appContext.close(); + } + } + + @Test + public void testConfigurationFactoryBeanDefinition() throws Exception { + final Map<String, String> settings = new LinkedHashMap<>(); + settings.put(ExtendableBuilder.LOCALIZED_TEMPLATE_LOOKUP_KEY_CAMEL_CASE, "true"); + + BeanDefinition beanDef = + BeanDefinitionBuilder.genericBeanDefinition(ConfigurationFactoryBean.class.getName()) + .addPropertyValue("settings", settings) + .getBeanDefinition(); + + appContext.registerBeanDefinition("freemarkerConfig", beanDef); + appContext.refresh(); + appContext.start(); + + Object bean = appContext.getBean("freemarkerConfig"); + assertTrue("Not a Configuration object: " + bean, bean instanceof Configuration); + + Configuration config = (Configuration) bean; + assertTrue(config.getLocalizedTemplateLookup()); + } + +}