Archive

Archive for the ‘Java’ Category

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 , , , ,

Apache CXF Linux Deployment

April 11th, 2011

A short reminder of small issues I had with deploying Apache CXF web service I built on linux server (CentOS in my case):

yum install tomcat5 went really well :) and then I had to add webapps and manager packages with separate commands.

tomcat5 package depends on jdk-1.4 so in case you haven’t already installed a more recent version of java you’ll get this one.

I used the tutorial from http://www.jroller.com/gmazza/entry/web_service_tutorial to set up the options for deploying to tomcat using maven and that went well, but jdk-1.4 was causing spring context loader to throw:

SEVERE: Exception sending context initialized event to listener instance of
class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org.springframework.core.CollectionFactory
at java.lang.Class.initializeClass(libgcj.so.7rh)
...

and the actual error displayed when I tried to access the service from the browser was:

threw exception javax.xml.transform.TransformerFactoryConfigurationError:
Provider org.apache.xalan.processor.TransformerFactoryImpl not found  at
javax.xml.transform.TransformerFactory.newInstance(Unknown Source)  at
org.apache.cxf.helpers.XMLUtils.newTransformer(XMLUtils.java:118)

This was solved by installing open-jdk 1.6 devel package with yum, but then another, trickier error appeared:

java.lang.NoClassDefFoundError: org/apache/xml/serializer/OutputPropertiesFactory
at org.apache.xalan.templates.OutputProperties.(OutputProperties.java:82)
at org.apache.xalan.transformer.TransformerIdentityImpl.(TransformerIdentityImpl.java:86)
...

At several links (here and there) I figured it out that it has something to do with xalan.jar which was unavailable to my service deployed on tomcat. A command:

yum list xerces* xalan* | grep installed

showed that I already had xalan-2.7.1 in my /usr/share/java so I tried with copying it to CATALINA_HOME/common/endorsed but that didn’t fix the problem. I also tried symlinking xalan.jar to that file but that didn’t work either.

In the end, at the link which appeared to be some forum’s archive I found that someone had problems similar to this and was advised revert to xalan-2.6 version. I’ve tried with jar in http://archive.apache.org/dist/xml/xalan-j/binaries/xalan-j_2_6_0-bin.zip and it worked.

Yeee “me happy” :)

Share/Save/Bookmark

Java, Links, linux , , ,

Griffon eclipse support plugin

December 3rd, 2010

Recently I was playing  with griffon which I used together with jung library to visualize binary decision diagrams. In this regard, I had to make several small projects. Since I do that in my spare time, at the moment I start a new project I forgot how I managed to integrate previous griffon project with eclipse IDE (actually I am using Spring Tool Suite with Groovy and Grails support but I guess it’s the same as with eclipse) so that IDE doesn’t pop up nasty message similar to:

The project was not built since its build path is incomplete.
Cannot find the class file for groovy.lang.GroovyObject.
Fix the build path then try building this project.
The type groovy.lang.GroovyObject cannot be resolved.
It is indirectly referenced from required .class files 
Config.groovy    /myprojectname/griffon-app/conf    line 1

I used: griffon integrate-with -eclipse command which actually allowed me to import griffon project to eclipse but that wasn’t enough.

The solution was eclipse support plugin.

Having that written down I hope I won’t forget it again. :)

Share/Save/Bookmark

Java, groovy , , ,

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 blob and mysql

September 20th, 2009

In certain occasions e.g. when you want to save scrapped html page or just a specific part of it (e.g. div with its contents) the recommendation I found is to use the blob type and save xml document as binary stream.

In groovy, this is achieved in a rather simple way:

class Document {
  int id
  String title
  String xmlContent
}
 
Sql sql = Sql.newInstance(
          "jdbc:mysql://localhost:3306/documents_db?useUnicode=true&characterEncoding=UTF-8&useBlobToStoreUTF8OutsideBMP=true",
          "user", "pass", "com.mysql.jdbc.Driver")
 
// We assume db table similar to:
sql.execute("""
    CREATE TABLE IF NOT EXISTS `document` (
      `id` bigint(32) NOT NULL AUTO_INCREMENT,
      `title` varchar(500) NOT NULL,
      `xmlContent` blob,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    """)
 
def saveDocuments(def docs) {
  println "Saving documents to database..."
  def documents = sql.dataSet("document")
  docs.each {Document doc ->
    documents.add(title: doc.title, xmlContent: doc.xmlContent)
  }
}

When you want to retrieve data, with Groovy this is not much complicated either:

def loadDocuments() {
  def list = []
  sql.eachRow("SELECT * FROM document") {
    Document doc = new Document(it.toRowResult())
    if (it['xmlContent'] != null) {
      doc.xmlContent = new String(it['xmlContent'])
    }
    list.add(doc)
  }
  println "Loaded ${list.size()} documents from database"
  list
}

I tried the code with mysql database but I assume that you can achieve the same with any other database.

One hint for mysql blobs and utf-8: although utf-8 charset behaves very well with other database table fields (if you add useUnicode=true and characterEncoding=UTF-8 parameters to your connection string), in order to work with utf-8  blob fields you should adjust one more parameter: useBlobToStoreUTF8OutsideBMP=true. With this parameter set, your utf8 encoded xml documents will behave well with mysql database.

Share/Save/Bookmark

Java, groovy , , ,

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 , , ,