001    package echopoint.util;
002    /*
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    
031    /*
032     * This class is take almost directly from the base Echo 2 class library
033     * and hence is Copyright (C) 2002-2005 NextApp, Inc.  It was made part of
034     * this code base on 11/05/2005
035     */
036    
037    /*
038     * This file is part of the Echo Web Application Framework (hereinafter "Echo").
039     * Copyright (C) 2002-2005 NextApp, Inc.
040     *
041     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
042     *
043     * The contents of this file are subject to the Mozilla Public License Version
044     * 1.1 (the "License"); you may not use this file except in compliance with
045     * the License. You may obtain a copy of the License at
046     * http://www.mozilla.org/MPL/
047     *
048     * Software distributed under the License is distributed on an "AS IS" basis,
049     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
050     * for the specific language governing rights and limitations under the
051     * License.
052     *
053     * Alternatively, the contents of this file may be used under the terms of
054     * either the GNU General Public License Version 2 or later (the "GPL"), or
055     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
056     * in which case the provisions of the GPL or the LGPL are applicable instead
057     * of those above. If you wish to allow use of your version of this file only
058     * under the terms of either the GPL or the LGPL, and not to allow others to
059     * use your version of this file under the terms of the MPL, indicate your
060     * decision by deleting the provisions above and replace them with the notice
061     * and other provisions required by the GPL or the LGPL. If you do not delete
062     * the provisions above, a recipient may use your version of this file under
063     * the terms of any one of the MPL, the GPL or the LGPL.
064     */
065    
066    
067    import java.io.IOException;
068    import java.util.HashMap;
069    import java.util.Iterator;
070    import java.util.Map;
071    
072    import nextapp.echo.app.util.PropertiesDiscovery;
073    
074    /**
075     * A PeerFactory that can return and peer object instance
076     * for a given string (rather than use class name
077     * which is what PeerFactory does)
078     */
079    public class StringPeerFactory {
080    
081        private final Map stringToPeerMap = new HashMap();
082    
083        public StringPeerFactory(String resourceName, ClassLoader classLoader) {
084            try {
085                Map peerNameMap = PropertiesDiscovery.loadProperties(resourceName, classLoader);
086                Iterator it = peerNameMap.keySet().iterator();
087                while (it.hasNext()) {
088                    String stringValue = (String) it.next();
089                    String peerClassName = (String) peerNameMap.get(stringValue);
090                    Class peerClass = classLoader.loadClass(peerClassName);
091                    Object peer = peerClass.newInstance();
092                    stringToPeerMap.put(stringValue, peer);
093                }
094            } catch (ClassNotFoundException ex) {
095                throw new RuntimeException("Unable to load string peer bindings: " + ex);
096            } catch (IOException ex) {
097                throw new RuntimeException("Unable to load string peer bindings: " + ex);
098            } catch (InstantiationException ex) {
099                throw new RuntimeException("Unable to load string peer bindings: " + ex);
100            } catch (IllegalAccessException ex) {
101                throw new RuntimeException("Unable to load string peer bindings: " + ex);
102            }
103        }
104    
105    
106        /**
107         * Retrieves the appropriate peer instance for a given
108         * <code>String</code>.
109         *
110         * @param matchString - the string to match
111         * @return the relevant peer, or null if none can be found
112         */
113        public Object getPeer(String matchString) {
114            Object peer = null;
115            peer = stringToPeerMap.get(matchString);
116            if (peer != null) {
117                return peer;
118            }
119            return null;
120        }
121    }
122