1 19 20 package org.netbeans.modules.websvc.core.client.wizard; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import org.netbeans.api.java.project.JavaProjectConstants; 33 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 34 import org.netbeans.api.progress.ProgressHandle; 35 import org.netbeans.api.progress.ProgressHandleFactory; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectUtils; 38 import org.netbeans.api.project.SourceGroup; 39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 41 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 42 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport; 43 import org.netbeans.modules.websvc.core.ClientWizardProperties; 44 import org.openide.DialogDisplayer; 45 import org.openide.ErrorManager; 46 import org.openide.NotifyDescriptor; 47 import org.openide.WizardDescriptor; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileUtil; 50 import org.openide.filesystems.URLMapper; 51 import org.openide.util.NbBundle; 52 import org.openide.util.RequestProcessor; 53 import org.netbeans.modules.j2ee.common.Util; 54 import org.openide.util.Task; 55 import org.netbeans.modules.websvc.core.ClientCreator; 56 57 61 public class JaxWsClientCreator implements ClientCreator { 62 private Project project; 63 private WizardDescriptor wiz; 64 65 private static final boolean DEBUG = false; 66 private static final int JSE_PROJECT_TYPE = 0; 67 private static final int WEB_PROJECT_TYPE = 1; 68 private static final int EJB_PROJECT_TYPE = 2; 69 private static final int CAR_PROJECT_TYPE = 3; 70 71 74 public JaxWsClientCreator(Project project, WizardDescriptor wiz) { 75 this.project = project; 76 this.wiz = wiz; 77 } 78 79 public void createClient() throws IOException { 80 81 final boolean isJsr109Supported = isJsr109Supported(); 82 final boolean isJsr109OldSupported = isJsr109OldSupported(); 83 final boolean isJWSDPSupported = isJWSDPSupported(); 84 85 final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(JaxWsClientCreator.class, "MSG_WizCreateClient")); 88 task = new Task(new Runnable () { 89 public void run() { 90 try { 91 handle.start(); 94 generate15Client((isJsr109Supported || isJWSDPSupported), handle); 95 } catch (IOException exc) { 100 handle.finish(); 102 103 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc); 104 } 105 } 106 }); 107 RequestProcessor.getDefault().post(task); 108 } 109 110 private void generate15Client(boolean isJsr109Platform, ProgressHandle handle) throws IOException { 111 112 JAXWSClientSupport jaxWsClientSupport=null; 114 if(project != null) { 115 jaxWsClientSupport = JAXWSClientSupport.getJaxWsClientSupport(project.getProjectDirectory()); 116 } 117 if(jaxWsClientSupport == null) { 118 String mes = NbBundle.getMessage(WebServiceClientWizardIterator.class, "ERR_NoWebServiceClientSupport"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 124 DialogDisplayer.getDefault().notify(desc); 125 } 126 127 String wsdlUrl = (String )wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_URL); 128 String filePath = (String )wiz.getProperty(ClientWizardProperties.WSDL_FILE_PATH); 129 if(wsdlUrl == null){ 131 wsdlUrl = FileUtil.toFileObject(FileUtil.normalizeFile(new File (filePath))).getURL().toExternalForm(); 132 } 133 String packageName = (String )wiz.getProperty(ClientWizardProperties.WSDL_PACKAGE_NAME); 134 if (packageName!=null && packageName.length()==0) packageName=null; 135 jaxWsClientSupport.addServiceClient(getWsdlName(wsdlUrl),wsdlUrl,packageName, isJsr109Platform); 136 137 handle.finish(); 138 } 139 140 private String getWsdlName(String wsdlUrl) { 141 int ind = wsdlUrl.lastIndexOf("/"); String wsdlName = ind>=0?wsdlUrl.substring(ind+1):wsdlUrl; 143 if (wsdlName.toUpperCase().endsWith("?WSDL")) wsdlName = wsdlName.substring(0,wsdlName.length()-5); ind = wsdlName.lastIndexOf(".wsdl"); if (ind>0) wsdlName = wsdlName.substring(0,ind); 146 return convertAllSpecialChars(wsdlName); 148 } 149 150 private String convertAllSpecialChars(String resultStr){ 151 StringBuffer sb = new StringBuffer (resultStr); 152 for(int i = 0; i < sb.length(); i++){ 153 char c = sb.charAt(i); 154 if( Character.isLetterOrDigit(c) || 155 (c == '/') || 156 (c == '.') || 157 (c == '_') || 158 (c == ' ') || 159 (c == '-')){ 160 continue; 161 }else{ 162 sb.setCharAt(i, '_'); 163 } 164 } 165 return sb.toString(); 166 } 167 168 175 static SourceGroup[] getJavaSourceGroups(Project project) { 176 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 177 JavaProjectConstants.SOURCES_TYPE_JAVA); 178 Set <SourceGroup> testGroups = getTestSourceGroups(project, sourceGroups); 179 List <SourceGroup> result = new ArrayList <SourceGroup>(); 180 for (int i = 0; i < sourceGroups.length; i++) { 181 if (!testGroups.contains(sourceGroups[i])) { 182 result.add(sourceGroups[i]); 183 } 184 } 185 return result.<SourceGroup>toArray(new SourceGroup[result.size()]); 186 } 187 188 private static Set <SourceGroup> getTestSourceGroups(Project project, SourceGroup[] sourceGroups) { 189 Map <FileObject, SourceGroup> foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 190 Set <SourceGroup> testGroups = new HashSet <SourceGroup>(); 191 for (int i = 0; i < sourceGroups.length; i++) { 192 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 193 } 194 return testGroups; 195 } 196 197 private static List <SourceGroup> getTestTargets(SourceGroup sourceGroup, Map <FileObject, SourceGroup> foldersToSourceGroupsMap) { 198 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 199 if (rootURLs.length == 0) { 200 return new ArrayList <SourceGroup>(); 201 } 202 List <SourceGroup> result = new ArrayList <SourceGroup>(); 203 List <FileObject> sourceRoots = getFileObjects(rootURLs); 204 for (int i = 0; i < sourceRoots.size(); i++) { 205 FileObject sourceRoot = sourceRoots.get(i); 206 SourceGroup srcGroup = foldersToSourceGroupsMap.get(sourceRoot); 207 if (srcGroup != null) { 208 result.add(srcGroup); 209 } 210 } 211 return result; 212 } 213 214 private static Map <FileObject, SourceGroup> createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 215 Map <FileObject, SourceGroup> result; 216 if (sourceGroups.length == 0) { 217 result = Collections.<FileObject, SourceGroup>emptyMap(); 218 } else { 219 result = new HashMap <FileObject, SourceGroup>(2 * sourceGroups.length, .5f); 220 for (int i = 0; i < sourceGroups.length; i++) { 221 SourceGroup sourceGroup = sourceGroups[i]; 222 result.put(sourceGroup.getRootFolder(), sourceGroup); 223 } 224 } 225 return result; 226 } 227 228 private static List <FileObject> getFileObjects(URL [] urls) { 229 List <FileObject> result = new ArrayList <FileObject>(); 230 for (int i = 0; i < urls.length; i++) { 231 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 232 if (sourceRoot != null) { 233 result.add(sourceRoot); 234 } else { 235 int severity = ErrorManager.INFORMATIONAL; 236 if (ErrorManager.getDefault().isNotifiable(severity)) { 237 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 238 "No FileObject found for the following URL: " + urls[i])); } 240 } 241 } 242 return result; 243 } 244 245 private J2eePlatform getJ2eePlatform(){ 246 J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 247 if(provider != null){ 248 String serverInstanceID = provider.getServerInstanceID(); 249 if(serverInstanceID != null && serverInstanceID.length() > 0) { 250 return Deployment.getDefault().getJ2eePlatform(serverInstanceID); 251 } 252 } 253 return null; 254 } 255 256 private boolean isJWSDPSupported(){ 257 J2eePlatform j2eePlatform = getJ2eePlatform(); 258 if(j2eePlatform != null){ 259 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP); 260 } 261 return false; 262 } 263 264 private boolean isJsr109Supported(){ 265 J2eePlatform j2eePlatform = getJ2eePlatform(); 266 if(j2eePlatform != null){ 267 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109); 268 } 269 return false; 270 } 271 272 private boolean isJsr109OldSupported(){ 273 J2eePlatform j2eePlatform = getJ2eePlatform(); 274 if(j2eePlatform != null){ 275 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE); 276 } 277 return false; 278 } 279 280 284 Task task; 285 } 286 | Popular Tags |