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


001:        /*
002:         * <copyright>
003:         *
004:         *  Copyright 1997-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.community.init;
028:
029:        import java.util.Collection;
030:        import java.util.HashMap;
031:        import java.util.Iterator;
032:        import java.util.Map;
033:        import java.util.Vector;
034:        import javax.naming.directory.BasicAttributes;
035:
036:        import org.cougaar.community.CommunityUtils;
037:        import org.cougaar.core.component.ServiceBroker;
038:        import org.cougaar.core.component.ServiceProvider;
039:        import org.cougaar.util.ConfigFinder;
040:        import org.cougaar.util.Strings;
041:        import org.xml.sax.Attributes;
042:        import org.xml.sax.InputSource;
043:        import org.xml.sax.XMLReader;
044:        import org.xml.sax.helpers.DefaultHandler;
045:        import org.xml.sax.helpers.XMLReaderFactory;
046:
047:        /**
048:         * Generates a community config from xml file.
049:         */
050:        class FileCommunityInitializerServiceProvider implements 
051:                ServiceProvider {
052:
053:            private static final String DEFAULT_FILE = "communities.xml";
054:
055:            // Node-level cache to avoid reading communities.xml file for each agent
056:            private static final Map fileCache = new HashMap();
057:
058:            public Object getService(ServiceBroker sb, Object requestor,
059:                    Class serviceClass) {
060:                if (serviceClass != CommunityInitializerService.class) {
061:                    throw new IllegalArgumentException(getClass()
062:                            + " does not furnish " + serviceClass);
063:                }
064:                return new CommunityInitializerServiceImpl();
065:            }
066:
067:            public void releaseService(ServiceBroker sb, Object requestor,
068:                    Class serviceClass, Object service) {
069:            }
070:
071:            private class CommunityInitializerServiceImpl implements 
072:                    CommunityInitializerService {
073:
074:                public Collection getCommunityDescriptions(String entityName) {
075:                    String file = System.getProperty(
076:                            "org.cougaar.community.configfile", DEFAULT_FILE);
077:                    Collection ret;
078:                    try {
079:                        ret = getCommunityConfigsFromFile(file, entityName);
080:                    } catch (RuntimeException ex) {
081:                        System.out
082:                                .println("Exception in getCommunityDescriptions from File: "
083:                                        + file);
084:                        ret = new Vector();
085:                    }
086:                    return ret;
087:                }
088:            }
089:
090:            /**
091:             * Get Collection of all CommunityConfig objects from XML file.  Uses
092:             * standard Cougaar config finder to locate XML file.
093:             * @param xmlFileName XML file containing community definitions
094:             * @return Collection of CommunityConfig objects
095:             */
096:            private static Collection getCommunityConfigsFromFile(
097:                    String xmlFileName) {
098:                synchronized (fileCache) {
099:                    Collection communityConfigs = (Collection) fileCache
100:                            .get(xmlFileName);
101:                    if (communityConfigs != null) {
102:                        return communityConfigs;
103:                    }
104:                    try {
105:                        XMLReader xr = XMLReaderFactory.createXMLReader();
106:                        SaxHandler myHandler = new SaxHandler();
107:                        xr.setContentHandler(myHandler);
108:                        InputSource is = new InputSource(ConfigFinder
109:                                .getInstance().open(xmlFileName));
110:                        xr.parse(is);
111:                        communityConfigs = myHandler.getCommunityConfigs();
112:                        fileCache.put(xmlFileName, communityConfigs);
113:                        return communityConfigs;
114:                    } catch (Exception ex) {
115:                        System.out
116:                                .println("Exception parsing Community XML definition, "
117:                                        + ex);
118:                        //System.out.println(getCommunityDescriptorText(fname));
119:                    }
120:                    return new Vector();
121:                }
122:            }
123:
124:            /**
125:             * Get Collection of CommunityConfig objects for named entity.  Uses
126:             * standard Cougaar config finder to locate XML file.  If entityName is null
127:             * all communities are returned.
128:             * @param xmlFileName XML file containing community definitions
129:             * @param entityName Name of member
130:             * @return Collection of CommunityConfig objects
131:             */
132:            private static Collection getCommunityConfigsFromFile(
133:                    String xmlFileName, String entityName) {
134:                Collection allCommunities = getCommunityConfigsFromFile(xmlFileName);
135:                Collection communitiesWithEntity = new Vector();
136:                for (Iterator it = allCommunities.iterator(); it.hasNext();) {
137:                    CommunityConfig cc = (CommunityConfig) it.next();
138:                    if (entityName == null || cc.hasEntity(entityName)) {
139:                        communitiesWithEntity.add(cc);
140:                    }
141:                }
142:                return communitiesWithEntity;
143:            }
144:
145:            /*
146:             * For testing.  Loads CommunityConfigs from XML File or database
147:             * and prints to screen.
148:             * @param args
149:             */
150:            public static void main(String args[]) throws Exception {
151:                String entityName = "OSC";
152:                System.out.print("<!-- load entity=\"" + entityName + " -->");
153:                //
154:                FileCommunityInitializerServiceProvider me = new FileCommunityInitializerServiceProvider();
155:                CommunityInitializerService cis = (CommunityInitializerService) me
156:                        .getService(null, null,
157:                                CommunityInitializerService.class);
158:                Collection configs = cis.getCommunityDescriptions(entityName);
159:                //
160:                System.out.println("<Communities>");
161:                for (Iterator it = configs.iterator(); it.hasNext();) {
162:                    System.out
163:                            .println(((CommunityConfig) it.next()).toString());
164:                }
165:                System.out.println("</Communities>");
166:            }
167:
168:            /**
169:             * SAX Handler for parsing Community XML files
170:             */
171:            private static class SaxHandler extends DefaultHandler {
172:
173:                public SaxHandler() {
174:                }
175:
176:                private Map communityMap = null;
177:
178:                private CommunityConfig community = null;
179:                private EntityConfig entity = null;
180:
181:                public void startDocument() {
182:                    communityMap = new HashMap();
183:                }
184:
185:                public Collection getCommunityConfigs() {
186:                    return communityMap.values();
187:                }
188:
189:                public void startElement(String uri, String localname,
190:                        String rawname, Attributes p3) {
191:                    try {
192:                        if (localname.equals("Community")) {
193:                            String name = null;
194:                            javax.naming.directory.Attributes attrs = new BasicAttributes();
195:                            for (int i = 0; i < p3.getLength(); i++) {
196:                                if (p3.getLocalName(i).equals("Name")) {
197:                                    name = p3.getValue(i).trim();
198:                                } else {
199:                                    attrs.put(Strings
200:                                            .intern(p3.getLocalName(i)),
201:                                            Strings.intern(p3.getValue(i)
202:                                                    .trim()));
203:                                }
204:                            }
205:                            community = new CommunityConfig(Strings
206:                                    .intern(name));
207:                            community.setAttributes(attrs);
208:                        } else if (localname.equals("Entity")) {
209:                            String name = null;
210:                            javax.naming.directory.Attributes attrs = new BasicAttributes();
211:                            for (int i = 0; i < p3.getLength(); i++) {
212:                                if (p3.getLocalName(i).equals("Name")) {
213:                                    name = p3.getValue(i).trim();
214:                                } else {
215:                                    attrs.put(Strings
216:                                            .intern(p3.getLocalName(i)),
217:                                            Strings.intern(p3.getValue(i)
218:                                                    .trim()));
219:                                }
220:                            }
221:                            entity = new EntityConfig(Strings.intern(name));
222:                            entity.setAttributes(attrs);
223:                        } else if (localname.equals("Attribute")) {
224:                            String id = null;
225:                            String value = null;
226:                            for (int i = 0; i < p3.getLength(); i++) {
227:                                if (p3.getLocalName(i).equals("ID")) {
228:                                    id = p3.getValue(i).trim();
229:                                } else if (p3.getLocalName(i).equals("Value")) {
230:                                    value = p3.getValue(i).trim();
231:                                }
232:                            }
233:                            if (id != null && value != null) {
234:                                id = Strings.intern(id);
235:                                value = Strings.intern(value);
236:                                if (entity != null)
237:                                    entity.addAttribute(id, value);
238:                                else
239:                                    community.addAttribute(id, value);
240:                            }
241:                        }
242:                    } catch (Exception ex) {
243:                        ex.printStackTrace();
244:                    }
245:                }
246:
247:                public void endElement(String uri, String localname,
248:                        String qname) {
249:                    try {
250:                        if (localname.equals("Community")) {
251:                            communityMap.put(community.getName(), community);
252:                            community = null;
253:                        } else if (localname.equals("Entity")) {
254:                            // Ensure entity has essential attributes defined, use defaults if absent
255:                            CommunityUtils.setAttribute(entity.getAttributes(),
256:                                    "Role", "Member");
257:                            if (!CommunityUtils.hasAttribute(entity
258:                                    .getAttributes(), "EntityType", "Agent")
259:                                    && !CommunityUtils.hasAttribute(entity
260:                                            .getAttributes(), "EntityType",
261:                                            "Community")) {
262:                                CommunityUtils
263:                                        .setAttribute(entity.getAttributes(),
264:                                                "EntityType", "Agent");
265:                            }
266:                            community.addEntity(entity);
267:                            entity = null;
268:                        }
269:                    } catch (Exception ex) {
270:                        ex.printStackTrace();
271:                    }
272:                }
273:
274:            }
275:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.