Read/Write System Properties From/Into XML Using Groovy

[ This Blog Was Moved To : http://www.diknows.com ]


Inspired by the following post ( http://www.ibm.com/developerworks/library/j-tiger02254.html ), I created the following code snippets to Read / Write System properties from/into XML files.

Writing system properties into XML file (output.xml):

def properties = System.properties
def fos = new FileOutputStream("output.xml")
properties.storeToXML(fos,"")

Reading system properties from XML file (input.xml):

def prop = new Properties()
def fis = new FileInputStream("input.xml")
prop.loadFromXML(fis)

And, ofcourse this line can be used to list the properties in any properties object

prop.list(System.out)

Here is the resultant XML File:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
 <comment/>
 <entry key="java.runtime.name">Java(TM) SE Runtime Environment</entry>
 <entry key="sun.boot.library.path">/opt/jdk1.6.0_17/jre/lib/amd64</entry>
 <entry key="java.vm.version">14.3-b01</entry>
 <entry key="java.vm.vendor">Sun Microsystems Inc.</entry>
 <entry key="java.vendor.url">http://java.sun.com/</entry>
 <entry key="path.separator">:</entry>
 <entry key="java.vm.name">Java HotSpot(TM) 64-Bit Server VM</entry>
 <entry key="file.encoding.pkg">sun.io</entry>
 <entry key="sun.java.launcher">SUN_STANDARD</entry>
 <entry key="user.country">US</entry>
 <entry key="sun.os.patch.level">unknown</entry>
 <entry key="program.name">groovyConsole</entry>
 <entry key="java.vm.specification.name">Java Virtual Machine Specification</entry>
 <entry key="user.dir">/home/dino/Documents</entry>
 <entry key="java.runtime.version">1.6.0_17-b04</entry>
 <entry key="java.awt.graphicsenv">sun.awt.X11GraphicsEnvironment</entry>
 <entry key="java.endorsed.dirs">/opt/jdk1.6.0_17/jre/lib/endorsed</entry>
 <entry key="os.arch">amd64</entry>
 <entry key="java.io.tmpdir">/tmp</entry>
 <entry key="line.separator">
 </entry>
 <entry key="java.vm.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="os.name">Linux</entry>
 <entry key="tools.jar">/opt/jdk1.6.0_17//lib/tools.jar</entry>
 <entry key="sun.jnu.encoding">UTF-8</entry>
 <entry key="script.name">/home/dino/installations/groovy/groovy-1.6.5/bin/groovyConsole</entry>
 <entry key="java.library.path">/opt/jdk1.6.0_17/jre/lib/amd64/server:/opt/jdk1.6.0_17/jre/lib/amd64:/opt/jdk1.6.0_17/jre/../lib/amd64:/usr/lib64/mpi/gcc/openmpi/lib64:/usr/java/packages/lib/amd64:/lib:/usr/lib</entry>
 <entry key="java.specification.name">Java Platform API Specification</entry>
 <entry key="java.class.version">50.0</entry>
 <entry key="sun.management.compiler">HotSpot 64-Bit Server Compiler</entry>
 <entry key="os.version">2.6.31.5-0.1-desktop</entry>
 <entry key="user.home">/home/dino</entry>
 <entry key="user.timezone">Africa/Cairo</entry>
 <entry key="java.awt.printerjob">sun.print.PSPrinterJob</entry>
 <entry key="file.encoding">UTF-8</entry>
 <entry key="java.specification.version">1.6</entry>
 <entry key="java.class.path">/home/dino/installations/groovy/groovy-1.6.5/lib/groovy-1.6.5.jar</entry>
 <entry key="user.name">dino</entry>
 <entry key="java.vm.specification.version">1.0</entry>
 <entry key="java.home">/opt/jdk1.6.0_17/jre</entry>
 <entry key="sun.arch.data.model">64</entry>
 <entry key="user.language">en</entry>
 <entry key="java.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="java.vm.info">mixed mode</entry>
 <entry key="java.version">1.6.0_17</entry>
 <entry key="java.ext.dirs">/opt/jdk1.6.0_17/jre/lib/ext:/usr/java/packages/lib/ext</entry>
 <entry key="sun.boot.class.path">/opt/jdk1.6.0_17/jre/lib/resources.jar:/opt/jdk1.6.0_17/jre/lib/rt.jar:/opt/jdk1.6.0_17/jre/lib/sunrsasign.jar:/opt/jdk1.6.0_17/jre/lib/jsse.jar:/opt/jdk1.6.0_17/jre/lib/jce.jar:/opt/jdk1.6.0_17/jre/lib/charsets.jar:/opt/jdk1.6.0_17/jre/classes</entry>
 <entry key="java.vendor">Sun Microsystems Inc.</entry>
 <entry key="file.separator">/</entry>
 <entry key="java.vendor.url.bug">http://java.sun.com/cgi-bin/bugreport.cgi</entry>
 <entry key="sun.io.unicode.encoding">UnicodeLittle</entry>
 <entry key="sun.cpu.endian">little</entry>
 <entry key="groovy.sanitized.stacktraces">org.codehaus.groovy.runtime.
 org.codehaus.groovy.
 groovy.lang.
 gjdk.groovy.lang.
 sun.
 java.lang.reflect.
 java.lang.Thread
 groovy.ui.Console</entry>
 <entry key="groovy.full.stacktrace">false</entry>
 <entry key="groovy.starter.conf">/home/dino/installations/groovy/groovy-1.6.5/conf/groovy-starter.conf</entry>
 <entry key="groovy.home">/home/dino/installations/groovy/groovy-1.6.5</entry>
 <entry key="sun.cpu.isalist"/>
</properties>

And this is it for now.

Grails Domains Internationalition

[ This Blog Was Moved To : http://www.diknows.com ]


I am currently working at a Project that should be internationalized ( Arabic – English ).  I am still developing it. I needed list of countries to be internationalized and I wanted it to be in the domain constraints (inList). I may later put the data in the database, but for the time being; I’m putting the data in the domain class.

Here is what I did

I put the data in the model like this :

class Address {

 static constraints = {
     gpsLng(nullable:true)
     gpsLat(nullable:true)
     address()
     city()
     state()
     country(inList:['afghanistan',
         'albania',
         'algeria',
         'american_samoa',
          ...
         'zaire',
         'zambia'])
     dateCreated(nullable:true)
     lastUpdated(nullable:true)
  }

  Date dateCreated
  Date lastUpdated

  String gpsLng
  String gpsLat

  String address
  String city
  String state
  String country

  String toString(){
     "$address"
  }
}

And at the properties files I did the internationalization like this


address.country.afghanistan=Afghanistan
address.country.albania=Albania
address.country.algeria=Algeria
address.country.american_samoa=American Samoa
...

And in the view .. I did this  ( I’m using beanField plugin too )


<bean:select beanName="address" property="country"
name="country" id="country"
from="${address.constraints.country.inList}"  valueMessagePrefix="address.country" value="${fieldValue(bean:address,field:'country')}"   optionValue="${message(code:it)}" />

And this is it ..  The valueMessagePrefix is used to indicate the prefix to be added to the key in inList list and get the value from the properties files. optionValue is what user see ( Label ).

When the form is submitted, the value in the inList will be passed back to the controller.

Hope I was helpful to someone.

Groovy Very Basic Statistics Tool

[ This Blog Was Moved To : http://www.diknows.com ]


I am in a project where I am in charge of watching and reporting performance of a JEE Application on JBoss Application Server. Due to my usual need to caculate Average, Max, Min values of the data I observe. I made this little code to help me out ( Which for me it did help out ).

Here it is:

pagesLoad = [ 5.767,9.296, 3.814, 1.826, 2.889,8.000,6.797,2.926,5.992,2.430,16.191,14.294,2.778,7.037,4.745,5.004,4.601,6.930,2.275,2.896,14.319,3.836,11.059,2.295,2.845,7.078,6.194,1.812,14.811,4.105,3.013,16.358,4.368,3.314,21.828,6.188,8.137 ]
bitsRecieved = [ 27764, 253033, 921870, 142091, 307, 214406, 890962, 307, 307, 1259790, 553839, 749870, 143216, 146175, 179923, 517335, 572690, 180094, 801059, 143218, 215843, 215407, 146973, 572912, 801120, 587021, 239474, 518619, 307, 2409085, 908437, 16453, 143218, 215847, 218320, 1121673, 215163, 119930, 578828, 135629, 23098, 384, 143422, 215847, 215040, 254052, 578110, 216877, 921819 ]

def showStatistics(data,over=1,append=''){
    float min = 100000000
    float max = 0
    int count = data.size()
    float total = 0;
    data.each {
        it= it/over
        if(it>max){
            max = it;
        }
        if(it<min){
            min = it;
        }
        total+= it
    }
    println "MAX IS : $max $append"
    println "MIN IS : $min $append"
    println "Total is : $total $append"
    println "Count is : $count"
    println "Average IS: ${total/count} $append"
    println '-'*100
}

//showStatistics(pagesLoad,1,'Seconds')
showStatistics(bitsRecieved,1024,'KB')
Posted in GROOVY. Tags: . Leave a Comment »

Virus Removal Groovy Script

[ This Blog Was Moved To : http://www.diknows.com ]


As I’m a Linux User since years ago. I faced the problem of NO Viruses at my PC, but tons of viruses on my Flash Memory ( USB Stick ), due to copying files to/from windows machines.

Actually, I was never affected by these viruses on my own, but I do NOT want to be a transportation facility for the virus to spread across other machines.

I noticed that there are two simple types of viruses that usually get attached to the memory stick.

  1. A .inf file and a .exe file that ofcourse got a strange name that I can visually detect and manually remove at linux.
  2. A .exe file gets the name of the super directory with the addition of the extension .exe. Which was usually easy to detect visually but hard to remove manually due to the amount of folders on the memory.

So, to fasten the process of  removing the second virus, I wrote this simple Groovy script ( which is in fact a reflection of  an old Java program that I created for the same virus years ago )

// Searchs for the .exe Virus that has the same name of its parent directory
checkVirus = { folder ->
    folder.eachFile{ file ->
        if(file.isDirectory()){
            checkVirus(file)
        }else if ("${folder.name}.exe" == file.name ){
            it.delete()
        }
    }
}
checkVirus (new File('/media/disk/'))

And this is it. Any enhancements or suggestions will be great.