1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.tools.metadata;
import java.lang.reflect.Method;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.holders.Holder;
import org.jboss.ws.Constants;
import org.jboss.ws.WSException;
import org.jboss.ws.core.jaxrpc.ParameterWrapping;
import org.jboss.ws.core.utils.HolderUtils;
import org.jboss.ws.metadata.umdm.FaultMetaData;
import org.jboss.ws.metadata.umdm.OperationMetaData;
import org.jboss.ws.metadata.umdm.ParameterMetaData;
import org.jboss.ws.metadata.umdm.WrappedParameter;
import org.jboss.ws.metadata.wsdl.WSDLUtils;
import org.jboss.ws.tools.ToolsUtils;
import org.jboss.ws.tools.Configuration.OperationConfig;
import org.jboss.wsf.common.JavaUtils;
/**
* Builds the Tools Endpoint Meta Data using Java Reflection
*
* @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
* @since Oct 19, 2005
*/
public class ReflectiveMetaDataBuilder
{
private Class seiClass = null;
private ToolsEndpointMetaData tmd = null;
private String targetNamespace = null;
private Map<String, List<OperationConfig>> operationMap = null;
private Map<String, Integer> operationNameCount = new HashMap<String, Integer>();
public ReflectiveMetaDataBuilder(ToolsEndpointMetaData tmd)
{
this.seiClass = tmd.getServiceEndpointInterface();
checkServiceEndpointInterface();
this.targetNamespace = tmd.getPortName().getNamespaceURI();
this.tmd = tmd;
}
public void setOperationMap(Map<String, List<OperationConfig>> operationMap)
{
this.operationMap = operationMap;
}
public ToolsEndpointMetaData generate()
{
generateOperationMetaData(seiClass.getMethods());
return tmd;
}
// PRIVATE METHODS
private void checkServiceEndpointInterface()
{
if (seiClass == null)
throw new IllegalArgumentException("Illegal Null Argument: seiClass");
if (seiClass.isInterface() == false)
throw new IllegalArgumentException("Illegal seiClass : not an interface");
if (! Remote.class.isAssignableFrom(seiClass))
throw new WSException("A service endpoint interface MUST extend java.rmi.Remote: " + seiClass.getName());
}
private FaultMetaData getFaultMetaData(Class exType, OperationMetaData om)
{
String exname = WSDLUtils.getInstance().getJustClassName(exType);
QName xmlName = new QName(tmd.typeNamespace, exname);
FaultMetaData fm = new FaultMetaData(om, xmlName, xmlName, exType.getName());
return fm;
}
private OperationConfig getOperationConfig(String name, Class<?>[] types)
{
if (operationMap == null)
return null;
List<OperationConfig> configs = operationMap.get(name);
if (configs == null)
return null;
for (OperationConfig config : configs)
{
if (config.params.size() == types.length)
{
int i;
for (i = 0; i < types.length; i++)
{
String typeName = config.params.get(i).javaType;
if (! JavaUtils.getSourceName(types[i]).equals(typeName))
break;
}
if (i == types.length)
return config;
}
}
return null;
}
private void generateOperationMetaData(Method[] marr)
{
if (marr == null)
throw new WSException("Number of methods in the seiClass is zero");
for (Method m : marr)
{
String methodname = m.getName();
Class[] paramTypes = m.getParameterTypes();
int len = paramTypes.length;
OperationMetaData om = getOperationMetaData(m);
OperationConfig opc = getOperationConfig(methodname, m.getParameterTypes());
if (opc != null)
om.setOneWay(opc.isOneWay);
ParameterMetaData wrappedParameter = null;
List<WrappedParameter> wrappedParameters = null;
if (om.isDocumentWrapped())
{
QName xmlName = new QName(tmd.typeNamespace, om.getQName().getLocalPart());
QName xmlType = xmlName;
wrappedParameter = new ParameterMetaData(om, xmlName, xmlType, null);
wrappedParameters = new ArrayList<WrappedParameter>(len);
wrappedParameter.setWrappedParameters(wrappedParameters);
om.addParameter(wrappedParameter);
if (! om.isOneWay())
{
xmlType = xmlName = new QName(tmd.typeNamespace, om.getResponseName().getLocalPart());
ParameterMetaData retMetaData = new ParameterMetaData(om, xmlName, xmlType, null);
retMetaData.setWrappedParameters(new ArrayList<WrappedParameter>(0));
om.setReturnParameter(retMetaData);
}
}
for (int i = 0; i < len; i++)
{
Class paramType = paramTypes[i];
/**
* Test if method param extends java.rmi.Remote
*/
if (Remote.class.isAssignableFrom(paramType))
throw new WSException("Param Type " + paramType.getName() + " should not extend java.rmi.Remote");
if (om.isDocumentWrapped() && !isHeaderParameter(opc, i))
{
QName xmlName = getXmlName(paramType, opc, i, null);
wrappedParameters.add(new WrappedParameter(xmlName, paramType.getName(), convertToProperty(xmlName.getLocalPart()), i));
}
else
{
om.addParameter(getParameterMetaData(paramType, om, opc, i));
}
}
// Handle return type
Class returnType = m.getReturnType();
if (void.class != returnType)
{
if (Remote.class.isAssignableFrom(returnType))
throw new WSException("Return Type " + returnType.getName() + " should not extend java.rmi.Remote");
if (om.isDocumentWrapped())
{
QName name = getReturnXmlName(opc, null);
WrappedParameter wrapped = new WrappedParameter(name, returnType.getName(), convertToProperty(name.getLocalPart()), -1);
ParameterMetaData retMetaData = om.getReturnParameter();
retMetaData.getWrappedParameters().add(wrapped);
}
else
{
om.setReturnParameter(getParameterMetaDataForReturnType(returnType, om, opc));
}
}
if (om.isDocumentWrapped())
{
ParameterWrapping.generateWrapper(wrappedParameter, false);
if (! om.isOneWay())
ParameterWrapping.generateWrapper(om.getReturnParameter(), false);
}
Class[] exceptionTypes = m.getExceptionTypes();
boolean remoteExceptionFound = false;
if (exceptionTypes != null)
{
for (int i = 0; i < exceptionTypes.length; i++)
{
if (RemoteException.class.isAssignableFrom(exceptionTypes[i]))
remoteExceptionFound = true;
else
om.addFault(getFaultMetaData(exceptionTypes[i], om));
}
}
if (! remoteExceptionFound)
throw new WSException(m.getName() + " does not throw RemoteException.");
om.assertDocumentBare();
tmd.addOperation(om);
}
}
private OperationMetaData getOperationMetaData(Method m)
{
String methodName = m.getName();
int count = 0;
if (operationNameCount.containsKey(methodName))
count = operationNameCount.get(methodName);
operationNameCount.put(methodName, ++count);
String localName = (count > 1) ? methodName + count : methodName;
OperationMetaData om = new OperationMetaData(tmd, new QName(targetNamespace, localName), methodName);
om.setSOAPAction("");// FIXME:Default SOAP Action
return om;
}
private boolean isHeaderParameter(OperationConfig config, int index)
{
if (config == null)
return false;
return config.params.get(index).header;
}
private ParameterMetaData getParameterMetaData(Class type, OperationMetaData om, OperationConfig config, int index)
{
QName xmlType = ToolsUtils.getXMLType(type, tmd.typeNamespace);
QName xmlName = getXmlName(type, config, index, om.isDocumentBare() ? om.getQName().getLocalPart() : null);
ParameterMetaData pm = new ParameterMetaData(om, xmlName, xmlType, type.getName());
pm.setInHeader(isHeaderParameter(config, index));
String mode = null;
if (config != null)
mode = config.params.get(index).mode;
boolean holder = Holder.class.isAssignableFrom(type);
if (holder)
{
pm.setJavaTypeName(HolderUtils.getValueType(type).getName());
if (mode != null)
{
if (mode.equals("OUT"))
pm.setMode(ParameterMode.OUT);
else if (mode.equals("INOUT"))
pm.setMode(ParameterMode.INOUT);
}
else
{
pm.setMode(ParameterMode.INOUT);
}
}
return pm;
}
private QName getXmlName(Class type, OperationConfig config, int index, String defaultName)
{
if (config != null)
{
QName name = config.params.get(index).xmlName;
if (name != null)
{
if ("".equals(name.getNamespaceURI()))
name = new QName(tmd.typeNamespace, name.getLocalPart());
return name;
}
}
if (defaultName == null)
defaultName = getDefaultName(type) + "_" + (index + 1);
QName xmlName = new QName(tmd.typeNamespace, defaultName);
return xmlName;
}
private QName getReturnXmlName(OperationConfig config, String defaultName)
{
if (config != null)
{
QName name = config.returnXmlName;
if (name != null)
{
if ("".equals(name.getNamespaceURI()))
name = new QName(tmd.typeNamespace, name.getLocalPart());
return name;
}
}
if (defaultName == null)
defaultName = Constants.DEFAULT_RPC_RETURN_NAME;
return new QName(tmd.typeNamespace, defaultName);
}
private ParameterMetaData getParameterMetaDataForReturnType(Class type, OperationMetaData om, OperationConfig config)
{
QName xmlType = ToolsUtils.getXMLType(type, tmd.typeNamespace);
QName xmlName = getReturnXmlName(config, om.isDocumentBare() ? om.getResponseName().getLocalPart() : null);
ParameterMetaData pm = new ParameterMetaData(om, xmlName, xmlType, type.getName());
return pm;
}
private String getDefaultName(Class javaClass)
{
String name = "";
WSDLUtils utils = WSDLUtils.getInstance();
if (Holder.class.isAssignableFrom(javaClass))
javaClass = utils.getJavaTypeForHolder(javaClass);
if (javaClass.isArray())
{
name = utils.getMessagePartForArray(javaClass);
}
else
name = utils.getJustClassName(javaClass);
return name;
}
private String convertToProperty(String variable)
{
if (Character.isUpperCase(variable.charAt(0)))
{
char c = Character.toLowerCase(variable.charAt(0));
StringBuilder builder = new StringBuilder(variable);
builder.setCharAt(0, c);
variable = builder.toString();
}
return variable;
}
}
|