Friday, December 16, 2011

Java snippet to open, modify and write back to same file

Objective :
Very often you find this requirement. Sometimes you try and overcome using linux commands rather than going by the Java programming technique.

Here is a snippet that would be handy.

 public static void main(String[] args) throws IOException {
   
    // Read XML as string
    byte[] buffer = new byte[(int) new File(args[0]).length()];
    FileInputStream f = new FileInputStream(args[0]);
    f.read(buffer);
   
    // format it
        String unformattedXml = new String(buffer);
        String formattedXml = new XmlFormatter().format(unformattedXml);
       
    // Write formatted string back into same file        
        FileOutputStream pOUTPUT;
        PrintStream pPRINT;
        pOUTPUT = new FileOutputStream(args[0]);
        pPRINT = new PrintStream(pOUTPUT);
        pPRINT.println (formattedXml + "\n");
        pPRINT.close();
        System.out.println(    formattedXml );

}


Found this handy even after experience in Java programmer.


Friday, December 9, 2011

Small end webserver that can meet up to aspirations of self-owned website

'Small end webserver that can meet up to aspirations of self-owned website'

Which is it ? It is Apache Tomcat . A web server ( not a full-fledged J2EE container ) but can meet up to most of your needs.

Off-late you may wonder on the kind of technical support from user community might be not very easy to get. But I was wrong.

https://blogs.apache.org/tomcat/
gives you very good support.
With respect to debugging the webserver is concerned . Never thought it would be so easy to do it on a  local machine.
Its so easy to do it, with Tomcat 7 - JMX - Sun JConsole

TOMCAT 7 - JMX - SUN JDK JConsole

STEP -1 ] Install SUNJDK 1.6+ or above
STEP -2 ] Install SUNJRE 1.6+ or above
STEP -3 ] Install TOMCAT 7.0 or above

STEP -4 ] Set the ENV properties
Linux platform
$ CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=test-idc.internet2.edu";
$ export CATALINA_OPTS;

On Windows
In envirnoment variables
create CATALINA_OPTS
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=localhost


STEP -5 ] Start Tomcat
C:\tomcat70\bin\Tomact7.exe

STEP -6 ] Start JConsole
C:\SUNJDK16\bin\jconsole.exe

STEP -7 ] Connnect to local PID of tomcat process.
Reference :
https://wiki.internet2.edu/confluence/display/CPD/Monitoring+Tomcat+with+JMX

 Thanks Apache Foundation.


Followers