Source Code Cross Referenced for DBCommunityInitializerServiceProvider.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.sql.Connection;
030:        import java.sql.ResultSet;
031:        import java.sql.SQLException;
032:        import java.sql.Statement;
033:        import java.util.Collection;
034:        import java.util.HashMap;
035:        import java.util.Iterator;
036:        import java.util.Map;
037:        import java.util.Vector;
038:
039:        import javax.naming.directory.Attribute;
040:        import javax.naming.directory.Attributes;
041:        import javax.naming.directory.BasicAttribute;
042:        import javax.naming.directory.BasicAttributes;
043:
044:        import org.cougaar.core.component.BindingSite;
045:        import org.cougaar.core.component.ServiceBroker;
046:        import org.cougaar.core.component.ServiceBrokerSupport;
047:        import org.cougaar.core.component.ServiceProvider;
048:        import org.cougaar.core.mts.MessageAddress;
049:        import org.cougaar.core.node.DBInitializerService;
050:        import org.cougaar.core.node.DBInitializerServiceImpl;
051:        import org.cougaar.core.node.NodeIdentificationService;
052:        import org.cougaar.core.node.NodeIdentificationServiceProvider;
053:        import org.cougaar.util.log.Logger;
054:        import org.cougaar.util.log.Logging;
055:
056:        /**
057:         * Generates a community config from database.
058:         **/
059:        class DBCommunityInitializerServiceProvider implements  ServiceProvider {
060:
061:            private final DBInitializerService dbInit;
062:            private final Logger logger;
063:
064:            public DBCommunityInitializerServiceProvider(
065:                    DBInitializerService dbInit) {
066:                this .dbInit = dbInit;
067:                this .logger = Logging.getLogger(getClass());
068:            }
069:
070:            public Object getService(ServiceBroker sb, Object requestor,
071:                    Class serviceClass) {
072:                if (serviceClass != CommunityInitializerService.class) {
073:                    throw new IllegalArgumentException(getClass()
074:                            + " does not furnish " + serviceClass);
075:                }
076:                return new CommunityInitializerServiceImpl();
077:            }
078:
079:            public void releaseService(ServiceBroker sb, Object requestor,
080:                    Class serviceClass, Object service) {
081:            }
082:
083:            private class CommunityInitializerServiceImpl implements 
084:                    CommunityInitializerService {
085:
086:                public Collection getCommunityDescriptions(String entityName) {
087:                    // param is xml file name for File Initializer. Unused here.
088:                    Collection ret = new Vector();
089:                    Map substitutions = null;
090:                    String query1 = null;
091:                    String query2 = null;
092:                    try {
093:                        substitutions = dbInit.createSubstitutions();
094:                        Connection conn = null;
095:                        try {
096:                            query1 = dbInit.getQuery(
097:                                    "queryCommunityEntityAttributes",
098:                                    substitutions);
099:                            query2 = dbInit.getQuery(
100:                                    "queryCommunityAttributes", substitutions);
101:                            conn = dbInit.getConnection();
102:                            ret = getParentCommunities(conn, entityName,
103:                                    query1, query2);
104:                        } finally {
105:                            // Must close the connection when done
106:                            if (conn != null) {
107:                                conn.close();
108:                            }
109:                        }
110:                    } catch (Exception ex) {
111:                        if (logger.isErrorEnabled()) {
112:                            logger.error(
113:                                    "Exception in getCommunityDescriptions from DB ("
114:                                            + "entityName=\"" + entityName
115:                                            + "\", subs=\"" + substitutions
116:                                            + "\", query1=\"" + query1
117:                                            + "\", query2=\"" + query2 + "\")",
118:                                    ex);
119:                        }
120:                    }
121:                    return ret;
122:                }
123:            }
124:
125:            //
126:            // Community configuration utilities:
127:            //
128:            // These are static for now, but could be promoted to non-static
129:            // methods...
130:            //
131:
132:            private static Attributes getCommunityAttributes(Connection conn,
133:                    String communityName, String query2) throws SQLException {
134:                Statement s = conn.createStatement();
135:                ResultSet rs = s.executeQuery(query2);
136:                //ResultSet rs = s.executeQuery("select * from community_attribute");
137:                javax.naming.directory.Attributes attrs = new BasicAttributes();
138:                while (rs.next()) {
139:                    if (rs.getString(1).equals(communityName)) {
140:                        String attrId = rs.getString(2);
141:                        String attrValue = rs.getString(3);
142:                        Attribute attr = attrs.get(attrId);
143:                        if (attr == null) {
144:                            attr = new BasicAttribute(attrId);
145:                            attrs.put(attr);
146:                        }
147:                        if (!attr.contains(attrValue))
148:                            attr.add(attrValue);
149:                    }
150:                }
151:
152:                // Close the result set and the statement
153:                try {
154:                    rs.close();
155:                } catch (SQLException e) {
156:                }
157:                try {
158:                    s.close();
159:                } catch (SQLException e) {
160:                }
161:
162:                return attrs;
163:            }
164:
165:            private static void addEntityAttribute(Map configMap,
166:                    String communityName, String entityName, String attrId,
167:                    String attrValue) {
168:                CommunityConfig cc = (CommunityConfig) configMap
169:                        .get(communityName);
170:                EntityConfig entity = cc.getEntity(entityName);
171:                if (entity == null) {
172:                    entity = new EntityConfig(entityName);
173:                    cc.addEntity(entity);
174:                }
175:                entity.addAttribute(attrId, attrValue);
176:            }
177:
178:            private static Collection getParentCommunities(Connection conn,
179:                    String entityName, String query1, String query2)
180:                    throws SQLException {
181:
182:                Statement s = conn.createStatement();
183:                ResultSet rs = s.executeQuery(query1);
184:                //ResultSet rs = s.executeQuery("select * from community_entity_attribute");
185:                Map configMap = new HashMap();
186:
187:                while (rs.next()) {
188:                    if (rs.getString(2).equals(entityName)) {
189:                        String communityName = rs.getString(1);
190:                        if (!configMap.containsKey(communityName)) {
191:                            CommunityConfig cc = new CommunityConfig(
192:                                    communityName);
193:                            cc.setAttributes(getCommunityAttributes(conn,
194:                                    communityName, query2));
195:                            configMap.put(communityName, cc);
196:                        }
197:                        addEntityAttribute(configMap, communityName,
198:                                entityName, rs.getString(3), rs.getString(4));
199:                    }
200:                }
201:
202:                // Close the result set and the statement
203:                try {
204:                    rs.close();
205:                } catch (SQLException e) {
206:                }
207:                try {
208:                    s.close();
209:                } catch (SQLException e) {
210:                }
211:
212:                return configMap.values();
213:            }
214:
215:            /*
216:             * For testing.
217:             */
218:            public static void main(String args[]) throws Exception {
219:                String trialId = System
220:                        .getProperty("org.cougaar.experiment.id");
221:                if (trialId == null) {
222:                    System.err
223:                            .println("Must specify a trial id (using \"-Dorg.cougaar.experiment.id\")");
224:                    return;
225:                }
226:                String nodeName = System.getProperty("org.cougaar.node.name");
227:                String entityName = (args.length < 1 ? "OSC" : args[0]);
228:                System.out.println("<!-- load trial=\"" + trialId
229:                        + "\" node=\"" + nodeName + "\" entity=\"" + entityName
230:                        + "\" -->");
231:
232:                // load the db-init-service
233:                Object requestor = new Object();
234:                final ServiceBroker sb = new ServiceBrokerSupport();
235:                if (nodeName != null) {
236:                    MessageAddress nodeId = MessageAddress
237:                            .getMessageAddress(nodeName);
238:                    NodeIdentificationServiceProvider nodeIdSP = new NodeIdentificationServiceProvider(
239:                            nodeId);
240:                    sb.addService(NodeIdentificationService.class, nodeIdSP);
241:                }
242:                BindingSite bs = new BindingSite() {
243:                    public ServiceBroker getServiceBroker() {
244:                        return sb;
245:                    }
246:
247:                    public void requestStop() {
248:                    }
249:                };
250:                DBInitializerService dbInit = new DBInitializerServiceImpl(
251:                        trialId);
252:
253:                // load my db community-init-service
254:                DBCommunityInitializerServiceProvider commInitSP = new DBCommunityInitializerServiceProvider(
255:                        dbInit);
256:                sb.addService(CommunityInitializerService.class, commInitSP);
257:
258:                // get the service
259:                CommunityInitializerService cis = (CommunityInitializerService) sb
260:                        .getService(requestor,
261:                                CommunityInitializerService.class, null);
262:
263:                // print the communities
264:                Collection configs = cis.getCommunityDescriptions(entityName);
265:                System.out.println("<Communities>");
266:                for (Iterator it = configs.iterator(); it.hasNext();) {
267:                    System.out
268:                            .println(((CommunityConfig) it.next()).toString());
269:                }
270:                System.out.println("</Communities>");
271:            }
272:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.