Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Thursday, December 9, 2010

Linux tips

> ls -l | grep -v total | awk '{ S+=$5} END { print "Total = ",S, ", Average = ", S/NR, ", Entries = ", NR, ", Columnss = ", NF}'
Total = 70238 , Average = 4389.88 , Entries = 16 , Columnss = 9
================================================================
> x=2008
> y=1957
> echo "$[$x - $y]"
51
> echo "$(($x - $y))"
51

================================================================
> gunzip < /usr/share/man/man8/vgrename.8.gz | nroff -man less

================================================================
> for i in `find /usr/home -name '*.jar'`
> do
> jar tvf $i | grep Something 2>/dev/null
> if [ $? -eq 0 ]; then
> echo $i
> fi
> done

================================================================
> groovy -e "System.env.each { println it }"
> groovy -e "System.props.each { println it }"

Wednesday, November 17, 2010

Groovy / REST / CouchDB

The codes below from REST up with CouchDB and Groovy's RESTClient was modified to use login authentication based on this link ...

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON

@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '[0.5.0,0.5.1)')
def getRESTClient(local = false) {
RESTClient client;

if (!local) {
client = new RESTClient("http://my.couchone.com:5498/")
} else {
client = new RESTClient("http://my.couchone.com:5984/")
}

def authHash = "user:password".getBytes('iso-8859-1').encodeBase64()
client.headers.Authorization = "Basic $authHash"

return client;
}

def client = getRESTClient(true)

try {
client.get(path: "parking_tickets")
println "Deleting DB parking_tickets ..."
client.delete(path: "parking_tickets")
} catch (Exception e) {
println "Creating new DB parking_tickets ..."
}

def response = client.put(path: "parking_tickets", requestContentType: JSON, contentType: JSON)

assert response.data.ok : "response from server wasn't ok"

Note: Grab will download required jars to C:\Users\userName\.groovy\grapes\org.codehaus.groovy.modules.http-builder

Monday, February 22, 2010

How to copy a file

How to copy a file

In Java 5 and 6:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
....
String orig ="file.xml";
String dest = "file.xml.bak";
InputStream in = new FileInputStream(orig);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();

In Java 7:
import java.file.io;
import org.apache.commons.io.FileUtils;
....
String orig ="file.xml";
String dest = "file.xml.bak";
File fOrig = new File(orig);
File fDest = new File(dest);
FileUtils.copyFile(fOrig, fDest);

In Groovy:
new File("/tmp/otherFile") << new File("/tmp/someFile").text
( new AntBuilder ( ) ).copy ( file : 'blah' , tofile : 'fobar' )

Reference

Sunday, February 7, 2010

groovy++ looks cool

Groovy++ make Groovy run a lot faster by trimming dynamism and flexibility that you don't need.
And after I looked at the performance of groovy++ here I feel more confident to say that Groovy will be gaining ground fast ... sooner or later :)