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 org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.util.Defense; 019 import org.apache.tapestry.IActionListener; 020 021 import java.util.Collection; 022 import java.util.Collections; 023 import java.util.HashMap; 024 import java.util.Map; 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.get(name); 074 075 if (invoker == null) 076 throw new ApplicationRuntimeException(ListenerMessages.objectMissingMethod(_target, name), 077 _target, null, null); 078 079 return new SyntheticListener(_target, invoker); 080 } 081 082 public Collection getListenerNames() 083 { 084 return Collections.unmodifiableCollection(_invokers.keySet()); 085 } 086 }