001: /**
002: * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2.1 of the License, or any later version.
007: *
008: * This library is distributed in the hope that it will be useful, but WITHOUT
009: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
011: * details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this library; if not, write to the Free Software Foundation, Inc.,
015: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: *
017: * Component of: Red Hat Application Server
018: *
019: * Initial Developers: Gregory Lapouchnian
020: * Patrick Smith
021: * --------------------------------------------------------------------------
022: * $Id: RegistryConnector.java 7027 2005-07-08 14:00:45Z glapouch $
023: * --------------------------------------------------------------------------
024: */package olstore.client;
025:
026: import java.net.PasswordAuthentication;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Properties;
033: import java.util.Set;
034:
035: import javax.xml.registry.BulkResponse;
036: import javax.xml.registry.BusinessQueryManager;
037: import javax.xml.registry.Connection;
038: import javax.xml.registry.ConnectionFactory;
039: import javax.xml.registry.JAXRException;
040: import javax.xml.registry.RegistryService;
041: import javax.xml.registry.infomodel.ExternalLink;
042: import javax.xml.registry.infomodel.Organization;
043: import javax.xml.registry.infomodel.Service;
044: import javax.xml.registry.infomodel.ServiceBinding;
045: import javax.xml.registry.infomodel.SpecificationLink;
046:
047: /**
048: * Connect to the UDDI registry and retrieve WSDL URLs for the OLstore client.
049: */
050: public class RegistryConnector {
051:
052: /** A connection to the UDDI registry. */
053: private Connection connection;
054:
055: /** Used to send queries to the registry. */
056: private BusinessQueryManager bqm;
057:
058: /** The list of URLs found for the OLStore specials endpoint. */
059: private List specialsWSDLLocations;
060:
061: /** The list of URLs found for the OLStore shopping cart endpoint. */
062: private List cartWSDLLocations;
063:
064: /**
065: * Prepare a registry connector to query the UDDI registry for WSDL
066: * information.
067: */
068: public RegistryConnector() {
069: specialsWSDLLocations = new ArrayList();
070: cartWSDLLocations = new ArrayList();
071: }
072:
073: /**
074: * Create a connection and retrieve the endpoint locations.
075: * @throws Exception if an error occurs while connecting and querying the
076: * registry
077: */
078: public void retrieveWSDLLocations() throws Exception {
079: // only connect to the registry if the WSDL location lists are empty
080: if (cartWSDLLocations.isEmpty()
081: || specialsWSDLLocations.isEmpty()) {
082: try {
083: makeConnection();
084: findWSDLLocations();
085: closeConnection();
086: } catch (JAXRException e) {
087: throw new Exception(
088: "Could not retrieve WSDL locations from the UDDI registry.\n"
089: + e.getMessage());
090: }
091: }
092: }
093:
094: /**
095: * Close the connection to the UDDI registry.
096: * @throws JAXRException if the connection could not be closed
097: */
098: public void closeConnection() throws JAXRException {
099: 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
112: .getProperty("queryURL"));
113: props.setProperty("javax.xml.registry.lifeCycleManagerURL",
114: config.getProperty("publishURL"));
115:
116: try {
117: // Create the connection, passing it the configuration properties
118: ConnectionFactory factory = ConnectionFactory.newInstance();
119: factory.setProperties(props);
120: connection = factory.createConnection();
121:
122: // Get registry service and managers
123: RegistryService rs = connection.getRegistryService();
124:
125: // Get manager capabilities from registry service
126: bqm = rs.getBusinessQueryManager();
127:
128: // Set client authorization information for privileged registry
129: // operations
130: PasswordAuthentication passwdAuth = new PasswordAuthentication(
131: config.getProperty("user"), config.getProperty(
132: "pass").toCharArray());
133: Set creds = new HashSet();
134: creds.add(passwdAuth);
135:
136: // Set credentials on the JAXR provider
137: connection.setCredentials(creds);
138:
139: // Set communication preference
140: connection.setSynchronous(true);
141:
142: } catch (Exception e) {
143: // try to close the connection if it was created
144: if (connection != null) {
145: try {
146: connection.close();
147: } catch (JAXRException je) {
148: throw je;
149: }
150: }
151: throw e;
152: }
153: }
154:
155: /**
156: * Find all the endpoint URLs in the registry for OLStore.
157: * @throws JAXRException if there is a problem with querying the registry
158: */
159: public void findWSDLLocations() throws JAXRException {
160:
161: Collection names = new ArrayList();
162: String orgName = "Red Hat Olstore";
163: names.add(orgName);
164:
165: BulkResponse br = bqm.findOrganizations(null, names, null,
166: null, null, null);
167: Iterator iter = br.getCollection().iterator();
168:
169: while (iter.hasNext()) {
170: Organization org = (Organization) iter.next();
171: Iterator serviceIter = org.getServices().iterator();
172:
173: while (serviceIter.hasNext()) {
174: Service service = (Service) serviceIter.next();
175:
176: // get the service bindings
177: Collection serviceBindings = service
178: .getServiceBindings();
179:
180: Iterator sbIter = serviceBindings.iterator();
181: while (sbIter.hasNext()) {
182: ServiceBinding serviceBinding = (ServiceBinding) sbIter
183: .next();
184:
185: // Get a collection of SpecificationLinks
186: Collection specificationLinks = serviceBinding
187: .getSpecificationLinks();
188:
189: Iterator linkIter = specificationLinks.iterator();
190: while (linkIter.hasNext()) {
191: SpecificationLink specificationLink = (SpecificationLink) linkIter
192: .next();
193:
194: // Get a collection of ExternalLinks
195: Collection externalLinks = specificationLink
196: .getExternalLinks();
197:
198: Iterator elinkIter = externalLinks.iterator();
199: while (elinkIter.hasNext()) {
200: ExternalLink externalLink = (ExternalLink) elinkIter
201: .next();
202: String externalURI = externalLink
203: .getExternalURI();
204:
205: // put this WSDL URL into the appropriate list
206: if (service.getName().getValue().equals(
207: "Red Hat Olstore - Shopping cart")) {
208: cartWSDLLocations.add(externalURI);
209: } else {
210: specialsWSDLLocations.add(externalURI);
211: }
212:
213: }
214: }
215: }
216: }
217: }
218:
219: }
220:
221: /**
222: * Get the list of shopping cart endpoint locations.
223: *
224: * @return Returns the cartWSDLLocations.
225: */
226: public List getCartWSDLLocations() {
227: return cartWSDLLocations;
228: }
229:
230: /**
231: * Get the list of OLStore endpoint locations.
232: *
233: * @return Returns the specialsWSDLLocations.
234: */
235: public List getSpecialsWSDLLocations() {
236: return specialsWSDLLocations;
237: }
238: }
|