001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.listener;
016    
017    import java.util.Collection;
018    import java.util.Collections;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.hivemind.util.Defense;
024    import org.apache.tapestry.IActionListener;
025    
026    /**
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public class ListenerMapImpl implements ListenerMap
031    {
032    
033        private final Object _target;
034    
035        /**
036         * Keyed on String method name, value is
037         * {@link org.apache.tapestry.listener.ListenerMethodInvoker}.
038         */
039    
040        private final Map _invokers;
041    
042        private final Map _listeners = new HashMap();
043    
044        public ListenerMapImpl(Object target, Map invokers)
045        {
046            Defense.notNull(target, "target");
047            Defense.notNull(invokers, "invokers");
048    
049            _target = target;
050            _invokers = invokers;
051        }
052    
053        public boolean canProvideListener(String name)
054        {
055            return _invokers.containsKey(name);
056        }
057    
058        public synchronized IActionListener getListener(String name)
059        {
060            IActionListener result = (IActionListener) _listeners.get(name);
061    
062            if (result == null)
063            {
064                result = createListener(name);
065                _listeners.put(name, result);
066            }
067    
068            return result;
069        }
070    
071        private IActionListener createListener(String name)
072        {
073            ListenerMethodInvoker invoker = (ListenerMethodInvoker) _invokers
074                    .get(name);
075    
076            if (invoker == null)
077                throw new ApplicationRuntimeException(ListenerMessages
078                        .objectMissingMethod(_target, name), _target, null, null);
079    
080            return new SyntheticListener(_target, invoker);
081        }
082    
083        public Collection getListenerNames()
084        {
085            return Collections.unmodifiableCollection(_invokers.keySet());
086        }
087    }