Friday, December 18, 2009

Spring : Create instance with constructor arguments

If our java class has a constructor like this with argument.

public class Home() {
    private String name;

    public Home(String s) {
         name = s;   
    }
}

We can write our spring bean like this.

       <bean id="home1" class="<class.path>.Home">
           <constructor-arg value="My_Home"/>
       </bean>

Sunday, November 8, 2009

Cron job for linux.



For more information visit to the original site.
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Example(s)

If you wished to have a script named /root/backup.sh run every day at 3am, my crontab entry would look like as follows:

(a) Install your cronjob:
<code> # crontab -e
</code>(b)Append following entry:
<code>0 3 * * * /root/backup.sh
</code>Run five minutes after midnight, every day:<code>
5 0 * * * /path/to/command</code>
Run at 2:15pm on the first of every month:
<code>15 14 1 * * /path/to/command
</code>Run at 10 pm on weekdays:<code>
0 22 * * 1-5 /path/to/command </code>
Run 23 minutes after midnigbt, 2am, 4am ..., everyday:<code>
23 0-23/2 * * * /path/to/command
</code>Run at 5 after 4 every sunday:<code>
5 4 * * sun /path/to/command</code>

How do I disabling Email output?

By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append >/dev/null 2>&1.<br /> For example:
0 3 * * * /root/backup.sh >/dev/null 2>&1


To mail output to particluer email account let us say vivek@nixcraft.in you need to define MAILTO variable to your cron job:
MAILTO="vivek@nixcraft.in"
0 3 * * * /root/backup.sh >/dev/null 2>&1



Thursday, August 20, 2009

The BASEDIR environment variable is not defined correctly

Tomcat Issue

The BASEDIR environment variable is not defined correctly
This environment variable is needed to run this program

This will occur due to using a copy of tomcat which is packed someone else.

The solution for this is go to the bin and change the mod into 777 of all .sh file.
chmod 777 *.sh

Saturday, July 18, 2009

How to convert string date into Date object.

How to convert string date into Date object.

Date date = getDate("18/07/2009 15:15:45");

private static Date getDate(String fullDate) throws ParseException {
          SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
          df.setLenient(false); return df.parse(fullDate);
}


Wednesday, July 15, 2009

Getting the IP Address of a Hostname

Getting the IP Address of a Hostname

        try {
                InetAddress addr = InetAddress.getByName("google.com");
                byte[] ipAddr = addr.getAddress();

                // Convert to dot representation
                String ipAddrStr = "";
                for (int i=0; i<ipAddr.length; i++) {
                    if (i > 0) {
                        ipAddrStr += ".";
                    }
                    ipAddrStr += ipAddr[i]&0xFF;
                }
            System.out.println("ipAddrStr = " + ipAddrStr);
            } catch (UnknownHostException e) {
            }

Wednesday, June 17, 2009

SVN: Merging a Branch into Trunk

Merge a Branch into Trunk

  1. Check out a copy of trunk:
    svn co svn+ssh://server/path/to/trunk
  2. Check out a copy of the branch you are going to merge:
    svn co svn+ssh://server/path/to/branch/myBranch
  3. Change your current working directory to "myBranch"
  4. Find the revision "myBranch" began at:
    svn log --stop-on-copy
    This should display back to you the changes that have been made back to the point the branch was cut. Remember that number (should be rXXXX, where XXXX is the revision number).
  5. Change your current working directory to trunk
  6. Perform an SVN update:
    svn up
    This will update your copy of trunk to the most recent version, and tell you the revision you are at. Make note of that number as well (should say "At revision YYYY" where YYYY is the second number you need to remember).
  7. Now we can perform an SVN merge:
    svn merge -rXXXX:YYYY svn+ssh://server/path/to/branch/myBranch
    This will put all updates into your current working directory for trunk.
  8. Resolve any conflicts that arose during the merge
  9. Check in the results:
    svn ci -m "MERGE myProject myBranch [XXXX]:[YYYY] into trunk"

That is it. You have now merged "myBranch" with trunk.

Friday, June 5, 2009

Java code base log apender

Create Log apender using java code.

This is a small example which use to log the text in different log files according to the shortcode provided.

    private void doLog(String shortCode, String text) {
        if (!loginBuffers.containsKey(shortCode)) {
            String fileName = shortCode + ".cdr";
            logger.info("Create cdr log for " + fileName);
            Logger smsLogger = Logger.getLogger("sms." + shortCode);
            DailyRollingFileAppender appender = new DailyRollingFileAppender();
            PatternLayout layout = new PatternLayout();
            layout.setConversionPattern(conversionPattern);
            appender.setDatePattern(appenderRollingDatePattern);
            appender.setLayout(layout);
            try {
                appender.setFile(path + File.separator + fileName, true, false, 1);
            } catch (IOException e) {
                logger.error("Can not create log file [" + path + File.separator + fileName + "]", e);
            }
            appender.setBufferedIO(false);
            appender.setImmediateFlush(true);
            appender.setBufferSize(1);  //4096
            appender.activateOptions();
            appender.setName("sms_" + shortCode);
            appender.setThreshold(Priority.INFO);
            smsLogger.addAppender(appender);
            loginBuffers.put(shortCode, smsLogger);
        }
        loginBuffers.get(shortCode).info(text);

        if (logger.isDebugEnabled()) {
            logger.debug("SMS is logged into " + loginBuffers.get(shortCode).getName());
        }
    }

Monday, May 11, 2009

How to count charactors in mysql.



mysql> select char_length('kumudu');
+--------------------------------+
| char_length('kumudu') |
+--------------------------------+
| 6 |
+--------------------------------+
1 row in set (0.00 sec)

You can use this in the where clause too.
Ex:
select * from vote where char_length(message)>1;

Monday, March 16, 2009

jsp page redirect

How to Rederect a jsp page
Put this code inside the <head> .

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=login.do">

Example :

<html>
<head><title>Simple jsp page</title>
  <META HTTP-EQUIV="Refresh"
      CONTENT="5; URL=login.do"></head>
  <body>Please wait...</body>
</html>

Note : The number "5;" means the current page will wait that amount of seconds before loading the login page.