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: /**
017: *
018: * TODO : Check if Classifications are being saved
019: * Check wheather need explicit association for
020: * : Primary Contact to Organization
021: * : Org offers Producer and Portlets Services
022: * : Associate Producer and Portlets as WSRPV1 types
023: */
024:
025: import com.sun.portal.log.common.PortalLogger;
026: import java.util.List;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030:
031: import com.sun.portal.wsrp.common.registry.RegistryLifeCycleManager;
032: import com.sun.portal.wsrp.common.registry.RegistryConfiguration;
033: import com.sun.portal.wsrp.common.registry.WSRPOrganization;
034: import com.sun.portal.wsrp.common.registry.WSRPProducer;
035: import com.sun.portal.wsrp.common.registry.WSRPPortlet;
036: import com.sun.portal.wsrp.WSRPException;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: import javax.xml.registry.infomodel.Classification;
041: import javax.xml.registry.infomodel.Concept;
042: import javax.xml.registry.infomodel.EmailAddress;
043: import javax.xml.registry.infomodel.ExternalLink;
044: import javax.xml.registry.infomodel.InternationalString;
045: import javax.xml.registry.infomodel.Organization;
046: import javax.xml.registry.infomodel.PersonName;
047: import javax.xml.registry.infomodel.Service;
048: import javax.xml.registry.infomodel.ServiceBinding;
049: import javax.xml.registry.infomodel.Slot;
050: import javax.xml.registry.infomodel.SpecificationLink;
051: import javax.xml.registry.infomodel.TelephoneNumber;
052: import javax.xml.registry.infomodel.User;
053: import javax.xml.registry.infomodel.Key;
054: import javax.xml.registry.infomodel.Association;
055: import javax.xml.registry.BusinessQueryManager;
056: import javax.xml.registry.BusinessLifeCycleManager;
057: import javax.xml.registry.JAXRException;
058: import javax.xml.registry.BulkResponse;
059:
060: //TODO: Remove this, @ see bug :6334671
061: import org.freebxml.omar.client.xml.registry.util.ProviderProperties;
062:
063: public class RegistryLifeCycleManagerImpl implements
064: RegistryLifeCycleManager, JESRegistryConstants {
065:
066: private BusinessQueryManager bqm = null;
067: private BusinessLifeCycleManager blcm = null;
068: private RegistryConfiguration rc = null;
069: private static final String DEFAULT_WSRP_PRODUCER_URL_DESC = "WSRP Producer URL";
070: private static final String DEFAULT_SPEC_DESC = "Link to WSRP V1 Types OASIS definition";
071: private static final String DEFAULT_SPEC_NAME = "WSRP V1 Types";
072: private static final String DEFAULT_WSRP_V1_DESC = "WSRP Types URL";
073: private static Logger logger = PortalLogger
074: .getLogger(RegistryLifeCycleManagerImpl.class);
075:
076: public RegistryLifeCycleManagerImpl(BusinessQueryManager bqm,
077: BusinessLifeCycleManager blcm, RegistryConfiguration rc) {
078: this .bqm = bqm;
079: this .blcm = blcm;
080: this .rc = rc;
081: }
082:
083: public void publishOrganization(WSRPOrganization wsrporg)
084: throws WSRPException {
085: try {
086: Organization org = getOrganization(wsrporg);
087:
088: List producers = getWSRPProducerServices(wsrporg
089: .getProducers());
090: List portlets = getWSRPPortletService(wsrporg.getPortlets());
091: org.addServices(producers);
092: org.addServices(portlets);
093: // Add organization and submit to registry
094: // Retrieve key if successful
095: List orgs = new ArrayList();
096: orgs.add(org);
097:
098: BulkResponse response = null;
099: String toXML = org.toXML();
100: response = blcm.saveOrganizations(orgs);
101: processResponse(response, true);
102: saveServices(producers);
103: saveServices(portlets);
104: createHasPortletAssociations(((Service) producers.get(0)),
105: portlets);
106: } catch (JAXRException je) {
107: logger.log(Level.SEVERE, "PSWS_CSPWPAB0027", je);
108: throw new WSRPException("Publish failed : "
109: + je.getMessage());
110: } catch (Throwable t) {
111: logger.log(Level.SEVERE, "PSWS_CSPWPAB0027", t);
112: throw new WSRPException("Publish failed : "
113: + t.getMessage());
114: }
115: }
116:
117: private Organization getOrganization(WSRPOrganization org)
118: throws JAXRException {
119: String key = org.getKey();
120: if (key != null && key.trim().length() > 0) {
121: return fetchOrganization(key.trim());
122: } else {
123: return createOrganization(org);
124: }
125: }
126:
127: private Organization fetchOrganization(String key)
128: throws JAXRException {
129: Organization org = (Organization) bqm.getRegistryObject(key);
130: return org;
131: }
132:
133: private Organization createOrganization(WSRPOrganization wsrporg)
134: throws JAXRException {
135:
136: Organization org = blcm.createOrganization(wsrporg.getName());
137: InternationalString s = blcm.createInternationalString(wsrporg
138: .getDescription());
139: org.setDescription(s);
140:
141: User primaryContact = getPrimaryContact(wsrporg);
142: // Set primary contact for organization
143: org.setPrimaryContact(primaryContact);
144: return org;
145:
146: }
147:
148: private User getPrimaryContact(WSRPOrganization wsrporg)
149: throws JAXRException {
150: // Create primary contact, set name
151: User primaryContact = blcm.createUser();
152: PersonName pName = blcm.createPersonName(wsrporg
153: .getPersonName());
154:
155: primaryContact.setPersonName(pName);
156:
157: // Set primary contact phone number
158: TelephoneNumber tNum = blcm.createTelephoneNumber();
159: tNum.setNumber(wsrporg.getPersonPhoneNo());
160:
161: Collection phoneNums = new ArrayList();
162: phoneNums.add(tNum);
163: primaryContact.setTelephoneNumbers(phoneNums);
164:
165: // Set primary contact email address
166: EmailAddress emailAddress = blcm.createEmailAddress(wsrporg
167: .getEmail());
168: Collection emailAddresses = new ArrayList();
169: emailAddresses.add(emailAddress);
170: primaryContact.setEmailAddresses(emailAddresses);
171: return primaryContact;
172: }
173:
174: public void publishProducer(WSRPProducer producer)
175: throws WSRPException {
176: try {
177: List producers = new ArrayList();
178: producers.add(producer);
179: List services = getWSRPProducerServices(producers);
180: saveServices(services);
181: } catch (JAXRException je) {
182: logger.log(Level.SEVERE, "PSWS_CSPWPAB0027", je);
183: throw new WSRPException("Publish failed : "
184: + je.getMessage());
185: }
186: }
187:
188: public void publishPortlets(WSRPProducer producer, List portlets)
189: throws WSRPException {
190: try {
191: List producers = new ArrayList();
192: producers.add(producer);
193:
194: //Save Producers
195: List producerServices = getWSRPProducerServices(producers);
196: saveServices(producerServices);
197:
198: //Save Portlets
199: List portletServices = getWSRPPortletService(portlets);
200: saveServices(portletServices);
201:
202: //Save Associations
203: createHasPortletAssociations(((Service) producerServices
204: .get(0)), portletServices);
205:
206: } catch (JAXRException je) {
207: logger.log(Level.SEVERE, "PSWS_CSPWPAB0027", je);
208: throw new WSRPException("Publish failed : "
209: + je.getMessage());
210: }
211: }
212:
213: public void publishProducer(WSRPOrganization org,
214: WSRPProducer producer) throws WSRPException {
215: publish(org, producer, null);
216: }
217:
218: public void publish(WSRPOrganization org, WSRPProducer producer,
219: List portlet) throws WSRPException {
220: if (org != null) {
221: publishOrganization(org);
222: return;
223: }
224:
225: if (portlet != null && producer != null) {
226: publishPortlets(producer, portlet);
227: return;
228: }
229:
230: if (producer != null) {
231: publishProducer(producer);
232: }
233:
234: }
235:
236: private void processResponse(BulkResponse response,
237: boolean failOnError) throws JAXRException, WSRPException {
238: Collection exceptions = response.getExceptions();
239:
240: if (exceptions == null) {
241:
242: Collection keys = response.getCollection();
243: Iterator keyIter = keys.iterator();
244:
245: if (keyIter.hasNext()) {
246: Key key = (Key) keyIter.next();
247: String id = key.getId();
248: }
249: } else {
250: Iterator excIter = exceptions.iterator();
251: Exception exception = null;
252: StringBuffer reason = new StringBuffer();
253: while (excIter.hasNext()) {
254: exception = (Exception) excIter.next();
255: reason.append(exception.toString());
256: reason.append("\n");
257: }
258: if (failOnError) {
259: throw new WSRPException("Publish failed at registry : "
260: + reason.toString());
261: }
262: }
263: }
264:
265: private void saveServices(List services) throws JAXRException,
266: WSRPException {
267: BulkResponse response = null;
268: response = blcm.saveServices(services);
269: processResponse(response, true);
270: }
271:
272: private List getWSRPProducerServices(List producers)
273: throws JAXRException, WSRPException {
274: Iterator it = producers.iterator();
275: // Create services and service
276: List services = new ArrayList();
277:
278: while (it.hasNext()) {
279: WSRPProducer producer = (WSRPProducer) it.next();
280: String serviceName = producer.getName();
281: String serviceDesc = producer.getDescription();
282:
283: Service service = blcm.createService(serviceName);
284: InternationalString is = blcm
285: .createInternationalString(serviceDesc);
286: service.setDescription(is);
287:
288: Collection serviceBindings = getProducerServiceBindings(producer);
289:
290: //TODO Check this value
291:
292: // Add service bindings to service
293: service.addServiceBindings(serviceBindings);
294:
295: //Create Classifications
296: Collection classifications = getProducerClassifications();
297: //Add classification to services
298: service.addClassifications(classifications);
299:
300: Collection slots = getProducerSlots(producer);
301:
302: service.addSlots(slots);
303:
304: // Add service to services, then add services to organization
305: services.add(service);
306:
307: }
308: return services;
309: }
310:
311: private List getWSRPPortletService(List portlets)
312: throws JAXRException {
313: Iterator it = portlets.iterator();
314: // Create services and service
315: List services = new ArrayList();
316:
317: while (it.hasNext()) {
318: WSRPPortlet portlet = (WSRPPortlet) it.next();
319: String serviceName = portlet.getName();
320: String serviceDesc = portlet.getDescription();
321:
322: Service service = blcm.createService(serviceName);
323:
324: InternationalString is = blcm
325: .createInternationalString(serviceDesc);
326: service.setDescription(is);
327:
328: Collection serviceBindings = getPortletServiceBindings(portlet);
329: service.addServiceBindings(serviceBindings);
330:
331: //Get Portlet Classifications
332: Collection classifications = getPortletClassifications();
333: //Add classification to services
334: service.addClassifications(classifications);
335:
336: Collection slots = getPortletSlots(portlet);
337: service.addSlots(slots);
338:
339: // Add service to services, then add services to organization
340: services.add(service);
341:
342: }
343: return services;
344:
345: }
346:
347: public String getSpecName() {
348: Object value = rc.get(JESRegistryConstants.WSRP_V1_SPEC_NAME);
349: if (value == null || value.toString().trim().length() < 1) {
350: return DEFAULT_SPEC_NAME;
351: }
352: return value.toString();
353: }
354:
355: public String getSpecDesc() {
356: Object value = rc.get(JESRegistryConstants.WSRP_VI_SPEC_DESC);
357: if (value == null || value.toString().trim().length() < 1) {
358: return DEFAULT_SPEC_DESC;
359: }
360: return value.toString();
361: }
362:
363: public String getWSRPURLDesc() {
364: Object value = rc.get(JESRegistryConstants.WSRP_V1_DESC);
365: if (value == null || value.toString().trim().length() < 1) {
366: return DEFAULT_WSRP_V1_DESC;
367: }
368: return value.toString();
369: }
370:
371: public String getProducerURLDesc() {
372: Object value = rc
373: .get(JESRegistryConstants.WSRP_PRODUCER_URL_DESC);
374: if (value == null || value.toString().trim().length() < 1) {
375: return DEFAULT_WSRP_PRODUCER_URL_DESC;
376: }
377: return value.toString();
378: }
379:
380: private Collection getPortletClassifications() throws JAXRException {
381: Collection classifications = getWSRPClassification();
382: Concept portletConcept = (Concept) bqm
383: .getRegistryObject(EBXML_WSRP_PORTLET_CONCEPT_STR);
384: Classification portletClassification = blcm
385: .createClassification(portletConcept);
386: classifications.add(portletClassification);
387: return classifications;
388: }
389:
390: private Collection getProducerClassifications()
391: throws JAXRException {
392: Collection classifications = getWSRPClassification();
393: Concept producerConcept = (Concept) bqm
394: .getRegistryObject(EBXML_WSRP_PRODUCER_CONCEPT_STR);
395: Classification producerClassification = blcm
396: .createClassification(producerConcept);
397: classifications.add(producerClassification);
398: return classifications;
399: }
400:
401: private Collection getWSRPClassification() throws JAXRException {
402: Concept wsrpv1Concept = (Concept) bqm
403: .getRegistryObject(EBXML_WSRP_V1_CONCEPT_STR);
404: Classification wsrpv1Classification = blcm
405: .createClassification(wsrpv1Concept);
406: Collection classifications = new ArrayList();
407: classifications.add(wsrpv1Classification);
408: return classifications;
409: }
410:
411: private Collection getProducerSlots(WSRPProducer producer)
412: throws JAXRException {
413: String reqRegis = Boolean.toString(producer
414: .getRequiresRegistration());
415: Slot requiresRegistrationSlot = blcm
416: .createSlot(
417: JESRegistryConstants.EBXML_WSRP_PRODUCER_REQUIRES_REGISTRATION,
418: reqRegis, "boolean");
419: Slot requiresInitCookieSlot = blcm
420: .createSlot(
421: JESRegistryConstants.EBXML_WSRP_PRODUCER_REQUIRES_INIT_COOKIE,
422: producer.getRequiresInitCookie(), "string");
423:
424: Collection slots = new ArrayList();
425: slots.add(requiresRegistrationSlot);
426: slots.add(requiresInitCookieSlot);
427: return slots;
428: }
429:
430: private Collection getPortletSlots(WSRPPortlet portlet)
431: throws JAXRException {
432: Slot portletHandle = blcm.createSlot(
433: JESRegistryConstants.EBXML_WSRP_PORTLET_HANDLE, portlet
434: .getHandle(), "list");
435: Slot markupType = blcm.createSlot(
436: JESRegistryConstants.EBXML_WSRP_PORTLET_MARKUP_TYPES,
437: portlet.getMarkupTypes(), "list");
438:
439: Collection slots = new ArrayList();
440: slots.add(portletHandle);
441: slots.add(markupType);
442: return slots;
443:
444: }
445:
446: private Collection getProducerServiceBindings(WSRPProducer producer)
447: throws JAXRException, WSRPException {
448:
449: Collection serviceBindings = new ArrayList();
450:
451: String serviceBindingDesc = getProducerURLDesc();
452:
453: //TODO : Multiple Access URI
454: Iterator urls = producer.getProducerURL().iterator();
455: String serviceBindingURI = urls.next().toString();
456:
457: // Create service bindings
458:
459: ServiceBinding binding = blcm.createServiceBinding();
460: InternationalString is = blcm
461: .createInternationalString(serviceBindingDesc);
462: binding.setDescription(is);
463:
464: // allow us to publish a fictitious URI without an error
465: binding.setValidateURI(false);
466: binding.setAccessURI(serviceBindingURI);
467:
468: SpecificationLink specLink = getSpecificationLink();
469:
470: binding.addSpecificationLink(specLink);
471:
472: serviceBindings.add(binding);
473: return serviceBindings;
474: }
475:
476: private Collection getPortletServiceBindings(WSRPPortlet portlet)
477: throws JAXRException {
478:
479: Collection serviceBindings = new ArrayList();
480: //TODO : Multiple Access URI
481: Iterator urls = portlet.getProducerURL().iterator();
482: String serviceBindingURI = urls.next().toString();
483: String serviceBindingDesc = getProducerURLDesc();
484: // Create service bindings
485:
486: ServiceBinding binding = blcm.createServiceBinding();
487: InternationalString is = blcm
488: .createInternationalString(serviceBindingDesc);
489: binding.setDescription(is);
490:
491: // allow us to publish a fictitious URI without an error
492: binding.setValidateURI(false);
493: binding.setAccessURI(serviceBindingURI);
494:
495: //Add Specification Link
496:
497: SpecificationLink specLink = getSpecificationLink();
498: binding.addSpecificationLink(specLink);
499:
500: serviceBindings.add(binding);
501: return serviceBindings;
502: }
503:
504: /*
505: * Creates a Specification Link to WSRP v1 TYPES URL as publihed in OASIS
506: */
507:
508: private SpecificationLink getSpecificationLink()
509: throws JAXRException {
510: SpecificationLink specLink = blcm.createSpecificationLink();
511: String specName = getSpecName();
512: String specDesc = getSpecDesc();
513: ;
514:
515: specLink.setName(blcm.createInternationalString(specName));
516: specLink.setDescription(blcm
517: .createInternationalString(specDesc));
518:
519: String externalURI = JESRegistryConstants.OASIS_WSRP_V1_SPEC_URL;
520: String description = getWSRPURLDesc();
521:
522: Key k = blcm.createKey("wsrp_v1_types");
523:
524: //This does not work
525: //ExternalLink link = blcm.createExternalLink(externalURI,description);
526:
527: // Just a workaround , Creating a ExternalLink always validates the URL. Hence
528: // Setting it to the registry server URL value first and then setting it to the original
529: // URL
530: ////TODO: Remove this, @ see bug : 6334671
531: String tempURL = getTemporaryExternalLink();
532: ExternalLink link = blcm.createExternalLink(tempURL,
533: description);
534: link.setValidateURI(false);
535: link.setExternalURI(externalURI);
536: link.setKey(k);
537:
538: specLink.setSpecificationObject(link);
539:
540: return specLink;
541: }
542:
543: private void createHasPortletAssociations(Service producer,
544: List portletServices) throws JAXRException, WSRPException {
545: Concept associationConcept = (Concept) bqm
546: .getRegistryObject(JESRegistryConstants.EBXML_WSRP_HAS_PORTLET_ASSOCIATION);
547: Service portletService = null;
548: Collection associations = new ArrayList();
549: for (int i = 0; i < portletServices.size(); i++) {
550: portletService = (Service) portletServices.get(i);
551: Association hasPortletAssociation = blcm.createAssociation(
552: portletService, associationConcept);
553: producer.addAssociation(hasPortletAssociation);
554: associations.add(hasPortletAssociation);
555: }
556: BulkResponse response = null;
557: response = blcm.saveAssociations(associations, true);
558: processResponse(response, true);
559: }
560:
561: //TODO: Remove this method , @ see bug : 6334671
562: // This is just a workaround
563: private String getTemporaryExternalLink() {
564: ProviderProperties providerProps = ProviderProperties
565: .getInstance();
566: String resolvableURL = providerProps.getProperty(
567: JESRegistryConstants.JAXR_QUERY_URL_STRING).toString();
568: return resolvableURL;
569: }
570:
571: }
|