001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.core.client.wizard;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.HashSet;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.Set;
054: import org.netbeans.api.java.project.JavaProjectConstants;
055: import org.netbeans.api.java.queries.UnitTestForSourceQuery;
056: import org.netbeans.api.progress.ProgressHandle;
057: import org.netbeans.api.progress.ProgressHandleFactory;
058: import org.netbeans.api.project.Project;
059: import org.netbeans.api.project.ProjectUtils;
060: import org.netbeans.api.project.SourceGroup;
061: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
062: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
063: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
064: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
065: import org.netbeans.modules.websvc.core.ClientWizardProperties;
066: import org.openide.DialogDisplayer;
067: import org.openide.ErrorManager;
068: import org.openide.NotifyDescriptor;
069: import org.openide.WizardDescriptor;
070: import org.openide.filesystems.FileObject;
071: import org.openide.filesystems.FileUtil;
072: import org.openide.filesystems.URLMapper;
073: import org.openide.util.NbBundle;
074: import org.openide.util.RequestProcessor;
075: import org.openide.util.Task;
076: import org.netbeans.modules.websvc.core.ClientCreator;
077:
078: /**
079: *
080: * @author Radko, Milan Kuchtiak
081: */
082: public class JaxWsClientCreator implements ClientCreator {
083: private Project project;
084: private WizardDescriptor wiz;
085:
086: private static final boolean DEBUG = false;
087: private static final int JSE_PROJECT_TYPE = 0;
088: private static final int WEB_PROJECT_TYPE = 1;
089: private static final int EJB_PROJECT_TYPE = 2;
090: private static final int CAR_PROJECT_TYPE = 3;
091:
092: /**
093: * Creates a new instance of WebServiceClientCreator
094: */
095: public JaxWsClientCreator(Project project, WizardDescriptor wiz) {
096: this .project = project;
097: this .wiz = wiz;
098: }
099:
100: public void createClient() throws IOException {
101:
102: final boolean isJsr109Supported = isJsr109Supported();
103: final boolean isJWSDPSupported = isJWSDPSupported();
104:
105: // Use Progress API to display generator messages.
106: final ProgressHandle handle = ProgressHandleFactory
107: .createHandle(NbBundle
108: .getMessage(JaxWsClientCreator.class,
109: "MSG_WizCreateClient")); //NOI18N
110:
111: task = new Task(new Runnable() {
112: public void run() {
113: try {
114: handle.start();
115: generate15Client(
116: (isJsr109Supported || isJWSDPSupported),
117: handle);
118: } catch (IOException exc) {
119: //finish progress bar
120: handle.finish();
121:
122: ErrorManager.getDefault().notify(
123: ErrorManager.EXCEPTION, exc);
124: }
125: }
126: });
127: RequestProcessor.getDefault().post(task);
128: }
129:
130: private void generate15Client(boolean isJsr109Platform,
131: ProgressHandle handle) throws IOException {
132:
133: // !PW Get client support from project (from first page of wizard)
134: JAXWSClientSupport jaxWsClientSupport = null;
135: if (project != null) {
136: jaxWsClientSupport = JAXWSClientSupport
137: .getJaxWsClientSupport(project
138: .getProjectDirectory());
139: }
140: if (jaxWsClientSupport == null) {
141: // notify no client support
142: // String mes = MessageFormat.format (
143: // NbBundle.getMessage (WebServiceClientWizardIterator.class, "ERR_WebServiceClientSupportNotFound"),
144: // new Object [] {"Servlet Listener"}); //NOI18N
145: String mes = NbBundle.getMessage(
146: WebServiceClientWizardIterator.class,
147: "ERR_NoWebServiceClientSupport"); // NOI18N
148: NotifyDescriptor desc = new NotifyDescriptor.Message(mes,
149: NotifyDescriptor.Message.ERROR_MESSAGE);
150: DialogDisplayer.getDefault().notify(desc);
151: handle.finish();
152: return;
153: }
154:
155: String wsdlUrl = (String) wiz
156: .getProperty(ClientWizardProperties.WSDL_DOWNLOAD_URL);
157: String filePath = (String) wiz
158: .getProperty(ClientWizardProperties.WSDL_FILE_PATH);
159: //if (wsdlUrl==null) wsdlUrl = "file:"+(filePath.startsWith("/")?filePath:"/"+filePath); //NOI18N
160: if (wsdlUrl == null) {
161: wsdlUrl = FileUtil.toFileObject(
162: FileUtil.normalizeFile(new File(filePath)))
163: .getURL().toExternalForm();
164: }
165: String packageName = (String) wiz
166: .getProperty(ClientWizardProperties.WSDL_PACKAGE_NAME);
167: if (packageName != null && packageName.length() == 0)
168: packageName = null;
169: jaxWsClientSupport.addServiceClient(getWsdlName(wsdlUrl),
170: wsdlUrl, packageName, isJsr109Platform);
171:
172: handle.finish();
173: }
174:
175: private String getWsdlName(String wsdlUrl) {
176: int ind = wsdlUrl.lastIndexOf("/"); //NOI18N
177: String wsdlName = ind >= 0 ? wsdlUrl.substring(ind + 1)
178: : wsdlUrl;
179: if (wsdlName.toUpperCase().endsWith("?WSDL"))
180: wsdlName = wsdlName.substring(0, wsdlName.length() - 5); //NOI18N
181: ind = wsdlName.lastIndexOf(".wsdl"); //NOI18N
182: if (ind > 0)
183: wsdlName = wsdlName.substring(0, ind);
184: // replace special characters with '_'
185: return convertAllSpecialChars(wsdlName);
186: }
187:
188: private String convertAllSpecialChars(String resultStr) {
189: StringBuffer sb = new StringBuffer(resultStr);
190: for (int i = 0; i < sb.length(); i++) {
191: char c = sb.charAt(i);
192: if (Character.isLetterOrDigit(c) || (c == '/')
193: || (c == '.') || (c == '_') || (c == ' ')
194: || (c == '-')) {
195: continue;
196: } else {
197: sb.setCharAt(i, '_');
198: }
199: }
200: return sb.toString();
201: }
202:
203: /**
204: * Returns Java source groups for all source packages in given project.<br>
205: * Doesn't include test packages.
206: *
207: * @param project Project to search
208: * @return Array of SourceGroup. It is empty if any probelm occurs.
209: */
210: static SourceGroup[] getJavaSourceGroups(Project project) {
211: SourceGroup[] sourceGroups = ProjectUtils
212: .getSources(project)
213: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
214: Set<SourceGroup> testGroups = getTestSourceGroups(project,
215: sourceGroups);
216: List<SourceGroup> result = new ArrayList<SourceGroup>();
217: for (int i = 0; i < sourceGroups.length; i++) {
218: if (!testGroups.contains(sourceGroups[i])) {
219: result.add(sourceGroups[i]);
220: }
221: }
222: return result.<SourceGroup> toArray(new SourceGroup[result
223: .size()]);
224: }
225:
226: private static Set<SourceGroup> getTestSourceGroups(
227: Project project, SourceGroup[] sourceGroups) {
228: Map<FileObject, SourceGroup> foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
229: Set<SourceGroup> testGroups = new HashSet<SourceGroup>();
230: for (int i = 0; i < sourceGroups.length; i++) {
231: testGroups.addAll(getTestTargets(sourceGroups[i],
232: foldersToSourceGroupsMap));
233: }
234: return testGroups;
235: }
236:
237: private static List<SourceGroup> getTestTargets(
238: SourceGroup sourceGroup,
239: Map<FileObject, SourceGroup> foldersToSourceGroupsMap) {
240: final URL[] rootURLs = UnitTestForSourceQuery
241: .findUnitTests(sourceGroup.getRootFolder());
242: if (rootURLs.length == 0) {
243: return new ArrayList<SourceGroup>();
244: }
245: List<SourceGroup> result = new ArrayList<SourceGroup>();
246: List<FileObject> sourceRoots = getFileObjects(rootURLs);
247: for (int i = 0; i < sourceRoots.size(); i++) {
248: FileObject sourceRoot = sourceRoots.get(i);
249: SourceGroup srcGroup = foldersToSourceGroupsMap
250: .get(sourceRoot);
251: if (srcGroup != null) {
252: result.add(srcGroup);
253: }
254: }
255: return result;
256: }
257:
258: private static Map<FileObject, SourceGroup> createFoldersToSourceGroupsMap(
259: final SourceGroup[] sourceGroups) {
260: Map<FileObject, SourceGroup> result;
261: if (sourceGroups.length == 0) {
262: result = Collections.<FileObject, SourceGroup> emptyMap();
263: } else {
264: result = new HashMap<FileObject, SourceGroup>(
265: 2 * sourceGroups.length, .5f);
266: for (int i = 0; i < sourceGroups.length; i++) {
267: SourceGroup sourceGroup = sourceGroups[i];
268: result.put(sourceGroup.getRootFolder(), sourceGroup);
269: }
270: }
271: return result;
272: }
273:
274: private static List<FileObject> getFileObjects(URL[] urls) {
275: List<FileObject> result = new ArrayList<FileObject>();
276: for (int i = 0; i < urls.length; i++) {
277: FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
278: if (sourceRoot != null) {
279: result.add(sourceRoot);
280: } else {
281: int severity = ErrorManager.INFORMATIONAL;
282: if (ErrorManager.getDefault().isNotifiable(severity)) {
283: ErrorManager.getDefault().notify(
284: severity,
285: new IllegalStateException(
286: "No FileObject found for the following URL: "
287: + urls[i])); //NOI18N
288: }
289: }
290: }
291: return result;
292: }
293:
294: private J2eePlatform getJ2eePlatform() {
295: J2eeModuleProvider provider = (J2eeModuleProvider) project
296: .getLookup().lookup(J2eeModuleProvider.class);
297: if (provider != null) {
298: String serverInstanceID = provider.getServerInstanceID();
299: if (serverInstanceID != null
300: && serverInstanceID.length() > 0) {
301: return Deployment.getDefault().getJ2eePlatform(
302: serverInstanceID);
303: }
304: }
305: return null;
306: }
307:
308: private boolean isJWSDPSupported() {
309: J2eePlatform j2eePlatform = getJ2eePlatform();
310: if (j2eePlatform != null) {
311: return j2eePlatform
312: .isToolSupported(J2eePlatform.TOOL_JWSDP);
313: }
314: return false;
315: }
316:
317: private boolean isJsr109Supported() {
318: J2eePlatform j2eePlatform = getJ2eePlatform();
319: if (j2eePlatform != null) {
320: return j2eePlatform
321: .isToolSupported(J2eePlatform.TOOL_JSR109);
322: }
323: return false;
324: }
325:
326: private boolean isJsr109OldSupported() {
327: J2eePlatform j2eePlatform = getJ2eePlatform();
328: if (j2eePlatform != null) {
329: return j2eePlatform
330: .isToolSupported(J2eePlatform.TOOL_WSCOMPILE);
331: }
332: return false;
333: }
334:
335: /**
336: *
337: * <b>DON'T USE</b>, for tests only
338: */
339: Task task;
340: }
|