Thursday, March 20, 2014

Reducing IF's Using Array



public class TooMuchIf {


       public static voidmain(String[] args) {

              TooMuchIf tmi = new TooMuchIf();

              System.out.println(tmi.fightMath(3, 3));

              System.out.println(tmi.fightMathArray(3, 3));

       }

       public int fightMath(int one, int two) {

           int result = -1;

           if(one == 0 && two == 0) { result = 0; }

           else if(one == 0 && two == 1) { result = 0; }

           else if(one == 0 && two == 2) { result = 1; }

           else if(one == 0 && two == 3) { result = 2; }

           else if(one == 1 && two == 0) { result = 0; }

           else if(one == 1 && two == 1) { result = 0; }

           else if(one == 1 && two == 2) { result = 2; }

           else if(one == 1 && two == 3) { result = 1; }

           else if(one == 2 && two == 0) { result = 2; }

           else if(one == 2 && two == 1) { result = 1; }

           else if(one == 2 && two == 2) { result = 3; }

           else if(one == 2 && two == 3) { result = 3; }

           else if(one == 3 && two == 0) { result = 1; }

           else if(one == 3 && two == 1) { result = 2; }

           else if(one == 3 && two == 2) { result = 3; }

           else if(one == 3 && two == 3) { result = 3; }


           return result;

       }
      

       public intfightMathArray(int one, int two) {

             
              final int[][] result = new int[][] {{ 0, 0, 1, 2 }, { 0, 0, 2, 1 }, { 2, 1, 3, 3 }, { 1, 2, 3, 3 }};

              returnresult[one][two];

       }



}

Friday, June 28, 2013

Creating a simple webservice using JAX-WS

This example creates a simple webservice using JAX-WS



Message.java 

package edu.demo.ws;

importjavax.jws.WebMethod;
importjavax.jws.WebService;

@WebService
public class Message {

      @WebMethod
      public String getMessage(String text) {
            return "Message " + text;
      }
}

Main.java


package edu.demo.ws;

importjavax.xml.ws.Endpoint;

public class Main {

      public static void main(String[] args) {
            Endpoint.publish("http://localhost:8181/message", new Message());

      }

}

Run the Main.java as Java Application and in the browser open the url  http://localhost:8181/message?wsdl



<?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.demo.edu/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.demo.edu/" name="MessageService">
- <types>
- <xsd:schema>
 <xsd:import namespace="http://ws.demo.edu/" schemaLocation="http://localhost:8181/message?xsd=1" />
 </xsd:schema>
 </types>
- <message name="getMessage">
 <part name="parameters" element="tns:getMessage" />
 </message>
- <message name="getMessageResponse">
 <part name="parameters" element="tns:getMessageResponse" />
 </message>
- <portType name="Message">
- <operation name="getMessage">
 <input message="tns:getMessage" />
 <output message="tns:getMessageResponse" />
 </operation>
 </portType>
- <binding name="MessagePortBinding" type="tns:Message">
 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="getMessage">
 <soap:operation soapAction="" />
- <input>
 <soap:body use="literal" />
 </input>
- <output>
 <soap:body use="literal" />
 </output>
 </operation>
 </binding>
- <service name="MessageService">
- <port name="MessagePort" binding="tns:MessagePortBinding">
 <soap:address location="http://localhost:8181/message" />
 </port>
 </service>
 </definitions>

  Use the URL in the above wsdl under SchemaLocation http://localhost:8181/message?xsd=1



 <?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <xs:schema xmlns:tns="http://ws.demo.edu/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://ws.demo.edu/">
 <xs:element name="getMessage" type="tns:getMessage" />
 <xs:element name="getMessageResponse" type="tns:getMessageResponse" />
- <xs:complexType name="getMessage">
- <xs:sequence>
 <xs:element name="arg0" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
- <xs:complexType name="getMessageResponse">
- <xs:sequence>
 <xs:element name="return" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
 </xs:schema>


Use SoapUI or Webservices Explorer in eclipse, post the Wsdl link and you should be able to use your first webservice