Introduction to sca

Source: Internet
Author: User
Tags sca
SCA is actually a bit like spring, but SCA injects real services and is about to become an industrial standard. The following is a typical calculator in the demo.

Addition interface:

Package calculator;

/**
* The add Service Interface
*/
Public interface addservice {

Double add (double N1, double N2 );

}

Addition implementation:

Package calculator;

/**
* An Implementation of the add service
*/
Public class addserviceimpl implements addservice {

Public double add (double N1, double N2 ){
Return N1 + N2;
}

}

Subtraction interface:

Package calculator;

/**
* The interface for the multiply Service
*/
Public interface subtractservice {

Double subtract (double N1, double N2 );

}
Implementation of subtraction:

Package calculator;

/**
* An Implementation of the subtract service.
*/
Public class subtractserviceimpl implements subtractservice {

Public double subtract (double N1, double N2 ){
Return N1-N2;
}

}

Multiplication interface:

Package calculator;

/**
* The interface for the multiply Service
*/
Public interface multiplyservice {

Double multiply (double N1, double N2 );

}

Multiplication:

Package calculator;

/**
* An Implementation of the multiply service.
*/
Public class multiplyserviceimpl implements multiplyservice {

Public double multiply (double N1, double N2 ){
Return N1 * N2;
}

}

Division interface:

Package calculator;

/**
* The divide Service Interface
*/
Public interface divideservice {

Double divide (double N1, double N2 );

}

Division implementation:

Package calculator;

/**
* An Implementation of the divide service.
*/
Public class divideserviceimpl implements divideservice {

Public double divide (double N1, double N2 ){
Return N1/N2;
}

}

Calculator Interface

Package calculator;

/**
* The calculator service interface.
*/
Public interface calculatorservice {

Double add (double N1, double N2 );

Double subtract (double N1, double N2 );

Double multiply (double N1, double N2 );

Double divide (double N1, double N2 );

}

Calculator implementation

Package calculator;

Import org. osoa. SCA. Annotations. reference;

/**
* An Implementation of the calculator service.
*/
Public class calculatorserviceimpl implements calculatorservice {

Private addservice;
Private subtractservice;
Private multiplyservice;
Private divideservice;

@ Reference
Public void setaddservice (addservice ){
This. addservice = addservice;
}

@ Reference
Public void setsubtractservice (subtractservice ){
This. subtractservice = subtractservice;
}

@ Reference
Public void setdivideservice (divideservice ){
This. divideservice = divideservice;
}

@ Reference
Public void setmultiplyservice (multiplyservice ){
This. multiplyservice = multiplyservice;
}

Public double add (double N1, double N2 ){
Return addservice. Add (N1, N2 );
}

Public double subtract (double N1, double N2 ){
Return subtractservice. Subtract (N1, N2 );
}

Public double multiply (double N1, double N2 ){
Return multiplyservice. Multiply (N1, N2 );
}

Public double divide (double N1, double N2 ){
Return divideservice. Divide (N1, N2 );
}

}

Calculator. Composite Assembly File

<Composite xmlns = "http://www.osoa.org/xmlns/sca/1.0"
Targetnamespace = "http: // sample"
Xmlns: Sample = "http: // sample"
Name = "Calculator">

<Component name = "calculatorservicecomponent">
<Implementation. Java class = "Calculator. calculatorserviceimpl"/>
<Reference name = "addservice" target = "addservicecomponent"/>
<Reference name = "subtractservice" target = "subtractservicecomponent"/>
<Reference name = "multiplyservice" target = "multiplyservicecomponent"/>
<Reference name = "divideservice" target = "divideservicecomponent"/>
</Component>

<Component name = "addservicecomponent">
<Implementation. Java class = "Calculator. addserviceimpl"/>
</Component>

<Component name = "subtractservicecomponent">
<Implementation. Java class = "Calculator. subtractserviceimpl"/>
</Component>

<Component name = "multiplyservicecomponent">
<Implementation. Java class = "Calculator. multiplyserviceimpl"/>
</Component>

<Component name = "divideservicecomponent">
<Implementation. Java class = "Calculator. divideserviceimpl"/>
</Component>

</Composite>

Client usage

Package calculator;

Import org. Apache. Tuscany. SCA. Host. Embedded. scadomain;

/**
* This client program shows how to create an SCA runtime, start it,
* And locate and invoke a SCA component
*/
Public class calculatorclient {
Public static void main (string [] ARGs) throws exception {

Scadomain = scadomain. newinstance ("Calculator. Composite ");

Calculatorservice =
Scadomain. getservice (calculatorservice. class, "calculatorservicecomponent ");

// Calculate
System. Out. println ("3 + 2 =" + calculatorservice. Add (3, 2 ));
System. Out. println ("3-2 =" + calculatorservice. Subtract (3, 2 ));
System. Out. println ("3*2 =" + calculatorservice. Multiply (3, 2 ));
System. Out. println ("3/2 =" + calculatorservice. Divide (3, 2 ));

Scadomain. Close ();
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.