Friday, May 30, 2008

Extending JBoss Classpath

In $JBOSS_HOME/server/{instance}/lib can you put all the external jar that you need, but if you want to put them separatly into another path, add the following entry in your $JBOSS_HOME/server/{instance}/jboss-service.xml
 <classpath codebase="file:///${jboss.myserver.lib.ext}" archives="*" /> 

Then you can set jboss.myserver.lib.ext simply in your JBoss start script as Java System Property, for example
export JAVA_OPT="$JAVA_OPT -Djboss.myserver.lib.ext=/to/path

This approach my be very practical to manage jar files, but also configurations files.

Wednesday, May 21, 2008

Transform a sequence of text in a Java String Array

When your input text sound like:
oil sunflowerse    tonn   march    said  tender  import  trader   egypt     may

than you will get the following String Array:
{"oil", "sunflowerse", "tonn", "march", "said", "tender", "import", "trader", "egypt", "may"}

by using this sed script:
sed 's/^ *//; s/ *$//; s/ \{1,\}/ /g; s/ /\", \"/g; s/^/{"/; s/$/"}/' filename

It performs this tasks:

  • Remove all multiple spaces at the start

  • Remove all multiple spaces at the end

  • Remove all multiple spaces between the words

  • Replace all single spaces with ", "

  • Add {" at the start

  • Add "} at the end

Saturday, May 10, 2008

Tranforming a text into Java String

Sometimes you may need, for example for unit-tests, to hard-code text in you java classes.
The following sed command can help you:
sed 's/"/\\"/g; s/^/"/; s/$/\\n"+/; $ s/\\n"+/";/' filename

It performs respectively this tasks:
  • Replace all occurencies of " with \"
  • Insert " at the begininng of each line
  • Insert \n"+ at the end of each line
  • Replace the last occurency of \n"+ with ";


For example:

A woman's face with nature's own hand painted,
Hast thou, the master mistress of my passion;
A woman's gentle heart, but not acquainted
With shifting change, as is false women's fashion:
An eye more bright than theirs, less false in rolling,
Gilding the object whereupon it gazeth;
A man in hue all hues in his controlling,
Which steals men's eyes and women's souls amazeth.
And for a woman wert thou first created;
Till Nature, as she wrought thee, fell a-doting,
And by addition me of thee defeated,
By adding one thing to my purpose nothing.
But since she prick'd thee out for women's pleasure,
Mine be thy love and thy love's use their treasure.

will be:

"A woman's face with nature's own hand painted,\n"+
"Hast thou, the master mistress of my passion;\n"+
"A woman's gentle heart, but not acquainted\n"+
"With shifting change, as is false women's fashion:\n"+
"An eye more bright than theirs, less false in rolling,\n"+
"Gilding the object \"whereupon\" it gazeth;\n"+
"A man in hue all hues in his controlling,\n"+
"Which steals men's eyes and women's souls amazeth.\n"+
"And for a woman wert thou \"first\" created;\n"+
"Till Nature, as she wrought thee, fell a-doting,\n"+
"And by addition me of thee defeated,\n"+
"By adding one thing to my purpose nothing.\n"+
"But since she prick'd thee out for women's pleasure,\n"+
"Mine be thy love and thy love's use their treasure.";


A very goldmine of "cooking tips" on sed can be found here

To distribute a long string across multiple lines:
fmt [filename] | sed 's/"/\\"/g; s/^/"/; s/$/\ " +/; $ s/\ " +/"/'