KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > RegistryConnector


1 /**
2  * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Component of: Red Hat Application Server
18  *
19  * Initial Developers: Gregory Lapouchnian
20  * Patrick Smith
21  * --------------------------------------------------------------------------
22  * $Id: RegistryConnector.java,v 1.2 2005/07/08 14:00:45 glapouch Exp $
23  * --------------------------------------------------------------------------
24  */

25 package olstore.client;
26
27 import java.net.PasswordAuthentication;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.Set;
35
36 import javax.xml.registry.BulkResponse;
37 import javax.xml.registry.BusinessQueryManager;
38 import javax.xml.registry.Connection;
39 import javax.xml.registry.ConnectionFactory;
40 import javax.xml.registry.JAXRException;
41 import javax.xml.registry.RegistryService;
42 import javax.xml.registry.infomodel.ExternalLink;
43 import javax.xml.registry.infomodel.Organization;
44 import javax.xml.registry.infomodel.Service;
45 import javax.xml.registry.infomodel.ServiceBinding;
46 import javax.xml.registry.infomodel.SpecificationLink;
47
48 /**
49  * Connect to the UDDI registry and retrieve WSDL URLs for the OLstore client.
50  */

51 public class RegistryConnector {
52
53     /** A connection to the UDDI registry. */
54     private Connection connection;
55
56     /** Used to send queries to the registry. */
57     private BusinessQueryManager bqm;
58
59     /** The list of URLs found for the OLStore specials endpoint. */
60     private List specialsWSDLLocations;
61
62     /** The list of URLs found for the OLStore shopping cart endpoint. */
63     private List cartWSDLLocations;
64
65     /**
66      * Prepare a registry connector to query the UDDI registry for WSDL
67      * information.
68      */

69     public RegistryConnector() {
70         specialsWSDLLocations = new ArrayList();
71         cartWSDLLocations = new ArrayList();
72     }
73
74     /**
75      * Create a connection and retrieve the endpoint locations.
76      * @throws Exception if an error occurs while connecting and querying the
77      * registry
78      */

79     public void retrieveWSDLLocations() throws Exception {
80         // only connect to the registry if the WSDL location lists are empty
81
if (cartWSDLLocations.isEmpty() || specialsWSDLLocations.isEmpty()) {
82             try {
83                 makeConnection();
84                 findWSDLLocations();
85                 closeConnection();
86             } catch (JAXRException e) {
87                 throw new Exception(
88                         "Could not retrieve WSDL locations from the UDDI registry.\n"
89                                 + e.getMessage());
90             }
91         }
92     }
93
94     /**
95      * Close the connection to the UDDI registry.
96      * @throws JAXRException if the connection could not be closed
97      */

98     public void closeConnection() throws JAXRException {
99         connection.close();
100     }
101
102     /**
103      * Create a connection to the UDDI registry.
104      * @throws Exception if there is a problem creating a connection.
105      */

106     public void makeConnection() throws Exception {
107
108         Configure config = new Configure();
109
110         Properties props = new Properties();
111         props.setProperty("javax.xml.registry.queryManagerURL", config.getProperty("queryURL"));
112         props.setProperty("javax.xml.registry.lifeCycleManagerURL", config.getProperty("publishURL"));
113
114         try {
115             // Create the connection, passing it the configuration properties
116
ConnectionFactory factory = ConnectionFactory.newInstance();
117             factory.setProperties(props);
118             connection = factory.createConnection();
119
120             // Get registry service and managers
121
RegistryService rs = connection.getRegistryService();
122
123             // Get manager capabilities from registry service
124
bqm = rs.getBusinessQueryManager();
125
126             // Set client authorization information for privileged registry
127
// operations
128
PasswordAuthentication passwdAuth = new PasswordAuthentication(
129                     config.getProperty("user"), config.getProperty("pass")
130                             .toCharArray());
131             Set creds = new HashSet();
132             creds.add(passwdAuth);
133
134             // Set credentials on the JAXR provider
135
connection.setCredentials(creds);
136
137             // Set communication preference
138
connection.setSynchronous(true);
139
140         } catch (Exception e) {
141             // try to close the connection if it was created
142
if (connection != null) {
143                 try {
144                     connection.close();
145                 } catch (JAXRException je) {
146                     throw je;
147                 }
148             }
149             throw e;
150         }
151     }
152
153     /**
154      * Find all the endpoint URLs in the registry for OLStore.
155      * @throws JAXRException if there is a problem with querying the registry
156      */

157     public void findWSDLLocations() throws JAXRException {
158
159         Collection names = new ArrayList();
160         String orgName = "Red Hat Olstore";
161         names.add(orgName);
162
163         BulkResponse br = bqm.findOrganizations(null, names, null, null, null,
164                 null);
165         Iterator iter = br.getCollection().iterator();
166
167         while (iter.hasNext()) {
168             Organization org = (Organization) iter.next();
169             Iterator serviceIter = org.getServices().iterator();
170
171             while (serviceIter.hasNext()) {
172                 Service service = (Service) serviceIter.next();
173
174                 // get the service bindings
175
Collection serviceBindings = service.getServiceBindings();
176
177                 Iterator sbIter = serviceBindings.iterator();
178                 while (sbIter.hasNext()) {
179                     ServiceBinding serviceBinding = (ServiceBinding) sbIter
180                             .next();
181
182                     // Get a collection of SpecificationLinks
183
Collection specificationLinks = serviceBinding
184                             .getSpecificationLinks();
185
186                     Iterator linkIter = specificationLinks.iterator();
187                     while (linkIter.hasNext()) {
188                         SpecificationLink specificationLink = (SpecificationLink) linkIter
189                                 .next();
190
191                         // Get a collection of ExternalLinks
192
Collection externalLinks = specificationLink
193                                 .getExternalLinks();
194
195                         Iterator elinkIter = externalLinks.iterator();
196                         while (elinkIter.hasNext()) {
197                             ExternalLink externalLink = (ExternalLink) elinkIter.next();
198                             String externalURI = externalLink.getExternalURI();
199
200                             // put this WSDL URL into the appropriate list
201
if (service.getName().getValue().equals("Red Hat Olstore - Shopping cart")) {
202                                 cartWSDLLocations.add(externalURI);
203                             } else {
204                                 specialsWSDLLocations.add(externalURI);
205                             }
206
207                         }
208                     }
209                 }
210             }
211         }
212
213     }
214
215     /**
216      * Get the list of shopping cart endpoint locations.
217      *
218      * @return Returns the cartWSDLLocations.
219      */

220     public List getCartWSDLLocations() {
221         return cartWSDLLocations;
222     }
223
224     /**
225      * Get the list of OLStore endpoint locations.
226      *
227      * @return Returns the specialsWSDLLocations.
228      */

229     public List getSpecialsWSDLLocations() {
230         return specialsWSDLLocations;
231     }
232 }
233
Popular Tags