KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > wsmgmt > registry > RegistryAccessObjectImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * RegistryAccessObjectImpl.java
26  *
27  * Created on June 6, 2005, 4:16 PM
28  */

29
30 package com.sun.enterprise.admin.wsmgmt.registry;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.net.InetAddress JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.FileReader JavaDoc;
46 import java.io.BufferedReader JavaDoc;
47 import javax.xml.registry.ConnectionFactory JavaDoc;
48 import com.sun.appserv.management.ext.wsmgmt.WebServiceEndpointInfo;
49 import com.sun.enterprise.admin.wsmgmt.WebServiceMgrBackEnd;
50 import com.sun.enterprise.config.ConfigException;
51 import com.sun.logging.LogDomains;
52 import java.util.logging.Logger JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import javax.xml.registry.*;
55 import javax.xml.registry.infomodel.*;
56 import com.sun.appserv.management.ext.wsmgmt.WebServiceEndpointInfo;
57
58 /**
59  * RegistryAccessObject is based on DataAccessObject pattern. It facilitates
60  * access to the registry operations. A new RegistryAccessObject is created
61  * for each publishToRegistry, unpublishFromRegistry and listRegistryLocations
62  * operation from asadmin.
63  *
64  * A RAO encapsulates connection objects to each of the listed registry locations
65  * specificed by throws the jndi name of the connector connection pool.
66  *
67  * @author Harpreet Singh
68  */

69 public class RegistryAccessObjectImpl implements RegistryAccessObject{
70     
71     private static final Logger JavaDoc _logger =
72             Logger.getLogger(LogDomains.ADMIN_LOGGER);
73     
74     private WebServiceMgrBackEnd mgrBE= null;
75     private Map JavaDoc webServiceInfoMap = null;
76     
77     private static final String JavaDoc DEFAULT_ORGANIZATION =
78             "Sun Microsystems";
79     private static final String JavaDoc DEFAULT_DESCRIPTION =
80             "Default Description for Sun Java Application Server Web Service";
81     
82     private boolean isUDDI = false;
83     private boolean isEbxml = false;
84     private static int MINUS_ONE = -1;
85     
86     private static String JavaDoc JAXR_CONNECTION_FACTORY =
87             "com.sun.connector.jaxr.JaxrConnectionFactory";
88     private static String JavaDoc EBXML_CONNECTION_FACTORY =
89             "com.sun.jaxr.ra.ebxml.JaxrConnectionFactory";
90     /*
91      * Set this to set a MockObject. There is a mock registry to publish
92      * to. This is just for testing purposes. This allows to configure
93      * domain xml file without having to setup a registry.
94      */

95     private String JavaDoc MOCK_REGISTRY = "com.sun.appserv.admin.wsmgmt.registry.mock";
96     private boolean mockRegistry = false;
97     
98     /** Creates a new instance of RegistryAccessObject */
99     public RegistryAccessObjectImpl() {
100         mgrBE = WebServiceMgrBackEnd.getManager();
101     }
102     
103     //<editor-fold defaultstate="collapsed" desc="Externally Visible Methods">
104
/**
105      * publish a web service to a set of registries
106      * @param String[] list of registry-locations specified by the jndi name
107      * of the connector-connection-pool. The list of names can be obtained by
108      * using the listRegistryLocations method
109      * @param String web service name
110      * @param String load balancer host where this webservice is (optionally)
111      * hosted. A null string signifies that the host is optional and is to be
112      * ignored while publishing this web service
113      * @param int load balancer port where this webservice is (optionally)
114      * hosted. A value of -1 indicates that this is optional and is to be
115      * ignored
116      * @param int load balancer ssl port where this webservice is (optionally)
117      * hosted. A value of -1 indicates that this is optional and is to be
118      * ignored
119      * @param String[] an (optional) list of categories that can qualify this
120      * webservice. A null value indicates that this optional and is to be
121      * ignored
122      * @param String description an (optional) description. A null value
123      * indicates that this is to be ignored
124      * @return boolean true if published, false otherwise.
125      * @todo Logging the exceptions
126      */

127     public boolean publish(String JavaDoc[] registryLocations, String JavaDoc webServiceName,
128             String JavaDoc lbhost, int lbport, int lbsslport, String JavaDoc[] categories,
129             String JavaDoc organization, String JavaDoc description) {
130         
131         webServiceInfoMap = getWebServiceInfoMap(webServiceName);
132         if(!isWebServiceNameValid(webServiceName)){
133             _logger.log(Level.SEVERE,
134                     "registry.invalid_webservice_name_publish_failure",
135                     webServiceName);
136             return false;
137         }
138         ConfigHelper ch =
139                 ConfigHelper.getInstanceToUpdateConfig(webServiceInfoMap);
140         
141         String JavaDoc[] duplicate = ch.checkForDuplicateRegistries(registryLocations);
142         if (duplicate != null) {
143             // Error, we are publishing duplicate entries to the same registry.
144
// Message has been logged. Throw an exception;
145
StringBuffer JavaDoc message = new StringBuffer JavaDoc(
146                     "Trying to publish Web Service with jndi entries pointing " +
147                     "to the same " +
148                     "registry. Remove jndi names that point to the same" +
149                     " connector pool and retry. The" +
150                     " duplicate entries are : ");
151             for (int i=0;i<duplicate.length; i++){
152                 message.append(duplicate[i] );
153                 if (i == (duplicate.length-1))
154                     message.append(".");
155                 else
156                     message.append(",");
157             }
158             throw new RuntimeException JavaDoc(message.toString());
159         }
160         
161         String JavaDoc[] alreadyPublishedRegistries =
162                 ch.listAlreadyPublishedRegistryLocations(webServiceName,
163                 registryLocations);
164         List JavaDoc<String JavaDoc> publishTo = new ArrayList JavaDoc<String JavaDoc>
165                 (Arrays.asList(registryLocations));
166         if (alreadyPublishedRegistries != null)
167             publishTo.removeAll(
168                     new ArrayList JavaDoc<String JavaDoc> (Arrays.asList(alreadyPublishedRegistries)));
169         
170         if(publishTo.size() == 0) {
171             String JavaDoc message = "Web Service " + webServiceName +
172                     " already published to the registries! Unpublish and " +
173                     "republish.";
174             _logger.log(Level.WARNING, message);
175             throw new RuntimeException JavaDoc(message);
176         }
177         String JavaDoc wsdlFile = getWSDL(webServiceName);
178         if(wsdlFile == null){
179             _logger.log(Level.SEVERE,
180                     "registry.wsdl_absent_publish_failure",
181                     webServiceName);
182             return false;
183         }
184         
185         Map JavaDoc<String JavaDoc, String JavaDoc> publishedTo = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
186         for(String JavaDoc registryLocation : publishTo){
187             try{
188                 boolean isPublished = false;
189                 mockRegistry = Boolean.getBoolean(MOCK_REGISTRY);
190                 if (!mockRegistry){// mock registry is true only for testing
191
// domain xml
192
ConnectionFactory JavaDoc cf = lookupConnection(registryLocation);
193                     if(cf != null){
194                         isPublished =
195                                 publishWSDL(wsdlFile, cf, lbhost, lbport, lbsslport,
196                                 categories, organization, description,
197                                 webServiceName);
198                     } else{
199                         _logger.log(Level.WARNING,
200                                 "registry.registry_location_absent_publish_failure",
201                                 registryLocation);
202                     }
203                 } else {
204                     isPublished = true;
205                 }
206                 if(isPublished)
207                     publishedTo.put(registryLocation, organization);
208                 
209             } catch(NamingException JavaDoc ne){
210                 _logger.log(Level.SEVERE,
211                         "registry.registry_location_absent_publish_failure",
212                         registryLocation);
213                 
214                 _logger.log(Level.SEVERE,
215                         "registry.registry_location_naming_exception_publish_failure",
216                         ne);
217             }
218         }
219         if(publishedTo.size() <= 0){
220             // nothing was published
221
_logger.log(Level.WARNING,
222                     "registry.not_published", webServiceName);
223             return false;
224         }
225         ch.addToConfig(webServiceName, publishedTo);
226         return true;
227     }
228     
229     /**
230      * Unpublishes a web service wsdl from a list of registries
231      * @param String[] list of registry-locations
232      * @param String web service name whose wsdl needs to be unpublised
233      * @return boolean true if unpublished successfully
234      */

235     public boolean unpublishFromRegistry(String JavaDoc[] registryLocations,
236             String JavaDoc webServiceName) {
237         
238         webServiceInfoMap = getWebServiceInfoMap(webServiceName);
239         if(!isWebServiceNameValid(webServiceName)){
240             _logger.log(Level.SEVERE,
241                     "registry.invalid_webservice_name_unpublish_failure",
242                     webServiceName);
243             return false;
244         }
245         List JavaDoc<String JavaDoc> unpublishedFrom = new ArrayList JavaDoc<String JavaDoc>();
246         for(int i=0; i<registryLocations.length;i++){
247             try{
248                 boolean unpublished = false;
249                 mockRegistry = Boolean.getBoolean(MOCK_REGISTRY);
250                 if (!mockRegistry){// true only for MockRegistry
251
ConnectionFactory JavaDoc cf = lookupConnection(registryLocations[i]);
252                     unpublished = unpublishFromRegistry(cf, webServiceName,
253                             registryLocations[i]);
254                 }else {
255                     unpublished = true;
256                 }
257                 if(unpublished)
258                     unpublishedFrom.add(registryLocations[i]);
259             } catch(NamingException JavaDoc ne){
260                 _logger.log(Level.SEVERE,
261                         "registry.registry_location_absent_unpublish_failure",
262                         registryLocations[i]);
263                 
264                 _logger.log(Level.SEVERE,
265                         "registry.registry_location_naming_exception_unpublish_failure",
266                         ne);
267             }
268         }
269         if(unpublishedFrom.size() <=0){
270             _logger.log(Level.WARNING,
271                     "registry.not_unpublished", webServiceName);
272             return false;
273         }
274         ConfigHelper ch =
275                 ConfigHelper.getInstanceToUpdateConfig(webServiceInfoMap);
276         String JavaDoc[] unpublished = new String JavaDoc[unpublishedFrom.size()];
277         unpublished = unpublishedFrom.toArray(unpublished);
278         ch.deleteFromConfig(webServiceName, unpublished);
279         return true;
280     }
281     
282     /**
283      * List the RegistryLocations. A registry location is the jndi name of a
284      * connection pool that points to a registry determined by the
285      * connector connection definition of the type JAXR_REGISTRY_TYPE
286      * @return String[] list of registry-location
287      */

288     public String JavaDoc[] listRegistryLocations(){
289         ConfigHelper ch = ConfigHelper.getInstanceToQueryRegistryLocations();
290         String JavaDoc[] list = null;
291         list = ch.listRegistryLocations();
292         return list;
293     }
294     
295     //</editor-fold>
296

297     //<editor-fold defaultstate="collapsed" desc="Publish Support Methods">
298
private boolean publishWSDL(String JavaDoc wsdlFile, ConnectionFactory JavaDoc cf,
299             String JavaDoc lbhost, int lbport, int lbsslport,
300             String JavaDoc[] categories, String JavaDoc organization, String JavaDoc description,
301             String JavaDoc webServiceName){
302         
303         boolean value = false;
304         try{
305             
306             String JavaDoc aURI =
307                     (String JavaDoc)webServiceInfoMap.get(WebServiceEndpointInfo.END_POINT_URI_KEY);
308             StringBuffer JavaDoc sbuf = null;
309             StringBuffer JavaDoc secureSbuf = null;
310             if(lbport != -1){
311                 sbuf = new StringBuffer JavaDoc();
312                 sbuf.append("http://"+lbhost+":"+lbport);
313                 if (!aURI.startsWith("/"))
314                     sbuf.append("/");
315                 sbuf.append(aURI+"?wsdl");
316                 _logger.log(Level.INFO, "registry.access_url",
317                         new Object JavaDoc[] {webServiceName, sbuf.toString()});
318                 
319             }
320             if (lbsslport != -1){
321                 secureSbuf = new StringBuffer JavaDoc();
322                 secureSbuf.append("https://"+lbhost+":"+lbsslport);
323                 if (!aURI.startsWith("/"))
324                     secureSbuf.append("/");
325                 secureSbuf.append(aURI+"?wsdl");
326                 _logger.log(Level.INFO, "registry.access_url",
327                         new Object JavaDoc[] {webServiceName, secureSbuf.toString()});
328             }
329             String JavaDoc[] accessURI;
330             if ((lbport != MINUS_ONE ) && (lbsslport != MINUS_ONE)){
331                 accessURI = new String JavaDoc [2];
332                 accessURI[0] = sbuf.toString();
333                 accessURI[1] = secureSbuf.toString();
334             } else {
335                 accessURI = new String JavaDoc[1];
336                 accessURI[0] = (sbuf!=null)?sbuf.toString():secureSbuf.toString();
337             }
338             
339             Connection con = getConnection(cf);
340             RegistryService rs = con.getRegistryService();
341             BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager();
342             BusinessQueryManager bqm = rs.getBusinessQueryManager();
343             
344             // Create organization name and description
345
// change the org type
346
if (organization == null)
347                 organization = DEFAULT_ORGANIZATION;
348             
349             Organization org = blcm.createOrganization(organization);
350             if(description == null)
351                 description = DEFAULT_DESCRIPTION;
352             
353             org.setName(blcm.createInternationalString(organization));
354             org.setDescription(blcm.createInternationalString(description));
355             
356             org = createClassificationSchemeAndClassification(blcm, bqm, org,
357                     webServiceName);
358             org = createServiceAndServiceBindingsAndExternalLink(blcm, org,
359                     description, accessURI, webServiceName);
360             
361             org = categorizeCategoriesViaSlots(blcm, org, webServiceName,
362                     categories);
363             // finally publish it.
364
value = publishOrg(blcm, org);
365         } catch (JAXRException je){
366             _logger.log(Level.SEVERE, "registry.publish_failure_exception", je);
367             throw new RuntimeException JavaDoc(je);
368         }
369         return value;
370     }
371     
372     private boolean publishOrg(BusinessLifeCycleManager blcm,
373             Organization org) throws JAXRException {
374         boolean value = false;
375         List JavaDoc<Organization> orgs = new ArrayList JavaDoc<Organization> ();
376         orgs.add(org);
377         BulkResponse response = blcm.saveOrganizations(orgs);
378         if (response.getStatus() == BulkResponse.STATUS_SUCCESS) {
379             _logger.log(Level.INFO, "registry.successful_publish",
380                     org.getName().getValue());
381             value = true;
382         } else{
383             _logger.log(Level.SEVERE, "registry.organization_not_published",
384                     org.getName().getValue());
385             
386             Collection JavaDoc exceptions = response.getExceptions();
387             displayExceptions(exceptions, Level.SEVERE);
388             value = false;
389         }
390         return value;
391     }
392     /**
393      * Create Classification and Classification Schemes
394      * the name of the category and the value is the Web Service Name.
395      * @param blcm BusinessLifeCycleManager to publish to
396      * @param Organization the organization that needs the slots
397      * @param webServiceName the name of the Web Service
398      * @return Organization modified Organization Object with the added slots
399      * @thows JAXRException when ServiceBinding cannot be created.
400      */

401     
402     private Organization createClassificationSchemeAndClassification(
403             BusinessLifeCycleManager blcm, BusinessQueryManager bqm,
404             Organization org,
405             String JavaDoc webServiceName)
406             throws JAXRException {
407         try{
408             ClassificationScheme classScheme = null;
409             Collection JavaDoc findQualifiers = createFindQualifiers();
410             
411             classScheme = bqm.findClassificationSchemeByName(findQualifiers,
412                     webServiceName);
413             
414             if (classScheme == null){
415                 classScheme = blcm.createClassificationScheme(webServiceName,
416                         webServiceName);
417                 List JavaDoc<ClassificationScheme> classificationSchemeList =
418                         new ArrayList JavaDoc<ClassificationScheme> ();
419                 classificationSchemeList.add(classScheme);
420                 if (this.isUDDI) // UDDI does not create a classification scheme
421
// in saveOrgs while ebxml creates one by default :-(
422
blcm.saveClassificationSchemes(classificationSchemeList);
423             }
424             // find the classification scheme
425

426             // Define find qualifiers and name patterns
427
if (this.isUDDI)
428                 classScheme = bqm.findClassificationSchemeByName(findQualifiers,
429                         webServiceName);
430             
431             if (classScheme == null){
432                 _logger.log(Level.SEVERE,
433                         "registr.cannot_find_classification_scheme",
434                         webServiceName);
435                 return org;
436             }
437             Collection JavaDoc<Classification> classificationExists
438                     = classScheme.getClassifications();
439             Classification wsdlSpecClassification = null;
440             if (!classificationExists.isEmpty()){
441                 for (Classification classification : classificationExists){
442                     if (classification.getName().getValue().equals(webServiceName)){
443                         wsdlSpecClassification = classification;
444                         break;
445                     }
446                 }
447             }
448             if(wsdlSpecClassification == null){
449                 if (this.isUDDI){
450                     wsdlSpecClassification =
451                             blcm.createClassification(classScheme, "wsdlSpec",
452                             "wsdlSpec");
453                     wsdlSpecClassification.setValue(webServiceName);
454                     
455                 } else {
456                     wsdlSpecClassification =
457                             blcm.createClassification(classScheme, webServiceName,
458                             webServiceName);
459                     wsdlSpecClassification.setValue(webServiceName);
460                     wsdlSpecClassification.setClassificationScheme(classScheme);
461                     wsdlSpecClassification.setName
462                             (blcm.createInternationalString(webServiceName));
463                 }
464                 classScheme.addClassification(wsdlSpecClassification);
465                 // have to do this again as adding classificationscheme and
466
// classification at the same time bombs on UDDI.
467
List JavaDoc<ClassificationScheme> classificationSchemeList =
468                         new ArrayList JavaDoc<ClassificationScheme> ();
469                 classificationSchemeList.add(classScheme);
470                 if (this.isUDDI)
471                     blcm.saveClassificationSchemes(classificationSchemeList);
472             }
473             org.addClassification(wsdlSpecClassification);
474         }catch (JAXRException je){
475             _logger.log(Level.SEVERE, "registry.classification_creation_failed");
476             throw je;
477         }
478         return org;
479     }
480     
481     
482     /**
483      * Categorize all listed categories into Slots. The name of the slot is
484      * the name of the category and the value is the Web Service Name.
485      * @param blcm BusinessLifeCycleManager to publish to
486      * @param Organization the organization that needs the slots
487      * @param webServiceName the name of the Web Service
488      * @param categories the list of categories to categorize the object in
489      * @return Organization modified Organization Object with the added slots
490      * @thows JAXRException when ServiceBinding cannot be created.
491      */

492     
493     private Organization categorizeCategoriesViaSlots(
494             BusinessLifeCycleManager blcm, Organization org,
495             String JavaDoc webServiceName, String JavaDoc[] categories)
496             throws JAXRException{
497         try{
498             // categorize categories into slots
499
if (categories == null || categories.length <=0 )
500                 return org;
501             for (int i = 0; i< categories.length; i++){
502                 List JavaDoc<String JavaDoc> slotValues = new ArrayList JavaDoc<String JavaDoc> ();
503                 slotValues.add(webServiceName);
504                 Slot slot = blcm.createSlot(categories[i], slotValues, null);
505                 org.addSlot(slot);
506             }
507         }catch (JAXRException je){
508             _logger.log(Level.SEVERE, "registry.slot_creation_failed");
509             throw je;
510         }
511         return org;
512     }
513     
514     
515     
516     /**
517      * Create a ServiceBinding Object in the registry
518      * @param blcm BusinessLifeCycleManager to publish to
519      * @param description for the ServiceBinding
520      * @param accessURI the URI the WSDL is found
521      * @param name of the service binding
522      * @return ServiceBinding Object
523      * @thows JAXRException when ServiceBinding cannot be created.
524      */

525     private ServiceBinding createServiceBinding(
526             BusinessLifeCycleManager blcm, String JavaDoc description, String JavaDoc accessURI,
527             String JavaDoc name)
528             throws JAXRException{
529         ServiceBinding binding = null;
530         try{
531             binding = blcm.createServiceBinding();
532             binding.setDescription(blcm.createInternationalString(description));
533             binding.setValidateURI(false);
534             binding.setAccessURI(accessURI);
535             binding.setName(blcm.createInternationalString(name+accessURI));
536         }catch (JAXRException je){
537             _logger.log(Level.SEVERE, "registry.service_binding_creation_failed");
538             throw je;
539         }
540         return binding;
541     }
542     
543     /**
544      * Create a Service with an enclosing ServiceBinding Object in the registry
545      * that points to the WSDL of the Web Service.
546      * @param blcm BusinessLifeCycleManager to publish to
547      * @param org Organization that the Service should be created under
548      * @param description for the ServiceBinding
549      * @param accessURI the URI the WSDL is found
550      * @return Organization the modified Organization Object
551      * @thows JAXRException when ServiceBinding cannot be created.
552      */

553     private Organization createServiceAndServiceBindingsAndExternalLink(
554             BusinessLifeCycleManager blcm, Organization org,
555             String JavaDoc description, String JavaDoc[] accessURI, String JavaDoc webServiceName)
556             throws JAXRException{
557         try{
558             String JavaDoc serviceName = org.getName().toString() + ":" +
559                     webServiceName;
560             // Creating a Organization Name qualified Service.
561
// This will allow web services with same names published by
562
// different organization to co-exist
563
Service service = blcm.createService(serviceName);
564             service.setDescription(blcm.createInternationalString(description));
565             for (String JavaDoc uri: accessURI){
566                 ServiceBinding binding = createServiceBinding(blcm, description,
567                         uri, webServiceName );
568                 service.addServiceBinding(binding);
569                 ExternalLink wsdlLink =
570                         blcm.createExternalLink( uri, description);
571                 wsdlLink.setValidateURI(false);
572                 wsdlLink.setName(blcm.createInternationalString(serviceName));
573                 // UDDI does not let us add external Links to services
574
// JAXR should really hide us from this - but does not :-(
575
if (this.isUDDI == false)
576                     service.addExternalLink(wsdlLink);
577                 org.addExternalLink(wsdlLink);
578             }
579             org.addService(service);
580         } catch (JAXRException je){
581             _logger.log(Level.SEVERE, "registry.service_creation_failed");
582             throw je;
583         }
584         return org;
585     }
586     
587     //</editor-fold>
588

589     //<editor-fold defaultstate="collapsed" desc="Unpublish Support Methods">
590
private boolean unpublishFromRegistry(ConnectionFactory JavaDoc cf,
591             String JavaDoc webServiceName, String JavaDoc registryLocation){
592         
593         boolean retvalue = false;
594         try{
595             Connection con = getConnection(cf);
596             RegistryService rs = con.getRegistryService();
597             BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager();
598             BusinessQueryManager bqm = rs.getBusinessQueryManager();
599             // Define find qualifiers and name patterns
600
Collection JavaDoc findQualifiers = createFindQualifiers();
601             ClassificationScheme classificationScheme =
602                     bqm.findClassificationSchemeByName(findQualifiers,
603                     webServiceName);
604             
605             if (classificationScheme == null){
606                 _logger.log(Level.SEVERE,
607                         "registry.cannot_find_classification_scheme",
608                         webServiceName);
609                 retvalue = false;
610             }
611             Classification classification = null;
612             if (this.isUDDI){
613                 classification =
614                         blcm.createClassification(classificationScheme, "wsdlSpec",
615                         "wsdlSpec");
616                 classification.setValue(webServiceName);
617                 
618             } else {
619                 classification =
620                         blcm.createClassification(classificationScheme,
621                         webServiceName, webServiceName);
622                 classification.setValue(webServiceName);
623             }
624             classification.setName(blcm.createInternationalString(webServiceName));
625             Collection JavaDoc<Classification> classList = null;
626             if (classificationScheme != null){
627                 classificationScheme.addClassification(classification);
628                 classList = classificationScheme.getClassifications();
629             } else {
630                 classList = new ArrayList JavaDoc<Classification> ();
631                 classList.add(classification);
632             }
633             webServiceInfoMap = getWebServiceInfoMap(webServiceName);
634             if(!isWebServiceNameValid(webServiceName)){
635                 _logger.log(Level.SEVERE,
636                         "registry.invalid_webservice_name_unpublish_failure",
637                         webServiceName);
638                 return false;
639             }
640             ConfigHelper ch =
641                     ConfigHelper.getInstanceToUpdateConfig(webServiceInfoMap);
642             String JavaDoc org = ch.getOrganizationName(webServiceName,
643                     registryLocation);
644             if (org == null){
645                 _logger.log(Level.SEVERE, "Cannot find Organization Name for " +
646                         "web service in the appserver");
647                 return false;
648             }
649             Collection JavaDoc orgs = new ArrayList JavaDoc <String JavaDoc>();
650             orgs.add(org);
651             BulkResponse br = bqm.findOrganizations(findQualifiers,
652                     (Collection JavaDoc) orgs,
653                     (Collection JavaDoc) null,
654                     (Collection JavaDoc) null,
655                     (Collection JavaDoc)null, (Collection JavaDoc)null);
656             
657             boolean deletedOrg = true;
658             if (br.getStatus() == BulkResponse.STATUS_SUCCESS){
659                 Collection JavaDoc<Organization> orgCollection = br.getCollection();
660                 for (Organization organization : orgCollection){
661                     String JavaDoc orgName = organization.getName().getValue();
662                     if (orgName == null)
663                         continue;
664                     if(!orgName.equals(org))
665                         continue;
666                     _logger.log(Level.INFO, "registry.about_to_unpublish",
667                             new Object JavaDoc[] {webServiceName , orgName});
668                     
669                     deleteServicesAndServiceBindingsAndExternalLinks(organization,
670                             webServiceName, blcm);
671                     
672                     organization.removeClassifications(
673                             organization.getClassifications());
674                     // delete organization
675
deleteOrg(blcm, organization, webServiceName);
676                     break;
677                 }
678                 deleteClassificationScheme(blcm, classificationScheme,
679                         webServiceName);
680                 retvalue = true;
681             }
682         } catch(JAXRException je){
683             _logger.log(Level.SEVERE, "registry.unpublish_failed", je);
684             retvalue = false;
685         }
686         return retvalue;
687     }
688     
689     
690     /**
691      * Delete Service and ServiceBinding Objects from registry.
692      * @param organization to delete from
693      * @param webServiceName name of web service
694      */

695     private void deleteServicesAndServiceBindingsAndExternalLinks(
696             Organization organization,
697             String JavaDoc webServiceName, BusinessLifeCycleManager blcm)
698             throws JAXRException {
699         Collection JavaDoc<Service> servicesCollection = organization.getServices();
700         // keys for all services to be deleted
701
Collection JavaDoc<Key> servicesKey = new ArrayList JavaDoc<Key>();
702         for (Service service : servicesCollection){
703             Collection JavaDoc<ServiceBinding> binding = service.getServiceBindings();
704             // Get all ServiceBindings for this service
705
Collection JavaDoc<Key> bindingsKey = new ArrayList JavaDoc<Key>();
706             for (ServiceBinding sb : binding){
707                 String JavaDoc bindingString = sb.getName().toString();
708                 bindingsKey.add(sb.getKey());
709             }
710             // delink bindings from services before deleting. Else deletion
711
// is not complete
712
service.removeServiceBindings(binding);
713             // delete service bindings
714
BulkResponse bulkResponse = blcm.deleteServiceBindings(bindingsKey);
715             Collection JavaDoc exceptions = bulkResponse.getExceptions();
716             if ( exceptions != null){
717                 _logger.log(Level.WARNING,
718                         "registry.delete_servicebindings_failed",
719                         new Object JavaDoc[]{webServiceName});
720                 displayExceptions(exceptions, Level.SEVERE);
721             }
722             // delink services from external links
723
// UDDI does not let us add external Links to services
724
// JAXR should really hide us from this - but does not :-(
725
if (this.isUDDI == false)
726                 service.removeExternalLinks(service.getExternalLinks());
727             servicesKey.add(service.getKey());
728         }
729         
730         // deleting ExternalLinks from the repository
731
deleteExternalLinks(organization, webServiceName, blcm);
732         
733         // delink services from organization else deletion is incomplete
734
organization.removeServices(servicesCollection);
735         // delete services
736
BulkResponse bulkResponse = blcm.deleteServices(servicesKey);
737         Collection JavaDoc exceptions = bulkResponse.getExceptions();
738         if ( exceptions != null){
739             _logger.log(Level.SEVERE, "registry.delete_services_failed",
740                     new Object JavaDoc[]{webServiceName});
741             displayExceptions(exceptions, Level.SEVERE);
742         }
743         
744     }
745     
746     private void deleteExternalLinks(Organization organization,
747             String JavaDoc webServiceName, BusinessLifeCycleManager blcm) {
748         try{
749             Collection JavaDoc<ExternalLink> exlink = organization.getExternalLinks();
750             // delinking ExternalLinks from the Organization
751
Collection JavaDoc<Key> key = new ArrayList JavaDoc<Key>();
752             for (ExternalLink link : exlink){
753                 key.add(link.getKey());
754             }
755             organization.removeExternalLinks(exlink);
756             BulkResponse bulkResponse = blcm.deleteObjects(key);
757             Collection JavaDoc exceptions = bulkResponse.getExceptions();
758             if ( exceptions != null){
759                 _logger.log(Level.SEVERE, "registry.delete_externallink_failed",
760                         new Object JavaDoc[]{webServiceName});
761                 displayExceptions(exceptions, Level.SEVERE);
762             }
763         } catch (JAXRException je){
764             // ignore but log it
765
_logger.log(Level.INFO, "Could not lookup ExternalLinks (URI) for" +
766                     " webservice " + webServiceName + ". Nothing to delete!");
767         }
768     }
769     private boolean deleteClassificationScheme(BusinessLifeCycleManager blcm,
770             ClassificationScheme scheme,
771             String JavaDoc webServiceName) throws JAXRException {
772         
773         Key key = scheme.getKey();
774         Collection JavaDoc<Key> keysToDelete = new ArrayList JavaDoc<Key>();
775         keysToDelete.add(key);
776         
777         BulkResponse bulkResponse = blcm.deleteClassificationSchemes(keysToDelete);
778         Collection JavaDoc exceptions = bulkResponse.getExceptions();
779         if ( exceptions != null){
780             _logger.log(Level.SEVERE,
781                     "registry.classification_scheme_delete_unsuccessful",
782                     scheme.getName().getValue());
783             displayExceptions(exceptions, Level.SEVERE);
784             return false;
785         } else {
786             _logger.log(Level.INFO,
787                     "registry.classification_scheme_delete_successful",
788                     new Object JavaDoc[]{scheme.getName().getValue(), webServiceName});
789             return true;
790         }
791     }
792     
793     private boolean deleteOrg(BusinessLifeCycleManager blcm, Organization org,
794             String JavaDoc webServiceName) throws JAXRException {
795         Key orgKey = org.getKey();
796         Collection JavaDoc services = org.getServices();
797         if (!services.isEmpty()){
798             _logger.log(Level.SEVERE, "registry.cleanup_services_first",
799                     org.getName().getValue());
800             return false;
801         }
802         Collection JavaDoc keysToDelete = new ArrayList JavaDoc();
803         keysToDelete.add(orgKey);
804         BulkResponse bulkResponse = blcm.deleteOrganizations(keysToDelete);
805         Collection JavaDoc exceptions = bulkResponse.getExceptions();
806         if ( exceptions != null){
807             _logger.log(Level.SEVERE, "registry.delete_org_failed",
808                     org.getName().getValue());
809             displayExceptions(exceptions, Level.SEVERE);
810             return false;
811         } else {
812             _logger.log(Level.INFO, "registry.delete_org_succeeded",
813                     new Object JavaDoc[] {org.getName().getValue(), webServiceName});
814             return true;
815         }
816     }
817     
818     private boolean deleteOrg(BusinessLifeCycleManager blcm,
819             String JavaDoc key, String JavaDoc webServiceName) throws JAXRException{
820         Key orgKey = blcm.createKey(key);
821         Collection JavaDoc keysToDelete = new ArrayList JavaDoc();
822         keysToDelete.add(orgKey);
823         BulkResponse bulkResponse = blcm.deleteOrganizations(keysToDelete);
824         Collection JavaDoc exceptions = bulkResponse.getExceptions();
825         if ( exceptions != null){
826             _logger.log(Level.SEVERE, "registry.delete_org_failed",
827                     key);
828             displayExceptions(exceptions, Level.SEVERE);
829             return false;
830         } else {
831             _logger.log(Level.INFO, "registry.delete_org_succeeded",
832                     new Object JavaDoc[] {key, webServiceName});
833             return true;
834         }
835     }
836     
837     //</editor-fold>
838

839     //<editor-fold defaultstate="collapsed" desc="Utility Methods">
840
private Connection getConnection(ConnectionFactory JavaDoc cf)
841     throws JAXRException {
842         Connection con = null;
843         try{
844             Method JavaDoc method = cf.getClass().getMethod("getConnection", (java.lang.Class JavaDoc[])null);
845             con = (Connection)method.invoke(cf, (Object JavaDoc[])null);
846             if (JAXR_CONNECTION_FACTORY.equals(cf.getClass().getName())){
847                 isUDDI = true;
848             } else if (EBXML_CONNECTION_FACTORY.equals(cf.getClass().getName())){
849                 isEbxml = true;
850             }
851             if (con == null){
852                 con = cf.createConnection();
853             }
854         }catch (java.lang.NoSuchMethodException JavaDoc nsme) {
855             con = cf.createConnection();
856         } catch (java.lang.IllegalAccessException JavaDoc iae){
857             con = cf.createConnection();
858         } catch (java.lang.reflect.InvocationTargetException JavaDoc ite){
859             con = cf.createConnection();
860         }
861         return con;
862     }
863   /*
864    * Given a web service name, returns it wsdl file as a string
865    * @param String webservice name
866    * @return String wsdl file returned as a string
867    * @todo modify api on WebServiceInfo to return the WSDL file in addition
868    * to its location.
869    */

870     private String JavaDoc getWSDL(String JavaDoc webServiceName){
871         
872         String JavaDoc wsdlFile =
873                 (String JavaDoc)webServiceInfoMap.get(WebServiceEndpointInfo.WSDL_FILE_KEY);
874         return wsdlFile;
875     }
876     
877     /*
878      * Determines if the webservice name is valid
879      * @param String webservice name
880      * @return boolean true if the name is valid
881      */

882     private boolean isWebServiceNameValid(String JavaDoc webServiceName){
883         if(webServiceName == null) //sanity check
884
return false;
885         
886         boolean retValue = (webServiceInfoMap == null)?false:true;
887         return retValue;
888     }
889     
890     /**
891      * Gets the map representation of WebServiceInfo
892      * @param String, the name of the webservice
893      * @return Map, map representation of WebServiceInfo
894      */

895     private Map JavaDoc getWebServiceInfoMap(String JavaDoc webServiceName){
896         webServiceInfoMap =
897                 (webServiceName == null)? null:
898                     mgrBE.getWebServiceInfoMap(webServiceName);
899         return webServiceInfoMap;
900     }
901     
902     /**
903      * Looks up a connection corresponding to a registryLocation
904      */

905     private ConnectionFactory JavaDoc lookupConnection(String JavaDoc registryLocation)
906     throws NamingException JavaDoc {
907         
908         InitialContext JavaDoc ic = new InitialContext JavaDoc();
909         ConnectionFactory JavaDoc cf = (javax.xml.registry.ConnectionFactory JavaDoc)
910         ic.lookup(registryLocation);
911         _logger.fine("RegistryAccessObject.lookupConnection : " +
912                 "for registryLocation " +registryLocation);
913         return cf;
914     }
915     
916     private Collection JavaDoc createFindQualifiers() {
917         List JavaDoc<String JavaDoc> findQualifiers = new ArrayList JavaDoc<String JavaDoc>();
918         findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
919 // findQualifiers.add(FindQualifier.EXACT_NAME_MATCH);
920
return findQualifiers;
921     }
922     
923     private void displayExceptions(Collection JavaDoc exceptions, Level JavaDoc level) {
924         Iterator JavaDoc it = exceptions.iterator();
925         while (it.hasNext()){
926             Exception JavaDoc e = (Exception JavaDoc) it.next();
927             _logger.log(level, " JAXR Exception ", e);
928         }
929     }
930     //</editor-fold>
931
// 2. create a special jaxr-ra host
932
}
933         
Popular Tags