Wednesday, July 13, 2016

slf4j logging -- log4j / logback

Simple Logging Facade for Java (SLF4J) is an API designed to give generic access to many logging frameworks
- log4j
- logback

To decide log4j or logback depends at the time of deployment ,agnostic to code

ex-

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld
{
    public static void main(String[] args)
    {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);
        logger.info("Hello World");
    }
}


For above code all you need is slf4j 

  1. SLF4j binding jar file
  2. Desired logging framework jar files (log4j or logback)
There are reasons to use logback ovr log4j because of all the improvments.
Please read below -





Wednesday, October 14, 2015

Remove Files/ Directory from Git


To Remove a file from Git Added mistakenly

$ git rm -r --cached <wrong-file>

$ git commit -m '(deleting files added mistakenly)'

$ git push

Note :
You should always have a gitignore file like below ,so that you don't add anything accidently.
just create a new file gitignore and copy the below contents to it -

/.project
/target/
/.settings/
/.classpath
*.idea
*.iml

Tuesday, June 23, 2015

React Js -- Modern Framework

React.js (http://facebook.github.io/react/)
It’s a Library created by Facebook and Instagram to create views. It’s not a framework, so let’s not waste time in comparing it with Angular (Apples don’t get compared with Oranges J).it’s just concentrating on view layer ,which it’s good at.

Why it’s fast?
It uses a virtual DOM, and syncs only the changed parts with the underlying page (accessing the DOM is still the slowest part of a modern web application, which is why the framework gets a performance boost by optimizing it).

How to write React?
1.) Create a component using React.createClass and then render
var component = React.createClass({
//your code
}),
render: function() {};
2.) use React.render(<component/>.document.body);


JSX – Allows us to write HTML like syntax which gets transformed to lightweight JavaScript objects.
Virtual DOM  - A JavaScript representation of the actual DOM.
React.createClass – The way in which you create a new component.
render (method) – What we would like our HTML Template to look like.
React.render – Renders a React component to a DOM node.
state – The internal data store (object) of a component.
getInitialState – The way in which you set the initial state of a component.
setState – A helper method for altering the state of a component.
props – The data which is passed to the child component from the parent component.
propTypes – Allows you to control the presence, or types of certain props passed to the child component.
getDefaultProps – Allows you to set default props for your component.
Component LifeCycle
componentWillMount – Fired before the component will mount
componentDidMount – Fired after the component mounted
componentWillReceiveProps – Fired whenever there is a change to props
componentWillUnmount – Fired before the component will unmount
Events
onClick
onSubmit
onChange

simplest React example:
React.render(
        <h1>Hello, world!</h1>,
        document.getElementById('example')
      );



Note :check in Next Blog to Build React app with Grunt.

Tuesday, June 9, 2015

WebService -- SOAP CXF Endpoint Steps without any xml configuration


Intelij Steps :
Go to Project  à New à Maven à create from archtetype  à search for webapp and select maven-archeype-webapp
Give the groupid and artifact id as defined in below pom
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <
parent>
        <
artifactId>test-endpoint</artifactId>
        <
groupId>com.sample.endpoint</groupId>
        <
version>1.0-SNAPSHOT</version>
    </
parent>
    <
modelVersion>4.0.0</modelVersion>

    <
artifactId>test-endpoint-service</artifactId>
    <
properties>
        <
spring.version>4.1.0.RELEASE</spring.version>
        <
spring.security.version>4.0.1.RELEASE</spring.security.version>
        <
hibernate.version>4.3.6.Final</hibernate.version>
        <
cxf.version>3.0.1</cxf.version>
        <
jackson.version>2.4.2</jackson.version>
    </
properties>

    <
dependencies>

        <
dependency>
            <
groupId>${parent.groupId}</groupId>
            <
artifactId>test-endpoint-client</artifactId>
            <
version>1.0-SNAPSHOT</version>
        </
dependency>

        <
dependency>
            <
groupId>org.springframework.security</groupId>
            <
artifactId>spring-security-web</artifactId>
            <
version>${spring.security.version}</version>
        </
dependency>

        <
dependency>
            <
groupId>org.springframework.security</groupId>
            <
artifactId>spring-security-config</artifactId>
            <
version>${spring.security.version}</version>
        </
dependency>




       
<!--CXF -->
       
<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-jaxrs</artifactId>
            <
version>${cxf.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.apache.cxf</groupId>
            <
artifactId>cxf-rt-transports-http</artifactId>
            <
version>${cxf.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.apache.cxf</groupId>
            <
artifactId>cxf-rt-ws-security</artifactId>
            <
version>3.0.2</version>
        </
dependency>


       
<!-- Jackson Dependencies-->
       
<dependency>
            <
groupId>com.fasterxml.jackson.jaxrs</groupId>
            <
artifactId>jackson-jaxrs-json-provider</artifactId>
            <
version>${jackson.version}</version>
        </
dependency>

        <
dependency>
            <
groupId>com.fasterxml.jackson.core</groupId>
            <
artifactId>jackson-core</artifactId>
            <
version>${jackson.version}</version>
        </
dependency>

        <
dependency>
            <
groupId>com.fasterxml.jackson.core</groupId>
            <
artifactId>jackson-annotations</artifactId>
            <
version>${jackson.version}</version>
        </
dependency>

        <
dependency>
            <
groupId>com.fasterxml.jackson.core</groupId>
            <
artifactId>jackson-databind</artifactId>
            <
version>${jackson.version}</version>
        </
dependency>

        <
dependency>
            <
groupId>com.fasterxml.jackson.datatype</groupId>
            <
artifactId>jackson-datatype-hibernate4</artifactId>
            <
version>${jackson.version}</version>
        </
dependency>

       
<!--Spring -->
       
<dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-web</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-beans</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-core</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-context-support</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-context</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-jdbc</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-orm</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-tx</artifactId>
            <
version>${spring.version}</version>
        </
dependency>
       
<!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-web</artifactId>-->
        <!--<version>1.2.3.RELEASE</version>-->
        <!--</dependency>-->

       
<dependency>
            <
groupId>javax.servlet</groupId>
            <
artifactId>javax.servlet-api</artifactId>
            <
version>3.1.0</version>
        </
dependency>

       
<!-- Log4j -->
       
<dependency>
            <
groupId>org.apache.logging.log4j</groupId>
            <
artifactId>log4j-api</artifactId>
            <
version>2.2</version>
        </
dependency>
        <
dependency>
            <
groupId>org.apache.logging.log4j</groupId>
            <
artifactId>log4j-core</artifactId>
            <
version>2.2</version>
        </
dependency>
        <
dependency>
            <
groupId>org.apache.logging.log4j</groupId>
            <
artifactId>log4j-1.2-api</artifactId>
            <
version>2.2</version>
        </
dependency>
        <
dependency>
            <
groupId>org.apache.logging.log4j</groupId>
            <
artifactId>log4j-slf4j-impl</artifactId>
            <
version>2.2</version>
        </
dependency>

       
<!--Test Dependencies -->

       
<dependency>
            <
groupId>org.springframework.data</groupId>
            <
artifactId>spring-data-jpa</artifactId>
            <
version>1.7.0.RELEASE</version>
        </
dependency>
        <
dependency>
            <
groupId>org.springframework</groupId>
            <
artifactId>spring-test</artifactId>
            <
version>3.2.3.RELEASE</version>
            <
scope>test</scope>
        </
dependency>
        <
dependency>
            <
groupId>org.hibernate</groupId>
            <
artifactId>hibernate-core</artifactId>
            <
version>${hibernate.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>org.hibernate</groupId>
            <
artifactId>hibernate-entitymanager</artifactId>
            <
version>${hibernate.version}</version>
        </
dependency>
        <
dependency>
            <
groupId>com.h2database</groupId>
            <
artifactId>h2</artifactId>
            <
version>1.4.182</version>
            <
scope>test</scope>
        </
dependency>
        <
dependency>
            <
groupId>junit</groupId>
            <
artifactId>junit</artifactId>
            <
version>4.11</version>
            <
scope>test</scope>
        </
dependency>
        <
dependency>
            <
groupId>org.mockito</groupId>
            <
artifactId>mockito-all</artifactId>
            <
version>1.9.5</version>
            <
scope>test</scope>
        </
dependency>

    </
dependencies>
    <
build>
        <
plugins>

            <
plugin>
                <
artifactId>maven-war-plugin</artifactId>
                <
configuration>
                    <
archive>
                        <
manifest>
                            <
addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <
addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </
manifest>
                    </
archive>
                    <
failOnMissingWebXml>false</failOnMissingWebXml>
                </
configuration>
            </
plugin>

            <
plugin>
                <
artifactId>maven-compiler-plugin</artifactId>


            </
plugin>
            <
plugin>
                <
groupId>org.codehaus.mojo</groupId>
                <
artifactId>cobertura-maven-plugin</artifactId>
                <
version>2.6</version>
            </
plugin>


            <
plugin>
                <
groupId>org.eclipse.jetty</groupId>
                <
artifactId>jetty-maven-plugin</artifactId>
                <
version>9.2.0.M1</version>
                <
configuration>
                    <
httpConnector>
                        <
port>8044</port>
                    </
httpConnector>
                    <
webApp>
                        <
contextPath>/${build.finalName}</contextPath>
                    </
webApp>

                   
<!--Configure external jetty config -->
                   
<jettyConfig>${jetty.env}</jettyConfig>
                </
configuration>

                <
dependencies>
                   
<!--You need this if you want to do JSP for the view -->
                   
<dependency>
                        <
groupId>org.eclipse.jetty</groupId>
                        <
artifactId>jetty-jsp</artifactId>
                        <
version>9.2.0.M1</version>
                    </
dependency>

                    <
dependency>
                        <
groupId>com.oracle</groupId>
                        <
artifactId>ojdbc6</artifactId>
                        <
version>11.2.0</version>
                    </
dependency>

                    <
dependency>
                        <
groupId>com.mchange</groupId>
                        <
artifactId>c3p0</artifactId>
                       
<!--0.9.2.1 has a bug that throws an NPE in spring -->
                        
<version>0.9.5-pre8</version>
                    </
dependency>
                </
dependencies>
            </
plugin>



        </
plugins>
    </
build>

</
project>


2.) Create a package – com.sample.endpoint
Create a package context under the above package and following classes under under it - 
               ApplicationConfig, ApplicationInitializer, CxfEndPointConfig
 
//****************************************************************************//
package com.sample.endpoint.context;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;

/**
 * Created by sjain on 6/8/2015.
 */

@ComponentScan(basePackages = {"com.sample.endpoint"})
@EnableAspectJAutoProxy

public class ApplicationConfig {
}
 
 
//****************************************************************************//
 
package com.sample.endpoint.context;

import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
/**
 * Created by sjain on 6/8/2015.
 */
public class ApplicationInitializer implements WebApplicationInitializer {


//    protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
//
//
//        //Init Spring application
//        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
//        rootContext.register(ApplicationConfig.class);
//        servletContext.addListener(new ContextLoaderListener(rootContext));
//
//        //Register Servlets
//        ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", new CXFServlet());
//        cxfServlet.setLoadOnStartup(1);
//        cxfServlet.addMapping("/*");
//    }



   
public void onStartup(ServletContext servletContext) throws ServletException {
       
//Init Spring application
        
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationConfig.
class);
        servletContext.addListener(
new ContextLoaderListener(rootContext));

       
//Register Servlets
       
ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", new CXFServlet());
         cxfServlet.setLoadOnStartup(
1);
        cxfServlet.addMapping(
"/*");
    }
}
 
 
//****************************************************************************//
package com.sample.endpoint.context;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.sample.endpoint.service.SampleSOAPEndPoint;
import org.apache.cxf.Bus;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.opensaml.xml.encryption.Public;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import javax.xml.ws.Endpoint;

/**
 * Created by sjain on 6/8/2015.
 */

@Configuration
@ImportResource
("classpath:META-INF/cxf/cxf.xml")
public class CxfEndPointConfig {

   
private static String ENDPOINT_PATH_SOAP_ACCOUNTs= "soap/test/account";

   
@Bean
   
public Endpoint jaxwsTestEndPoint(Bus cxfBus,SampleSOAPEndPoint sampleSOAPEndPoint){

        EndpointImpl endpoint =
new EndpointImpl(cxfBus,sampleSOAPEndPoint);
        endpoint.publish(
ENDPOINT_PATH_SOAP_ACCOUNTs);
       
return endpoint;
    }


}


//****************************************************************************//

3.)  Create a package service under above com.sample.endpoint package and class “SampleSOAPendPointImpl” class
//****************************************************************************//


package com.sample.endpoint.service;



import com.sample.endpoint.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sjain on 6/8/2015.
 */


@WebService(name = "TestAccountDataService", targetNamespace = "http://www.charter.com/test/account")
@Service("testAccountEndpoint")
public class SampleSOAPEndPointImpl implements SampleSOAPEndPoint {


   
@Override
   
public List<Account> getAllAccounts(@WebParam(name = "AccountType") String accountType,@WebParam(name = "AccountId") int accountId) {
        List<Account> accounts =
new ArrayList<Account>();

        Account account =
new Account(1,"savings");
        Account account1 =
new Account(2,"checking");
        Account account2 =
new Account(accountId,accountType);
        accounts.add(account);
        accounts.add(account1);
        accounts.add(account2);

       
return accounts;
}    }

//****************************************************************************//


Steps to Build the project :
1.)      Do a maven clean and maven install
Steps to run the project
You can use jetty webserver to run it , as it is already defined in the pom,
·          select the run debug configuration
·          And select maven from + sign to run the project
·          Browse the working directory of the project
·          Command line write – jetty:run
·          Ok and execute
Now go to browser and run the below urls’

http://localhost:8044/test-endpoint-service-1.0-SNAPSHOT/soap/wss/test/account?wsdl