that were shone when I got tempered!

Posts Tagged ‘tomcat

Tomcat 8 Important Tips

leave a comment »

1. You can start the tomcat using by running as follows.

/[path to tomcat]/apache-tomcat-8.0.8/bin/ run.

2. If there’s an error check the localhost log (eg: localhost.2014-06-29.log) in /[path to tomcat]/apache-tomcat-8.0.8/logs folder.

3. Tomcats deploy folder is : /[path to tomcat]/apache-tomcat-8.0.8/webapps

4. If localhost is not worked properly add the “ localhost” to the etc/hosts file. You can edit it as follows

gksudo gedit /etc/hosts command

.If the browser is chrome try And try unchecking “File > Work Offline” in firefox


Reference :


Written by Namal Fernando

June 29, 2014 at 10:04 pm

Posted in Tomcat

Tagged with

Demo REST project with Camel CXF

leave a comment »

1. Create a .war project (java 1.7 or above) using Maven as follows.

Here we are using the eclipse as the IDE and the Maven. The prefered java version is 1.7 or greater.


2. This is the folder structure of it once it is created.


3. Include the necessary dependencies to the pom.xml file as follows.

Here, we’re mainly include the Apache Camel (including camel cxf jars), Apache Common IO and SLF4j jars. Camel is using Common IO internally so it must be also used. And spring jars are also used in here. Springs are used to load the ApplicationContexts automatically.

<project xmlns="" xmlns:xsi=""
<name>Rest API demo project</name>
<description>Rest API demo project</description>



<!-- Spring + Camel jars -->








<!-- Other jars (logging, io etc) -->





4. Add the below configurations to the WEB-INF/web.xml

Here, we are adding the applicationContext.xml as the contextConfigLocation and the listener classes. Package of the  all routers implementation classes must be mentioned in the applicationContext.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""

5. Add the below configurations to the WEB-INF/applicationContext.xml.

The package containing all the routes must be specified in the camelContext/camel:package tag. Here in our demo project, the router is mentioned in the camel.route package.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
 license agreements. See the NOTICE file distributed with this work for additional
 information regarding copyright ownership. The ASF licenses this file to
 You under the Apache License, Version 2.0 (the "License"); you may not use
 this file except in compliance with the License. You may obtain a copy of
 the License at Unless required
 by applicable law or agreed to in writing, software distributed under the
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License. -->

<beans xmlns=""
 xmlns:xsi="" xmlns:context=""
 xmlns:camel="" xmlns:p=""

<bean id="contextApplicationContextProvider" class="conf.ApplicationCtxProvider"></bean>

 <camelContext xmlns=""

 <!-- All the Routes should be created within below Package -->



6. Add the ApplicationContext classes

These classes are loaded during the Spring-Initialization.

a. ApplicationCtxProvider

This class which implements ApplicationContextAware, is automatically loaded during Spring-Initialization.

package conf;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationCtxProvider implements ApplicationContextAware {

public void setApplicationContext(ApplicationContext ctx)
throws BeansException {


This is injected from the class “ApplicationCtxProvider” which is automatically loaded during Spring-Initialization. This get access to the Spring ApplicationContext from everywhere in your Application.

package conf;

import org.springframework.context.ApplicationContext;

public class ApplicationCtx {

private static ApplicationContext ctx;

// Injected from the class "ApplicationCtxProvider" which is automatically loaded during Spring-Initialization.

public static void setApplicationContext(
ApplicationContext applicationContext) {
ctx = applicationContext;

// Get access to the Spring ApplicationContext from everywhere in your Application.

public static ApplicationContext getApplicationContext() {
return ctx;

7. Implement the Camel router (DemoRouteBuilder).

This class is the router that takes all the REST calls to our REST service. To be run automatically, this must be included in the camel.route as it is mentioned in the aplicationContext.xml. So, it will run automatically start to run when the server is starting. Here we are mentioning the cxfrs URI that will be considered as the Endpoint URI. Here we should specify the resourceClasses to point the request to the implementation. Here,  http://localhost:9003/rest is considered as the root URI for the REST service. Rest of the path will be defined in the resource classes. Here, MappingProcessor implements the Camel Process

package camel.route;

import org.apache.camel.builder.RouteBuilder;

import camel.process.MappingProcessor;
import rs.DemoRequestServiceImpl;

public class DemoRouteBuilder extends RouteBuilder {

private static final String REST_ENDPOINT_URI = "cxfrs://http://localhost:9003/rest?resourceClasses=rs.DemoRequestServiceImpl";

public void configure() {

.process(new MappingProcessor(new DemoRequestServiceImpl()));


8. Implement the Camel Processes (MappingProcessor).

Here, MappingProcessor implements the Camel Process. It takes the REST Impl instance and uses the reflection in the process method for it’s implementation as below.

package camel.process;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.cxf.common.message.CxfConstants;
public class MappingProcessor implements Processor {

private Class<?> beanClass;
private Object instance;

public MappingProcessor(Object obj) {
beanClass = obj.getClass();
instance = obj;

public void process(Exchange exchange) throws Exception {
String operationName = exchange.getIn().getHeader(CxfConstants.OPERATION_NAME, String.class);
Method method = findMethod(operationName,exchange.getIn().getBody(Object[].class));
try {
Object response = method.invoke(instance, exchange.getIn().getBody(Object[].class));
} catch (InvocationTargetException e) {
throw (Exception) e.getCause();

private Method findMethod(String operationName, Object[] parameters)throws SecurityException, NoSuchMethodException {
return beanClass.getMethod(operationName,getParameterTypes(parameters));

private Class<?>[] getParameterTypes(Object[] parameters) {
if (parameters == null) {
return new Class[0];
Class<?>[] answer = new Class[parameters.length];
int i = 0;
for (Object object : parameters) {
answer[i] = object.getClass();
return answer;


9. Creating the Rest IMPL classes.

Here, this is the main IMPL classes of the REST implementation. Here the paths are defined by using the @Path annotation. Here, the main path is defined as the /hotelservice. So, each method defined in this class must be called starting from http://localhost:9003/rest/hotelservice. Each implementation methods is defined with dedicated paths.

Here,  getAvailabilityResults(Long id) will be called as http://localhost:9003/rest/hotelservice./hotels/10. Here this methods takes the Long path parameter as 10. And it will be forwarded to the concrete implementation in class. Here, getAvailabilityResults is defined as a GET method and getSearhResults(String request) as a POST methos.


package rs;

import javax.jws.WebParam;

public interface IDemoRequestService {

 Response getAvailabilityResults(@PathParam("id") Long id); 

 String getSearhResults(@WebParam(name="request") String request); 



package rs;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemoRequestServiceImpl implements IDemoRequestService {

 final Logger logger = LoggerFactory.getLogger(DemoRequestServiceImpl.class);

 public String getSearhResults(String request) {"getSearhResults().request : " + request);

 return "Hello request " + request;

 public Response getAvailabilityResults(Long id){"getSearhResults().Long : " + id);
 return Response.ok("Hello request ID :"+id).status(Status.OK).build();


9. Final Folder structure.


10. Build the project by running the pom.xml through Maven.

Here, I have used clean install package as the maven goal. I executed the maven goals within the eclipse.

11. Deploy the demo.restapi-1.0.0.war in a web container like tomcat.

Here, I used the tomcat8 to deploy the REST service. When tomcat is starting, router is deployed automatically. Then the specified requests (GET ot POST) we are sent to the END POINT URI will be taken to process by the router.

12. Test the GET method.

Here, we can test the GET method using the browser as follows

Hit http://localhost:9003/rest/hotelService/hotels/17 in the browser and get the response as “Hello request ID :17”

13. Test the POST method

– Await !!!

Written by Namal Fernando

June 29, 2014 at 5:18 pm

Static analysis with HammurAPI

leave a comment »

Install the HammurAPI.



  • Then go to the path that hammurapi-5.7.0-setup.jar is downloaded.
  • Install hammmurapi using this command in the terminal.
    • java -jar hammurapi-5.7.0-setup.jar
  • Then this will install hammerapi in your home folder. This contains DB (HSQLDB) and Tomcat also in the same package (eg : /home/namal/Hammurapi)

Start the DB (HSQLDB)

  • Go to the /home/namal/Hammurapi/db.
  • Give the excecution permission to runServer.bat.
  • Run it. (I double clicked and selected to “Run In the Terminal”).
    • The default port for the db is 9001. If the port is already bound, kill it after identifying the process ids using the netstat commands as mentioned below and run the runServer.bat again.
      • netstat –listen – You’ll get all the port numbers currently using
      • netstat -lpn | grep 9001 – You’ll get the process with process id that is using the port 9001
      • sudo kill 4758 : kill it

Start the Tomcat

  • Go to the /home/namal/Hammurapi/tomcat/bin
  • Give the excecution permission to ./
  • Run it as ./
  • If not executing run chmod +x *.sh first and then run it ./ again.
  • Test it with the http://localhost:8080

Install the ant to your system


  • Download the archive using the mentioned link.
  • Extract it to some directory. (In my case I copied it to the project folder that I’m willing to test)

Test the provided sample project.

There’s a sample project in the Hammurapi. We can test it first to verify that everything is OK behind the scene.

  • Go to the /home/namal/Hammurapi/sample
  • Run the following tasks on build.xml
    • apache-ant-1.9.3/bin/ant
    • apache-ant-1.9.3/bin/ant review
    • apache-ant-1.9.3/bin/ant wget
      • Here the report will be extracted into the /home/namal/Hammurapi/report folder
      • In this case provide the as a VM argument as mentioned above.

Test the project.

  • Take the project to the /home/namal/Hammurapi folder.
  • Put all the related jars refering from it in the lib folder.
  • Copy the build.xml to that folder.
  • Follow above tasks mentioned in the sample project


Written by Namal Fernando

May 1, 2014 at 7:49 am

Posted in HammurAPI, Static Analysis

Tagged with , ,

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.


Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language


that were shone when I got tempered!