You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by de...@apache.org on 2005/10/12 11:14:48 UTC
svn commit: r314833 - in
/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2:
databinding/utils/ADBPullParser.java rpc/receivers/
rpc/receivers/BeanSerializer.java rpc/receivers/RPCMessageReceiver.java
rpc/receivers/SimpleTypeMapper.java
Author: deepal
Date: Wed Oct 12 02:14:33 2005
New Revision: 314833
URL: http://svn.apache.org/viewcvs?rev=314833&view=rev
Log:
Implemented a RPCMessageReceiver and which can work with
1. Any simple type
2. Any JavaBean
(not all complete , there are some validation has to be done )
Added:
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/BeanSerializer.java
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/SimpleTypeMapper.java
Modified:
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java?rev=314833&r1=314832&r2=314833&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/utils/ADBPullParser.java Wed Oct 12 02:14:33 2005
@@ -1,6 +1,7 @@
package org.apache.axis2.databinding.utils;
import org.apache.axis2.databinding.ADBBean;
+import org.apache.axis2.util.BeanSerializerUtil;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
@@ -158,7 +159,7 @@
ADBBean adbBean = (ADBBean) object;
childPullParser = (ADBPullParser) adbBean.getPullParser((QName) o);
} else {
-
+ childPullParser = (ADBPullParser) BeanSerializerUtil.getPullParser(object, (QName) o );
}
accessingChildPullParser = true;
return this.next();
Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/BeanSerializer.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/BeanSerializer.java?rev=314833&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/BeanSerializer.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/BeanSerializer.java Wed Oct 12 02:14:33 2005
@@ -0,0 +1,102 @@
+package org.apache.axis2.rpc.receivers;
+import org.apache.axis2.om.OMElement;
+import org.apache.axis2.AxisFault;
+
+import java.util.Iterator;
+import java.util.HashMap;
+import java.lang.reflect.Field;
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*
+*/
+
+/**
+ * Author: Deepal Jayasinghe
+ * Date: Oct 12, 2005
+ * Time: 10:36:58 AM
+ */
+public class BeanSerializer {
+
+ private Class beanClass;
+ private OMElement beanElement;
+ private HashMap fields;
+
+ public BeanSerializer(Class beanClass, OMElement beanElement) {
+ this.beanClass = beanClass;
+ this.beanElement = beanElement;
+ this.fields = new HashMap();
+ fillMethods();
+ }
+
+ public Object deserilze() throws AxisFault {
+ Object beanObj ;
+ try {
+ beanObj = beanClass.newInstance();
+ Iterator elements = beanElement.getChildren();
+ while (elements.hasNext()) {
+ OMElement parts = (OMElement) elements.next();
+ String partsLocalName = parts.getLocalName();
+
+ //getting the setter field
+ Field field =(Field)fields.get(partsLocalName);
+ if(field == null){
+ throw new AxisFault("User Error , In vaild bean ! field does not exist " + "set" +
+ partsLocalName);
+ } else {
+ Class parameters = field.getType();
+ Object partObj = SimpleTypeMapper.getSimpleTypeObject(parameters,parts);
+ if(partObj == null){
+ // Assuming paramter itself as a bean
+ partObj = new BeanSerializer(parameters,parts).deserilze();
+ }
+// Object [] parms = new Object[]{partObj};
+ field.setAccessible(true);
+ field.set(beanObj,partObj);
+ field.setAccessible(false);
+ }
+
+
+ }
+ } catch (InstantiationException e) {
+ throw new AxisFault("InstantiationException : " + e);
+ } catch (IllegalAccessException e) {
+ throw new AxisFault("IllegalAccessException : " + e);
+ }
+ return beanObj;
+ }
+
+ /**
+ * This will fill the hashmap by getting all the methods in a given class , since it make easier
+ * to acess latter
+ */
+ private void fillMethods(){
+
+ Field [] fields = beanClass.getDeclaredFields();
+ for (int i = 0; i < fields.length; i++) {
+ Field field = fields[i];
+ this.fields.put(field.getName(),field);
+ }
+
+
+//
+// Method[] methods = beanClass.getMethods();
+// for (int i = 0; i < methods.length; i++) {
+// Method method = methods[i];
+// fields.put(method.getName(),method);
+// }
+ }
+
+}
Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java?rev=314833&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/RPCMessageReceiver.java Wed Oct 12 02:14:33 2005
@@ -0,0 +1,175 @@
+package org.apache.axis2.rpc.receivers;
+
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.util.BeanSerializerUtil;
+import org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver;
+import org.apache.axis2.soap.SOAPEnvelope;
+import org.apache.axis2.om.OMElement;
+import org.apache.axis2.om.OMNamespace;
+import org.apache.axis2.om.OMAbstractFactory;
+import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
+import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
+import org.apache.axis2.engine.DependencyManager;
+import org.apache.axis2.description.OperationDescription;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import java.lang.reflect.Method;
+import java.util.Iterator;
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*
+*/
+
+/**
+ * Author: Deepal Jayasinghe
+ * Date: Oct 11, 2005
+ * Time: 3:43:38 PM
+ */
+public class RPCMessageReceiver extends AbstractInOutSyncMessageReceiver {
+
+
+ private Method method;
+
+ /**
+ * reflect and get the Java method
+ * - for each i'th param in the java method
+ * - get the first child's i'th child
+ * -if the elem has an xsi:type attr then find the deserializer for it
+ * - if not found,
+ * lookup deser for th i'th param (java type)
+ * - error if not found
+ * - deserialize & save in an object array
+ * - end for
+ * <p/>
+ * - invoke method and get the return value
+ * <p/>
+ * - look up serializer for return value based on the value and type
+ * <p/>
+ * - create response msg and add return value as grand child of <soap:body>
+ *
+ * @param inMessage
+ * @param outMessage
+ * @throws AxisFault
+ */
+
+ public void invokeBusinessLogic(MessageContext inMessage, MessageContext outMessage) throws AxisFault {
+ try {
+ // get the implementation class for the Web Service
+ //todo namespace , checking
+ Object obj = getTheImplementationObject(inMessage);
+
+ Class ImplClass = obj.getClass();
+ DependencyManager.configureBusinessLogicProvider(obj, inMessage, null);
+
+ OperationDescription op = inMessage.getOperationContext()
+ .getAxisOperation();
+ if (op == null) {
+ throw new AxisFault(
+ "Operation is not located, if this is doclit style the SOAP-ACTION should " +
+ "specified via the SOAP Action to use the RawXMLProvider");
+ }
+ String methodName = op.getName().getLocalPart();
+ Method[] methods = ImplClass.getMethods();
+ //todo method validation has to be done
+ for (int i = 0; i < methods.length; i++) {
+ if (methods[i].getName().equals(methodName)) {
+ this.method = methods[i];
+ break;
+ }
+ }
+
+
+ Class[] parameters = method.getParameterTypes();
+ int paracount = 0;
+ int numberofparas = parameters.length;
+
+ Object [] objarray = new Object[numberofparas];
+ OMElement methodElement = inMessage.getEnvelope().getBody()
+ .getFirstElement();
+ Iterator parts = methodElement.getChildren();
+ /**
+ * Take the number of paramters in the method and , only take that much of child elements
+ * from the OMElement , other are ignore , as an example
+ * if the method is , foo(String a , int b)
+ * and if the OMElemet
+ * <foo>
+ * <arg0>Val1</arg0>
+ * <arg1>Val2</arg1>
+ * <arg2>Val3</arg2>
+ *
+ * only the val1 and Val2 take into account
+ */
+ while (parts.hasNext() && paracount < numberofparas) {
+ OMElement omElement = (OMElement) parts.next();
+ Class parameter = parameters[paracount];
+ Object simpleObj = SimpleTypeMapper.getSimpleTypeObject(parameter, omElement);
+ if (simpleObj != null) {
+ objarray[paracount] = simpleObj;
+ } else {
+ //Handle only the JavaBean
+ simpleObj = new BeanSerializer(parameter, omElement).deserilze();
+ objarray[paracount] = simpleObj;
+ }
+ paracount ++;
+ }
+ Object reS = method.invoke(obj, objarray);
+
+ // Handling the response
+ OMNamespace ns = getSOAPFactory().createOMNamespace(
+ "http://soapenc/", "res");
+ SOAPEnvelope envelope = getSOAPFactory().getDefaultEnvelope();
+ OMElement bodyContent = getSOAPFactory().createOMElement(
+ method.getName() + "Response", ns);
+ if (reS == null) {
+ // No return type , has to send a emepty body message , no need to add element to the
+ // body element
+ } else {
+ if (SimpleTypeMapper.isSimpleType(reS)) {
+ OMElement child = getSOAPFactory().createOMElement("arg0", null);
+ child.addChild(fac.createText(child, reS.toString()));
+ bodyContent.addChild(child);
+ } else {
+ XMLStreamReader xr = BeanSerializerUtil.getPullParser(reS, new QName("return"));
+ StAXOMBuilder stAXOMBuilder =
+ OMXMLBuilderFactory.createStAXOMBuilder(
+ OMAbstractFactory.getSOAP11Factory(), xr);
+ OMElement documentElement = stAXOMBuilder.getDocumentElement();
+
+ if (documentElement != null) {
+ bodyContent.addChild(documentElement);
+ }
+ /**
+ * Else first has to check whethere a serilizer is there to serilize the object
+ */
+ }
+ }
+ if (bodyContent != null) {
+ envelope.getBody().addChild(bodyContent);
+ }
+ outMessage.setEnvelope(envelope);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw AxisFault.makeFault(e);
+ }
+ }
+
+
+}
Added: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/SimpleTypeMapper.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/SimpleTypeMapper.java?rev=314833&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/SimpleTypeMapper.java (added)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/rpc/receivers/SimpleTypeMapper.java Wed Oct 12 02:14:33 2005
@@ -0,0 +1,112 @@
+package org.apache.axis2.rpc.receivers;
+import org.apache.axis2.om.OMElement;
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*
+*/
+
+/**
+ * Author: Deepal Jayasinghe
+ * Date: Oct 12, 2005
+ * Time: 10:50:22 AM
+ */
+public class SimpleTypeMapper {
+
+ private static final String STRING = "java.lang.String";
+ private static final String INT = "int";
+ private static final String BOOLEAN = "boolean";
+ private static final String BYTE = "byte";
+ private static final String DOUBLE = "double";
+ private static final String SHORT = "short";
+ private static final String LONG = "long";
+ private static final String FLOAT = "float";
+ private static final String CHAR = "char";
+
+ public static Object getSimpleTypeObject(Class paramter , OMElement value){
+ if(paramter.getName().equals(STRING)){
+ return value.getText();
+ } else if (paramter.getName().equals(INT)){
+ return new Integer(value.getText());
+ } else if (paramter.getName().equals(BOOLEAN)){
+ return Boolean.valueOf(value.getText());
+ } else if (paramter.getName().equals(BYTE)){
+ return new Byte(value.getText());
+ } else if (paramter.getName().equals(DOUBLE)){
+ return new Double(value.getText());
+ } else if (paramter.getName().equals(SHORT)){
+ return new Short(value.getText());
+ } else if (paramter.getName().equals(LONG)){
+ return new Long(value.getText());
+ } else if (paramter.getName().equals(FLOAT)){
+ return new Float(value.getText());
+ } else if (paramter.getName().equals(CHAR)){
+ return new Character(value.getText().toCharArray()[0]);
+ } else {
+ return null;
+ }
+ }
+
+ public static boolean isSimpleType(Object obj){
+ String objClassName =obj.getClass().getName();
+ if(objClassName.equals(STRING)){
+ return true;
+ } else if (objClassName.equals(INT)){
+ return true;
+ } else if (objClassName.equals(BOOLEAN)){
+ return true;
+ } else if (objClassName.equals(BYTE)){
+ return true;
+ } else if (objClassName.equals(DOUBLE)){
+ return true;
+ } else if (objClassName.equals(SHORT)){
+ return true;
+ } else if (objClassName.equals(LONG)){
+ return true;
+ } else if (objClassName.equals(FLOAT)){
+ return true;
+ } else if (objClassName.equals(CHAR)){
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public static boolean isSimpleType(Class obj){
+ String objClassName =obj.getName();
+ if(objClassName.equals(STRING)){
+ return true;
+ } else if (objClassName.equals(INT)){
+ return true;
+ } else if (objClassName.equals(BOOLEAN)){
+ return true;
+ } else if (objClassName.equals(BYTE)){
+ return true;
+ } else if (objClassName.equals(DOUBLE)){
+ return true;
+ } else if (objClassName.equals(SHORT)){
+ return true;
+ } else if (objClassName.equals(LONG)){
+ return true;
+ } else if (objClassName.equals(FLOAT)){
+ return true;
+ } else if (objClassName.equals(CHAR)){
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+}