Java tutorial
/** * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.component.factory.web; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletContext; import org.apache.commons.lang.StringUtils; import org.joda.beans.BeanBuilder; import org.joda.beans.BeanDefinition; import org.joda.beans.JodaBeanUtils; import org.joda.beans.MetaProperty; import org.joda.beans.Property; import org.joda.beans.PropertyDefinition; import org.joda.beans.impl.direct.DirectBeanBuilder; import org.joda.beans.impl.direct.DirectMetaProperty; import org.joda.beans.impl.direct.DirectMetaPropertyMap; import org.springframework.web.context.ServletContextAware; import com.opengamma.OpenGammaRuntimeException; import com.opengamma.component.ComponentRepository; import com.opengamma.component.factory.AbstractComponentFactory; import com.opengamma.web.FreemarkerOutputter; import freemarker.cache.FileTemplateLoader; import freemarker.cache.MultiTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.cache.WebappTemplateLoader; import freemarker.template.Configuration; /** * Component factory for initializing Freemarker. */ @BeanDefinition public class FreemarkerConfigurationComponentFactory extends AbstractComponentFactory { /** * Prefix used for a file relative to the servlet context. */ public static final String SERVLET_CONTEXT = "servlet-context"; /** * Prefix used for a simple file location. */ public static final String FILE = "file"; /** * The locations of the templates. */ @PropertyDefinition(validate = "notEmpty") private String _templateLocations; @Override public void init(ComponentRepository repo, final LinkedHashMap<String, String> configuration) throws Exception { String[] locations = _templateLocations.split(","); repo.registerServletContextAware(new FreemarkerInitializer(locations)); } //------------------------------------------------------------------------- static final class FreemarkerInitializer implements ServletContextAware { private final String[] _locations; FreemarkerInitializer(String[] locations) { _locations = locations; } @Override public void setServletContext(ServletContext servletContext) { Configuration cfg = FreemarkerOutputter.createConfiguration(); cfg.setTemplateLoader(new MultiTemplateLoader(createLoaders(_locations, servletContext))); FreemarkerOutputter.init(servletContext, cfg); } } static TemplateLoader[] createLoaders(String[] locations, ServletContext servletContext) { Collection<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>(); for (String location : locations) { String[] prefixAndBase = StringUtils.split(location, ":", 2); if (prefixAndBase.length != 2) { throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location); } String prefix = prefixAndBase[0].trim(); String base = prefixAndBase[1].trim(); if (SERVLET_CONTEXT.equals(prefix)) { templateLoaders.add(new WebappTemplateLoader(servletContext, base)); } else if (FILE.equals(prefix)) { try { templateLoaders.add(new FileTemplateLoader(new File(base))); } catch (IOException e) { throw new OpenGammaRuntimeException("Unable to load Freemarker templates from " + base, e); } } else { throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location); } } return templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]); } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code FreemarkerConfigurationComponentFactory}. * @return the meta-bean, not null */ public static FreemarkerConfigurationComponentFactory.Meta meta() { return FreemarkerConfigurationComponentFactory.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(FreemarkerConfigurationComponentFactory.Meta.INSTANCE); } @Override public FreemarkerConfigurationComponentFactory.Meta metaBean() { return FreemarkerConfigurationComponentFactory.Meta.INSTANCE; } @Override protected Object propertyGet(String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 826348548: // templateLocations return getTemplateLocations(); } return super.propertyGet(propertyName, quiet); } @Override protected void propertySet(String propertyName, Object newValue, boolean quiet) { switch (propertyName.hashCode()) { case 826348548: // templateLocations setTemplateLocations((String) newValue); return; } super.propertySet(propertyName, newValue, quiet); } @Override protected void validate() { JodaBeanUtils.notEmpty(_templateLocations, "templateLocations"); super.validate(); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { FreemarkerConfigurationComponentFactory other = (FreemarkerConfigurationComponentFactory) obj; return JodaBeanUtils.equal(getTemplateLocations(), other.getTemplateLocations()) && super.equals(obj); } return false; } @Override public int hashCode() { int hash = 7; hash += hash * 31 + JodaBeanUtils.hashCode(getTemplateLocations()); return hash ^ super.hashCode(); } //----------------------------------------------------------------------- /** * Gets the locations of the templates. * @return the value of the property, not null */ public String getTemplateLocations() { return _templateLocations; } /** * Sets the locations of the templates. * @param templateLocations the new value of the property, not null */ public void setTemplateLocations(String templateLocations) { JodaBeanUtils.notEmpty(templateLocations, "templateLocations"); this._templateLocations = templateLocations; } /** * Gets the the {@code templateLocations} property. * @return the property, not null */ public final Property<String> templateLocations() { return metaBean().templateLocations().createProperty(this); } //----------------------------------------------------------------------- /** * The meta-bean for {@code FreemarkerConfigurationComponentFactory}. */ public static class Meta extends AbstractComponentFactory.Meta { /** * The singleton instance of the meta-bean. */ static final Meta INSTANCE = new Meta(); /** * The meta-property for the {@code templateLocations} property. */ private final MetaProperty<String> _templateLocations = DirectMetaProperty.ofReadWrite(this, "templateLocations", FreemarkerConfigurationComponentFactory.class, String.class); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap(this, (DirectMetaPropertyMap) super.metaPropertyMap(), "templateLocations"); /** * Restricted constructor. */ protected Meta() { } @Override protected MetaProperty<?> metaPropertyGet(String propertyName) { switch (propertyName.hashCode()) { case 826348548: // templateLocations return _templateLocations; } return super.metaPropertyGet(propertyName); } @Override public BeanBuilder<? extends FreemarkerConfigurationComponentFactory> builder() { return new DirectBeanBuilder<FreemarkerConfigurationComponentFactory>( new FreemarkerConfigurationComponentFactory()); } @Override public Class<? extends FreemarkerConfigurationComponentFactory> beanType() { return FreemarkerConfigurationComponentFactory.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- /** * The meta-property for the {@code templateLocations} property. * @return the meta-property, not null */ public final MetaProperty<String> templateLocations() { return _templateLocations; } } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }