001    /* 
002     * This file is part of the Echo Point Project.  This project is a collection
003     * of Components that have extended the Echo Web Application Framework.
004     *
005     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006     *
007     * The contents of this file are subject to the Mozilla Public License Version
008     * 1.1 (the "License"); you may not use this file except in compliance with
009     * the License. You may obtain a copy of the License at
010     * http://www.mozilla.org/MPL/
011     *
012     * Software distributed under the License is distributed on an "AS IS" basis,
013     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014     * for the specific language governing rights and limitations under the
015     * License.
016     *
017     * Alternatively, the contents of this file may be used under the terms of
018     * either the GNU General Public License Version 2 or later (the "GPL"), or
019     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020     * in which case the provisions of the GPL or the LGPL are applicable instead
021     * of those above. If you wish to allow use of your version of this file only
022     * under the terms of either the GPL or the LGPL, and not to allow others to
023     * use your version of this file under the terms of the MPL, indicate your
024     * decision by deleting the provisions above and replace them with the notice
025     * and other provisions required by the GPL or the LGPL. If you do not delete
026     * the provisions above, a recipient may use your version of this file under
027     * the terms of any one of the MPL, the GPL or the LGPL.
028     */
029    package echopoint.template.ui;
030    
031    import echopoint.util.StringPeerFactory;
032    
033    import java.util.Map;
034    import java.util.WeakHashMap;
035    
036    
037    /**
038     * <code>TemplateCompilerLoader</code> is repsonsible for finding
039     * a template compiler instance for a given content type
040     */
041    public class TemplateCompilerLoader {
042            
043        private static final String PEERS_PATH = "META-INF/echopoint/template/TemplateCompilers.properties";
044        
045        private static final Map classLoaderToLoaderMap = new WeakHashMap();
046        
047        /**
048         * Creates or retrieves a <code>TemplateCompilerLoader</code>
049         * givena <code>ClassLoader</code>.
050         * 
051         * @param classLoader the <code>ClassLoader</code> to use for 
052         *        dynamically loading classes
053         * 
054         * @return the <code>TemplateCompilerLoader</code>
055         */
056        public static TemplateCompilerLoader forClassLoader(ClassLoader classLoader) {
057            synchronized(classLoaderToLoaderMap) {
058                    TemplateCompilerLoader loader = (TemplateCompilerLoader) classLoaderToLoaderMap.get(classLoader);
059                if (loader == null) {
060                    loader = new TemplateCompilerLoader(classLoader);
061                    classLoaderToLoaderMap.put(classLoader, loader);
062                }
063                return loader;
064            }
065        }
066    
067        private StringPeerFactory peerFactory;
068        
069        /**
070         * Creates a new <code>TemplateCompilerLoader</code>.
071         * 
072         * @param classLoader the <code>ClassLoader</code> to use for 
073         *        dynamically loading property classes
074         */
075        private TemplateCompilerLoader(ClassLoader classLoader) {
076            super();
077            peerFactory = new StringPeerFactory(PEERS_PATH, classLoader);
078        }
079        
080        /**
081         * Returns the <code>TemplateCompiler</code> that can compile
082         * the given content type.
083         * 
084         * @param contentType - the content type of the template data
085         * @return the XML parsing peer
086         */
087        public TemplateCompiler getTemplateCompiler(String contentType) {
088            return (TemplateCompiler) peerFactory.getPeer(contentType);
089        }
090    }