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


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 1997-2004 SRA International
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:
028:        package org.cougaar.logistics.plugin.policy;
029:
030:        import org.cougaar.core.blackboard.SubscriberException;
031:        import org.cougaar.planning.plugin.legacy.SimplePlugin;
032:        import org.cougaar.mlm.plugin.ldm.*;
033:        import org.cougaar.planning.ldm.policy.Policy;
034:
035:        import java.util.Enumeration;
036:        import java.util.Properties;
037:        import java.util.Vector;
038:        import java.util.Iterator;
039:        import java.util.HashSet;
040:        import java.util.Set;
041:        import java.util.Collection;
042:        import java.util.Collections;
043:        import org.cougaar.util.MutableTimeSpan;
044:        import org.cougaar.core.util.UID;
045:        import org.cougaar.logistics.ldm.Constants;
046:        import org.cougaar.planning.ldm.plan.RelationshipSchedule;
047:        import org.cougaar.core.blackboard.IncrementalSubscription;
048:        import org.cougaar.glm.ldm.asset.Organization;
049:        import org.cougaar.util.UnaryPredicate;
050:        import org.cougaar.glm.ldm.oplan.TimeSpan;
051:        import org.cougaar.core.mts.MessageAddress;
052:        import org.cougaar.planning.ldm.plan.RelationshipImpl;
053:        import org.cougaar.core.plugin.ComponentPlugin;
054:        import org.cougaar.core.service.DomainService;
055:        import org.cougaar.core.service.UIDService;
056:        import org.cougaar.planning.ldm.PlanningFactory;
057:        import org.cougaar.core.component.ServiceRevokedListener;
058:        import org.cougaar.core.component.ServiceRevokedEvent;
059:        import org.cougaar.planning.service.LDMService;
060:        import org.cougaar.multicast.AttributeBasedAddress;
061:
062:        import org.cougaar.core.service.LoggingService;
063:        import org.cougaar.core.service.community.CommunityService;
064:
065:        /**
066:         * An instance of an LDMPlugin that reads a Cluster's startup policy
067:         * from an XML file.
068:         *
069:         * This Plugin is invoked with zero or more parameters. Parameters should be
070:         * listed in pairs, the policy to be read in and the role to which it should
071:         * be propagated. The files are found using the cluster's ConfigFinder.
072:         * This plugin also handles LogisticsPolicies which are propagated to it.
073:         *  Example from a sample cluster.ini file:
074:         * <PRE>
075:         * plugin=org.cougaar.logistics.plugin.policy.LogisticsPolicyManagerPlugin(POLICY=policy.ldm.xml,DESTINATION=Subordinate )
076:         * </PRE>
077:         *
078:         *
079:         */
080:        public class LogisticsPolicyManagerPlugin extends ComponentPlugin {
081:            private boolean debugOn = false;
082:
083:            private XMLPolicyCreator policyCreator;
084:            //private Properties globalParameters = new Properties();
085:            private String xmlfilename;
086:            protected Organization myOrganization_ = null;
087:            private IncrementalSubscription selfOrganizations_;
088:            private IncrementalSubscription allOrganizations_;
089:            private IncrementalSubscription allPolicies_;
090:            protected boolean policyRead = false;
091:            protected String myOrgName_;
092:            private PlanningFactory theLDMF = null;
093:            private UIDService uidService = null;
094:            private CommunityService comserv = null;
095:            private LoggingService logger;
096:
097:            /**
098:             * constructor
099:             */
100:            public LogisticsPolicyManagerPlugin() {
101:                super ();
102:                //globalParameters.put( "XMLFile", xmlfilename );
103:            }
104:
105:            IncrementalSubscription sub;
106:            IncrementalSubscription comSub;
107:
108:            private static UnaryPredicate CommunityPolicyPredicate_ = new UnaryPredicate() {
109:
110:                public boolean execute(Object o) {
111:                    if (o instanceof  LogisticsPolicy) {
112:                        LogisticsPolicy lp = (LogisticsPolicy) o;
113:                        if (lp.getRole().equals("Subordinate")
114:                                || (lp.getRole().equals("none"))) {
115:                            return false;
116:                        } else
117:                            return true;
118:                    }
119:                    return false;
120:                }
121:            };
122:
123:            private static UnaryPredicate SubordinatePolicyPredicate_ = new UnaryPredicate() {
124:
125:                public boolean execute(Object o) {
126:                    if (o instanceof  LogisticsPolicy) {
127:                        LogisticsPolicy lp = (LogisticsPolicy) o;
128:                        if (lp.getRole().equals("Subordinate")
129:                                || (lp.getRole().equals("none"))) {
130:                            return true;
131:                        }
132:                    }
133:                    return false;
134:                }
135:            };
136:
137:            /**
138:             * Predicate that looks for host Organization
139:             */
140:
141:            private static UnaryPredicate myOrgsPredicate_ = new UnaryPredicate() {
142:                public boolean execute(Object o) {
143:                    if (o instanceof  Organization) {
144:                        Organization org = (Organization) o;
145:                        boolean self = org.isSelf();
146:                        return self;
147:                    }
148:                    return false;
149:                }
150:            };
151:
152:            /**
153:             * Predicate to look for other Organizations
154:             */
155:
156:            private static UnaryPredicate allOrgsPredicate_ = new UnaryPredicate() {
157:                public boolean execute(Object o) {
158:                    if (o instanceof  Organization) {
159:                        Organization org = (Organization) o;
160:                        if (org.isSelf()) {
161:                            return false;
162:                        } else {
163:                            return true;
164:                        }
165:                    }
166:                    return false;
167:                }
168:            };
169:
170:            private static UnaryPredicate policiesPredicate_ = new UnaryPredicate() {
171:                public boolean execute(Object o) {
172:                    boolean isInstance = false;
173:                    if (o instanceof  Policy) {
174:                        isInstance = true;
175:                    }
176:                    return isInstance;
177:                }
178:
179:            };
180:
181:            protected void setupSubscriptions() {
182:                DomainService dService = null;
183:                if (theLDMF == null) {
184:                    dService = (DomainService) getBindingSite()
185:                            .getServiceBroker()
186:                            .getService(
187:                                    this ,
188:                                    org.cougaar.core.service.DomainService.class,
189:                                    new ServiceRevokedListener() {
190:                                        public void serviceRevoked(
191:                                                ServiceRevokedEvent re) {
192:                                            theLDMF = null;
193:                                        }
194:                                    });
195:                }
196:                if (uidService == null) {
197:                    uidService = (UIDService) getBindingSite()
198:                            .getServiceBroker().getService(this ,
199:                                    org.cougaar.core.service.UIDService.class,
200:                                    null);
201:                }
202:                //use the service
203:                theLDMF = (PlanningFactory) dService.getFactory("planning");
204:                selfOrganizations_ = (IncrementalSubscription) getBlackboardService()
205:                        .subscribe(myOrgsPredicate_);
206:                allOrganizations_ = (IncrementalSubscription) getBlackboardService()
207:                        .subscribe(allOrgsPredicate_);
208:                allPolicies_ = (IncrementalSubscription) getBlackboardService()
209:                        .subscribe(policiesPredicate_);
210:                getBlackboardService().getSubscriber().setShouldBePersisted(
211:                        false);
212:
213:                sub = (IncrementalSubscription) getBlackboardService()
214:                        .subscribe(SubordinatePolicyPredicate_);
215:
216:                comSub = (IncrementalSubscription) getBlackboardService()
217:                        .subscribe(CommunityPolicyPredicate_);
218:
219:                if (comserv == null) {
220:                    comserv = (CommunityService) getBindingSite()
221:                            .getServiceBroker()
222:                            .getService(
223:                                    this ,
224:                                    org.cougaar.core.service.community.CommunityService.class,
225:                                    new ServiceRevokedListener() {
226:                                        public void serviceRevoked(
227:                                                ServiceRevokedEvent re) {
228:                                            theLDMF = null;
229:                                        }
230:                                    });
231:                }
232:
233:                if (getBlackboardService().didRehydrate()) {
234:                    if (!selfOrganizations_.isEmpty()) {
235:                        myOrganization_ = (Organization) selfOrganizations_
236:                                .iterator().next();
237:                        myOrgName_ = myOrganization_.getItemIdentificationPG()
238:                                .getItemIdentification();
239:                    } else {
240:                        logger
241:                                .error(" Self organization subscription was empty on rehydration ");
242:                    }
243:                }
244:            }
245:
246:            /**
247:             * method to go through parameters of filenames and roles and create policies from xml files using XMLPolicyCreator
248:             */
249:
250:            private void readInPolicies() {
251:                if (logger.isDebugEnabled()) {
252:                    logger.debug("in readInPolicies");
253:                }
254:                try {
255:
256:                    Collection pv = getParameters();
257:                    if (pv != null) {
258:                        if (logger.isDebugEnabled()) {
259:                            logger
260:                                    .debug("about to enter for loop vector size: "
261:                                            + pv.size());
262:                        }
263:                        // iterate through the list of XML Policy files to parse
264:                        Vector policyVector = new Vector();
265:                        Vector communityVector = new Vector();
266:                        for (Iterator pi = pv.iterator(); pi.hasNext();) {
267:                            xmlfilename = (String) pi.next();
268:                            if (logger.isDebugEnabled()) {
269:                                logger.debug("startsWith: "
270:                                        + xmlfilename.startsWith("POLICY="));
271:                            }
272:                            if (xmlfilename.startsWith("POLICY=")) {
273:                                xmlfilename = xmlfilename.replaceFirst(
274:                                        "POLICY=", "");
275:                            }
276:                            policyCreator = new XMLPolicyCreator(xmlfilename,
277:                                    getConfigFinder(), theLDMF);
278:                            Policy policies[] = policyCreator.getPolicies();
279:                            String role = (String) pi.next();
280:                            if (role.startsWith("DESTINATION=")) {
281:                                role = role.replaceFirst("DESTINATION=", "");
282:                            }
283:                            if (logger.isDebugEnabled()) {
284:                                logger.debug("*******policies.length "
285:                                        + policies.length);
286:                            }
287:
288:                            for (int i = 0; i < policies.length; i++) {
289:                                if (myOrganization_ == null
290:                                        && logger.isDebugEnabled()) {
291:                                    logger.debug("Org is null");
292:                                }
293:                                if (logger.isDebugEnabled()) {
294:                                    logger.debug("policy UID: "
295:                                            + policies[i].getUID());
296:                                }
297:                                boolean continu = true;
298:                                for (Iterator t = allPolicies_.iterator(); t
299:                                        .hasNext();) {
300:                                    Policy compP = (Policy) t.next();
301:                                    if (compP.getName().equals(
302:                                            policies[i].getName())) {
303:                                        continu = false;
304:                                    }
305:                                }
306:                                if (continu == true) {
307:                                    LogisticsPolicy lp = new LogisticsPolicy(
308:                                            policies[i], null,
309:                                            Collections.EMPTY_SET, myOrgName_,
310:                                            role);
311:                                    if (role.equals("Subordinate")
312:                                            || role.equals("none")) {
313:                                        policyVector.add(lp);
314:                                    } else {
315:                                        communityVector.add(lp);
316:                                    }
317:                                }
318:
319:                            }
320:                        }
321:                        //	distributeCheckOrgs(policyVector.elements(), allOrganizations_.elements());
322:                        distributeCheckPolicies(policyVector.elements());
323:                        if (!communityVector.isEmpty()) {
324:                            distributeCommunityPolicies(communityVector
325:                                    .elements());
326:                        }
327:                    }
328:                } catch (SubscriberException se) {
329:                    se.printStackTrace();
330:                }
331:                policyRead = true;
332:            }
333:
334:            /**
335:             * responds to changes in subscriptions
336:             */
337:            public void execute() {
338:
339:                if (selfOrganizations_.hasChanged()) {
340:                    if (myOrganization_ == null) {
341:                        Enumeration new_orgs = selfOrganizations_.elements();
342:                        if (logger.isDebugEnabled()) {
343:                            logger.debug("selfOrganizations has changed");
344:                        }
345:                        if (new_orgs.hasMoreElements()) {
346:                            myOrganization_ = (Organization) new_orgs
347:                                    .nextElement();
348:                            myOrgName_ = myOrganization_
349:                                    .getItemIdentificationPG()
350:                                    .getItemIdentification();
351:                        }
352:                        readInPolicies();
353:                    }
354:                }
355:
356:                if (sub.hasChanged()) {
357:                    if (myOrganization_ != null) {
358:                        if (logger.isDebugEnabled()) {
359:                            logger
360:                                    .debug("sub has changed and myOrg is defined");
361:                        }
362:                        distributeCheckPolicies(sub.getAddedList());
363:                        distributeCheckPolicies(sub.getChangedList());
364:                    }
365:                }
366:
367:                if (allOrganizations_.hasChanged()) {
368:                    if (myOrganization_ != null) {
369:                        Enumeration allOrgChanged = allOrganizations_
370:                                .getChangedList();
371:                        Enumeration allOrgAdded = allOrganizations_
372:                                .getAddedList();
373:                        if (logger.isDebugEnabled()) {
374:                            logger
375:                                    .debug("AllOrganizations has changed. AddedList is "
376:                                            + allOrgAdded
377:                                            + " ChangedList is "
378:                                            + allOrgChanged);
379:                        }
380:                        //        distributeCheckOrgs(sub.elements(), allOrganizations_.getAddedList());
381:                        //        distributeCheckOrgs(sub.elements(), allOrganizations_.getChangedList());
382:                        distributeCheckOrgs(sub.elements(), allOrgAdded);
383:                        distributeCheckOrgs(sub.elements(), allOrgChanged);
384:
385:                    }
386:                }
387:
388:                if (comSub.hasChanged()) {
389:                    communityPolicyPublisher(comSub.getAddedList());
390:                }
391:            }
392:
393:            private void communityPolicyPublisher(Enumeration comPols) {
394:                while (comPols.hasMoreElements()) {
395:                    LogisticsPolicy lp = (LogisticsPolicy) comPols
396:                            .nextElement();
397:                    Policy p = (Policy) lp.getPolicy();
398:                    if (isConflict(lp) == false) {
399:                        getBlackboardService().publishAdd(p);
400:                    }
401:                }
402:
403:            }
404:
405:            private void distributeCommunityPolicies(Enumeration policies) {
406:                Collection communities = comserv.listAllCommunities();
407:                while (policies.hasMoreElements()) {
408:                    LogisticsPolicy lp = (LogisticsPolicy) policies
409:                            .nextElement();
410:                    for (Iterator i = communities.iterator(); i.hasNext();) {
411:                        String comName = (String) i.next();
412:                        lp.addTarget(AttributeBasedAddress
413:                                .getAttributeBasedAddress(comName, "Role", lp
414:                                        .getRole()));
415:                    }
416:                    if (lp.getUID() == null) {
417:                        lp.setUID(uidService.nextUID());
418:                    }
419:                    lp.setSource(myOrganization_.getMessageAddress());
420:                    getBlackboardService().publishAdd(lp);
421:                }
422:            }
423:
424:            /**
425:             * method to check changed or added policies for whether they a) need to be published and b) need to be propagated. calls distributeCheckOrgs if more than zero policies qualify to continue
426:             * @param policies Enumeration of LogisticsPolicies to be checked
427:             */
428:            private void distributeCheckPolicies(Enumeration policies) {
429:                boolean debugging = false;
430:                if (logger.isDebugEnabled()) {
431:                    debugging = true;
432:                }
433:                if (debugging)
434:                    logger.debug("in checkPolicies");
435:                Vector disPolicies = new Vector();
436:                if (debugging)
437:                    logger.debug("in Check");
438:                while (policies.hasMoreElements()) {
439:                    if (debugging)
440:                        logger.debug("Check creating LP");
441:                    LogisticsPolicy lp = (LogisticsPolicy) policies
442:                            .nextElement();
443:                    if (debugging)
444:                        logger.debug("lp.getSource: " + lp.getSource()
445:                                + " myOrg: "
446:                                + myOrganization_.getMessageAddress());
447:                    if (lp.getSource() != null) {
448:                        if (debugging)
449:                            logger
450:                                    .debug("so equals? "
451:                                            + lp
452:                                                    .getSource()
453:                                                    .equals(
454:                                                            myOrganization_
455:                                                                    .getMessageAddress()));
456:                    } else {
457:                        if (debugging)
458:                            logger.debug("lp.getSource is null");
459:                    }
460:                    if (debugging)
461:                        logger.debug("lp.targets " + lp.getTargets());
462:                    if (lp.getSource() == null
463:                            || !lp.getSource().equals(
464:                                    myOrganization_.getMessageAddress())
465:                            || (lp.getSource().equals(
466:                                    myOrganization_.getMessageAddress()) && (lp
467:                                    .getTargets() == null || lp.getTargets()
468:                                    .isEmpty()))) {
469:                        if (debugging)
470:                            logger.debug("role: " + lp.getRole()
471:                                    + " condition? "
472:                                    + (!lp.getRole().equals("none")));
473:                        Policy p = lp.getPolicy();
474:                        if (p.getUID() == null) {
475:                            p.setUID(uidService.nextUID());
476:                        }
477:                        if (!lp.getRole().equals("none")) {
478:                            if (debugging)
479:                                logger.debug("adding lp to disPolicies");
480:                            disPolicies.add(lp);
481:                        }
482:                        if (isConflict(lp) == false) {
483:                            getBlackboardService().publishAdd(p);
484:                            if (debugging)
485:                                logger.debug("published Policy " + p.getUID()
486:                                        + " at " + myOrgName_);
487:                        }
488:                    }
489:                }
490:                if (debugging)
491:                    logger.debug("validPolicies.size: " + disPolicies.size());
492:                if (disPolicies.size() != 0) {
493:                    if (debugging)
494:                        logger.debug("sending to distribute from Check");
495:                    distributeCheckOrgs(disPolicies.elements(),
496:                            allOrganizations_.elements());
497:
498:                }
499:            }
500:
501:            /**
502:             * method to check an Enumeration of Organizations as targets for each policy
503:             * @param policies Enumeration of LogisticsPolicies to determine new targets for
504:             * @param targets Enumeration of Organizations to be checked as targets
505:             */
506:
507:            private void distributeCheckOrgs(Enumeration policies,
508:                    Enumeration targets) {
509:                if (logger.isDebugEnabled()) {
510:                    logger.debug("in checkOrgs");
511:                }
512:                Vector allTargets = new Vector();
513:                while (targets.hasMoreElements()) {
514:                    Organization o = (Organization) targets.nextElement();
515:                    allTargets.add(o);
516:                    if (logger.isDebugEnabled()) {
517:                        logger.debug("New or Changed Org is: " + o);
518:                    }
519:                    //allTargets.add(targets.nextElement());
520:                }
521:                Vector goodTargets = new Vector();
522:                while (policies.hasMoreElements()) {
523:                    LogisticsPolicy lp = (LogisticsPolicy) policies
524:                            .nextElement();
525:                    MutableTimeSpan mts = new MutableTimeSpan();
526:                    Enumeration t = allTargets.elements();
527:                    while (t.hasMoreElements()) {
528:                        Organization org = (Organization) t.nextElement();
529:                        Collection sups = org.getSuperiors(mts.getStartTime(),
530:                                mts.getEndTime()); //assume subordinates are what we are matching for now
531:                        if (logger.isDebugEnabled()) {
532:                            logger.debug("Test: " + org.getMessageAddress()
533:                                    + " sups: " + sups.toString());
534:                        }
535:                        for (Iterator i = sups.iterator(); i.hasNext();) {
536:                            RelationshipImpl ri = (RelationshipImpl) i.next();
537:                            Organization oa = (Organization) ri.getA();
538:                            Organization ob = (Organization) ri.getB();
539:
540:                            if (oa.equals(myOrganization_)
541:                                    || ob.equals(myOrganization_)) {
542:                                goodTargets.add(org);
543:                            }
544:                        }
545:                    }
546:                    distribute(lp, goodTargets.elements());
547:                }
548:            }
549:
550:            /**
551:             * method to distribute one LogisticsPolicy to an Enumeration of targets
552:             * @param lp LogisticsPolicy to be distributed, by PublishAdd or PublishChange
553:             * @param targets Enumeration of Organizations from which MessageAddresses will be pulled to populated the LogisticsPolicy's targets
554:             */
555:
556:            private void distribute(LogisticsPolicy lp, Enumeration targets) {
557:                Set mas = new HashSet();
558:                if (logger.isDebugEnabled()) {
559:                    logger.debug("in distribute. target.hasMore: "
560:                            + targets.hasMoreElements());
561:                }
562:                if (targets != null && targets.hasMoreElements()) {
563:                    while (targets.hasMoreElements()) {
564:                        Organization org = (Organization) targets.nextElement();
565:                        MessageAddress ma = (MessageAddress) org
566:                                .getMessageAddress();
567:                        if (!lp.getTargets().contains(ma)) {
568:                            if (logger.isDebugEnabled()) {
569:                                logger.debug("adding ma " + ma.toString());
570:                            }
571:                            mas.add(ma);
572:                        }
573:                    }
574:                } else {
575:                    mas = Collections.EMPTY_SET;
576:                }
577:                if (logger.isDebugEnabled()) {
578:                    logger.debug("lp.getSource: " + lp.getSource());
579:                    if (lp.getSource() != null)
580:                        logger.debug("1source equal myOrg? "
581:                                + lp.getSource().equals(
582:                                        myOrganization_.getMessageAddress()));
583:                    logger
584:                            .debug("lp.second if statement: "
585:                                    + (lp.getSource() == null || !lp
586:                                            .getSource()
587:                                            .equals(
588:                                                    myOrganization_
589:                                                            .getMessageAddress())));
590:                }
591:                if (!(lp.getSource() == null
592:                        && (lp.getTargets() == null || lp.getTargets()
593:                                .isEmpty()) && lp.getAuthority() == myOrgName_)) {
594:                    if (lp.getUID() == null) {
595:                        UID uid = uidService.nextUID();
596:                        lp.setUID(uid);
597:                    }
598:                    if (!((mas == null) || mas.isEmpty() || mas.equals(lp
599:                            .getTargets()))) {
600:                        lp.setTargets(mas);
601:                        lp.setSource(myOrganization_.getMessageAddress());
602:                        getBlackboardService().publishChange(lp);
603:                        if (logger.isDebugEnabled()) {
604:                            logger.debug("publishChange: "
605:                                    + lp.getUID().toString() + " at "
606:                                    + myOrgName_ + " targets: "
607:                                    + lp.getTargets().toString());
608:                        }
609:                    }
610:                } else {
611:                    LogisticsPolicy newLP = new LogisticsPolicy(lp.getPolicy(),
612:                            myOrganization_.getMessageAddress(), mas, lp
613:                                    .getAuthority(), lp.getRole());
614:                    UID uid = uidService.nextUID();
615:                    newLP.setUID(uid);
616:                    if (isConflict(lp) == false) {
617:                        getBlackboardService().publishAdd(newLP);
618:                        if (logger.isDebugEnabled()) {
619:                            logger.debug("publishAdd: "
620:                                    + newLP.getUID().toString() + " at "
621:                                    + myOrgName_ + " targets: "
622:                                    + newLP.getTargets().toString());
623:                        }
624:                    }
625:                }
626:            }
627:
628:            private boolean isConflict(LogisticsPolicy lp) {
629:                Policy p = lp.getPolicy();
630:                Enumeration pols = allPolicies_.elements();
631:                boolean match = false;
632:                Policy matchPol = null;
633:                //see if the type of policy in question has been published already
634:                while (pols.hasMoreElements()) {
635:                    Policy pol = (Policy) pols.nextElement();
636:                    if (pol.getName().equals(p.getName()) && pol != p) {
637:                        match = true;
638:                        matchPol = pol;
639:                    }
640:                    //If this is the exact object then there is a "conflict" because we don't want to 
641:                    //republish anything.
642:                    else if (pol == p) {
643:                        return true;
644:                    }
645:                }
646:                if (match == true) {
647:                    boolean eq = false;
648:                    LogisticsPolicy matchLP = null;
649:                    //if so, check to see if the policy has a corresponding LogisticsPolicy
650:                    //if not, the policy was fed in at the agent itself and that agent becomes the authority
651:                    Enumeration lps = sub.elements();
652:                    while (lps.hasMoreElements()) {
653:                        LogisticsPolicy curLP = (LogisticsPolicy) lps
654:                                .nextElement();
655:                        if (curLP.getPolicy().equals(matchPol)) {
656:                            eq = true;
657:                            matchLP = curLP;
658:                        }
659:                    }
660:                    if (eq == false) {
661:                        Enumeration comlps = comSub.elements();
662:                        while (comlps.hasMoreElements()) {
663:                            LogisticsPolicy curLP = (LogisticsPolicy) comlps
664:                                    .nextElement();
665:                            if (curLP.getPolicy().equals(matchPol)) {
666:                                eq = true;
667:                                matchLP = curLP;
668:                            }
669:                        }
670:                    }
671:                    if (matchLP != null
672:                            && matchLP.getPolicy().equals(lp.getPolicy()))
673:                        eq = false;
674:                    //get instance of Organization that is source of each Policy
675:                    if (eq == true) {
676:                        //conflict -- if true, remove matchLP
677:                        if (matchLP.getAuthority().equals(lp.getAuthority())) {
678:                            getBlackboardService().publishRemove(matchLP);
679:                            getBlackboardService().publishRemove(matchPol);
680:                            return false;
681:                        } else if ((matchLP.getSource() != null && lp
682:                                .getSource() != null)
683:                                && matchLP.getSource().equals(lp.getSource())) {
684:                            getBlackboardService().publishRemove(matchLP);
685:                            getBlackboardService().publishRemove(matchPol);
686:                            return false;
687:                        } else {
688:                            Enumeration orgs = allOrganizations_.elements();
689:                            Organization newOrg = null;
690:                            Organization oldOrg = null;
691:                            while (orgs.hasMoreElements()) {
692:                                Organization o = (Organization) orgs
693:                                        .nextElement();
694:                                if (lp.getSource() == null) {
695:                                    newOrg = myOrganization_;
696:                                } else if (lp.getSource().toString().equals(
697:                                        o.getItemIdentificationPG()
698:                                                .getItemIdentification())) {
699:                                    newOrg = o;
700:                                }
701:                                if (matchLP.getSource().toString().equals(
702:                                        o.getItemIdentificationPG()
703:                                                .getItemIdentification())) {
704:                                    oldOrg = o;
705:                                }
706:
707:                            }
708:                            if (oldOrg == null
709:                                    && !(matchLP.getSource().toString().equals(
710:                                            myOrgName_) && newOrg == null)) {
711:                                if (matchLP != null
712:                                        && (!matchLP.getRole().equals(
713:                                                "Subordinate") || (matchLP
714:                                                .getRole()
715:                                                .equals("Subordinate") && lp
716:                                                .getRole()
717:                                                .equals("Subordinate")))) {
718:                                    //					if (matchLP != null && !matchLP.getRole().equals("Subordinate")){
719:                                    getBlackboardService().publishRemove(
720:                                            matchLP);
721:                                }
722:                                if (matchPol != null) {
723:                                    getBlackboardService().publishRemove(
724:                                            matchPol);
725:                                }
726:                                return false;
727:                            } else if (newOrg != null) {
728:                                MutableTimeSpan mts = new MutableTimeSpan();
729:                                Collection sups = myOrganization_.getSuperiors(
730:                                        mts.getStartTime(), mts.getEndTime());
731:                                /***********/
732:                                boolean containsOldOrg = false;
733:                                for (Iterator i = sups.iterator(); i.hasNext();) {
734:                                    RelationshipImpl ri = (RelationshipImpl) i
735:                                            .next();
736:                                    Organization oa = (Organization) ri.getA();
737:                                    Organization ob = (Organization) ri.getB();
738:                                    if (oa.equals(newOrg) || ob.equals(newOrg)) {
739:                                        getBlackboardService().publishRemove(
740:                                                matchLP);
741:                                        getBlackboardService().publishRemove(
742:                                                matchPol);
743:                                        return false;
744:                                    } else if (oa.equals(oldOrg)
745:                                            || ob.equals(oldOrg)) {
746:                                        containsOldOrg = true;
747:                                    }
748:                                }
749:                                if (containsOldOrg == false) {
750:                                    getBlackboardService().publishRemove(
751:                                            matchLP);
752:                                    getBlackboardService().publishRemove(
753:                                            matchPol);
754:                                    return false;
755:                                }
756:                            }
757:                        }
758:                        //if there is no LP match for previous Policy, that means Policy was injected locally
759:                        //so check new LP to see if the source is the Organization's superior
760:                    } else {
761:                        Enumeration orgs = allOrganizations_.elements();
762:                        Organization newOrg = null;
763:                        while (orgs.hasMoreElements()) {
764:                            Organization o = (Organization) orgs.nextElement();
765:                            if (lp.getSource() != null
766:                                    && lp.getSource().toString().equals(
767:                                            o.getItemIdentificationPG()
768:                                                    .getItemIdentification())) {
769:                                newOrg = o;
770:                            }
771:                        }
772:                        if (newOrg != null) {
773:                            MutableTimeSpan mts = new MutableTimeSpan();
774:                            Collection sups = myOrganization_.getSuperiors(mts
775:                                    .getStartTime(), mts.getEndTime());
776:                            for (Iterator i = sups.iterator(); i.hasNext();) {
777:                                RelationshipImpl ri = (RelationshipImpl) i
778:                                        .next();
779:                                Organization oa = (Organization) ri.getA();
780:                                Organization ob = (Organization) ri.getB();
781:                                if (oa.equals(newOrg) || ob.equals(newOrg)) {
782:                                    getBlackboardService().publishRemove(
783:                                            matchPol);
784:                                    return false;
785:                                }
786:                            }
787:                        }
788:                    }
789:                } else {
790:                    return false;
791:                }
792:                //check this
793:                if (lp != null && lp.getSource() != null
794:                        && !lp.getSource().toString().equals(myOrgName_)) {
795:
796:                    //needs to be a check here
797:                    getBlackboardService().publishRemove(lp);
798:                }
799:                return true;
800:
801:            }
802:
803:            /**
804:             *  Calls ComponentPlugin's load(), creates the Logging Service.
805:             **/
806:            public void load() {
807:                super .load();
808:                logger = getLoggingService(this );
809:            } // load
810:
811:            public LoggingService getLoggingService(Object requestor) {
812:                return (LoggingService) getServiceBroker().getService(
813:                        requestor, LoggingService.class, null);
814:            }
815:
816:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.