Friday, January 31, 2014

When people call YYYYDDD a Julian Date


I've a developer and quite often I have clients asking for a date in Julian date format, and normally this is followed with 'you know the day number format'. What they are really after is a thing called an Ordinal Date which is all explained here http://en.wikipedia.org/wiki/ISO_8601#Ordinal_dates. If every person is told that its not a Julian date but an ordinal date, and we no long have the confusion, the trees will suddenly be greener and all cars will run on a thing called Crukshuka that is so environmentally friendly it will make the trees grow faster!

If I ask a developer to put in some code to do a Julian date, I will either get a huge method copied from the internet, or some very odd and obscure code that will convert a date. But there is an easier way!

JodaDate to the rescue

Joda is a fantastic date and time library. It contains everything a person would ever need for date/time manipulation and formatting. How easy is it to output an ordinal date?

 import org.joda.time.LocalDate;  
 import org.joda.time.format.ISODateTimeFormat;  
...
 public String getDateYYYYDDD() {  
   return LocalDate.now().toString(ISODateTimeFormat.basicOrdinalDate());  
 }  

And thats it. All done and in one line of code (well 3 really).

Friday, August 23, 2013

Multiple applications listening on One Port

I've had the most ridiculous time with Apache Tomcat on a windows machine for the past 2 days that became so easy to fix I really could throw a shoe at myself.

The Setup

I was running Tomcat on port 8080, and also have a VirtualBox machine that is performing port forwarding of 8080 to a Glassfish running on a virtual machine. I didn't realise it until much much later though....

The Weirdness

The Glassfish server is running an application and is accessed via a domain name (pxmsvcjfb.... for me). Thats because its an opensso server and has to have a domain name. And from a browser it works no problems.
And from a browser I can also go to http://localhost:8080/ and the Tomcat application shows, and if I use http://pxmsvcjfb/opensso no problem.

The Restful Web services can also be called from the browser against http://localhost:8080/. However : if I try to call the web services through java client (Jersey WebResource), I only ever get a 404 not found. Yet had another webapp on port 7080 and that worked just fine. I was starting to think it was the Jersey client doing something strange.

Checking a netstat -an gave this :

 TCP    0.0.0.0:8080           bushlaptop:0           LISTENING
 TCP    0.0.0.0:8080           bushlaptop:0           LISTENING


Eek there are 2 processes listening on port 8080!

The solution

I changed the Tomcat port to 9080 and miraculously everything started to work!

Friday, October 07, 2011

Some cool Eclipse regexes

I have this line of text :

public Integer getAccountId() { return account_id.getInteger(); }

And I need to turn it into :

public void setAccountId(Integer value) { return account_id.getInteger(); }

One regex can do that for me as follows :

Find:
(public\ )([A-Za-z]+)\ g([A-Za-z]+\()(\)\t+\{\ )(return\ )([A-Za-z_]+\.)(g)([A-Za-z]+\()

Replace:
$1void s$3$2 value$4$6s$8value

Thats it. Yay. Nice and simple!

Tuesday, May 10, 2011

Using groovy 1.8 with Jmeter 2.4

You want to use Jmeter with the latest groovy? But it just throws errors about not finding libraries?

Copy these 3 jar files from the groovy/lib directory into jmeter/lib directory and you'll be all ready to go
  • groovy-1.8.0.jar
  • antlr-2.7.7.jar
  • asm-3.2.jar
I tried to dynamically set the CLASSPATH to load all libs without having to include the groovy libraries, but it just did not want to know about it. This seems to be the only way it will work straight away. Even setting search_paths or setting user.classpath as from JMeter website didn't help


Saturday, November 20, 2010

Groovy Coolness - How to save files to database

I had a dilemna today. As part of a code upgrade I moved the storing of some template files out of the database and over to cache store, or directly from resource. Before this we had to manually load new templates into database for each release. Gets quite messy... Anyway....

I needed to get all the files back into the database, but needed to do it automatically... Because the table was altered to store the actual file name instead of the file, it makes it quite simple to do... And here comes groovy to the rescue.

With this small amount of code, I was able to read all records in database, get the file associated to that record and store the actual file contents back to the database. Sweet :)


def db=Sql.newInstance("jdbc:db2://192.168.20.9:50000/TESTDB","foo","bar","com.ibm.db2.jcc.DB2Driver")

db.eachRow("SELECT template_name FROM form_version") { row ->
println "Reading file " + row.template_name
def f= new File("d:/testing/resources/$row.template_name")
println "Found $f"
if (f.canRead()) {
def b = f.getText("UTF-8")
db.executeUpdate "UPDATE form_version SET dcs_form_layout=$b WHERE template_name=$row.template_name"
println "All done"
}
else {
println "Cannot read file $f. This could be real bad!!"
}
}



Groovy makes database and file manipulation a real breeze. If I had used java there would be a lot of lines of code. Makes my day so much quicker!