Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Work with managed resource operations having complex type parameters

Work with managed resource operations having complex type parameters

Author: Saurabh Dravid
email: sadravid@in.ibm.com
Last updated:

Create a capability having this complex type

  • Create a new Manageability Endpoint Project by the name of UserConfigurationModel. All our WSDM artifacts will be stored in this project.

    Wsdl complex type new mep wizard.png

  • For this example we create a capability by importing it from another WSDL file, this wsdl file define an operation "connect".This operation takes a complex type of parameter "UserConfig" and returns the boolean response. Following is the WSDL file (UserConfiguration.wsdl) we used to import the operation for the capability

    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    
    xmlns:tns="http://www.example.org/UserConfiguartion/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserConfiguartion" 
    
    targetNamespace="http://www.example.org/UserConfiguartion/">
      <wsdl:types>
        <xsd:schema targetNamespace="http://www.example.org/UserConfiguartion/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <xsd:element name="Connect">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="userConfig" type="tns:UserConfigType"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="ConnectResponse" type="xsd:boolean"/>
          <xsd:complexType name="UserConfigType">
          	<xsd:sequence>
          		<xsd:element name="UserName" type="xsd:string"/>
          		<xsd:element name="Password" type="xsd:string"/>
          	</xsd:sequence>
          </xsd:complexType>
        </xsd:schema>
      </wsdl:types>
      <wsdl:message name="ConnectRequest">
        <wsdl:part element="tns:Connect" name="parameters"/>
      </wsdl:message>
      <wsdl:message name="ConnectResponse">
        <wsdl:part element="tns:ConnectResponse" name="parameters"/>
      </wsdl:message>
      <wsdl:portType name="UserConfiguartion">
        <wsdl:operation name="Connect">
          <wsdl:input message="tns:ConnectRequest"/>
          <wsdl:output message="tns:ConnectResponse"/>
        </wsdl:operation>
      </wsdl:portType>  
    </wsdl:definitions>
    
  • Create a new capability by specifying following user inputs defined in following screenshot. Click Next.

    Wsdl complex type new cap wizard 1.png

  • Select the "UserConfiguration.wsdl" as the wsdl file to import the operations. Click Next.

    Wsdl complex type new cap wizard 2.png

  • Choose "Connect" operation to be included in the newly created capability. Click Finish.

    Wsdl complex type new cap wizard 3.png

Create a manageable resource type having this capability

  • Create a new Manageable resource type (WSDM resource type) and add the "UserConfiguration" capability to it.

    Wsdl complex type new mrt wizard 1.png

    Wsdl complex type new mrt wizard 2.png

Create a deployment descriptor

  • Right click the newly created "UserConfiguration.mrt" file and select the option Generate DD Template to create a new deployment descriptor from this manageable resource type. This will create a new DD file as "UserConfiguration.dd".

    Wsdl complex type generate dd.png

  • Open the created "UserConfiguration.dd" file and move to the "Custom Serializers" tab. It will show a serializer for the complex type "{http://www.example.org/UserConfiguartion/}UserConfigType".

    Wsdl complex type custom serializer tab.png

  • Double click the defined serializer for complex type. It will bring the following dialog to specify the classes for serializer. Specify the class name for "Serializable Type" field as org.eclipse.UserConfiguration and class name for "Custom Serializer Class" as org.eclipse.UserConfigurationSerializer. Save the DD file.

    Wsdl complex type edit custom serializer dialog.png

Generate code from deployment descriptor

  • Right click the "UserConfiguration.dd" file and select the option Generate Java Code. This will bring a new wizard to do the code generation for defined managed resource type.

    Wsdl complex type codegen wizard.png

Create custom serializers for complex data type

  • Create a new Java class in /UserConfiguration/JavaSource by the name of org.eclipse.UserConfiguration and put the following code in this class. This class will become the Java representation for the defined complex type. We already have defined this class as Serializable Type in the deployment descriptor in one of the previous steps.

    package org.eclipse;
    
    public class UserConfiguration 
    {
    	
    	private String _userName= null;
    	private String _password = null;
    	
    	public UserConfiguration(String userName, String password)
    	{
    		_userName = userName;
    		_password = password;
    	}
    	
    	public String getUserName()
    	{
    		return _userName;
    	}
    	
    	public String getPassword()
    	{
    		return _password;
    	}
    	
    	public String toString()
    	{
    		return "UserName = "+_userName+" \nPassword = "+_password;
    	}
    }
    
  • Apache muse 2.2 provides an interface org.apache.muse.core.serializer for serializing and deserializing objects to and from XML. This interface consists of 3 methods.

    Object fromXML(Element xml) throws SoapFault;
    This method takes a DOM element and deserializes it into Java object.

    Element toXML(Object obj, QName qname) throws SoapFault;
    This method serializes the given Java object into a DOM element and this DOM element will be wrapped inside a root element with the given QName.

    Class getSerializableType();
    It should return the Java class equivalent to the complex type definition.


  • Create a new Java class in /UserConfiguration/JavaSource by the name of org.eclipse.UserConfigurationSerializer and put the following code in this class. This class will serialize and deserialize the object of complex data type.We already have defined this class as Custome Serializer Class in the deployment descriptor in one of the previous steps.

    package org.eclipse;
    
    import javax.xml.namespace.QName;
    
    import org.apache.muse.core.serializer.Serializer;
    import org.apache.muse.util.xml.XmlUtils;
    import org.apache.muse.ws.addressing.soap.SoapFault;
    import org.w3c.dom.Element;
    
    public class UserConfigurationSerializer implements Serializer 
    {
    
    	public static final QName USERNAME_QNAME = new QName(
    			"http://www.example.org/UserConfiguartion/", "UserName");
    	public static final QName PASSWORD_QNAME = new QName(
    			"http://www.example.org/UserConfiguartion/", "Password");
    
    	public Object fromXML(Element arg0) throws SoapFault 
    	{
    		String userName = XmlUtils.getElementText(arg0, USERNAME_QNAME);
    		String password = XmlUtils.getElementText(arg0, PASSWORD_QNAME);
    		UserConfiguration userConfig = new UserConfiguration(userName, password);
    		return userConfig;
    	}
    
    	public Class getSerializableType() 
    	{
    		return UserConfiguration.class;
    	}
    
    	public Element toXML(Object arg0, QName arg1) throws SoapFault 
    	{
    		UserConfiguration userConfig = (UserConfiguration) arg0;
    
    		Element root = XmlUtils.createElement(arg1);
    		XmlUtils.setElement(root, USERNAME_QNAME, userConfig.getUserName());
    		XmlUtils.setElement(root, PASSWORD_QNAME, userConfig.getPassword());
    
    		return root;
    
    	}
    
    }
    
      

Modify generated interface and implementation classes to use the custom serializer

  • Modify the method signature of method "connect" available in org.example.www.UserConfiguartion.IMyCapability interface to
    public boolean connect(UserConfiguration userConfig) throws Exception;
  • Provide the following implementation to the method "connect" of the class org.example.www.UserConfiguartion.MyCapability

        public boolean connect(UserConfiguration userConfig) throws Exception
        {
            if(userConfig.getUserName().equals("User") && userConfig.getPassword().equals("UserAdmin"))
            	return true;
            return false;
        }
        
  • At this point we are done with the modelling of the managed resource and provided implementation to it. Now deploy the generated managed resource to Tomcat server.

Generate proxy for the resource and work with resource

  • Right click the "UserConfiguration.dd" file and select the option Generate Java Code. This will bring a new wizard to do the proxy generation for defined managed resource type.

    Wsdl complex type proxy wizard.png

  • Copy classes org.eclipse.UserConfiguration and org.eclipse.UserConfigurationSerializer from /UserConfiguration/JavaSource and put them into /UserConfigurationProxy/src.

  • Put the following code in the class org.eclipse.www.UserConfiguration.MyServiceProxy (available in /UserConfigurationProxy/src) as a static block. This code will register the custom serializer at the client side serializer registry.

            static
    	{
    		SerializerRegistry registry = SerializerRegistry.getInstance();
    		registry.registerSerializer(UserConfiguration.class, new UserConfigurationSerializer());
    	}
            
  • Modify the method signature of method "connect" in org.eclipse.www.UserConfiguration.MyService interface to
    boolean connect(UserConfiguration userConfig)throws SoapFault;
  • Modify the method signature of method "connect" in org.eclipse.www.UserConfiguration.MyServiceProxy class to
    public boolean connect(UserConfiguration userConfig)throws SoapFault
  • Write and run the following Test class to test the functionality of the managed resource by invoking the "connect" operation and by passing the instance of "UserConfiguration" class to it.

    	package org.eclipse.www.UserConfiguration;
    
    import java.net.URI;
    
    import org.apache.muse.ws.addressing.EndpointReference;
    import org.eclipse.UserConfiguration;
    
    public class Test 
    {
    	public static void main(String[] args) 
    	{
    		//
    		// change these to point to different applications/servers
    		//
    		String serviceURI = "http://localhost:8080/UserConfiguration/services/UserConfiguration";
    
    		//
    		// create EPR for test resource
    		//
    		URI address = URI.create(serviceURI);
    		EndpointReference epr = new EndpointReference(address);
    
    		//
    		// create proxy - turn on tracing of SOAP messages
    		//
    		MyServiceProxy client = new MyServiceProxy(epr);
    		client.setTrace(true);
    
    		try 
    		{
    			UserConfiguration userConfig = new UserConfiguration("User","UserAdmin");
    			boolean connected = client.connect(userConfig);
    			System.out.println("Connection status : " + connected);
    		} 
    		catch (Throwable error) 
    		{
    			error.printStackTrace();
    		}
    	}
    }
    
    	


Comments

Back to the top