01: /* ContextUtil.java
02: {{IS_NOTE
03: Purpose:
04:
05: Description:
06:
07: History:
08: Jul 25, 2007 10:03:38 AM , Created by Dennis Chen
09: }}IS_NOTE
10:
11: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
12:
13: {{IS_RIGHT
14: This program is distributed under GPL Version 2.0 in the hope that
15: it will be useful, but WITHOUT ANY WARRANTY.
16: }}IS_RIGHT
17: */
18: package org.zkoss.seam;
19:
20: import org.jboss.seam.Component;
21: import org.jboss.seam.ScopeType;
22: import org.jboss.seam.contexts.Context;
23:
24: /**
25: * This class helps developers to do thing with Seam's context
26: * @author Dennis.Chen
27: */
28: public class ContextUtil {
29:
30: /**
31: * Get bean form Seam's Context by name.
32: * @param name name of component
33: * @return bean of name
34: */
35: static public Object getBean(String name) {
36: return Component.getInstance(name);
37: }
38:
39: /**
40: * Update bean to Seam's context by name.<br/>
41: * This method will force getting a previous bean in context or by Seam's Component.getInstance().
42: * (This is becase if there is nothing in context, then there is nothing to update into context.
43: * This also make sure Seam will inject updated bean in context into other beans.)
44: * @param name name of component
45: * @param bean new instance or updated instance of component.
46: */
47: static public void updateToContext(String name, Object bean) {
48: //get From context or crate or nothing.
49: Object obj = Component.getInstance(name);
50: if (obj == null)
51: return;
52: putToContext(name, bean);
53: }
54:
55: /**
56: * Put bean into Seam's context ,<br/>
57: * the scope is depends on component's scope.
58: * Note that, if there is no component found by name, the scope will be ScopeType.CONVERSATION.
59: * @param name name of component
60: * @param bean new instance or updated instance of component.
61: */
62: static public void putToContext(String name, Object bean) {
63: Component comp = Component.forName(name);
64: ScopeType scope;
65: if (comp != null) {
66: scope = comp.getScope();
67: } else {
68: scope = ScopeType.CONVERSATION;
69: }
70: putToContext(name, bean, scope);
71:
72: }
73:
74: /**
75: * Put bean into Seam's context
76: * @param name name of component
77: * @param bean new instance or updated instance of component.
78: */
79: static public void putToContext(String name, Object bean,
80: ScopeType scope) {
81: if (ScopeType.STATELESS.equals(scope))
82: return;
83:
84: if (scope.isContextActive()) {
85: Context context = scope.getContext();
86: context.set(name, bean);
87: }
88:
89: }
90: }
|