001    
002    /*
003     * Copyright (C) 2012 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: SpringXSLUpdateTransformConfigurer.java 241 2012-01-20 02:30:49Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.pobj;
009    
010    import org.springframework.beans.factory.config.BeanPostProcessor;
011    import org.springframework.context.ResourceLoaderAware;
012    import org.springframework.core.io.DefaultResourceLoader;
013    import org.springframework.core.io.ResourceLoader;
014    
015    /**
016     * Spring {@link BeanPostProcessor} that looks for {@link SpringXSLPersistentObjectSchemaUpdate} beans that don't have
017     * an explicit {@link SpringXSLPersistentObjectSchemaUpdate#setTransform transform resource configured}, and configures
018     * them using a resource location based on the bean name, by simply adding a configured prefix and suffix.
019     *
020     * @see SpringXSLPersistentObjectSchemaUpdate
021     */
022    public class SpringXSLUpdateTransformConfigurer implements BeanPostProcessor, ResourceLoaderAware {
023    
024        /**
025         * Default location prefix: <code>{@value}</code>.
026         */
027        public static final String DEFAULT_LOCATION_PREFIX = "/";
028    
029        /**
030         * Default location suffix: <code>{@value}</code>.
031         */
032        public static final String DEFAULT_LOCATION_SUFFIX = ".xsl";
033    
034        private ResourceLoader resourceLoader = new DefaultResourceLoader();
035        private String prefix = DEFAULT_LOCATION_PREFIX;
036        private String suffix = DEFAULT_LOCATION_SUFFIX;
037    
038        /**
039         * Set the location prefix.
040         */
041        public void setPrefix(String prefix) {
042            if (prefix == null)
043                prefix = "";
044            this.prefix = prefix;
045        }
046    
047        /**
048         * Set the location suffix.
049         */
050        public void setSuffix(String suffix) {
051            if (suffix == null)
052                suffix = "";
053            this.suffix = suffix;
054        }
055    
056        @Override
057        public void setResourceLoader(ResourceLoader resourceLoader) {
058            this.resourceLoader = resourceLoader;
059        }
060    
061        @Override
062        public Object postProcessBeforeInitialization(Object bean, String beanName) {
063            if (bean instanceof SpringXSLPersistentObjectSchemaUpdate) {
064                SpringXSLPersistentObjectSchemaUpdate update = (SpringXSLPersistentObjectSchemaUpdate)bean;
065                if (update.getTransform() == null)
066                    update.setTransform(this.resourceLoader.getResource(this.getImpliedTransformResourceLocation(beanName)));
067            }
068            return bean;
069        }
070    
071        @Override
072        public Object postProcessAfterInitialization(Object bean, String beanName) {
073            return bean;
074        }
075    
076        /**
077         * Derive the implied transform resource location for the update with the given bean name.
078         *
079         * <p>
080         * The implementation in {@link SpringXSLUpdateTransformConfigurer} simply prepends the configured
081         * prefix and appends the configured suffix to {@code beanName}.
082         */
083        protected String getImpliedTransformResourceLocation(String beanName) {
084            return this.prefix + beanName + this.suffix;
085        }
086    }
087