KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > listener > ListenerMapImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.listener;
16
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.util.Defense;
24 import org.apache.tapestry.IActionListener;
25
26 /**
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class ListenerMapImpl implements ListenerMap
31 {
32     private final Object JavaDoc _target;
33
34     /**
35      * Keyed on String method name, value is
36      * {@link org.apache.tapestry.listener.ListenerMethodInvoker}.
37      */

38
39     private final Map JavaDoc _invokers;
40
41     private final Map JavaDoc _listeners = new HashMap JavaDoc();
42
43     public ListenerMapImpl(Object JavaDoc target, Map JavaDoc invokers)
44     {
45         Defense.notNull(target, "target");
46         Defense.notNull(invokers, "invokers");
47
48         _target = target;
49         _invokers = invokers;
50     }
51
52     public boolean canProvideListener(String JavaDoc name)
53     {
54         return _invokers.containsKey(name);
55     }
56
57     public synchronized IActionListener getListener(String JavaDoc name)
58     {
59         IActionListener result = (IActionListener) _listeners.get(name);
60
61         if (result == null)
62         {
63             result = createListener(name);
64             _listeners.put(name, result);
65         }
66
67         return result;
68     }
69
70     private IActionListener createListener(String JavaDoc name)
71     {
72         ListenerMethodInvoker invoker = (ListenerMethodInvoker) _invokers.get(name);
73
74         if (invoker == null)
75             throw new ApplicationRuntimeException(ListenerMessages.objectMissingMethod(
76                     _target,
77                     name), _target, null, null);
78
79         return new SyntheticListener(_target, invoker);
80     }
81
82     public Collection JavaDoc getListenerNames()
83     {
84         return Collections.unmodifiableCollection(_invokers.keySet());
85     }
86 }
Popular Tags