Archive for the ‘java’ Category

Java WebStart UnavailableServiceException

Thursday, August 7th, 2008

If you are getting javax.jnlp.UnavailableServiceException from javax.jnlp.ServiceManager.lookup() or if javax.jnlp.ServiceManager.getServicesNames returns null you must be aware that those call only return useful thing when you run your code fromt the Java Web Start environment.

It´s not enough to include the jnlp.jar or javaws.jar in the classpath.

For example to make it work from Netbeans you must enable WebStart in the project and Set Configuration to WebStart.

Adding Netbeans API source navigation to your Netbeans IDE

Thursday, February 14th, 2008

It took a while to find this entry describing how to include the Netbeans APIs source code. In short, you have to download the actual sources (the link in Geertjan’s Weblog is outdated) at http://download.netbeans.org/netbeans/6.0/final/zip/ (file netbeans-*-platform-src.zip) and then link that zip file to the platform in the platform manager. Make sure that the path doesn´t contains whitespaces (it didn´t work for me).

To access the Netbeans Platform Manager just do Tools -> Netbeans Platforms

netbeansplatformmenu.png

And then click on the Sources tab and “Add Zip/Folder”

netbeansplatformsource.png

The process is documented in the Netbeans help at Help > Help Contents > Netbeans modules > Using the Netbeans api > Extending Skeleton API Implementation > Registering the NetBeans Sources and Javadoc.

More graphs

Sunday, November 18th, 2007

I continue looking for graph visualization software. I rn into prefuse. Looks promising, take a look into the gallery to see some examples.

prefuse1.png

But it´s still only a toolkit so it means that I have to develop at least a little application to be able to visualize my graph. I tried using the examples that come with prefuse and they work but they are not enough. I just converted my data to GraphML and I was able to load it into the GraphView example. But I need to add some functionality to it before I can actually use it to analyze my graph. I´m not sure I can use prefuse to print out the graph anyway so I´m giving up for a while. This graph thing it´s taking too much time, but I will try to come back to it some day.

Graph visualization

Wednesday, November 14th, 2007

guess.jpgAfter spending a bunch of time with the graphviz utilities (neato, dot, circo and twopi) trying to create a graph to understand the software dependencies between modules in a project I´ve been I assigned I realized that those tools don´t work well with large graphs. I tried and tried with no luck, I thought that the old linux kernel map poster used graphviz to create the poster but after googling a bit and reading the code for the FCGP project I found that they write PostScript directly. I was looking for something simpler.

After giving up with graphviz I found a bunch of tools for working with graphs most of them are toolkits to develop your own tools but then I hit GUESS. GUESS is a graph browser so to say. You generate a .gdf similar to graphviz’s .dot files and GUESS will layout the graph for you. But the really good thing is that it is a browser you can actually interact with the graph, changing the layout or coloring items, etc. It comes with a scripting language that allow to manipulate the graph very easily and to neat things like change the size of the nodes depending on the value of certain field of the node. All this in runtime. So if you wan to spot all the nodes in your graph where the field “module” is equal to “provisioning” you just enter this in the console

selectednodes = (module == "provisioning) 
selectednode.color = red

Pretty easy doesn´t it

Passing configuration parameter to Axis2 services

Tuesday, October 16th, 2007

One way to pass parameters to you Axis2 service:

1) write a <parameter> tag inside your services.xml

 
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was auto-generated from WSDL -->
<!-- by the Apache Axis2 version: 1.3  Built on : Aug 10, 2007 (04:45:47 LKT) -->
<serviceGroup>
    <service name="xxxxxxx" class="MyServiceLifeCycleImpl">
 
....
        <parameter name="jdbcConnectionString">jdbc:derby:c:/demoderby2b;create=true;user=a;password=b;</parameter>
        <parameter name="jdbcDriver">org.apache.derby.jdbc.EmbeddedDriver</parameter>
 
...
    </service>
</serviceGroup>

2) Write a ServiceLifeCycle class


public class MyServiceLifeCycleImpl implements ServiceLifeCycle {
    private Log log = LogFactory.getLog(MyServiceLifeCycleImpl.class);

    public void startUp(ConfigurationContext confCtx, AxisService axisService) {
        try {


            String jdbcConnectionString = (String) axisService.getParameterValue("jdbcConnectionString");
            String jdbcDriver = (String) axisService.getParameterValue("jdbcDriver");
            Class.forName(jdbcDriver).newInstance();
            Connection connection = DriverManager
                    .getConnection(jdbcConnectionString);
            axisService.addParameter("jdbcConnection", connection);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

....


3) Add init method to your service


....
       private ServiceContext serviceContext;

    public void init(ServiceContext serviceContext) {
        this.serviceContext = serviceContext;

    }
....

4) Access the parameter jdbcConnection from your service through serviceContext


....
        Connection conn = (Connection) serviceContext.getAxisService()
                    .getParameterValue("jdbcConnection");

....

How to get information on UNIQUE keys on Apache Derby

Tuesday, October 16th, 2007

It´s not easy to get information on derby keys once you created them. I was looking for a command like “SHOW CREATE TABLE <tablename>” but no luck. I realized that the answer should lay in SYS tables. After googling a while I found the following bit of wisdom:

The following query will give you the UNIQUE constraints on a table:

select c.constraintname, c.constraintid
from sys.systables t, sys.sysconstraints c
where t.tablename = 'FOO'
and t.tableid = c.tableid
and c.type = 'U'
;

The following query will return a descriptor object for each constraint
on the table.  The descriptor will tell you which columns are in each
constraint. As noted in the Reference Guide section on
SYS.SYSCONGLOMERATES, the descriptor object implements
org.apache.derby.catalog.IndexDescriptor. Please note that the
descriptor object is not part of Derby' public API and can therefore
change from release to release:

select g.descriptor
from sys.systables t, sys.sysconstraints c, sys.syskeys k,
sys.sysconglomerates g
where t.tablename = 'FOO'
and t.tableid = c.tableid
and c.type = 'U'
and c.constraintid = k.constraintid
and k.conglomerateid = g.conglomerateid
;

Two web applications sharing the same Derby database

Monday, October 15th, 2007

I just realized that to be able to open/share the same Derby database from two different web applications running in the same Tomcat instance (same JVM) you´ll need to put derby.jar in the $TOMCAT_HOME/common/lib and remove it from your applications WEB-INF/lib. I got the clue from this RIFE web page

the jarfiles you need are derby.jar and derbytools.jar . Due to classloader peculiarities, don’t copy them to your application’s web/WEB-INF/lib/ subdirectory, or to Tomcat’s shared/lib/ directory. Tomcat’s common/lib/ directory works, and probably common/endorsed/ does too.

Dojo, JSON, Xstream and FLEXJSON

Tuesday, October 9th, 2007

I´ve been trying to use XStream to generate JSON to be consumed by Dojo. But I can´t find the way to generate the right JSON from XStream, it keeps adding extraneous {} around. I even tried this but no luck . For example I want to generate this JSON output (taken from Dojo FilteringSelect example) :


{identifier:"abbreviation",
items: [
    {name:"Alaska", label:"Alaska",abbreviation:"AK"},
    {name:"Wyoming", label:"Wyoming",abbreviation:"WY"}
]}

My attempt usign XStream:



package com.rubenlaguna.json;

import java.util.ArrayList;
import java.util.Collection;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

class ItemCollection {
    public String identifier;
    public Collection<Item> items = new ArrayList<Item>();

    public ItemCollection(String identifier) {
        this.identifier = identifier;
    }

    public boolean add(Item arg0) {
        return items.add(arg0);
    }

}

class Item {
    public String name;
    public String label;
    public String abbreviation;

    public Item(String name, String label, String abbreviation) {
        this.name = name;
        this.label = label;
        this.abbreviation = abbreviation;
    }

}

public class WriteXStreamDojoTest {

    public static void main(String[] args) {
        ItemCollection itemCollection = new ItemCollection("abbreviation");
        itemCollection.add(new Item("Alaska","Alaska","AK"));
        itemCollection.add(new Item("Wyoming","Wyoming","WY"));
        
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        String erroneousJsonStr = xstream.toXML(itemCollection);
        System.out.println(erroneousJsonStr);

    }

}


resulted in the following JSON Output (i added some space and breaklines for readability):


{"com.rubenlaguna.json.ItemCollection":
       {"identifier":"abbreviation",
         "items":{ "@class":"list",
                      "com.rubenlaguna.json.Item":[  
                         {"name":"Alaska","label":"Alaska","abbreviation":"AK"},
                         {"name":"Wyoming","label":"Wyoming","abbreviation":"WY"}
                       ]
                   }
      }
}


I couldn´t figure out how to get XStream to output the desired JSON. The main issue is that I couldn´t make “items” a simple JSON array, it always end up as an object with an array in it. So I decided to give out a try to FLEXJSON. I really found much easier to control the output with this tool. I guess that XStream is more focused on serialization/deserialization and doesn´t allow customization without relaying in custom converters (I didn´t walk that road, too much work). Here is the example source code usign FLEXJSON:


package com.rubenlaguna.json.flexjson;

import flexjson.JSONSerializer;

public class WriteFlexjsonDojoTest {

    public static void main(String[] args) {
        ItemCollection itemCollection = new ItemCollection("abbreviation");
        itemCollection.add(new Item("Alaska", "Alaska", "AK"));
        itemCollection.add(new Item("Wyoming", "Wyoming", "WY"));

        JSONSerializer serializer = new JSONSerializer().include("items").exclude("*.class");
        String correctJsonStr = serializer.serialize(itemCollection);
        System.out.println(correctJsonStr);

    }

}


that yields the following correct result:


{"identifier":"abbreviation",
  "items":[ {"name":"Alaska","label":"Alaska","abbreviation":"AK"},
              {"name":"Wyoming","label":"Wyoming","abbreviation":"WY"}
            ]}

Eclipse Bug #201116 update: Switch compare viewer for java files

Thursday, September 6th, 2007

I’ve submitted a patch (Attachment #77777 and a couple of screenshots ( Attachment #77774 y Attachment #77776) to Bug #201116. It’s only a preliminary work but it enables the user to select with contentMergeViewer to use with each FileType/ContentType. Now it’s only useful if you use my java formatting compare plugin. Currently the org.eclipse.compare subsystem will allow only one contentMergeViewer per fileType or contentType and you cannot tell which one it will be as Szymon Brandys comments. With this patch the user can select/switch which one he wants to use among all viewer registered for a given file extension/content type.

comparecontenttypeassociationspreferencepage.png

comparefileassociationspreferencepage1.png

Bug 201116 – Compare will silently discard additional contentMergeViewers associated with the same file extension

Friday, August 31st, 2007

I filled Bug 201116 – Compare will silently discard additional contentMergeViewers associated with the same file extension a couple of days ago. As Szymon Brandys says eclipse should allow the user to switch between the differente merge viewer available for a given extension. He proposes two solutions:

1) Having a mechanishm inside the compare view to do the switch 2) or having a preference page to do the same

I feel more inclined towards the second solution, as I’m used to change the defautl editor for .xml, etc though the preference page General -> Editors -> File Associations. Having a similar preference page would fit me. I’m trying to figure out how to integrate a preference page to org.eclipse.compare and send it back to eclipse.org to include it in the next release.