001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.wsrp.common.registry.ebxml;
015:
016: import com.sun.portal.wsrp.common.registry.RegistryQueryManager;
017: import com.sun.portal.wsrp.common.registry.WSRPProducer;
018: import com.sun.portal.wsrp.common.registry.WSRPPortlet;
019: import com.sun.portal.wsrp.common.registry.WSRPOrganization;
020: import com.sun.portal.wsrp.WSRPException;
021:
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import javax.xml.registry.BusinessQueryManager;
028: import javax.xml.registry.BusinessLifeCycleManager;
029: import javax.xml.registry.JAXRException;
030: import javax.xml.registry.infomodel.Concept;
031: import javax.xml.registry.infomodel.Classification;
032: import javax.xml.registry.infomodel.Service;
033: import javax.xml.registry.infomodel.Association;
034: import javax.xml.registry.infomodel.Organization;
035: import javax.xml.registry.BulkResponse;
036: import javax.xml.registry.FindQualifier;
037: import javax.xml.registry.JAXRResponse;
038: import javax.xml.registry.infomodel.ServiceBinding;
039:
040: public class RegistryQueryManagerImpl implements RegistryQueryManager,
041: JESRegistryConstants {
042:
043: private BusinessQueryManager bqm = null;
044: private BusinessLifeCycleManager blcm = null;
045:
046: public RegistryQueryManagerImpl(BusinessQueryManager bqm,
047: BusinessLifeCycleManager blcm) {
048: this .bqm = bqm;
049: this .blcm = blcm;
050: }
051:
052: public List searchProducer(String name, String description)
053: throws WSRPException {
054: try {
055: Collection classifications = getProducerClassifications();
056: Collection findQualifiers = getFindQualifiers(name,
057: description);
058: Collection namePatterns = getNamePatterns(name, description);
059: BulkResponse br = bqm.findServices(null, findQualifiers,
060: namePatterns, classifications, null);
061: return processSearchProducerResults(br);
062:
063: } catch (JAXRException je) {
064: throw new WSRPException(je);
065: }
066: }
067:
068: private List processSearchProducerResults(BulkResponse br)
069: throws JAXRException, WSRPException {
070: if (br.getStatus() != JAXRResponse.STATUS_SUCCESS) {
071: processException(br);
072: return new ArrayList();
073: }
074:
075: Collection services = br.getCollection();
076: Iterator iter = services.iterator();
077: ArrayList results = new ArrayList();
078:
079: if (!iter.hasNext()) {
080: return results;
081: } else {
082: WSRPProducer producer = null;
083: while (iter.hasNext()) {
084: Service service = (Service) iter.next();
085: String sname = RegistryUtil.getName(service);
086: Collection links = service.getServiceBindings();
087: producer = new WSRPProducerImpl();
088: producer.setName(sname);
089: producer.setDescription(RegistryUtil
090: .getDescription(service));
091: producer.setProducerURL(getWSDLURLs(links));
092: producer.setKey(RegistryUtil.getKey(service));
093: results.add(producer);
094: }
095: }
096: return results;
097: }
098:
099: private void processException(BulkResponse br)
100: throws JAXRException, WSRPException {
101: //TODO : Log these messages as severe
102: System.err.println("ERROR: One or more JAXRExceptions "
103: + "occurred during the query operation:");
104: Collection exceptions = br.getExceptions();
105: Iterator iter = exceptions.iterator();
106: StringBuffer reason = new StringBuffer();
107: while (iter.hasNext()) {
108: Exception e = (Exception) iter.next();
109: reason.append(e.toString());
110: reason.append("\n");
111: }
112: throw new WSRPException("Search failed at registry : "
113: + reason.toString());
114: }
115:
116: public List searchPortlet(String name, String description)
117: throws WSRPException {
118: try {
119: Collection classifications = getPortletClassifications();
120: Collection findQualifiers = getFindQualifiers(name,
121: description);
122: Collection namePatterns = getNamePatterns(name, description);
123: BulkResponse br = bqm.findServices(null, findQualifiers,
124: namePatterns, classifications, null);
125:
126: return processSearchPortletResults(br);
127:
128: } catch (JAXRException je) {
129: throw new WSRPException(je);
130: }
131: }
132:
133: private List processSearchPortletResults(BulkResponse br)
134: throws JAXRException, WSRPException {
135: if (br.getStatus() != JAXRResponse.STATUS_SUCCESS) {
136: processException(br);
137: return new ArrayList();
138: }
139: Collection services = br.getCollection();
140: Iterator iter = services.iterator();
141: ArrayList results = new ArrayList();
142: String key = null;
143:
144: if (!iter.hasNext()) {
145: return results;
146: } else {
147: WSRPPortlet portlet = null;
148: while (iter.hasNext()) {
149: Service service = (Service) iter.next();
150: String sname = RegistryUtil.getName(service);
151: portlet = new WSRPPortletImpl();
152: portlet.setName(sname);
153: portlet.setDescription(RegistryUtil
154: .getDescription(service));
155: portlet.setKey(RegistryUtil.getKey(service));
156: key = RegistryUtil.getKey(service);
157: Collection links = getAssociatedProducerURL(key);
158: portlet.setProducerURL(links);
159: results.add(portlet);
160: }
161:
162: }
163: return results;
164: }
165:
166: private Collection getAssociatedProducerURL(String portletServiceKey)
167: throws JAXRException, WSRPException {
168: Collection associationTypes = new ArrayList();
169: associationTypes.add(getHasPortletAssociation());
170: BulkResponse br = bqm.findAssociations(null, null,
171: portletServiceKey, associationTypes);
172: return processSearchAssociationResults(br);
173: }
174:
175: private Concept getHasPortletAssociation() throws JAXRException {
176: Concept associationConcept = (Concept) bqm
177: .getRegistryObject(JESRegistryConstants.EBXML_WSRP_HAS_PORTLET_ASSOCIATION);
178: return associationConcept;
179: }
180:
181: private Collection processSearchAssociationResults(BulkResponse br)
182: throws JAXRException, WSRPException {
183: if (br.getStatus() != JAXRResponse.STATUS_SUCCESS) {
184: processException(br);
185: return new ArrayList();
186: }
187: Collection associations = br.getCollection();
188: Iterator iter = associations.iterator();
189: Collection results = new ArrayList();
190:
191: if (!iter.hasNext()) {
192: return results;
193: } else {
194: while (iter.hasNext()) {
195: Association hasPortlet = (Association) iter.next();
196: Service producer = (Service) hasPortlet
197: .getSourceObject();
198: Collection links = producer.getServiceBindings();
199: return getWSDLURLs(links);
200: }
201: }
202: return results;
203: }
204:
205: public List searchOrganization(String name, String description)
206: throws WSRPException {
207: try {
208: Collection findQualifiers = getFindQualifiers(name,
209: description);
210: Collection namePatterns = getNamePatterns(name, description);
211: //TODO : Check to see if we can get a SpecLink of WSRP types
212: // and use it for querying Organization. . This should be faster query than
213: // looking up orgs ans then finding services of WSRP types
214: BulkResponse br = bqm.findOrganizations(findQualifiers,
215: namePatterns, null, null, null, null);
216: return processSearchOrganizationResults(br);
217:
218: } catch (JAXRException je) {
219: throw new WSRPException(je);
220: }
221: }
222:
223: private List processSearchOrganizationResults(BulkResponse br)
224: throws JAXRException, WSRPException {
225: if (br.getStatus() != JAXRResponse.STATUS_SUCCESS) {
226: processException(br);
227: return new ArrayList();
228: }
229: Collection orgs = br.getCollection();
230: Iterator iter = orgs.iterator();
231: ArrayList results = new ArrayList();
232:
233: if (!iter.hasNext()) {
234: return results;
235: } else {
236: WSRPOrganization wsrpOrg = null;
237: //TODO : Find a way to identify portlet and producer services
238: Organization org = null;
239:
240: while (iter.hasNext()) {
241: org = (Organization) iter.next();
242: wsrpOrg = new WSRPOrganizationImpl();
243: wsrpOrg.setName(RegistryUtil.getName(org));
244: wsrpOrg
245: .setDescription(RegistryUtil
246: .getDescription(org));
247: wsrpOrg.setKey(RegistryUtil.getKey(org));
248:
249: Collection services1 = org.getServices();
250: Iterator siter = services1.iterator();
251: while (siter.hasNext()) {
252: Service service = (Service) siter.next();
253: processService(service, wsrpOrg);
254: }
255: results.add(wsrpOrg);
256: }
257: }
258: return results;
259: }
260:
261: private void processService(Service service, WSRPOrganization org)
262: throws JAXRException {
263: Collection classifications = service.getClassifications();
264: if (isWSRPPortletService(classifications)) {
265: WSRPPortlet portletService = new WSRPPortletImpl();
266: portletService.setKey(RegistryUtil.getKey(service));
267: portletService.setName(RegistryUtil.getName(service));
268: portletService.setDescription(RegistryUtil
269: .getDescription(service));
270: org.addPortlet(portletService);
271:
272: } else if (isWSRPProducerService(classifications)) {
273: WSRPProducer producerService = new WSRPProducerImpl();
274: producerService.setKey(RegistryUtil.getKey(service));
275: producerService.setName(RegistryUtil.getName(service));
276: producerService.setDescription(RegistryUtil
277: .getDescription(service));
278: org.addProducer(producerService);
279: }
280: }
281:
282: private boolean isWSRPPortletService(Collection classifications)
283: throws JAXRException {
284: Iterator it = classifications.iterator();
285: Classification portletClassification = null;
286: while (it.hasNext()) {
287: portletClassification = (Classification) it.next();
288: String value = portletClassification.getConcept().getKey()
289: .getId();
290: if (value == EBXML_WSRP_PORTLET_CONCEPT_STR) {
291: return true;
292: }
293: }
294: return false;
295: }
296:
297: private boolean isWSRPProducerService(Collection classifications)
298: throws JAXRException {
299: Iterator it = classifications.iterator();
300: Classification producerClassification = null;
301: while (it.hasNext()) {
302: producerClassification = (Classification) it.next();
303: String value = producerClassification.getConcept().getKey()
304: .getId();
305: if (value == EBXML_WSRP_PRODUCER_CONCEPT_STR) {
306: return true;
307: }
308: }
309: return false;
310: }
311:
312: private Collection getPortletClassifications() throws JAXRException {
313: Collection classifications = getWSRPClassification();
314: Concept portletConcept = (Concept) bqm
315: .getRegistryObject(EBXML_WSRP_PORTLET_CONCEPT_STR);
316: Classification portletClassification = blcm
317: .createClassification(portletConcept);
318: classifications.add(portletClassification);
319: return classifications;
320: }
321:
322: private Collection getProducerClassifications()
323: throws JAXRException {
324: Collection classifications = getWSRPClassification();
325: Concept producerConcept = (Concept) bqm
326: .getRegistryObject(EBXML_WSRP_PRODUCER_CONCEPT_STR);
327: Classification producerClassification = blcm
328: .createClassification(producerConcept);
329: classifications.add(producerClassification);
330: return classifications;
331: }
332:
333: private Collection getWSRPClassification() throws JAXRException {
334: Concept wsrpv1Concept = (Concept) bqm
335: .getRegistryObject(EBXML_WSRP_V1_CONCEPT_STR);
336: Classification wsrpv1Classification = blcm
337: .createClassification(wsrpv1Concept);
338: Collection classifications = new ArrayList();
339: classifications.add(wsrpv1Classification);
340: return classifications;
341: }
342:
343: private Collection getNamePatterns(String name, String description) {
344: if (name == null && description == null) {
345: return null;
346: }
347: Collection namePatterns = new ArrayList();
348: if (name != null) {
349: namePatterns.add(name);
350: }
351: if (description != null) {
352: namePatterns.add(description);
353: }
354: return namePatterns;
355: }
356:
357: private Collection getFindQualifiers(String name, String description) {
358: if (name == null && description == null) {
359: return null;
360: }
361:
362: Collection findQualifiers = new ArrayList();
363: if (name != null) {
364: findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
365: }
366:
367: if (description != null) {
368: findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
369: }
370: return findQualifiers;
371: }
372:
373: private Collection getWSDLURLs(Collection bindings)
374: throws JAXRException {
375: Iterator it = bindings.iterator();
376: Collection urls = new ArrayList();
377: ServiceBinding sb = null;
378: String url = null;
379: while (it.hasNext()) {
380: sb = (ServiceBinding) it.next();
381: url = sb.getAccessURI();
382: urls.add(url);
383: }
384: return urls;
385: }
386: }
|