The SCA specification defines a set of SCA Java annotations that enable service components and service clients to be built in the Java programming language.(more information here).
This section describes how to use the SCA Java annotations on the previous helloworld POJO example.
First, the PrintService Java interface implemented by the server
is described with the <service/>
tag from lines 22 to
24 of the helloworld-pojo.composite
file, in the section called “Hello World SCA composite”. These lines can be omitted when using
the @Service
annotation on the PrintService
Java interface:
package org.ow2.frascati.examples.helloworld.annotated;
/** A basic service used to print messages. */
@Service
public interface PrintService {
void print(String msg);
}
Concerning the server component, its Java implementation can also
be annotated for the description of its property (header), described
line 26 in the SCA composite of the section called “Hello World SCA composite”.
This annotation exempts also the developper to write getters and setters
for SCA properties defined with this annotation. Thus, the
Server
Java class now looks like:
package org.ow2.frascati.examples.helloworld.annotated;
/** The print service implementation. */
public class Server implements PrintService {
@Property
protected String header = "->";
private int count = 1;
/** Default constructor. */
public Server() {
System.out.println("SERVER created.");
}
/** PrintService implementation. */
public void print(final String msg) {
System.out.println("SERVER: begin printing...");
for (int i = 0; i < count; ++i) {
System.out.println(header + msg);
}
System.out.println("SERVER: print done.");
}
}
The Java modifier of an SCA annotated property can't be
private
: it must be protected
at
least.
The last annotation we will add to this example is
@Reference
. This annotation represents the
<reference />
tag (lines 14 to 16 of
helloworld-pojo.composite
in the previous section.
The client component also provides a runnable service named
r
. Then, the annotated Java class for the client component
is:
package org.ow2.frascati.examples.helloworld.annotated;
/** A print service client. */
public class Client implements Runnable {
@Reference
protected PrintService s;
/** Default constructor. */
public Client() {
System.out.println("CLIENT created.");
}
/** Run the client. */
public void run() {
s.print("Hello World!");
}
}
With these annotations, the helloworld composite file is now much more shorter:
1 <?xml version="1.0" encoding="ISO-8859-15"?> 2 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" 3 name="helloworld-annotated"> 4 5 <service name="r" promote="client/r"/> 6 7 <component name="client"> 8 <implementation.java class="org.ow2.frascati.examples.helloworld.annotated.Client"/> 9 </component> 10 11 <component name="server"> 12 <implementation.java class="org.ow2.frascati.examples.helloworld.annotated.Server"/> 13 </component> 14 15 </composite> 16