Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

OAuth 2.0

OAuth 2.0 is an open authorization framework and mainly focuses on authorization flows fand secures access to many well-known web APIs.

Java 8 features

The key features of JDK 8 are Project Lambda (JSR 335), the Nashorn JavaScript Engine, a new Date and Time API (JSR 310), a set of Compact Profiles and the removal of the "permanent generation" from the HotSpot Java Virtual Machine (JVM). A complete list of the new features and capabilities of JDK 8 is available.

Lambda Expression

A lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body.

SOAP vs RESTful

SOAP : Web service use XML messages that follow the Simple Object Access Protocol (SOAP) standard.
RESTful : Web service, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions.

Softwares Engineering / SDLC

Software engineers apply the principles of engineering to the design, development, maintenance, testing, and evaluation of the software and systems that make computers or anything containing software work.

Tuesday, November 25, 2014

E-Books : Design Patterns

Head First Design Pattern


 

 Click to Download or View

 



Monday, October 20, 2014

Software Engineering Videos

Saturday, October 18, 2014

E - Books : Spring

Spring in Action

 Click to Download or View

 

Struts 2 Videos

  • The struts 2 framework is used to develop MVC-based web application.
  • Struts 2 is the combination of webwork framework of opensymphony and struts 1.
                            Struts2 = Webwork + Struts1
  • The struts framework was initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001.
  • The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.

Click on the links to Download or Watch

01 - Introduction To MVC
02 - Introduction to Struts 2
03 - Setting Up
04 - Writing a Struts 2 Application - Part 1
04 - Writing a Struts 2 Application - Part 2
05 - Understanding Namespaces
06 - A Tag And A Business Service
07 - The ValueStack


Tuesday, September 30, 2014

AngularJS : Introduction and Hello example





Angular JS is a JavaScript MVC framework created by Google that lets you build well structured, easily testable, and maintainable front-end applications that makes it easier to implement RIA web applications.

What is AngularJS?
AngularJS is based on the MVC pattern (Model View Control). Therefore AngularJS separates your RIA application into models, views and controllers.
The views are specified using HTML + AngularJS's own template language.
The models and controllers are specified via JavaScript objects and JavaScript functions.
Thus, the views are specified declaratively, as HTML normally , and the models and controllers are specified imperatively, as JavaScript normally is.
If you don't know the difference between declarative and imperative programming, don't worry. It is not important to know before learning AngularJS. Besides, it is pretty simple to find the definition on the web.

And Why Should I Use It?

  • If you haven’t tried AngularJS yet, you’re missing out. The framework consists of a tightly integrated toolset that will help you build well structured, rich client-side applications in a modular fashion—with less code and more flexibility.
  • AngularJS extends HTML by providing directives that add functionality to your markup and allow you to create powerful dynamic templates. You can also create your own directives, crafting reusable components that fill your needs and abstracting away all the DOM manipulation logic.
  • It also implements two-way data binding, connecting your HTML (views) to your JavaScript objects (models) seamlessly. In simple terms, this means that any update on your model will be immediately reflected in your view without the need for any DOM manipulation or event handling (e.g., with jQuery).
  • Angular provides services on top of XHR that dramatically simplify your code and allow you to abstract API calls into reusable services. With that, you can move your model and business logic to the front-end and build back-end agnostic web apps.
  • Finally, I love Angular because of its flexibility regarding server communication. Like most JavaScript MVC frameworks, it lets you work with any server-side technology as long as it can serve your app through a RESTful web API. But Angular also provides services on top of XHR that dramatically simplify your code and allow you to abstract API calls into reusable services. As a result, you can move your model and business logic to the front-end and build back-end agnostic web apps. In this post, we’ll do just that, one step at a time. 
Example : Hello , AngularJS 
 













Note  : The {{  }} are a declarative way of specifying data binding locations in the HTML. AngularJS will automatically update this text whenever the yourName property changes.

Watch another example


Monday, September 29, 2014

Hibernate Videos


  • Hibernate is an Object-Relational Mapping(ORM) solution for JAVA, providing a framework for mapping an object-oriented domain model to a traditional relational database.
  • It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application.
  • Hibernate maps Java classes to database tables and from Java data types to SQL data types.
  • Hibernate sits between traditional Java objects and database server to handle all the work in persisting those objects based on the appropriate O/R mechanisms and patterns.

Click on the links to Download or Watch

01 - Setup
02 - Setup  
03 - Customize cfg.xml  
04 - Our First Class  
05 - Create table from Class  
06 - Schema Change  
07 - Insert record (object)  
08 - Chapter 1 Review 1  
09 - more annotations

10 - Auto Generate Primary key 
11 - One class to Two tables
12 - Two classes to One table 
13 - Compound Primary Key 
14 - Inheritance Mapping
15 - One to One Mapping 
16 - One to One Bi-directional
17 - One to Many Mapping  
18 - Many to Many Mapping

Thursday, July 3, 2014

Unmarshalling and Marshalling

Unmarshalling is the process of binding the XML document using JAXB compiler and generating mapping java classes then constructing the instances with values available in XML document.

Marshalling is the reverse process of it. First we construct the instances and then write a XML document using the constructed instances.

In both the cases, we need create POJO classes first. If we have the XML schema that can be used to generate those classes using a JAXB binding compiler. If the XML schema is not available, then we should manually code those POJOs. Only then we can do Marshalling or Unmarshalling in an application.

Marshalling and Unmarshalling can be done with other sources. That means, the XML need not be in file form. It can be a InputStream object, a URL, a DOM node, SAXSource.


To Unmarshal 
  1. Create POJOs or bind the schema and generate the classes.
  2. Create a JAXBContext object. (javax.xml.bind.JAXBContext)
  3. Create an Unmarshaller object. (javax.xml.bind.Unmarshaller)
  4. Call the unmarshal method.
  5. Use the get methods available in schema-genearated classes to access the values.
 
 
To Marshal
Marshalling provides a client application the ability to convert a JAXB-derived Java object tree back into XML data. By default, the Marshaller uses UTF-8 encoding when generating XML data. Client applications are not required to validate the Java content tree before marshalling. There is also no requirement that the Java content tree be valid with respect to its original schema to marshal it back into XML data

  1. Create POJOs or bind the schema and generate the classes.
  2. Create the content tree by using set methods.
  3. Create a JAXBContext object. (javax.xml.bind.JAXBContext)
  4. Create a Marshaller object. (javax.xml.bind.Marshaller)
  5. Call the marshal method to persist the created content tree as XML document. 
 
 
Validation:
It is the process of verifying that an XML document meets all the constraints expressed in the schema. JAXB 1.0 provided validation at unmarshal time and also enabled on-demand validation on a JAXB content tree. JAXB 2.0 only allows validation at unmarshal and marshal time.

 
Advantages :
  1. JAXB requires a DTD.
  2. Using JAXB ensures the validity of your XML.
  3. A JAXB parser is actually faster than a generic SAX parser.
  4. A tree created by JAXB is smaller than a DOM tree.
  5. It’s much easier to use a JAXB tree for application-specific code.
  6. You can modify the tree and save it as XML.
Dis-Advantages:
  1. JAXB requires a DTD.
  2. Hence, you cannot use JAXB to process generic XML (for example, if you are writing an XML editor or other tool).
  3. You must do additional work up front to tell JAXB what kind of tree you want it to construct.
  4. But this more than pays for itself by simplifying your application. 

JAXB : Architecture & Binding Process

 
XML Schema : An XML schema is a definition file for a XML document & uses XML syntax to describe the relationships among elements, attributes and entities in an XML document. It maintains the rule/syntax for a XML document. An XML document need not always have a schema. While using JAXB XML schema is not a mandatory requirement. Process can start with java pojo classes. But it is convenient to have associated XML schema so that we can use a binding compiler and generate the java classes.

Binding Customizations : By default, the JAXB binding compiler binds Java classes and packages to a source XML schema based on rules. In most cases, the default binding rules are sufficient to generate a robust set of schema-derived classes from a wide range of schemas.

Binding Compiler : The JAXB binding compiler is the core of the JAXB processing model. Its function is to transform, or bind, a source XML schema to a set of JAXB content classes in the Java programming language.

Implementation of javax.xml.bind
: The JAXB binding framework implementation is a runtime API that provides interfaces for unmarshalling, marshalling, and validating XML content in a Java application. The binding framework comprises interfaces in the javax.xml.bind package.

Schema-Derived Classes: These are the schema-derived classes generated by the binding JAXB compiler. The specific classes will vary depending on the input schema.

Java Application : In the context of JAXB, a Java application is a client application that uses the JAXB binding framework to unmarshal XML data, validate and modify Java content objects, and marshal Java content back to XML data.

XML Input Documents: XML content that is unmarshalled as input to the JAXB binding framework -- that is, an XML instance document, from which a Java representation in the form of a content tree is generated.

XML Output Documents : XML content that is marshalled out to an XML document. In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema.


Binding Process :
 
 
 
Generate classes: An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema.

Compile classes
: All of the generated classes, source files, and application code must be compiled.

UnMarshal: XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework.

Note : JAXB also supports unmarshalling XML data from sources other than files/documents, such as DOM nodes, string buffers, SAX Sources, and so forth.

Generate content tree: The unmarshalling process generates a content tree of data objects instantiated from the generated JAXB classes; this content tree represents the structure and content of the source XML documents.

Validate (optional) : The unmarshalling process optionally involves validation of the source XML documents before generating the content tree. Note that if you modify the content tree in Step 6, below, you can also use the JAXB Validate operation to validate the changes before marshalling the content back to an XML document.

Process content : The client application can modify the XML data represented by the Java content tree by means of interfaces generated by the binding compiler.

Marshal : The processed content tree is marshalled out to one or more XML output documents. The content may be validated before marshalling.


JAXB : Introduction & Features

JAXB is an acronym of Java Architecture for XML Binding. 
  • JAXB provides API to access and process a XML document.
  • We can read or write XML files using JAXB.
  • SAX and DOM are generic XML parsers& they will parse any well-structured XML. 
  • JAXB creates a parser that is specific to your DTD.
  • A JAXB parser will parse only valid XML (as defined by your DTD).DOM and JAXB both produce a tree in memory.
  • DOM produces a generic tree, everything is a Node.JAXB produces a tree of Objects with names and attributes as described by your DTD.
Features of JAXB :
  • Ease of development
  • Typed access to XML content
  • Leverage Java SE 5 features
  • 100% XML Schema support
  • Java classes to XML Schema binding
  • Data binding for Java API for XML Web Services (JAX-WS)
  • Schema evolution
  • Provide optional on-demand validation of XML documents
  • Hides the complexity of DOM/SAX APIs
  • Is portable (A JAXB application should be able to utilize different JAXB implementations by just regenerating the schema classes and doesn't require change to the actual application code.)
http://javapapers.com/wp-content/uploads/2012/07/jaxb.png

Friday, June 27, 2014

XML Schema using JAXB in Eclipse IDE

Tuesday, June 24, 2014

WSDL Explaination


WSDL describes Web Services
WSDL stands for Web Services Description Language.
WSDL is a document written in XML. The document describes a Web service. It specifies the location of the service and the operations (or methods) the service exposes.
A WSDL document is just a simple XML document.
It contains set of definitions to describe a web service.
WSDL should have methods, input arguments for methods & return type for methods

Let’s take an example with one method

If we mention @WebMethod(exclude=true), it is not going to publish this method when ever access specified is public also
1) portType: This tag is  an element, contains  name attribute of service class name[ProductCatalog].
Inside Operations the sub element ,contains the name attribute of service class, service method name[getProductCategories()].

In Operation tag, two tags  are defined which takes input and output .

Here Input  is the Request  and sends like getProductCateogriesRequest.

Here Output  is the Response  and sends like getProductCateogriesResponse.

If we have multiples arguments  defined in method, there are bundled together as a message.
Every input is takes a message & output is given as message

Operation takes I/p as one message and o/p as one message

2) types : if we have custom data types ,  we need to define it over here in this types tags & input  and output are referenced for these types.

3)message : Here message has as attribute name which is same as operation name & this message is declared in input tag under message attribute & in output message with suffix at last Response &  message has a sub element tag with name part and name as parameters which are the arguments in the service method

4)binding : WSDL defines  some binding’s information , how web service accepts these request values & gives response

We have element called
 

[which means this web service accepts SOAP request over http]

5) service  : it’s a list of ports ,

where service  name is  suffixed with the name  service at last

(ProductCatalog{Service}) ==> ProductCatalogService

sub element tag is port where name is  suffixed with the name  Port at last

(ProductCatalog{Port}) ==> ProductCatalogPort

& binding attribute also  and we have another sub element for port

i.e...  

this is the location where web service is available & Service has a Port, which follows the binding

Server : Glassfish

1)When ever the server see a class with @WebService annotation, it’s going to create a Service & a Port with class name  & the ports has bindings how the web service is going get  called  with soap over http.

2) Coming to methods they are annotated with @WebMethod, its gone create operations for that , so port consists of operations And it consist of message of types & types are defined

  • Service consists port
  • port defines bindings
  • bindings  points to portType & which has operations
  • operations has an input & output & I/p & o/p message
  • message refers to types
  • types are defined in own section.




SOAP:JAX-WS

Web services are client and server applications that communicate over the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP). As described by the World Wide Web Consortium (W3C), web services provide a standard means of interoperating between software applications running on a variety of platforms and frameworks. Web services are characterized by their great interoperability and extensibility, as well as their machine-processable descriptions, thanks to the use of XML. Web services can be combined in a loosely coupled way to achieve complex operations. Programs providing simple services can interact with each other to deliver sophisticated added-value services. 
  • Web service can be written in any language java, .net, c++, python etc..;
  • Web service standard in interoperability (java web service can call .net code) & (java code can call .net web service)
Web services use XML messages that follow the Simple Object Access Protocol (SOAP) standard, an XML language defining a message architecture and message formats. Such systems often contain a machine-readable description of the operations offered by the service, written in the Web Services Description Language (WSDL), an XML language for defining interfaces syntactically.
 
E.g. let’s take getProducts () as a method as created a business service for it & this business service is deployed in your application server.
Here A, B & C are this .classes in the same application server & they are free to call this java  method to get the list of products .And if u want to provide this feature to users, so u could have like MVC Module that makes this available as a web application
 So the user can actually enter the URL and can see the list of products on their web browser, which is actually making call to this web service method getProducts(), so the MVC module  has a call to getProducts() & shows the products in a nice HTML designed format.
Lets take another server 2  in another machine , which contains one deployed application, and from this application we want to call this getProducts () method & show list of all products in this application.
Yes this is possible by packaging the business service method in .jar file, and deploying this jar file in this server2 & calling the business method   which is available in the jar file & this getProducts() is calling to a database, which has the list of products & this database is hosted along the sever 1 .

Dis-advantages:
  1. Server 2 has different database structure &  here data base which is hosted with server 1 has different products list which we are not interested.
  2. And when we create a version 2 for  getProducts(), we need to re- package the .jar file  & application need to rebuild and redeploy in server 2, by this way the new functionality is provided in machine server 2
Advantages:
  1. If the getProducts() call could directly call the business service method which is hosted in server 1,if it would directly call the instance this would be the best to get the service & this instance is directly access to the database & when there is a version change & functionality change we will have the latest list of products.
  2. This directly call will be worked only by using WEB SERVICE. Here it will allow 2 different machines and 2 different codes to be able to talk to each other over the network by using web service technology.