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.ApplicationContext;
26 import org.seasar.uruma.context.ContextFactory;
27 import org.seasar.uruma.context.WindowContext;
28 import org.seasar.uruma.exception.DuplicateComponentIdException;
29 import org.seasar.uruma.util.AssertionUtil;
30
31 /**
32 * {@link ApplicationContext} の実装クラスです。<br />
33 *
34 * @author y-komori
35 */
36 public class ApplicationContextImpl implements ApplicationContext {
37 private List<WindowContext> windowContextList = new ArrayList<WindowContext>();
38
39 private Map<String, Object> valueMap = new HashMap<String, Object>();
40
41 /**
42 * {@link ApplicationContextImpl} を構築します。<br />
43 * 本クラスのインスタンスを生成するには、{@link ContextFactory#createApplicationContext()}
44 * メソッドを利用してください。<br />
45 */
46 public ApplicationContextImpl() {
47 super();
48 }
49
50 /*
51 * @see org.seasar.uruma.context.ApplicationContext#getWindowContext(java.lang.String)
52 */
53 public WindowContext getWindowContext(final String windowName) {
54 for (WindowContext context : windowContextList) {
55 if (context.getName().equals(windowName)) {
56 return context;
57 }
58 }
59 return null;
60 }
61
62 /*
63 * @see org.seasar.uruma.context.ApplicationContext#getWindowContexts()
64 */
65 public Collection<WindowContext> getWindowContexts() {
66 return Collections.unmodifiableCollection(windowContextList);
67 }
68
69 /**
70 * {@link WindowContext} オブジェクトを追加します。<br />
71 *
72 * @param context
73 * 追加する {@link WindowContext} オブジェクト
74 * @throws DuplicateComponentIdException
75 * ウィンドウ名称が既に登録されている場合
76 */
77 public void addWindowContext(final WindowContext context) {
78 AssertionUtil.assertNotNull("context", context);
79
80 if (getWindowContext(context.getName()) == null) {
81 windowContextList.add(context);
82 } else {
83 throw new DuplicateComponentIdException(context.getName());
84 }
85 }
86
87 /**
88 * {@link WindowContext} を削除します。<br />
89 *
90 * @param windowName
91 * ウィンドウ名称
92 */
93 public void disposeWindowContext(final String windowName) {
94 WindowContext context = getWindowContext(windowName);
95 if (context != null) {
96 windowContextList.remove(context);
97 }
98 }
99
100 /*
101 * @see org.seasar.uruma.context.ApplicationContext#getValue(java.lang.String)
102 */
103 public Object getValue(final String name) {
104 return valueMap.get(name);
105 }
106
107 /*
108 * @see org.seasar.uruma.context.ApplicationContext#setValue(java.lang.String,
109 * java.lang.Object)
110 */
111 public void setValue(final String name, final Object value) {
112 AssertionUtil.assertNotEmpty("name", name);
113 valueMap.put(name, value);
114 }
115 }