Commit ec32b948 authored by Administrator's avatar Administrator

Adds advance examples

These examples include the use of JAX-B advanced features.
parent f2553ac4
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<groupId>es.uvigo.esei.dai.ws</groupId> <groupId>es.uvigo.esei.dai.ws</groupId>
<artifactId>calculator_service</artifactId> <artifactId>calculator_service</artifactId>
<version>1.0.0</version> <version>1.1.0</version>
<name>Ejemplos de DAI - Servicios Web: Calculator Service</name> <name>Ejemplos de DAI - Servicios Web: Calculator Service</name>
<inceptionYear>2014</inceptionYear> <inceptionYear>2014</inceptionYear>
......
package es.uvigo.esei.dai.ws.calculator; package es.uvigo.esei.dai.ws.calculator;
import java.util.Collection;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedResults;
import jakarta.jws.WebMethod; import jakarta.jws.WebMethod;
import jakarta.jws.WebService; import jakarta.jws.WebService;
@WebService @WebService
// @SOAPBinding(style = Style.RPC)
public interface CalculatorService { public interface CalculatorService {
@WebMethod @WebMethod
public double add(double op1, double op2); public double add(double op1, double op2);
...@@ -16,5 +19,38 @@ public interface CalculatorService { ...@@ -16,5 +19,38 @@ public interface CalculatorService {
public double multiply(double op1, double op2); public double multiply(double op1, double op2);
@WebMethod @WebMethod
public double divide(double op1, double op2) throws CalculatorException; public double divide(double op1, double op2)
throws CalculatorException;
@WebMethod
public double operate(Operation operation, double op1, double op2)
throws CalculatorException;
@WebMethod
public double serialOperate(Operation operation, double ... op)
throws CalculatorException;
@WebMethod
public double arrayOperate(Operation operation, double[] op)
throws CalculatorException;
@WebMethod
public double listOperate(Operation operation, Collection<Double> op)
throws CalculatorException;
/*
* JAXB no soporta esta declaración de método porque hace uso de Map,
* que es una interfaz y JAXB no es capaz de trabajar con interfaces
* directamente.
*
* @WebMethod
* public Map<String, Double> mapOperate(
* Map<String, Operation> operations, double op1, double op2
* ) throws CalculatorException;
*/
@WebMethod
public NamedResults mapOperate(
NamedOperations operations, double op1, double op2
) throws CalculatorException;
} }
...@@ -2,9 +2,14 @@ package es.uvigo.esei.dai.ws.calculator; ...@@ -2,9 +2,14 @@ package es.uvigo.esei.dai.ws.calculator;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedResults;
import jakarta.xml.ws.Service; import jakarta.xml.ws.Service;
public class CalculatorServiceClient { public class CalculatorServiceClient {
...@@ -20,10 +25,37 @@ public class CalculatorServiceClient { ...@@ -20,10 +25,37 @@ public class CalculatorServiceClient {
final CalculatorService calculator = service.getPort(CalculatorService.class); final CalculatorService calculator = service.getPort(CalculatorService.class);
System.out.println("Basic methods results");
System.out.println(calculator.add(20d, 30d)); System.out.println(calculator.add(20d, 30d));
System.out.println(calculator.subtract(20d, 30d)); System.out.println(calculator.subtract(20d, 30d));
System.out.println(calculator.multiply(20d, 0d)); System.out.println(calculator.multiply(20d, 30d));
System.out.println(calculator.divide(20d, 30d)); System.out.println(calculator.divide(20d, 30d));
System.out.println("\nOperate results");
System.out.println(calculator.operate(Operation.ADD, 20d, 30d));
System.out.println(calculator.operate(Operation.SUB, 20d, 30d));
System.out.println(calculator.operate(Operation.MUL, 20d, 30d));
System.out.println(calculator.operate(Operation.DIV, 20d, 30d));
System.out.println("\nCollections results");
System.out.println("Serial: " + calculator.serialOperate(Operation.ADD, 10d, 20d, 30d));
System.out.println("Array: " + calculator.arrayOperate(Operation.ADD, new double[]{10d, 20d, 30d}));
System.out.println("List: " + calculator.listOperate(Operation.ADD, Arrays.asList(10d, 20d, 30d)));
System.out.println("\nMap results");
final Map<String, Operation> operations = new HashMap<String, Operation>();
for (Operation operation : Operation.values()) {
operations.put(operation.name(), operation);
}
final NamedResults results = calculator.mapOperate(
new NamedOperations(operations), 20d, 30d
);
for (Map.Entry<String, Double> entry : results.getResults().entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("\nException");
try { try {
System.out.println(calculator.divide(20d, 0d)); System.out.println(calculator.divide(20d, 0d));
} catch (CalculatorException e) { } catch (CalculatorException e) {
......
package es.uvigo.esei.dai.ws.calculator; package es.uvigo.esei.dai.ws.calculator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedOperations;
import es.uvigo.esei.dai.ws.calculator.adapters.NamedResults;
import jakarta.jws.WebService; import jakarta.jws.WebService;
@WebService(endpointInterface = "es.uvigo.esei.dai.ws.calculator.CalculatorService") @WebService(endpointInterface = "es.uvigo.esei.dai.ws.calculator.CalculatorService")
public class CalculatorServiceImpl implements CalculatorService { public class CalculatorServiceImpl implements CalculatorService {
@Override @Override
public double add(double op1, double op2) { public double add(double op1, double op2) {
return op1 + op2; return this.operate(Operation.ADD, op1, op2);
} }
@Override @Override
public double subtract(double op1, double op2) { public double subtract(double op1, double op2) {
return op1 - op2; return this.operate(Operation.SUB, op1, op2);
} }
@Override @Override
public double multiply(double op1, double op2) { public double multiply(double op1, double op2) {
return op1 * op2; return this.operate(Operation.MUL, op1, op2);
} }
@Override @Override
public double divide(double op1, double op2) throws CalculatorException { public double divide(double op1, double op2) throws CalculatorException {
if (op2 == 0d) return this.operate(Operation.DIV, op1, op2);
throw new CalculatorException("op2 can't be zero"); }
@Override
public double operate(Operation operation, double op1, double op2)
throws CalculatorException {
return operation.calculate(op1, op2);
}
@Override
public double serialOperate(Operation operation, double ... op)
throws CalculatorException {
if (op.length <= 1) {
throw new CalculatorException("There must be, at least, two operands");
} else {
double result = op[0];
for (int i = 1; i < op.length; i++) {
result = operation.calculate(result, op[i]);
}
return result;
}
}
@Override
public double arrayOperate(Operation operation, double[] op)
throws CalculatorException {
return this.serialOperate(operation, op);
}
@Override
public double listOperate(Operation operation, Collection<Double> op)
throws CalculatorException {
if (op.size() <= 1) {
throw new CalculatorException("There must be, at least, two operands");
} else {
final List<Double> values = new ArrayList<Double>(op);
double result = values.get(0);
for (int i = 1; i < op.size(); i++) {
result = operation.calculate(result, values.get(i));
}
return result;
}
}
/*
* Ejemplo de implementación para declaración no válida de "mapOperate"
*
* @Override
* public Map<String, Double> mapOperate(
* Map<String, Operation> operations, double op1, double op2
* ) throws CalculatorException {
* final Map<String, Double> results = new HashMap<String, Double>();
*
* for (Map.Entry<String, Operation> entry : operations.entrySet()) {
* results.put(entry.getKey(), entry.getValue().calculate(op1, op2));
* }
* return results;
* }
*/
@Override
public NamedResults mapOperate(
NamedOperations operations, double op1, double op2
) throws CalculatorException {
final NamedResults results = new NamedResults();
final Map<String, Operation> operationsMap = operations.getOperations();
final Map<String, Double> resultsMap = results.getResults();
for (Map.Entry<String, Operation> entry : operationsMap.entrySet()) {
resultsMap.put(entry.getKey(), entry.getValue().calculate(op1, op2));
}
return op1 / op2; return results;
} }
} }
package es.uvigo.esei.dai.ws.calculator;
public enum Operation {
ADD, SUB, MUL, DIV;
public double calculate(double op1, double op2)
throws CalculatorException {
switch(this) {
case ADD: return op1 + op2;
case SUB: return op1 - op2;
case MUL: return op1 * op2;
case DIV:
if (op2 == 0d) {
throw new CalculatorException(
"op2 can't be zero", "op2 can't be zero"
);
} else {
return op1 / op2;
}
default:
throw new CalculatorException(
"Unknown operation: " + this,
"Unknown operation: " + this
);
}
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.HashMap;
import java.util.Map;
import es.uvigo.esei.dai.ws.calculator.Operation;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class NamedOperations {
private Map<String, Operation> operations;
public NamedOperations() {
this.operations = new HashMap<String, Operation>();
}
public NamedOperations(Map<String, Operation> operations) {
super();
this.operations = operations;
}
@XmlJavaTypeAdapter(NamedOperationsMapAdapter.class)
public Map<String, Operation> getOperations() {
return operations;
}
public void setOperations(Map<String, Operation> operations) {
this.operations = operations;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.Map;
import es.uvigo.esei.dai.ws.calculator.Operation;
public class NamedOperationsEntry {
private String key;
private Operation value;
public NamedOperationsEntry() {
}
public NamedOperationsEntry(String key, Operation value) {
this.key = key;
this.value = value;
}
public NamedOperationsEntry(Map.Entry<String, Operation> entry) {
this(entry.getKey(), entry.getValue());
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Operation getValue() {
return value;
}
public void setValue(Operation value) {
this.value = value;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import es.uvigo.esei.dai.ws.calculator.Operation;
public class NamedOperationsMap {
private List<NamedOperationsEntry> entries;
public NamedOperationsMap() {
this.entries = new LinkedList<NamedOperationsEntry>();
}
public NamedOperationsMap(Map<String, Operation> map) {
this();
for (Map.Entry<String, Operation> entry : map.entrySet()) {
this.entries.add(new NamedOperationsEntry(entry));
}
}
public Map<String, Operation> createMap() {
final Map<String, Operation> map = new HashMap<String, Operation>();
for (NamedOperationsEntry entry : this.entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
public List<NamedOperationsEntry> getEntries() {
return entries;
}
public void setEntries(List<NamedOperationsEntry> entries) {
this.entries = entries;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.Map;
import es.uvigo.esei.dai.ws.calculator.Operation;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
public class NamedOperationsMapAdapter
extends XmlAdapter<NamedOperationsMap, Map<String, Operation>> {
@Override
public Map<String, Operation> unmarshal(NamedOperationsMap v) {
return v.createMap();
}
@Override
public NamedOperationsMap marshal(Map<String, Operation> v) {
return new NamedOperationsMap(v);
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.HashMap;
import java.util.Map;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class NamedResults {
private Map<String, Double> results;
public NamedResults() {
this.results = new HashMap<String, Double>();
}
public NamedResults(Map<String, Double> results) {
this.results = results;
}
@XmlJavaTypeAdapter(NamedResultsMapAdapter.class)
public Map<String, Double> getResults() {
return results;
}
public void setResults(Map<String, Double> results) {
this.results = results;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.Map;
public class NamedResultsEntry {
private String key;
private Double value;
public NamedResultsEntry() {
}
public NamedResultsEntry(String key, Double value) {
this.key = key;
this.value = value;
}
public NamedResultsEntry(Map.Entry<String, Double> entry) {
this(entry.getKey(), entry.getValue());
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class NamedResultsMap {
private List<NamedResultsEntry> entries;
public NamedResultsMap() {
this.entries = new LinkedList<NamedResultsEntry>();
}
public NamedResultsMap(Map<String, Double> map) {
this();
for (Map.Entry<String, Double> entry : map.entrySet()) {
this.entries.add(new NamedResultsEntry(entry));
}
}
public Map<String, Double> createMap() {
final Map<String, Double> map = new HashMap<String, Double>();
for (NamedResultsEntry entry : this.entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
public List<NamedResultsEntry> getEntries() {
return entries;
}
public void setEntries(List<NamedResultsEntry> entries) {
this.entries = entries;
}
}
package es.uvigo.esei.dai.ws.calculator.adapters;
import java.util.Map;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
public class NamedResultsMapAdapter
extends XmlAdapter<NamedResultsMap, Map<String, Double>> {
@Override
public Map<String, Double> unmarshal(NamedResultsMap v) {
return v.createMap();
}
@Override
public NamedResultsMap marshal(Map<String, Double> v) {
return new NamedResultsMap(v);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment