Attributes provide additional information about programming elements. They can be associated with classes, fields or methods.
For example, you can define a custom security attribute that specifies that a given type will be encrypted or digitally signed. You could also define an attribute that specifies that a given class' field corresponds to a particular database column.
In WSO2 SOA Enablement Server, attributes can be defined persistently in a deployment descriptor or at runtime. You can define attributes per service endpoint, service instance or service client. They can be associated with class, field, method or parameter of method.
Example 42 shows a deployment descriptor with the attributes remoteServer, associated with class and service instance, and upperCase, associated with service endpoint and method. The remoteServer attribute defines the remote server on which to publish a service endpoint with a given service instance. The attribute upperCase defines that the given parameter of the echo method will be returned in upper case.
The syntax for specifying the method name is similar to that in Javadoc: method(VarType1,VarType2 ...), where method is the name of the method and VarType1..n are the parameter types, given in fully qualified form.
The number attribute of the <parameter> sub-element specifies the position of the parameter. Parameters are indexed from zero, meaning the first parameter of the method has its number attribute equal to zero. A negative value of number denotes a return method.
Please see the Deployment Descriptor Reference for further information.
Example 42. Deployment Descriptor with Custom Attributes
<?xml version="1.0" encoding="UTF-8"?> <package name="AttributableService" targetNamespace="http://wso2.com/package/example/basics/webservices" version="1.0" xmlns="http://wso2.com/wasp/package/1.2" xmlns:ns0="http://wso2.com/wsdl/example/basics/webservices/" xmlns:tns="http://wso2.com/package/example/basics/webservices" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsd:schemaLocation="http://wso2.com/wasp/package/1.2"> <license>http://wso2.com</license> <location>http://wso2.com</location> <service-endpoint name="AttributableServiceEndpoint" path="/AttributableService" service-instance="tns:AttributableServiceInstance" service-type="java" xml-protocol="soap11"> <wsdl service="ns0:JavaService" uri="AttributableService.wsdl"/> <attributes> <class name="example.basics.webservices.AttributableService"> <method name="echo(java.lang.String)"> <parameter number="0"> <attribute key="upperCase" value=""/> </parameter> </method> </class> </attributes> </service-endpoint> <service-instance implementation-class="example.basics.webservices.AttributableService" instantiation-method="shared" name="AttributableServiceInstance" preload="false"> <attributes> <class name="example.basics.webservices.AttributableService"> <attribute key="remoteServer" value="http://mercury.com:6060"/> </class> </attributes> </service-instance> </package>
At runtime, attributes can be obtained by using the getAttributes() method of the ServiceEndpoint class. This method is also defined in the ServiceInstance and ServiceClient classes.
This is shown in Example 43 Java class with custom attributes.
Example 43. Java Class with Custom Attributes
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.webservices; import org.systinet.wasp.Wasp; import org.systinet.wasp.webservice.Attributes; import org.systinet.wasp.webservice.Current; import org.systinet.wasp.webservice.Initializable; import org.systinet.wasp.webservice.Registry; import org.systinet.wasp.webservice.ServiceInstance; import java.lang.reflect.Method; import java.util.Map; public class AttributableService implements Initializable { public void init(ServiceInstance serviceInstance) { try { // gets service instance attributes Attributes attributes = serviceInstance.getAttributes(); // gets attributes associated with the class Map classAttributes = attributes.get(this.getClass()); if (classAttributes.containsKey("remoteServer")) { // start remote server Wasp.startServer((String) classAttributes.get("remoteServer")); // publish service endpoint Registry.publish( "/AttributableService", this.getClass()); } } catch (Exception e) { e.printStackTrace(); } } public void destroy() { // nothing } public String echo(String echo) { // gets service endpoint attributes Attributes attributes = Current.getServiceEndpointContext() .getServiceEndpoint() .getAttributes(); try { // gets this method Method method = this.getClass() .getMethod( "echo", new Class[] { String.class }); // attributes associated with this method Map methodAttributes = attributes.get(method); if (methodAttributes.containsKey("upperCase")) { // return upper case of echo return echo.toUpperCase(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } return echo; } }