Source Code Cross Referenced for GenericContainer.java in  » J2EE » hgcommons » biz » hammurapi » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » hgcommons » biz.hammurapi.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package biz.hammurapi.config;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collection;
005:        import java.util.Collections;
006:        import java.util.Iterator;
007:        import java.util.LinkedHashMap;
008:        import java.util.List;
009:        import java.util.Map;
010:        import java.util.Set;
011:
012:        import biz.hammurapi.metrics.MeasurementCollector;
013:        import biz.hammurapi.metrics.MeasurementConsumer;
014:        import biz.hammurapi.util.Attributable;
015:        import biz.hammurapi.util.CollectionVisitable;
016:        import biz.hammurapi.util.VisitableBase;
017:        import biz.hammurapi.util.Visitor;
018:
019:        public class GenericContainer extends VisitableBase implements 
020:                Component, Command, Attributable, Context,
021:                MeasurementCollector, MeasurementConsumer {
022:
023:            private Map componentMap = new LinkedHashMap();
024:
025:            protected Map getComponentMap() {
026:                return componentMap;
027:            }
028:
029:            private boolean started;
030:            private Object owner;
031:
032:            private PathNavigator pathNavigator = new PathNavigator(this ) {
033:
034:                protected Object getParent() {
035:                    return owner;
036:                }
037:
038:                protected Object getChild(String name) {
039:                    return componentMap.get(name);
040:                }
041:
042:            };
043:
044:            private LinkedHashMap attributes = new LinkedHashMap();
045:            private MeasurementConsumer measurementConsumer;
046:
047:            /**
048:             * Looks up component in component tree.
049:             * @param name
050:             * @return
051:             */
052:            public Object get(String name) {
053:                return pathNavigator.get(name);
054:            }
055:
056:            /**
057:             * Adds component and starts it.
058:             * @param name
059:             * @param component
060:             * @throws ConfigurationException 
061:             */
062:            public void addComponent(String name, Object component)
063:                    throws ConfigurationException {
064:                if (name == null) {
065:                    name = "Anonymous component " + componentMap.size();
066:                }
067:
068:                // Remove previous component
069:                Object prevComponent = componentMap.remove(name);
070:                if (prevComponent != null) {
071:                    if (prevComponent instanceof  Component) {
072:                        if (started) {
073:                            ((Component) prevComponent).stop();
074:                        }
075:                        ((Component) prevComponent).setOwner(null);
076:                    }
077:
078:                    if (prevComponent instanceof  MeasurementCollector) {
079:                        ((MeasurementCollector) prevComponent)
080:                                .setMeasurementConsumer(null);
081:                    }
082:                }
083:
084:                if (component != null) {
085:                    if (component instanceof  MeasurementCollector) {
086:                        final String componentName = name;
087:                        MeasurementConsumer cmc = new MeasurementConsumer() {
088:                            public void addMeasurement(String mName,
089:                                    double value, long time) {
090:                                if (measurementConsumer != null) {
091:                                    measurementConsumer
092:                                            .addMeasurement(
093:                                                    componentName + "." + mName,
094:                                                    value,
095:                                                    time == 0 ? System
096:                                                            .currentTimeMillis()
097:                                                            : time);
098:                                }
099:                            }
100:                        };
101:                        ((MeasurementCollector) component)
102:                                .setMeasurementConsumer(cmc);
103:                    }
104:
105:                    if (component instanceof  NamedComponent) {
106:                        ((NamedComponent) component).setName(name);
107:                    }
108:
109:                    // Add new component
110:                    if (component instanceof  Component) {
111:                        Component theComponent = (Component) component;
112:                        theComponent.setOwner(this );
113:                        if (started) {
114:                            theComponent.start();
115:                        }
116:                    }
117:
118:                    if (name != null) {
119:                        componentMap.put(name, component);
120:                    }
121:
122:                }
123:            }
124:
125:            public void start() throws ConfigurationException {
126:                if (getMeasurementConsumer() instanceof  Component) {
127:                    ((Component) getMeasurementConsumer()).start();
128:                }
129:
130:                Iterator it = componentMap.values().iterator();
131:                while (it.hasNext()) {
132:                    Object component = it.next();
133:                    if (component instanceof  Component) {
134:                        ((Component) component).start();
135:                    }
136:                }
137:                started = true;
138:            }
139:
140:            public void stop() throws ConfigurationException {
141:                started = false;
142:                List reverseComponentList = new ArrayList(componentMap.values());
143:                Collections.reverse(reverseComponentList);
144:                Iterator it = reverseComponentList.iterator();
145:                while (it.hasNext()) {
146:                    Object component = it.next();
147:                    if (component instanceof  Component) {
148:                        ((Component) component).stop();
149:                    }
150:                }
151:                if (getMeasurementConsumer() instanceof  Component) {
152:                    ((Component) getMeasurementConsumer()).stop();
153:                }
154:            }
155:
156:            public void setOwner(Object owner) {
157:                this .owner = owner;
158:            }
159:
160:            /**
161:             * Invokes execute() of executable components sequentially.
162:             */
163:            public void execute(Object executionContext) {
164:                Iterator it = componentMap.values().iterator();
165:                while (it.hasNext()) {
166:                    Object component = it.next();
167:                    if (component instanceof  Command) {
168:                        ((Command) component).execute(executionContext);
169:                    }
170:                }
171:            }
172:
173:            protected void acceptChildren(Visitor visitor) {
174:                new CollectionVisitable(componentMap.values(), false)
175:                        .accept(visitor);
176:            }
177:
178:            protected Collection getComponents() {
179:                return componentMap.values();
180:            }
181:
182:            public Set getComponentNames() {
183:                return Collections.unmodifiableSet(componentMap.keySet());
184:            }
185:
186:            public void setAttribute(Object key, Object value) {
187:                attributes.put(key, value);
188:            }
189:
190:            public Object getAttribute(Object key) {
191:                return attributes.get(key);
192:            }
193:
194:            public Object removeAttribute(Object key) {
195:                return attributes.remove(key);
196:            }
197:
198:            public void setMeasurementConsumer(
199:                    MeasurementConsumer measurementConsumer) {
200:                this .measurementConsumer = measurementConsumer;
201:                if (measurementConsumer instanceof  Component) {
202:                    ((Component) measurementConsumer).setOwner(this );
203:                }
204:            }
205:
206:            public MeasurementConsumer getMeasurementConsumer() {
207:                return measurementConsumer;
208:            }
209:
210:            public void addMeasurement(String name, double value, long time) {
211:                if (measurementConsumer != null) {
212:                    measurementConsumer.addMeasurement(name, value, time);
213:                }
214:            }
215:
216:            /**
217:             * Finds component owner of particular type.
218:             * @param ownerType
219:             * @return Owner which is an instance of specified type or null if no such owner is found.
220:             */
221:            public Object getOwner(Class ownerType) {
222:                if (owner == null || ownerType.isInstance(owner)) {
223:                    return owner;
224:                }
225:
226:                if (owner instanceof  ComponentBase) {
227:                    return ((ComponentBase) owner).getOwner(ownerType);
228:                }
229:
230:                if (owner instanceof  GenericContainer) {
231:                    return ((GenericContainer) owner).getOwner(ownerType);
232:                }
233:
234:                return null;
235:            }
236:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.