package org.ow2.jonas.ws.axis2.test.jaxws.ejb3;


import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.soap.SOAPFaultException;

@WebService(name="jaxwsbean",
            serviceName="jaxwsbean",
            wsdlLocation="META-INF/wsdl/Jaxwsbean.wsdl")

@Stateless(mappedName="JAXWSBean")
@Remote(JAXWSGreeter.class)
@HandlerChain(file="handlers.xml", name = "Handler")
@SOAPBinding(style=SOAPBinding.Style.RPC,
             use=SOAPBinding.Use.LITERAL,
             parameterStyle=SOAPBinding.ParameterStyle.WRAPPED
)
public class JAXWSBean implements JAXWSGreeter {

    public String greetMe(final String me) {
        System.out.println("i'm a ejb ws: " + me);
        if (!"foo bar".equals(me)) {
            throw new RuntimeException("Wrong parameter");
        }
        return "Hello " + me;
    }

    public void greetMeFault(final String me) {
        System.out.println("generate SOAP fault");
        SOAPFault fault = null;
        try {
            fault = SOAPFactory.newInstance().createFault();
            fault.setFaultCode(new QName("http://foo", "MyFaultCode"));
            fault.setFaultString("my error");
            fault.setFaultActor("my actor");
        } catch (SOAPException ex) {
            throw new RuntimeException(ex);
        }

        throw new SOAPFaultException(fault);
    }

}
