Archive

Posts Tagged ‘Java’

Podcasts for Developers

April 29th, 2011

Until I started to travel on a regular basis I haven’t been aware of the value and information that can be found audio podcasts for developers. I thought, “Come on, what can you learn from listening to a radio?”.

Nevertheless, since I had to fulfill my time with something other than staring at empty space (I travel by night) I tried listening to some podcasts and since then I haven’t stopped to follow the ones that I liked the most.

In my opinion (being a Java developer most of the time), Java posse is the best podcast around. The guys behind it are very amusing, have different characters (opinions), and often make a roundups on hot topics where they include more people in discussions. For example, episode #337 they made together with guys from .net rocks which is I guess the best podcast for .net developers.

Apart from Java posse I like listening to grails podcast although they are not as regular with new stuff as Java posse is.

In near future I plan to check some python or ruby podcasts and compare their awesomeness to Java posse.

Try listening to any podcast for developers and you won’t regret. The feeling you’ll probably have is like you have found yourself a good developer friend who is willing to share his personal insights and programming knowledge with you. At least that is how it was in my case.

Share/Save/Bookmark

General, Java, fun, groovy, ruby , , , , ,

Dbunit composite primary keys

April 20th, 2011

For some task I was doing I had to make a snapshot of mysql database having only jdbc connection available. I wasn’t able to use mysqldump (remote access) or other backup tools (I wanted to do this as part of my java program - programmatically).

So, I came across http://dbunit.sourceforge.net/ which allowed me to backup my database to xml file.

The routine is rather simple (please note the escape parameter setting which is required if (like in my case) author of legacy code you have to use put ‘table’ or some other odd name as a column name :) :

public void saveDb(OutputStream os) 
   throws DatabaseUnitException, SQLException, IOException {
 
   IDatabaseConnection connection = new DatabaseConnection(getConnection());
   DatabaseConfig config = connection.getConfig();
   config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, 
      new MySqlDataTypeFactory());
   config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, 
      new MySqlMetadataHandler());
   config.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "`?`");
 
   IDataSet fullDataSet = connection.createDataSet();
   DefaultTableFilter tableFilter = new DefaultTableFilter();
   tableFilter.excludeTable("ignoredtable*");
 
   IDataSet filteredDataSet = new FilteredDataSet(tableFilter, fullDataSet);
   FlatXmlDataSet.write(filteredDataSet, os);
 
}

Then, when I wanted to restore it with:

public void restoreDb() throws Exception {
 
   IDatabaseConnection connection = new DatabaseConnection(db.getConnection());
   DatabaseConfig config = connection.getConfig();
   config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, 
      new MySqlDataTypeFactory());
   config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, 
      new MySqlMetadataHandler());
   config.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "`?`");
 
   IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File(fileName));
 
   DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
   DatabaseOperation.REFRESH.execute(connection, dataSet);
}

This method wont’t work if you have a table without primary key or oddly defined composite keys. It will throw: org.dbunit.dataset.NoPrimaryKeyException

The solution is to add primary key filter configuration option like this:

config.setProperty(DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, new IColumnFilter() {
 
 Map<String, List<String>> tablePrimaryKeyMap = new HashMap<String, List<String>>();
   {
      tablePrimaryKeyMap.put("table_with_composite1",
         Arrays.asList(new String[] { "composite_part1", "composite_part2" }));
      tablePrimaryKeyMap.put("table_with_composite2",
       Arrays.asList(new String[] { "other_part1", "other_part2", "other_part3" }));
      // ...
   }
 
   @Override
   public boolean accept(String tableName, Column column) {
      if (tablePrimaryKeyMap.containsKey(tableName)) {
         return tablePrimaryKeyMap.get(tableName).contains(column.getColumnName());
      } else {
         // you can add default handling if for example all
         // other table ids are named as 'id'
         return column.getColumnName().equalsIgnoreCase("id");
      }
   }
});

And that’s it. Yee!

Share/Save/Bookmark

Java, testing , , , ,

Debugging GUI events

September 22nd, 2009

Debugging gui events is usually not possible with regular line per line debugging and is done mostly by tracing.
For debugging code that relies on Java swing events like drag and drop, mouse moving, etc., having something similar to code below is very useful:

 
public boolean isMouseAboveHeaderPanel() {
System.out.println("HeaderPanel.isMouseAboveHeaderPanel() called from: "
+ Thread.currentThread().getStackTrace()[2].getClassName() + "."
+ Thread.currentThread().getStackTrace()[2].getMethodName());
 
// ...
 
}

In this way you trace not only current function but also you got information from where you entered that function. Of course to make things easier you should make some kind of a shortcut for entering this trace line. In Eclipse you can do that with code template:

System.out.println("${enclosing_type}.${enclosing_method}() called from: "
+ Thread.currentThread().getStackTrace()[2].getClassName() + "." 
+ Thread.currentThread().getStackTrace()[2].getMethodName());

This helped me in lot of situations, I hope that it will help you as well.

Share/Save/Bookmark

General, Java , ,

Groovy meliorates and dries

June 12th, 2009


Dynamic languages are very popular at this time and you probably had insight into at least one of them (Ruby, Python, Groovy).

If you are bound to Java platform the best way to “be dynamic” IMHO is to work with Groovy.

Apart from the http://groovy.codehaus.org/ where you can find documentation, getting started examples etc. there are many blogs around that blog about groovy.

My favorite (although very slow) is Andres Almiray’s blog. He mostly blogs about griffon and groovy and has nice tutorials and examples. There I found info about many open source libraries I never heard of but which are quite useful, for example: FEST, Glazed list, Swing clarity, etc.

Try Groovy cause it’s groovy :)

I felt like when I run my first program at elementary school and it wrote “Ninja” on the screen :)

Share/Save/Bookmark

Java, groovy ,

HtmlUnit as Java Screen Scraping Library

January 23rd, 2009

If you are needing behavior ‘as though a real browser was scraping and using the page’ HtmlUnit is definitely the best option available. It was designed for testing websites but works great for screen scraping and navigating through multiple pages. It takes care of cookies and other session-related stuff and can execute (if you want it to) the Javascript in the page.

I’ve personally tried several other tools from this list (Jericho, Web Harvest) but neither of them is as good as this library. For example, writing a screen scraper with Web Harvest is an easy task, but badly formatted pages cause xml parser to break and this happened to me a lot of times. Jericho is ok but it took me much more coding to achieve the same as with HtmlUnit.

Take a look at HtmlUnit getting started and start scraping in no time.

Share/Save/Bookmark

Java , , , ,

Swing blog recommendation

December 9th, 2008

Pushing-pixels by Kirill Grouchnikov is a dynamic blog on GUIs with a special attention to swing. It has weekly digest on popular swing links and is good place to start when searching for news in that field.

Share/Save/Bookmark

Java, Links , , ,

Java Resources

October 22nd, 2008

Ok, lets recommend resources in following categories: ebooks, blogs, forums.

Ebooks:

The JavaSeries is supported, endorsed, and authored by the creators of the Java technology at Sun Microsystems, Inc. It is the official place to go for complete, expert, and definitive information on Java technology. The books in this Series provide the inside information you need to build effective, robust, and portable applications and applets. The Series is an indispensable resource for anyone targeting the Java 2 platform.

Actually this quote is 100% true.

Blogs:

The recommendation goes to Artima.

Artima Developer is an online community where developers learn from experts in the software industry as well as interact, share information, and learn from each other.

Forum:

Javaranch - In two words - great forum. There are lot of interesting discussions at all levels (from beginner to expert). Nearly 200,000 members. Book writers are frequent forum visitors.

Share/Save/Bookmark

Java, Links , , ,