Saturday, March 31, 2012

Prototype Property in JavaScript

The prototype property allows us to add properties and methods to an object.
Please note that we are not talking about the Prototype JavaScript Framework  implemented in  prototype.js


Prototype Property
and  Dynamic inheritance:


The functions in JavaScript are objects and
prototype is one of is  property.
   

  • You cause a class to inherit using ChildClassName.prototype = new ParentClass();.
  • You need to remember to reset the constructor property for the class using ChildClassName.prototype.constructor=ChildClassName.
  • You can call ancestor class methods which your child class has overridden using the Function.call() method.
  • Javascript does not support protected methods.

 Eg.
 
>>>var boo = function(){};>>>typeof boo.prototype
returns "object"


Prototypes can be augmented
>>> boo.prototype.a = 1;
>>> boo.prototype.sayAh = function(){};

Prototypes can be overwritten
>>> boo.prototype = {a: 1, b: 2};


How is the prototype used?

when a function is invoked as a constructor

var Person = function(name) {
  this.name = name;
};
Person.prototype.say = function() {
  return this.name;
}


>>> var dude = new Person('dude');
>>> dude.name;
"dude"
>>> dude.say();
"dude"


say() is a property of the prototype object
but it behaves as if it's a property of the dude object

Can we tell the difference? Own properties vs. prototype’s:

>>> dude.hasOwnProperty('name');
true
>>> dude.hasOwnProperty('say');
false


isPrototypeOf():

>>> Person.prototype.isPrototypeOf(dude);
true
>>> Object.prototype.isPrototypeOf(dude);
true

__proto__:

__proto__ is a secret link to the prototype The secret link is exposed in Firefox it does not exist in Internet Explorer,
__proto__ is not the same as prototype. __proto__ is a property of the instances, whereas prototype is a property of the constructor functions.

>>> dude.__proto__.hasOwnProperty('say')
true
>>> dude.prototype
??? // Trick question
>>> dude.__proto__.__proto__.hasOwnProperty('toString')
true

In Object oriented client side programming prototype property is highly used.Inheretence can be achieved by prototyping.

Exception Handling in Javascript


Let's talk about Exception Handling in Javascript

We can use try catch and finally ver welll in our client side code The Error object in all browsers support the following two properties:
name: The name of the error, or more specifically, the name of the constructor function the error belongs to.
message: A description of the error, with this description varying depending on the browser.

inbuilt Exceptions  :
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
   
   
Throwing your own errors (exceptions)
   
    throw new Error("Oh oh, an error has occured");
     
    throw new SyntaxError("Your syntax is no good");
   
    throw{
 name: "JavaScriptKit Error",
 message: "Error detected. Please contact webmaster"
}
   
function addClass(element, className){
    if (element != null && typeof element.className == "string"){
        element.className += " " + className;
    } else {
        throw new Error("addClass(): First arg must be a DOM element.");
    }
}

try catch and finally exmaple:

try{
 undefinedfunction()
 alert('I guess you do exist')
}
catch(e){
 alert('An error has occurred: '+e.message)
}
finally{
 alert('I am alerted regardless of the outcome above')
}

composite primary key using one of two annotations: @IdClass or @EmbeddedId.

@Entity
@AssociationOverride( name="logRequest.fileName", joinColumns = { @JoinColumn(name="log_request_file_name") } )
public class Reporting {

    @EmbeddedId
    private ReportingFile logRequest;

    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable(name = "t_reports", schema="", joinColumns = {@JoinColumn(name = "log_report")})
    @Fetch(FetchMode.SELECT)
    private List<ReportingFile> reports;

    @Column(name="generated_date",nullable=true)
    private Date generatedDate;

    [...]
}

@Embeddable
public class ReportingFile {

    @Column(name="file_name",length=255)
    private String fileName;

    @Column(name="xml_content")
    private Clob xmlContent;

    [...]
}
 

Monday, March 26, 2012

CXF Webservice Java to WSDL Implementation

Java To WSDL

whereTypes defined in separate xsd.

Service Endpoint Interface

package com;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService ( targetNamespace = "http://techyhouse.blogspot.com", name = "DemoService" )
public interface DemoService
{
      @WebMethod
      public void testOne(TestVO testVO) throws TestException;
     
      @WebMethod
      public String testTwo(String name) throws TestException;
     
      @WebMethod
      public TestVO testThree(String name) throws TestException;

}



Service Implementation



package com;

import javax.jws.WebService;

@WebService(targetNamespace = "http://techyhouse.blogspot.com", serviceName = "DemoService",
      endpointInterface = "com.DemoService", portName = "DemoServicePort")
public class DemoServiceImpl implements DemoService
{

      @Override
      public void testOne ( TestVO testVO ) throws TestException{}

      @Override
      public String testTwo ( String name ) throws TestException{
            return null;
      }

      @Override
      public TestVO testThree ( String name ) throws TestException{
            return null;
      }
}
 
Java Beans  



package com;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType( XmlAccessType.FIELD )
@XmlType
public class TestVO implements Serializable
{
      private static final long serialVersionUID  = 5367711816L;
     
      @XmlElement(required=true, defaultValue="1", nillable=false)
      private Integer id;
     
      @XmlElement(required=false, nillable=true)
      private String name;
     
      @XmlElement(required=false)
      private String[] data;

      public Integer getId () { return id; }

      public void setId ( Integer id ){ this.id = id; }

      public String getName (){ return name; }

      public void setName ( String name ) { this.name = name; }

      public String[] getData (){ return data;  }

      public void setData ( String[] data ){ this.data = data; }
}
  
Exception classes

package com;

public class TestException extends Exception
{
      private static final long     serialVersionUID  = 8319892150394168546L;
}


Maven configuration

<properties><cxf.version>2.4.1</cxf.version></properties>
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
</dependency>
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
</dependency>
<plugin>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-java2ws-plugin</artifactId>
      <version>${cxf.version}</version>
      <dependencies>
            <dependency>
                  <groupId>org.apache.cxf</groupId>
                  <artifactId>cxf-rt-frontend-jaxws</artifactId>
                  <version>${cxf.version}</version>
            </dependency>
            <dependency>
                  <groupId>org.apache.cxf</groupId>
                  <artifactId>cxf-rt-frontend-simple</artifactId>
                  <version>${cxf.version}</version>
            </dependency>
      </dependencies>

      <executions>
            <execution>
                  <id>process-classes</id>
                  <phase>process-classes</phase>
                  <configuration>
                        <className>com.DemoServiceImpl</className>
                        <genWsdl>true</genWsdl>
                        <verbose>true</verbose>
                        <argline> -createxsdimports </argline>
                  </configuration>
                  <goals>
                        <goal>java2ws</goal>
                  </goals>
            </execution>
      </executions>
</plugin>
  


 After the maven project is built, check the targer/generated/wsdl folder for the wsdl and xsd.

DemoServiceImpl.wsdl


<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions name="DemoService" targetNamespace="http://techyhouse.blogspot.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://techyhouse.blogspot.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

  <wsdl:types>

<schema xmlns="http://www.w3.org/2001/XMLSchema">

  <import namespace="http://techyhouse.blogspot.com" schemaLocation="DemoServiceImpl_schema1.xsd"/>

</schema>

  </wsdl:types>

  <wsdl:message name="testThreeResponse">

    <wsdl:part name="parameters" element="tns:testThreeResponse">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="testTwoResponse">

    <wsdl:part name="parameters" element="tns:testTwoResponse">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="TestException">

    <wsdl:part name="TestException" element="tns:TestException">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="testOne">

    <wsdl:part name="parameters" element="tns:testOne">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="testTwo">

    <wsdl:part name="parameters" element="tns:testTwo">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="testOneResponse">

    <wsdl:part name="parameters" element="tns:testOneResponse">

    </wsdl:part>

  </wsdl:message>

  <wsdl:message name="testThree">

    <wsdl:part name="parameters" element="tns:testThree">

    </wsdl:part>

  </wsdl:message>

 
   <wsdl:portType name="DemoService">

    <wsdl:operation name="testThree">

      <wsdl:input name="testThree" message="tns:testThree">

    </wsdl:input>

      <wsdl:output name="testThreeResponse" message="tns:testThreeResponse">

    </wsdl:output>

      <wsdl:fault name="TestException" message="tns:TestException">

    </wsdl:fault>

    </wsdl:operation>

    <wsdl:operation name="testTwo">

      <wsdl:input name="testTwo" message="tns:testTwo">

    </wsdl:input>

      <wsdl:output name="testTwoResponse" message="tns:testTwoResponse">

    </wsdl:output>

      <wsdl:fault name="TestException" message="tns:TestException">

    </wsdl:fault>

    </wsdl:operation>

    <wsdl:operation name="testOne">

      <wsdl:input name="testOne" message="tns:testOne">

    </wsdl:input>

      <wsdl:output name="testOneResponse" message="tns:testOneResponse">

    </wsdl:output>

      <wsdl:fault name="TestException" message="tns:TestException">

    </wsdl:fault>

    </wsdl:operation>

  </wsdl:portType>

  <wsdl:binding name="DemoServiceSoapBinding" type="tns:DemoService">

    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

    <wsdl:operation name="testThree">

      <soap:operation soapAction="" style="document"/>

      <wsdl:input name="testThree">

        <soap:body use="literal"/>

      </wsdl:input>

      <wsdl:output name="testThreeResponse">

        <soap:body use="literal"/>

      </wsdl:output>

      <wsdl:fault name="TestException">

        <soap:fault name="TestException" use="literal"/>

      </wsdl:fault>

    </wsdl:operation>

    <wsdl:operation name="testTwo">

      <soap:operation soapAction="" style="document"/>

      <wsdl:input name="testTwo">

        <soap:body use="literal"/>

      </wsdl:input>

      <wsdl:output name="testTwoResponse">

        <soap:body use="literal"/>

      </wsdl:output>

      <wsdl:fault name="TestException">

        <soap:fault name="TestException" use="literal"/>

      </wsdl:fault>

    </wsdl:operation>        

   
    <wsdl:operation name="testOne">

      <soap:operation soapAction="" style="document"/>

      <wsdl:input name="testOne">

        <soap:body use="literal"/>

      </wsdl:input>

      <wsdl:output name="testOneResponse">

        <soap:body use="literal"/>

      </wsdl:output>

      <wsdl:fault name="TestException">

        <soap:fault name="TestException" use="literal"/>

      </wsdl:fault>

    </wsdl:operation>

  </wsdl:binding>

  <wsdl:service name="DemoService">

    <wsdl:port name="DemoServicePort" binding="tns:DemoServiceSoapBinding">

      <soap:address location="http://localhost:9090/DemoServicePort"/>

    </wsdl:port>

  </wsdl:service>

</wsdl:definitions>





DemoServiceImpl_Schema.xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:tns="http://techyhouse.blogspot.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://techyhouse.blogspot.com">

  <xs:element name="testOne" type="tns:testOne"/>

  <xs:element name="testOneResponse" type="tns:testOneResponse"/>

  <xs:element name="testThree" type="tns:testThree"/>

  <xs:element name="testThreeResponse" type="tns:testThreeResponse"/>

  <xs:element name="testTwo" type="tns:testTwo"/>

  <xs:element name="testTwoResponse" type="tns:testTwoResponse"/>

  <xs:complexType name="testThree">

    <xs:sequence>

      <xs:element minOccurs="0" name="arg0" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testThreeResponse">

    <xs:sequence>

      <xs:element minOccurs="0" name="return" type="tns:testVO"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testVO">

    <xs:sequence>

      <xs:element default="1" name="id" type="xs:int"/>

      <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>

      <xs:element maxOccurs="unbounded" minOccurs="0" name="data" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testTwo">

    <xs:sequence>

      <xs:element minOccurs="0" name="arg0" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testTwoResponse">

    <xs:sequence>

      <xs:element minOccurs="0" name="return" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testOne">

    <xs:sequence>

      <xs:element minOccurs="0" name="arg0" type="tns:testVO"/>

    </xs:sequence>

  </xs:complexType>

  <xs:complexType name="testOneResponse">

    <xs:sequence/>

  </xs:complexType>

  <xs:complexType name="TestException">

    <xs:sequence/>

  </xs:complexType>

  <xs:element name="TestException" type="tns:TestException"/>

</xs:schema>