import com.hp.ucmdb.generated.UcmdbServiceStub;
import org.apache.axis2.AxisFault;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.*;
import org.apache.axis2.transport.http.impl.httpclient3.HttpTransportPropertiesImpl;

import java.net.MalformedURLException;
import java.net.URL;

abstract class Demo {
    static UcmdbServiceStub stub;
    static UcmdbServiceStub.CmdbContext context;


    static {
        try {
            setStub(createUcmdbServiceStub("admin", "W3lcome%"));
            setContext();
        } catch (Exception e) {
//handle exception
        }
    }

    public static UcmdbServiceStub getStub() {
        return stub;
    }

    public static void setStub(UcmdbServiceStub ucmdbStub) {
        stub = ucmdbStub;
    }

    public UcmdbServiceStub.CmdbContext getContext() {
        return context;
    }

    public static void setContext() {
        UcmdbServiceStub.CmdbContext ctx = new UcmdbServiceStub.CmdbContext();
        ctx.setCallerApplication("demo");
        context = ctx;
    }

    //connection to service - for axis2/jibx client
    private static final String PROTOCOL = "http";
    private static final String HOST_NAME = "host_name";
    private static final int PORT = 8080;
    private static final String FILE = "/axis2/services/UcmdbService";

    protected static UcmdbServiceStub createUcmdbServiceStub
            (String username, String password) throws Exception {
        URL url;
        UcmdbServiceStub serviceStub;
        try {
            url = new URL
                    (Demo.PROTOCOL, Demo.HOST_NAME,
                            Demo.PORT, Demo.FILE);
            serviceStub = new UcmdbServiceStub(url.toString());
            HttpTransportPropertiesImpl.Authenticator auth = new HttpTransportPropertiesImpl.Authenticator();
            auth.setUsername(username);
            auth.setPassword(password);

            serviceStub._getServiceClient().getOptions().setProperty
                    (HTTPConstants.AUTHENTICATE, auth);
        } catch (AxisFault axisFault) {
            throw new Exception
                    ("Failed to create SOAP adapter for "
                            + Demo.HOST_NAME, axisFault);
        } catch (MalformedURLException e) {
            throw new Exception
                    ("Failed to create SOAP adapter for "
                            + Demo.HOST_NAME, e);
        }
        return serviceStub;
    }
}