001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.framework.component;
008:
009: import java.io.File;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.Map;
013: import java.util.Collection;
014:
015: import org.jfox.framework.BaseException;
016: import org.jfox.framework.BaseRuntimeException;
017: import org.jfox.framework.ComponentId;
018: import org.jfox.framework.Framework;
019: import org.jfox.framework.event.ComponentEvent;
020:
021: /**
022: * Component 上下文
023: * ComponentContext 和 Component 是一对一的关系
024: * ComponentContex 和 ComponentMeta �一定是一对一的关系
025: * 对于Non Singleton Component,一个 ComponentMeta�能对应多个Component,也就对应多个ComponentContex
026: *
027: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
028: */
029: public class ComponentContext {
030:
031: //注æ„?: 这里ä¸?能ä¿?å˜Component的实例,而应该æ¯?次从Moduleä¸å?–å¾—ï¼Œè¿™æ ·æ‰?能实现çƒéƒ¨ç½²
032: //private Component concreteComponent;
033:
034: private Framework framework;
035:
036: private String moduleName;
037:
038: private ComponentId componentId;
039:
040: /**
041: * 用户属性
042: */
043: private Map<String, Object> attributes = new HashMap<String, Object>();
044:
045: ComponentContext(Framework framework, String module,
046: ComponentId componentId) {
047: this .framework = framework;
048: this .moduleName = module;
049: this .componentId = componentId;
050: }
051:
052: private Module getModule() {
053: return framework.getModule(getModuleName());
054: }
055:
056: public String getModuleName() {
057: return moduleName;
058: }
059:
060: public File getModuleDir() {
061: return getModule().getModuleDir();
062: }
063:
064: public Component getMyselfComponent() {
065: try {
066: return getComponentById(componentId);
067: } catch (BaseException e) {
068: throw new BaseRuntimeException(
069: "Can not get Myself Component!", e);
070: }
071: }
072:
073: public ComponentId getComponentId() {
074: return componentId;
075: }
076:
077: public void setAttribute(String key, Object value) {
078: attributes.put(key, value);
079: }
080:
081: public Object removeAttribute(String key) {
082: return attributes.remove(key);
083: }
084:
085: public Iterator<String> attributeKeys() {
086: return attributes.keySet().iterator();
087: }
088:
089: public Object hasAttribute(String key) {
090: return attributes.containsKey(key);
091: }
092:
093: public ExtentionPoint getExtentionPoint(String pointId) {
094: try {
095: return getModule().getComponentMeta(componentId)
096: .getExtentionPoint(pointId);
097: } catch (ComponentNotFoundException e) {
098: return null;
099: } catch (ComponentNotExportedException e) {
100: return null;
101: }
102: }
103:
104: /**
105: * Component 通过 ComponentContext �以获得需�的 Component 引用
106: *
107: * @param componentId component Id
108: * @throws ComponentNotFoundException e
109: * @throws ComponentNotExportedException e
110: */
111: public Component getComponentById(ComponentId componentId)
112: throws ComponentNotFoundException,
113: ComponentNotExportedException {
114: return getModule().getComponent(componentId);
115: }
116:
117: /**
118: * find components by the interface/super class provided
119: * 注æ„?:仅在本 Module ä¸æŸ¥æ‰¾
120: *
121: * @param interfaceClass interface class
122: */
123: public <T extends Component> Collection<T> getComponentsByInterface(
124: Class<T> interfaceClass) {
125: return getModule().findComponentByInterface(interfaceClass);
126: }
127:
128: public <T extends Component> Collection<T> getComponentsByInterface(
129: Class<T> interfaceClass, String moduleName) {
130: return getModule().findComponentByInterface(interfaceClass,
131: moduleName);
132: }
133:
134: public void fireComponentEvent(ComponentEvent componentEvent) {
135: getModule().getFramework().getEventManager()
136: .fireComponentEvent(componentEvent);
137: }
138:
139: public static void main(String[] args) {
140:
141: }
142:
143: }
|