Reading properties file at glassfish starttime

Goto JVM options (server-config) and add this line

-Dproperty.file.path=......filepath/einvoice.properties


einvoice.properties:

smtp.address = localhost
ipcommerce.url = http://someipcommerceurl.com/
email.body = Plese check the attachment for invoice.
email.subject = Invoice
file.create.location = /home/shakeelstha/Interac\ Liferay\ Repo/invoice
file.upload.location = /home/shakeelstha/Interac\ Liferay\ Repo/upload
jasper.location =
smtp.mail.password =
smtp.mail.username = root
mail.sender.address = Interac<shakeelshrestha@lftechnology.com>


ContextListener.java

package com.lftech.interac.listener;

import com.lftech.interac.dto.EInvoiceProperty;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author shakeelstha
 */
public class ContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent se) {
        String einvoicePropertyFile = System.getProperty("property.file.path");
        System.out.println("einvoicePropertyFile ::: "+einvoicePropertyFile);
        Properties props = new Properties();
        try {
            //System.out.println("==============================CONTEXT LISTENER START=========================================");
            FileInputStream fis = new FileInputStream(einvoicePropertyFile);
            props.load(fis);
            EInvoiceProperty.smtpHost = props.getProperty("smtp.address");
            EInvoiceProperty.ipCommerceUrl = props.getProperty("ipcommerce.url");
            EInvoiceProperty.emailBodyText = props.getProperty("email.body");
            EInvoiceProperty.emailSubject = props.getProperty("email.subject");
            EInvoiceProperty.fileCreatedLocation = props.getProperty("file.create.location");
            EInvoiceProperty.fileUploadLocation = props.getProperty("file.upload.location");
            EInvoiceProperty.jasperFileLocation = props.getProperty("jasper.location");
            EInvoiceProperty.mailPassword = props.getProperty("smtp.mail.password");
            EInvoiceProperty.mailUsername = props.getProperty("smtp.mail.username");
            EInvoiceProperty.mailSenderAddress = props.getProperty("mail.sender.address");
            fis.close();
            //System.out.println("==============================CONTEXT LISTENER STOP=========================================");
           
            System.out.println(EInvoiceProperty.smtpHost+" "+EInvoiceProperty.ipCommerceUrl+" "+EInvoiceProperty.mailSenderAddress);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent se) {
    }
}


Add this in web.xml

<listener>
        <listener-class>package name to ContextListener</listener-class
</listener>


Create EInvoiceProperty.java , a plain pojo with static variables

package com.lftech.interac.dto;

/**
 *
 * @author shakeelstha
 */
public class EInvoiceProperty {

    public static String ipCommerceUrl;
    public static String smtpHost;
    public static String mailUsername;
    public static String mailPassword;
    public static String fileUploadLocation;
    public static String fileCreatedLocation;
    public static String emailSubject;
    public static String emailBodyText;
    public static String jasperFileLocation;
    public static String mailSenderAddress;

    public static String getEmailBodyText() {
        return emailBodyText;
    }

    public static void setEmailBodyText(String emailBodyText) {
        EInvoiceProperty.emailBodyText = emailBodyText;
    }

    public static String getEmailSubject() {
        return emailSubject;
    }

    public static void setEmailSubject(String emailSubject) {
        EInvoiceProperty.emailSubject = emailSubject;
    }

    public static String getFileCreatedLocation() {
        return fileCreatedLocation;
    }

    public static void setFileCreatedLocation(String fileCreatedLocation) {
        EInvoiceProperty.fileCreatedLocation = fileCreatedLocation;
    }

    public static String getFileUploadLocation() {
        return fileUploadLocation;
    }

    public static void setFileUploadLocation(String fileUploadLocation) {
        EInvoiceProperty.fileUploadLocation = fileUploadLocation;
    }

    public static String getIpCommerceUrl() {
        return ipCommerceUrl;
    }

    public static void setIpCommerceUrl(String ipCommerceUrl) {
        EInvoiceProperty.ipCommerceUrl = ipCommerceUrl;
    }

    public static String getJasperFileLocation() {
        return jasperFileLocation;
    }

    public static void setJasperFileLocation(String jasperFileLocation) {
        EInvoiceProperty.jasperFileLocation = jasperFileLocation;
    }

    public static String getMailPassword() {
        return mailPassword;
    }

    public static void setMailPassword(String mailPassword) {
        EInvoiceProperty.mailPassword = mailPassword;
    }

    public static String getMailUsername() {
        return mailUsername;
    }

    public static void setMailUsername(String mailUsername) {
        EInvoiceProperty.mailUsername = mailUsername;
    }

    public static String getSmtpHost() {
        return smtpHost;
    }

    public static void setSmtpHost(String smtpHost) {
        EInvoiceProperty.smtpHost = smtpHost;
    }

    public static String getMailSenderAddress() {
        return mailSenderAddress;
    }

    public static void setMailSenderAddress(String mailSenderAddress) {
        EInvoiceProperty.mailSenderAddress = mailSenderAddress;
    }
}


Comments

Popular posts from this blog

Simple Invoice Creation With Jasper Report

Dynamic Image in Jasper Report

Auto Increment Oracle Table Id Mapping With JPA Entity