Archive

Archive for the ‘Java’ Category

Reading property file in java

February 26, 2010 Leave a comment

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DBProperties {

private static final Log log = LogFactory.getLog(DBProperties.class);
private static final String fileName = “database.properties”;
private static Properties properties;
private static DBProperties instance = null;

private DBProperties() {
properties = new Properties();
try {
InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
properties.load(is);
} catch (IOException ioe) {
log.error(“Exception occurred while reading the ” + fileName + “file. ” + ioe.getMessage());
}
}

public static DBProperties getInstance() {
log.info(“GetInstance method of DBProperties class”);
if (null == instance) {
instance = new DBProperties();
}
return instance;
}

public String getProperty(String propertyName) {
return properties.getProperty(propertyName);
}
}

Categories: Java

SEVERE: JSF1054: (Phase ID: RESTORE_VIEW 1, View ID: ) Exception thrown during phase execution: javax.faces.event.PhaseEvent

February 4, 2010 Leave a comment

I was using JSF and trying to refresh data automatically using ajax4jsf.

Scenario & Problem :

1. Server is started and the dashboard.seam page is viewed.
2. Now stop the server and do not close the browser…
3. Now restart the server,  following error is getting displayed on the server console.

SEVERE: JSF1054: (Phase ID: RESTORE_VIEW 1, View ID: ) Exception thrown during phase execution: javax.faces.event.PhaseEvent
Feb 4, 2010 12:07:37 PM org.jboss.seam.web.ExceptionFilter doFilter
WARNING: handling uncaught exception
javax.servlet.ServletException: viewId:/dashboard.seam – View /dashboard.seam could not be restored.
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:270)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:532)

Caused by: javax.faces.application.ViewExpiredException: viewId:/dashboard.seam – View /dashboard.seam could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:186)

Feb 4, 2010 12:07:37 PM org.jboss.seam.web.ExceptionFilter doFilter
WARNING: exception root cause
javax.faces.application.ViewExpiredException: viewId:/dashboard.seam – View /dashboard.seam could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:186)

Feb 4, 2010 12:07:37 PM org.ajax4jsf.webapp.BaseXMLFilter doXmlFilter
SEVERE: Exception in the filter chain
javax.servlet.ServletException: viewId:/dashboard.seam – View /dashboard.seam could not be restored.

Solution :

1. Once the server is stopped,  the page that is trying to get the fresh data should also be closed. Otherwise the page will try to get the data and will throw the above error will occur.
2. Once the server is started, now refresh the previously opened page.

Regards,

Ram

Categories: Jboss Seam, JSF

how to configure a startup class for a web application in Tomcat

September 17, 2009 Leave a comment

In this tutorial you will learn how to configure a startup class for a web application in Tomcat 5.5.x or Tomcat 6.

Step 1:Let us assume Tomcat is installed in C:\tomcat. Let us assume you want to define a startup class for a web application named reminder. Navigate to C:\tomcat\webapps\reminder\WEB-INF and open the file web.xml. Configure the startup class ApplicationStartUpListener under the package com.corelabs as in the screen shot below.

<web-app>
          <listener>
            <listener-class>com.corelabs.ApplicationStartUpListener</listener-class>
        </listener>

 Step 2: The startup class ApplicationStartUpListener should follow certain rules like implementing javax.servlet.ServletContextListener. The Java source of the ApplicationStartUpListener is provided in the below screen shot.

package com.corelabs;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;

/**
 * @author ramb
 */
public class ApplicationStartUpListener implements ServletContextListener {

    private static final Logger log = Logger.getLogger(ApplicationStartUpListener.class);
    private ServletContext context = null;

    public ApplicationStartUpListener() {
    }

    public void contextInitialized(ServletContextEvent event) {
  //Do Something you wish when application is loaded.
    }

    public void contextDestroyed(ServletContextEvent event) {
        this.context = null;
    }
}

Categories: Java

How do I sort items in a Set?

May 5, 2009 Leave a comment

The trick to sort a java.util.Set is to use the implementation of a java.util.SortedSet such as the java.util.TreeSet class. The example below shows you the result of using the java.util.TreeSet class, in which the items in it will be sorted based on the element’s natural order.

 
package com.corelabs.example.util;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {

        // The TreeSet class is an implementation of a SortedSet, this means
        // that when your are using the TreeSet to store you data collections
        // you'll get the items ordered base on its elements natural order.

        Set<String> set = new TreeSet<String>();
        // In the example below we add some letters to the TreeSet, this mean
        // that the alphabets will be ordered based on the alphabet order
        // whichs is from A to Z.
        //
        set.add("Z");
        set.add("A");
        set.add("F");
        set.add("B");
        set.add("H");
        set.add("X");
        set.add("N");

        for (String item : set) {
            System.out.print(item + " ");
        }
    }
}

This demo prints:

A B F H N X Z
Categories: Java

What are the differences between SAX and DOM parser.

April 18, 2009 Leave a comment

Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.

SAX

DOM

Parses node by node

Stores the entire XML document into memory before processing

Doesn’t store the XML in memory

Occupies more memory

We can’t insert or delete a node

We can insert or delete nodes

Top to bottom traversing

Traverse in any direction.

SAX is an event based parser

DOM is a tree model parser

SAX is a Simple API for XML

Document Object Model (DOM) API

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;
 

doesn’t preserve comments

preserves comments

SAX generally runs a little faster than DOM

SAX generally runs a little faster than DOM

 

If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

 

More Info:  SAX Parser is the one which is developed to run the java programs especially. It uses a memory resident model. DOM is developed by w3c consortium so that it is an open standard. In DOM the xml file is arranged as a tree and backward and forward search is possible for searching the tags and evaluating the data which it holds. It depends upon the application and ease of data accessiblity. If we want to extract the data from a xml file once we have to move to SAX which is one time top to bottom read approach and if we want ot randomly pick the data in an xml file then the tree reperesentation of DOM model is to be put into use.

Categories: Java

How to read values from properties file in JBoss Seam

April 11, 2009 Leave a comment
Let us assume we have two properties files namely  messages.properties and application.properties

In components.xml we place the below line

<core:resource-loader bundle-names=”messages application”/>

Then in the Seam component where we want to retrieve the value for a given property (key), we inject the ResourceBundle as below.

   @In
    java.util.ResourceBundle resourceBundle;

The retrieval is done as below

String imagesLocation = resourceBundle.getString(“IMAGESLOCATION”);
Categories: Jboss Seam Tags:

Signing a JAR file

April 11, 2009 Leave a comment

JDK is to be installed and make sure the bin folder under Java
installation home folder has keytool.exe and jarsigner.exe and are available in the PATH.

Assuming the algorithm used to generate key is RSA, execute the following command navigating to the directory in which jar file resides[let us assume the name of the jar file to be signed is testapplet.jar].

keytool -genkey -alias keystorename -keyalg RSA -keypass urpassword -keystore appkeystore.jks -storepass urpassword

You will be prompted to enter values for your state, oraganisation etc.
Enter appropriate values and then execute the following commandto
complete the signing of the jar file.
jarsigner -keystore appkeystore.jks testapplat.jar keystorename

Example : keytool -genkey -alias COREDEV -keyalg RSA -keypass urpassword -keystore appkeystore.jks -storepass urpassword

Categories: Java