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


001:        /* 
002:         * <copyright>
003:         *  
004:         *  Copyright 2002-2004 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.adaptivity;
028:
029:        import java.io.InputStreamReader;
030:        import java.io.Reader;
031:        import java.util.ArrayList;
032:        import java.util.Iterator;
033:        import java.util.List;
034:
035:        import org.cougaar.bootstrap.SystemProperties;
036:        import org.cougaar.core.component.ServiceBroker;
037:        import org.cougaar.core.component.ServiceProvider;
038:        import org.cougaar.core.plugin.ComponentPlugin;
039:        import org.cougaar.core.service.LoggingService;
040:        import org.cougaar.core.service.PlaybookConstrainService;
041:        import org.cougaar.core.service.PlaybookReadService;
042:        import org.cougaar.util.CircularQueue;
043:
044:        /**
045:         * A container for the active Plays. The plays are initialized from a
046:         * file specified as a plugin parameter. Playbook users access the
047:         * plays through two services: {@link PlaybookReadService} and
048:         * {@link PlaybookConstrainService}. The former returns the constrained
049:         * plays. The latter constrains the original plays with
050:         * {@link OperatingModePolicy}s.
051:         **/
052:        public class PlaybookManager extends ComponentPlugin implements 
053:                ServiceProvider {
054:            private Playbook playbook;
055:            private LoggingService logger;
056:            private List listeners = new ArrayList(2);
057:            private CircularQueue todo = new CircularQueue();
058:
059:            private class PlaybookReadServiceImpl implements 
060:                    PlaybookReadService {
061:                private boolean active = true;
062:
063:                /**
064:                 * Gets an array of the current plays
065:                 * @return an array of the current plays
066:                 **/
067:                public Play[] getCurrentPlays() {
068:                    if (!active)
069:                        throw new RuntimeException(
070:                                "Service has been released or revoked");
071:                    return playbook.getCurrentPlays();
072:                }
073:
074:                /**
075:                 * Add a listener to the playbook. The listener will be
076:                 * publishChanged if this Playbook is modified.
077:                 * @param l the Listener
078:                 **/
079:                public void addListener(Listener l) {
080:                    if (!active)
081:                        throw new RuntimeException(
082:                                "Service has been released or revoked");
083:                    listeners.add(l);
084:                }
085:
086:                /**
087:                 * Remove a listener to the playbook. The listener will no longer
088:                 * be publishChanged if this Playbook is modified.
089:                 * @param l the Listener
090:                 **/
091:                public void removeListener(Listener l) {
092:                    if (!active)
093:                        throw new RuntimeException(
094:                                "Service has been released or revoked");
095:                    listeners.remove(l);
096:                }
097:            }
098:
099:            private class PlaybookConstrainServiceImpl implements 
100:                    PlaybookConstrainService {
101:                private boolean active = true;
102:
103:                /**
104:                 * Add another OperatingModePolicy constraint. The plays are
105:                 * modified so that in all cases where the if clause of the
106:                 * constraint is true the OperatingMode ranges will all fall
107:                 * within the constraint.
108:                 * @param omp the constraint to add
109:                 **/
110:                public void constrain(OperatingModePolicy omp) {
111:                    if (!active)
112:                        throw new RuntimeException(
113:                                "Service has been released or revoked");
114:                    playbook.addConstraint(omp);
115:                    fireListenersLater();
116:                }
117:
118:                /**
119:                 * Remove an OperatingModePolicy constraint. The current plays are
120:                 * recomputed to omit the removed constraint.
121:                 * @param omp the constraint to remove
122:                 **/
123:                public void unconstrain(OperatingModePolicy omp) {
124:                    if (!active)
125:                        throw new RuntimeException(
126:                                "Service has been released or revoked");
127:                    playbook.removeConstraint(omp);
128:                    fireListenersLater();
129:                }
130:            }
131:
132:            /**
133:             * Override to register the services we provide.
134:             **/
135:            public void load() {
136:                super .load();
137:                logger = (LoggingService) getServiceBroker().getService(this ,
138:                        LoggingService.class, null);
139:                playbook = new Playbook(logger);
140:                getServiceBroker().addService(PlaybookReadService.class, this );
141:                getServiceBroker().addService(PlaybookConstrainService.class,
142:                        this );
143:            }
144:
145:            /**
146:             * Override to unregister the services we provide.
147:             **/
148:            public void unload() {
149:                getServiceBroker().revokeService(
150:                        PlaybookConstrainService.class, this );
151:                getServiceBroker().revokeService(PlaybookReadService.class,
152:                        this );
153:                getServiceBroker().releaseService(this , LoggingService.class,
154:                        logger);
155:                super .unload();
156:            }
157:
158:            /**
159:             * Read the plays from a file.
160:             **/
161:            public void setupSubscriptions() {
162:                Iterator iter = getParameters().iterator();
163:                if (!iter.hasNext()) {
164:                    logger.error("Missing playbook file name.");
165:                } else {
166:                    String playFileName = iter.next().toString();
167:                    try {
168:                        Reader is = new InputStreamReader(getConfigFinder()
169:                                .open(playFileName));
170:                        try {
171:                            Parser p = new Parser(is, logger);
172:                            Play[] plays = p.parsePlays();
173:                            playbook.setPlays(plays);
174:                            fireListeners();
175:                        } finally {
176:                            is.close();
177:                        }
178:                    } catch (Exception e) {
179:                        logger.error("Error parsing play file", e);
180:                    }
181:                }
182:            }
183:
184:            /**
185:             * Handle requests that arrived through our services. These requests
186:             * all fire listeners. The services cannot themselves do this
187:             * because of the possibility of a deadlock due to attempts to open
188:             * two blackboard transactions simultaneously. The requests are
189:             * placed in a queue and executed here.
190:             **/
191:            public void execute() {
192:                synchronized (todo) {
193:                    while (todo.size() > 0) {
194:                        try {
195:                            ((Runnable) todo.next()).run();
196:                        } catch (RuntimeException e) {
197:                            logger.error("Error running delayed job", e);
198:                        }
199:                    }
200:                }
201:            }
202:
203:            /**
204:             * Gets (creates) one of our services. Part of the implementation of
205:             * the ServiceProvider API
206:             * @param sb the ServiceBroker making the request
207:             * @param requestor the actual requestor on whose behalf the broker is acting
208:             * @param serviceClass the class of the Service desired.
209:             * @return an instance of the requested Service if it one we supply.
210:             **/
211:            public Object getService(ServiceBroker sb, Object requestor,
212:                    Class serviceClass) {
213:                if (serviceClass == PlaybookReadService.class) {
214:                    return new PlaybookReadServiceImpl();
215:                }
216:                if (serviceClass == PlaybookConstrainService.class) {
217:                    return new PlaybookConstrainServiceImpl();
218:                }
219:                return null;
220:            }
221:
222:            /**
223:             * Release one of our services. The services use no resources, so
224:             * there is nothing to do.
225:             **/
226:            public void releaseService(ServiceBroker sb, Object requestor,
227:                    Class serviceClass, Object service) {
228:                if (service instanceof  PlaybookReadServiceImpl) {
229:                    ((PlaybookReadServiceImpl) service).active = false;
230:                    return;
231:                }
232:                if (service instanceof  PlaybookConstrainServiceImpl) {
233:                    ((PlaybookConstrainServiceImpl) service).active = false;
234:                    return;
235:                }
236:                throw new IllegalArgumentException("Not my service: " + service);
237:            }
238:
239:            private void fireListenersLater() {
240:                synchronized (todo) {
241:                    todo.add(new Runnable() {
242:                        public void run() {
243:                            fireListeners();
244:                        }
245:                    });
246:                }
247:                blackboard.signalClientActivity();
248:            }
249:
250:            private void fireListeners() {
251:                for (Iterator i = listeners.iterator(); i.hasNext();) {
252:                    PlaybookReadService.Listener l = (PlaybookReadService.Listener) i
253:                            .next();
254:                    blackboard.publishChange(l);
255:                }
256:                if (logger.isDebugEnabled())
257:                    logger.debug("New constrained plays"
258:                            + SystemProperties.getProperty("line.separator")
259:                            + this);
260:            }
261:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.