KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxrpc > dev > wizard > JaxRpcServiceCreator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.jaxrpc.dev.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.api.progress.ProgressHandle;
27 import org.netbeans.api.progress.ProgressHandleFactory;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
30 import org.netbeans.modules.websvc.api.webservices.WsCompileEditorSupport;
31 import org.netbeans.modules.websvc.core.ServiceCreator;
32 import org.netbeans.modules.websvc.core.dev.wizard.NewWebServiceWizardIterator;
33 import org.netbeans.modules.websvc.core.dev.wizard.ProjectInfo;
34 import org.netbeans.modules.websvc.core.dev.wizard.WizardProperties;
35 import org.netbeans.spi.project.ui.templates.support.Templates;
36 import org.openide.DialogDisplayer;
37 import org.openide.ErrorManager;
38 import org.openide.NotifyDescriptor;
39 import org.openide.WizardDescriptor;
40 import org.openide.filesystems.FileObject;
41 import org.openide.util.NbBundle;
42 import org.openide.util.RequestProcessor;
43
44 /**
45  *
46  * @author Peter Liu
47  */

48 public class JaxRpcServiceCreator implements ServiceCreator {
49     
50     private Project project;
51     private ProjectInfo projectInfo;
52     private WizardDescriptor wiz;
53     private String JavaDoc wsName;
54     private int serviceType;
55     private int projectType;
56     
57     /** Creates a new instance of JaxRpcServiceCreator */
58     public JaxRpcServiceCreator(Project project, ProjectInfo projectInfo, WizardDescriptor wiz) {
59         this.project = project;
60         this.projectInfo = projectInfo;
61         this.wiz = wiz;
62     }
63     
64     public void createService() throws IOException JavaDoc {
65         System.out.println("create JaxRpc Service");
66         
67         serviceType = ((Integer JavaDoc) wiz.getProperty(WizardProperties.WEB_SERVICE_TYPE)).intValue();
68         projectType = projectInfo.getProjectType();
69         
70         // Use Progress API to display generator messages.
71
final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(JaxRpcServiceCreator.class, "TXT_WebServiceGeneration")); //NOI18N
72
handle.start(100);
73         
74         Runnable JavaDoc r = new Runnable JavaDoc() {
75             public void run() {
76                 try {
77                     generateWebService(handle);
78                 } catch (Exception JavaDoc e) {
79                     //finish progress bar
80
handle.finish();
81                     String JavaDoc message = e.getLocalizedMessage();
82                     if(message != null) {
83                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
84                         NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE);
85                         DialogDisplayer.getDefault().notify(nd);
86                     } else {
87                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
88                     }
89                 }
90             }
91         };
92         RequestProcessor.getDefault().post(r);
93         
94     }
95     
96     public void createServiceFromWsdl() throws IOException JavaDoc {
97         System.out.println("create JaxRpc Service from wsdl");
98     }
99     
100     private void generateWebService(ProgressHandle handle) throws Exception JavaDoc {
101         FileObject pkg = Templates.getTargetFolder(wiz);
102         WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
103         assert wsSupport != null;
104         
105         wsName = getUniqueJaxrpcName(wsSupport, Templates.getTargetName(wiz));
106         
107         WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
108         handle.progress(NbBundle.getMessage(JaxRpcServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
109
generator.generateWebService();
110         
111         URI JavaDoc targetNS = null;
112         URI JavaDoc typeNS = null;
113         try {
114             targetNS = generator.getTargetNS();
115             typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
116
} catch(java.net.URISyntaxException JavaDoc e) {
117             String JavaDoc mes = NbBundle.getMessage(JaxRpcServiceCreator.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
118
throw new Exception JavaDoc(mes);
119         }
120         //Create config file
121
handle.progress(NbBundle.getMessage(JaxRpcServiceCreator.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
122
String JavaDoc servantClassName = generator.getServantClassName();
123         String JavaDoc seiClassName = generator.getSEIClassName();
124         FileObject configFile = null;
125         configFile = generator.generateConfigFile(seiClassName, servantClassName, targetNS, typeNS);
126         handle.progress(70);
127         
128         //Add web service entries to the project's property file, project file
129
wsSupport.addServiceImpl(wsName, configFile, false);
130         handle.progress(90);
131         
132         //Add web service entries to the module's DD
133
wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
134         
135         //Add webservice entry in webservices.xml
136
handle.progress(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
137
String JavaDoc portTypeName = null;
138         generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
139         
140         handle.finish();
141     }
142     
143     private String JavaDoc getUniqueJaxrpcName(WebServicesSupport wsSupport, String JavaDoc origName){
144         List JavaDoc<WsCompileEditorSupport.ServiceSettings> webServices = wsSupport.getServices();
145         List JavaDoc<String JavaDoc> serviceNames = new ArrayList JavaDoc<String JavaDoc>(webServices.size());
146         for(WsCompileEditorSupport.ServiceSettings service: webServices){
147             serviceNames.add(service.getServiceName());
148         }
149         return uniqueWSName(origName, serviceNames);
150     }
151     
152     private String JavaDoc uniqueWSName(final String JavaDoc origName, List JavaDoc<String JavaDoc> names ){
153         int uniquifier = 0;
154         String JavaDoc truename = origName;
155         while(names.contains(truename)){
156             truename = origName + String.valueOf(++uniquifier);
157         }
158         return truename;
159     }
160 }
161
Popular Tags