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.binding.context;
17
18 import java.util.List;
19
20 import org.seasar.framework.beans.PropertyDesc;
21 import org.seasar.uruma.context.ApplicationContext;
22 import org.seasar.uruma.core.UrumaMessageCodes;
23 import org.seasar.uruma.log.UrumaLogger;
24
25 /**
26 * 任意のオブジェクトと {@link ApplicationContext} の間でバインディングを行うためのクラスです。<br />
27 *
28 * @author y-komori
29 */
30 public class ApplicationContextBinder {
31 private static final UrumaLogger logger = UrumaLogger
32 .getLogger(ApplicationContextBinder.class);
33
34 private ApplicationContextBinder() {
35
36 }
37
38 /**
39 * プロパティから {@link ApplicationContext} へ値をバインドします。<br />
40 * 対象となるプロパティは、<code>defs</code> で指定される {@link ApplicationContextDef}
41 * のリストです。<br />
42 *
43 * @param target
44 * ターゲットオブジェクト
45 * @param defs
46 * {@link ApplicationContextDef} のリスト
47 * @param context
48 * {@link ApplicationContext} オブジェクト
49 */
50 public static void exportObjects(final Object target,
51 final List<ApplicationContextDef> defs,
52 final ApplicationContext context) {
53 for (ApplicationContextDef def : defs) {
54 PropertyDesc pd = def.getPropertyDesc();
55 String name = def.getName();
56 Object value = pd.getValue(target);
57
58 if (logger.isDebugEnabled()) {
59 logger.log(UrumaMessageCodes.EXPORT_APPLICATION_CONTEXT,
60 UrumaLogger.getObjectDescription(target), pd
61 .getPropertyName(), name, value);
62 }
63 context.setValue(name, value);
64 }
65 }
66
67 /**
68 * {@link ApplicationContext} からプロパティへ値をバインドします。<br />
69 * 対象となるプロパティは、<code>defs</code> で指定される {@link ApplicationContextDef}
70 * のリストです。<br />
71 *
72 * @param target
73 * ターゲットオブジェクト
74 * @param defs
75 * {@link ApplicationContextDef} のリスト
76 * @param context
77 * {@link ApplicationContext} オブジェクト
78 */
79 public static void importObjects(final Object target,
80 final List<ApplicationContextDef> defs,
81 final ApplicationContext context) {
82 for (ApplicationContextDef def : defs) {
83 PropertyDesc pd = def.getPropertyDesc();
84 String name = def.getName();
85 Object value = context.getValue(name);
86
87 if (logger.isDebugEnabled()) {
88 logger.log(UrumaMessageCodes.IMPORT_APPLICATION_CONTEXT,
89 UrumaLogger.getObjectDescription(target), pd
90 .getPropertyName(), name, value);
91 }
92
93 pd.setValue(target, value);
94 }
95 }
96 }