View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.context.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.seasar.uruma.context.WidgetHandle;
26  import org.seasar.uruma.context.WidgetHolder;
27  import org.seasar.uruma.core.UrumaMessageCodes;
28  import org.seasar.uruma.exception.DuplicateWidgetIdException;
29  import org.seasar.uruma.log.UrumaLogger;
30  import org.seasar.uruma.util.AssertionUtil;
31  
32  /**
33   * {@link WidgetHolder} の実装クラスです。<br />
34   * 
35   * @author y-komori
36   */
37  public abstract class AbstractWidgetHolder implements WidgetHolder {
38      private static final UrumaLogger logger = UrumaLogger
39              .getLogger(AbstractWidgetHolder.class);
40  
41      private Map<String, WidgetHandle> handleMap = new HashMap<String, WidgetHandle>();
42  
43      /*
44       * @see
45       * org.seasar.uruma.context.WidgetHolder#getWidgetHandle(java.lang.String)
46       */
47      public WidgetHandle getWidgetHandle(final String handleId) {
48          return handleMap.get(handleId);
49      }
50  
51      /*
52       * @see org.seasar.uruma.context.WidgetHolder#getWidgetHandles()
53       */
54      public Collection<WidgetHandle> getWidgetHandles() {
55          return Collections.unmodifiableCollection(handleMap.values());
56      }
57  
58      /*
59       * @see
60       * org.seasar.uruma.context.WidgetHolder#hasWidgetHandle(java.lang.String)
61       */
62      public boolean hasWidgetHandle(final String handleId) {
63          return handleMap.containsKey(handleId);
64      }
65  
66      /*
67       * @see
68       * org.seasar.uruma.context.WidgetHolder#putWidgetHandle(org.seasar.uruma
69       * .context.WidgetHandle)
70       */
71      public void putWidgetHandle(final WidgetHandle handle) {
72          AssertionUtil.assertNotNull("handle", handle);
73  
74          if (hasWidgetHandle(handle.getId())) {
75              throw new DuplicateWidgetIdException(handle.getId(), handle
76                      .getWidgetClass().getName());
77          }
78          logger.log(UrumaMessageCodes.WIDGET_REGISTERED, handle.getId(), handle
79                  .getWidgetClass().getName());
80          handleMap.put(handle.getId(), handle);
81      }
82  
83      /*
84       * @see
85       * org.seasar.uruma.context.WidgetHolder#getWidgetHandles(java.lang.Class)
86       */
87      public List<WidgetHandle> getWidgetHandles(final Class<?> clazz) {
88          List<WidgetHandle> handles = new ArrayList<WidgetHandle>();
89          for (WidgetHandle handle : handleMap.values()) {
90              if (handle.instanceOf(clazz)) {
91                  handles.add(handle);
92              }
93          }
94          return handles;
95      }
96  }