Poster.java
/*
* Copyright © 2011 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.capitals;
import javax.xml.rpc.Stub;
import java.rmi.RemoteException;
/**
* The Poster class does the call to the remote web service
* It creates the stub and fill the parameters
* The call is done in a separate thread and the results
* are delivered asynchronoulsy via PosterListener
*/
public class Poster implements Runnable {
// true if IO thread is running, false if not
private boolean isThreadRunning = false;
// The listener
private PosterListener listener;
private String nation;
private String endPoint;
public Poster(PosterListener listener, String endPoint) {
if (listener == null) {
throw new IllegalArgumentException("Listener cannot be null");
}
this.endPoint = endPoint;
this.listener = listener;
}
/**
* Start the WebService call in a separate thread.
* It checks that only one call is active at the same time
*/
public synchronized void requestCapital(String country) {
if (!isThreadRunning) {
isThreadRunning = true;
this.nation = country;
new Thread(this).start();
}
}
/**
* This is an IO operation and thus is executed in a
* separate thread
*/
public void run() {
CapitalBinding_Stub capitalServiceStub = new CapitalBinding_Stub();
capitalServiceStub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, endPoint);
CapitalPortType capitalService = (CapitalPortType) capitalServiceStub;
try {
String capital = capitalService.getCapital(nation);
listener.onCapitalRequestComplete(nation, capital);
} catch (RemoteException e) {
listener.onCapitalRequestError(e.getMessage());
}
}
}