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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.websvc.saas.util;
041:
042: import com.sun.tools.ws.processor.model.java.JavaParameter;
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.IOException;
046: import java.lang.reflect.GenericArrayType;
047: import java.lang.reflect.ParameterizedType;
048: import java.lang.reflect.Type;
049: import java.util.ArrayList;
050: import java.util.List;
051: import java.util.Properties;
052: import java.util.StringTokenizer;
053: import java.net.URL;
054:
055: /**
056: * Utility method taken from websvc.manager ManagerUtil.java
057: */
058: public class TypeUtil {
059:
060: public static String typeToString(Type type) {
061:
062: if (type instanceof ParameterizedType) {
063: ParameterizedType paramType = (ParameterizedType) type;
064: if (paramType.getOwnerType() != null)
065: return null;
066:
067: Type rawType = paramType.getRawType();
068: if (!(rawType instanceof Class)) {
069: return null;
070: }
071: Class rawClass = (Class) rawType;
072:
073: Type[] argTypes = paramType.getActualTypeArguments();
074: if (argTypes == null || argTypes.length == 0) {
075: return null;
076: }
077:
078: StringBuffer arguments = new StringBuffer();
079: for (int i = 0; i < argTypes.length; i++) {
080: String argument = typeToString(argTypes[0]);
081: if (argument == null) {
082: return null;
083: } else {
084: arguments.append(argument);
085: }
086:
087: if (i != argTypes.length - 1) {
088: arguments.append(',');
089: }
090: }
091:
092: return rawClass.getCanonicalName() + "<"
093: + arguments.toString() + ">";
094: } else if (type instanceof GenericArrayType) {
095: String component = typeToString(((GenericArrayType) type)
096: .getGenericComponentType());
097: if (component != null) {
098: return component + "[]";
099: }
100: } else if (type instanceof Class) {
101: return ((Class) type).getCanonicalName();
102: }
103:
104: return null;
105: }
106:
107: /**
108: * Creates a classpath from a set of properties from the $userdir/build.properties file
109: *
110: * @param srcPath
111: * @param isJaxWS true if the classpath should include the JAX-WS jars, false if JAX-RPC should be used
112: * @return a URL array containing the classpath jars and directories
113: * @throws java.io.IOException
114: */
115: public static List<URL> buildClasspath(File srcPath, boolean isJaxWS)
116: throws IOException {
117: // The classpath needs to be equivalent to (plus the ws client package root):
118: // classpath="${java.home}/../lib/tools.jar:${libs.jaxrpc16.classpath}:${libs.jsf12-support.classpath}"
119: ArrayList<URL> urls = new ArrayList<URL>();
120: Properties properties = new Properties();
121: properties.load(new FileInputStream(System
122: .getProperty("netbeans.user")
123: + "/build.properties"));
124:
125: if (srcPath != null) {
126: urls.add(srcPath.toURI().toURL());
127: }
128:
129: File toolsJar = new File(System.getProperty("java.home"))
130: .getParentFile();
131: toolsJar = new File(toolsJar, "lib" + File.separator
132: + "tools.jar");
133: urls.add(toolsJar.toURI().toURL());
134:
135: String pathSeparator = System.getProperty("path.separator");
136: String longCP = properties
137: .getProperty("libs.jsf12-support.classpath");
138: String libProperty = isJaxWS ? "libs.jaxws21.classpath"
139: : "libs.jaxrpc16.classpath"; // NOI18N
140: longCP = properties.getProperty(libProperty) + pathSeparator
141: + longCP;
142:
143: StringTokenizer st = new StringTokenizer(longCP, pathSeparator);
144:
145: while (st.hasMoreTokens()) {
146: String next = st.nextToken();
147: File nextFile = new File(next);
148: urls.add(nextFile.toURI().toURL());
149: }
150: return urls;
151: }
152:
153: /**
154: * Bug fix: 5059732
155: * This method will return the correct type for the parameter. If the JavaParameter is considered a "Holder"
156: * the holder class name will be used.
157: *
158: * TODO: include in JAX-RPC API
159: *
160: * If the parameter is a "Holder" we need the holder type and not the JavaType. This is
161: * typically the case when there is no return type and the parameter's meant to be mutable, pass-by-reference
162: * type parameters. I took the code below directly from the JAX-RPC class:
163: * "org.netbeans.modules.visualweb.xml.rpc.processor.generator.StubGenerator" except that I'm not checking the Operation for an Array type.
164: * - David Botterill 6/8/2004
165: * @param inParameter The JavaParameter to determine the type for.
166: * @return String representing the class name for the type. A null will be returned if the correct name cannot be resolved.
167: */
168: public static String getParameterType(JavaParameter inParameter) {
169:
170: String parameterType = null;
171: // ClientProcessorEnvironment env = new ClientProcessorEnvironment(new ByteArrayOutputStream(), null, null);
172:
173: if (inParameter.isHolder()) {
174: parameterType = inParameter.getHolderName();
175: } else {
176: parameterType = inParameter.getType().getName();
177: }
178:
179: return parameterType;
180:
181: }
182: }
|