Source Code Cross Referenced for GetServiceComponent.java in  » Science » Cougaar12_4 » org » cougaar » core » node » 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 » Science » Cougaar12_4 » org.cougaar.core.node 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 2001-2007 BBNT Solutions, LLC
005:         *  under sponsorship of the Defense Advanced Research Projects
006:         *  Agency (DARPA).
007:         * 
008:         *  You can redistribute this software and/or modify it under the
009:         *  terms of the Cougaar Open Source License as published on the
010:         *  Cougaar Open Source Website (www.cougaar.org).
011:         * 
012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023:         *  
024:         * </copyright>
025:         */
026:
027:        package org.cougaar.core.node;
028:
029:        import java.util.List;
030:        import org.cougaar.core.component.Component;
031:        import org.cougaar.core.component.ServiceAvailableEvent;
032:        import org.cougaar.core.component.ServiceAvailableListener;
033:        import org.cougaar.core.component.ServiceBroker;
034:        import org.cougaar.core.service.LoggingService;
035:        import org.cougaar.core.util.Reflection;
036:        import org.cougaar.util.GenericStateModelAdapter;
037:
038:        /**
039:         * This component can be used to export an internal service out to an
040:         * external framework.
041:         * <p>
042:         * For example, the {@link NodeApplet} uses this component to get the
043:         * {@link NodeControlService}.
044:         * <p>
045:         * Two parameters are required:<br>
046:         * &nbsp;&nbsp; 1) The Service class or classname, which can be {@link ServiceBroker}.<br>
047:         * &nbsp;&nbsp; 2) The {@link GetServiceCallback} listener, which can be<br>
048:         * &nbsp;&nbsp;&nbsp;&nbsp; specified as a class, classname, or instance.<br>
049:         * A third parameter is optional:<br>
050:         * &nbsp;&nbsp; 3) The service requestor, or "this" for this component, which<br>
051:         * &nbsp;&nbsp;&nbsp;&nbsp; defaults to "this".<br>
052:         * A fourth parameter is optional:<br>
053:         * &nbsp;&nbsp; 4) Use a late-binding service listener if the service is not<br>
054:         * &nbsp;&nbsp;&nbsp;&nbsp; available at load time, defaults to "true".<br>
055:         * <p>
056:         * Reflection is used to wrap the callback as the GetServiceCallback API, even
057:         * if it doesn't implement that API.  This allows an external client to
058:         * specify:<pre>
059:         *    public class MyCallback {
060:         *      public void setService(Class cl, Object service) {..}
061:         *    }
062:         * </pre>
063:         * even though "MyCallback instanceof GetServiceCallback" is false.  This is
064:         * supported to avoid awkward compile and classloader dependencies.
065:         */
066:        public class GetServiceComponent extends GenericStateModelAdapter
067:                implements  Component {
068:
069:            private ServiceBroker sb;
070:            private List params;
071:
072:            private LoggingService log;
073:
074:            private Class cl;
075:            private boolean is_sb;
076:            private Object svc;
077:            private GetServiceCallback cb;
078:            private Object req;
079:
080:            public void setServiceBroker(ServiceBroker sb) {
081:                this .sb = sb;
082:            }
083:
084:            public void setParameter(Object o) {
085:                if (!(o instanceof  List)) {
086:                    throw new IllegalArgumentException("Expecting a List, not "
087:                            + (o == null ? "null" : o.getClass().getName()));
088:                }
089:                params = (List) o;
090:            }
091:
092:            public void load() {
093:                super .load();
094:
095:                log = (LoggingService) sb.getService(this ,
096:                        LoggingService.class, null);
097:                if (log == null) {
098:                    log = LoggingService.NULL;
099:                }
100:
101:                try {
102:                    _load();
103:                } catch (Exception e) {
104:                    throw new RuntimeException("Unable to load " + this , e);
105:                }
106:            }
107:
108:            private void _load() throws Exception {
109:                // extract parameters
110:                int n = (params == null ? 0 : params.size());
111:                if (n < 2 || n > 4) {
112:                    throw new RuntimeException(
113:                            "Expecting 2..4 parameters, not " + n);
114:                }
115:                Object cl_obj = params.get(0);
116:                Object cb_obj = params.get(1);
117:                Object req_obj = (n > 2 ? params.get(2) : null);
118:                Object is_late_obj = (n > 3 ? params.get(3) : "true");
119:
120:                // get class
121:                if (cl_obj instanceof  String) {
122:                    cl_obj = Class.forName((String) cl_obj);
123:                }
124:                if (!(cl_obj instanceof  Class)) {
125:                    throw new RuntimeException(
126:                            "Expecting a Class or String, not " + cl_obj);
127:                }
128:                cl = (Class) cl_obj;
129:                is_sb = ServiceBroker.class.isAssignableFrom(cl);
130:
131:                // get callback
132:                if (cb_obj == null) {
133:                    throw new RuntimeException("Null callback_object");
134:                }
135:                if (cb_obj instanceof  String) {
136:                    cb_obj = Class.forName((String) cb_obj);
137:                }
138:                if (cb_obj instanceof  Class) {
139:                    cb_obj = ((Class) cb_obj).newInstance();
140:                }
141:                if (!(cb_obj instanceof  GetServiceCallback)) {
142:                    cb_obj = Reflection.makeProxy(cb_obj,
143:                            GetServiceCallback.class);
144:                }
145:                cb = (GetServiceCallback) cb_obj;
146:
147:                // get requestor, which is typically "this"
148:                if (req_obj == null || "this".equals(req_obj) || is_sb) {
149:                    req_obj = this ;
150:                }
151:                if (req_obj instanceof  String) {
152:                    req_obj = Class.forName((String) req_obj);
153:                }
154:                if (req_obj instanceof  Class) {
155:                    req_obj = ((Class) req_obj).newInstance();
156:                }
157:                req = req_obj;
158:
159:                // get service
160:                if (is_sb || sb.hasService(cl)) {
161:                    // found it
162:                    svc = (is_sb ? sb : sb.getService(req, cl, null));
163:                    cb.setService(cl, svc);
164:                    return;
165:                }
166:                if (!"true".equals(is_late_obj)) {
167:                    // fail
168:                    cb.setService(cl, null);
169:                    return;
170:                }
171:                // listen
172:                ServiceAvailableListener sal = new ServiceAvailableListener() {
173:                    public void serviceAvailable(ServiceAvailableEvent ae) {
174:                        if (cl.isAssignableFrom(ae.getService())) {
175:                            svc = sb.getService(req, cl, null);
176:                            cb.setService(cl, svc);
177:                            //sb.removeServiceListener(this);
178:                        }
179:                    }
180:                };
181:                sb.addServiceListener(sal);
182:            }
183:
184:            public void unload() {
185:                if (svc != null) {
186:                    try {
187:                        cb.setService(cl, null);
188:                    } catch (Exception e) {
189:                    }
190:                    if (!is_sb) {
191:                        sb.releaseService(req, cl, svc);
192:                    }
193:                    svc = null;
194:                }
195:
196:                cl = null;
197:                cb = null;
198:                req = null;
199:
200:                if (log != null && log != LoggingService.NULL) {
201:                    sb.releaseService(this , LoggingService.class, log);
202:                    log = null;
203:                }
204:
205:                super.unload();
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.