Thursday, October 15, 2009

Publishing a JAX-WS Endpoint not just to localhost

Interestingly enough, publishing the JAX-WS Endpoint with endpoint.publish(url) as described in a previous post, publishes the WSDL to http://localhost:8080 but this socket is not accessible from any other host ! Strange default, but there you go ...
So in order to really make a JAX-WS Endpoint public to the world, you can revert to the slightly more cumbersome API of com.sun.net.httpserver.HttpServer (at least on Sun's JDK):

HttpServer server = HttpServer.create(new InetSocketAddress(port), 3 ); // backlog
server.start();
  
endpoint.setExecutor( Executors.newFixedThreadPool(10) );
  
// publish WebService
HttpContext wsContext = server.createContext( "/" + service );
  
//wsContext.setAuthenticator(new TestBasicAuthenticator("test"));
endpoint.publish( wsContext );

Sunday, October 4, 2009

Quarkbase

Ever wondered who are the key people behind a website ?
Check out http://www.quarkbase.com/ and enter the URL to check.
Not always perfect, though ... ;-)

Monday, September 7, 2009

60 seconds on SOAP-based Web Services

According to this book, there are 74 distinct initiatives trying to define what a Web Service should look like. And I bet, there are at least 74 different abbreviations around to describe different parts of it. So it's surprising how simple it is to create a Web Service with JDK 1.6. First you define an interface with a bunch of annotations:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
// use RPC or 'wrapped' document-style
@SOAPBinding(style = Style.RPC)
//@SOAPBinding(style = Style.DOCUMENT,
// parameterStyle = SOAPBinding.ParameterStyle.WRAPPED )
public interface Example
{
 @WebMethod public String getGreeting( @WebParam(name="myname") String myname);
}
And then implement the service itself. Use a thread-pool if your application is multi-threaded.

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(endpointInterface = "simple.Example")
public class ExampleImpl implements Example
{
 @Override
 public String getGreeting(String myname)
 {
   return "hello, " + myname;
 }

 public static void main(String[] args)
 {
   String url = "http://127.0.0.1:9876/example";
   System.out.println("Starting WebService at: " + url);
   System.out.println("WSDL available at:      " + url + "?wsdl");

   Endpoint endpoint = Endpoint.create(new ExampleImpl());
   //endpoint.setExecutor( Executors.newFixedThreadPool(10) );
   endpoint.publish(url);
 }
}
The resulting WSDL definition of this service can be looked at http://127.0.0.1:9876/example?wsdl (use Firefox or IE, not Chrome!). Any SOAP-based client (such as the XML Test Utility in General Interface, see previous post) can now invoke this service.
REST-style would be harder to implement, but easier to test. In the REST case, you can simply do: http://127.0.0.1:9876/rs?name="Pete" for invocations. The difference to the previous solution is that with REST-style, the client uses an HTTP GET to invoke a method and the return value can be any kind of XML, not just a SOAP envelope.
The code above implements the main 'standard', but its also the most bloated possibilty. More efficient, but less standardized, is a message encoding of JSON instead of XML, or better, use the Google Web Toolkit. If you are in full control of the server and the client and don't need to follow standards, GWT seems an excellent choice right now.

Friday, September 4, 2009

General Interface Primer

There's now an open-source Ajax GUI builder called General Interface which looks quite promising. A bunch of components that are somewhat familiar to Swing developers. Of course, all logic is Javascript :-(
After installation, here some tips that could come in handy:
  1. Look at the video tutorials, the best one for tables(matrix) isn't actually on that page, but here.
  2. Defining Javascript functions doesn't always work, even after Save/Reload. Try to restart and reload the page before giving up.
  3. You might sneeze at the package names, but trust me, in Javascript conflicts are easy to create. Here's a simple example on a function called jmsw.doAction(index) :
    
    // name of the package to create: jmsw
    jsx3.lang.Package.definePackage(  "jmsw", function(jmsw) {
     jmsw.doAction = function(index) // define doAction(index)
     {
       ...
     };
    });
    
  4. Do Control+Click on a component (such as a button) to locate its definition in the 'Component Hierachy'.
  5. Float a Palette to get more space or to move it to the bottom panel: Upper Right Corner of a Palette View, click on 'Floating'. Use Menu->Palettes to enable/disable a palette.
  6. The 'XML Mapping Tool' always starts with the same default of ..Address.wsdl, but this will not be the one you will be working with. To change the default of this initial URI, edit C:\TIBCO\gi-3.8-max\GI_Builder\plugins\jsx3.ide.mapping\components\Inputs\wsdl.xml and search for 'Address, comment it out and add your own WebService URI:
    
           <!-- use different default for XML Mapping Tool:
           <strings jsxname="jsx_schema_wsdlurl" jsxvalue="jsxplugin://jsx3.ide.mapping/samples/Address.wsdl" jsxwidth="100%"/>
           -->
    
           <strings jsxname="jsx_schema_wsdlurl" jsxvalue="http://127.0.0.1:9876/rs?wsdl" jsxwidth="100%"/>
    

Monday, August 31, 2009

Java Bi-directional Maps: Apache commons or Google collections ?

In case you need a bi-directional Map in Java, you can, of course, easily roll your own.
But this is a common problem, so others have done this work already.
Here's a simple how-to on bi-directional maps with Apache and Google commons.
Apache commons collections offers a BidiMap interface with various implementations.
For example:
public class TestBidiMap
{
static BidiMap bimap = new DualHashBidiMap();
public static void main(String[] args)
{
bimap.put("D", 3);
System.out.println("D -> " + bimap.get("D") );
System.out.println("3 -> " + bimap.inverseBidiMap().get(3) );
}
}
Nice and easy, but unfortunately this version of BidiMap doesn't use Java generics, which I have learned to like over time (bimpap.get(3) will not show a compile error) !
Google collections has an alternative that uses generics: BiMap
public class TestBiMap
{
static BiMap<String,Integer> bimap = HashBiMap.create();
public static void main(String[] args)
{
 bimap.put("D", 3);
 System.out.println("D -> " + bimap.get("D") );
 System.out.println("3 -> " + bimap.inverse().get(3) );
}
}
Also nice and easy, what's interesting is the factory method HashBiMap.create(). This removes the need to duplicate the generics type specification of String, Integer. Kiss rules, so two points for Google collections here.
Summary: any mid-size Java project probably shouldn't live without Apache commons, but as a useful addition, Google collections is definitely worth a look.

Monday, August 17, 2009

Amazon S3 intro

Started to look into Amazon EC2/S3 recently (technology you should know about).
Here's a short S3 intro, one with lots of screenshots and therefore easy to digest:

Monday, August 10, 2009

Spring and Pipes&Filters

Finally an IoC container that almost everyone agrees on: Spring.
After many inhouse, half-baked attempts and 'framework wars' a default solution. TIBCO folks might remember the days of TAF (1996), GAF, XXTAF or PushBeans. Also
openadaptor.org should be noted here.

Now Spring certainly is nice and generic, but for building EAI adapters or 'message processing pipelines' it is not ideal. In case of these pipelines, the message path is very structured: from a source bean through several transformation beans until it finally reaches one (or many) sinks. XML already imposes an 'order of things' so an ideal configuration language of a little adapter might look something like this:
<pipelines> <pipe name="Jdbc2JMSPipe"> <jdbc dbref="db1" querysql="select * from table1"> <concatenate method1="getFirstName" method2="getLastName" result="setName"> <jmsObject2MapMessage> <jmsPublish topic="PERSON"> </pipe> </pipelines> This example configures a processing pipeline to get rows from a JDBC connection, invoke the concatenation filter, convert an object to a JMS MapMessage and finally publish the result to a JMS topic named 'T.PERSON'.

The configuration above is easy to understand and manipulate. One can imagine extensions with for-loops, if-clauses etc.  For example:<pipelines>
<pipe name="Jdbc2JMSPipe"> <jdbc dbref="db1" querysql="select * from table1"> <concatenate method1="getFirstName" method2="getLastName" result="setName"> <jmsObject2MapMessage> <if method="getName" startswith="a-z"> <jmsPublish topic="PERSON.LOWERCASE"> <else> <jmsPublish topic="PERSON.UPPERCASE"> </else> </if> </pipe> </pipelines> Configuring the first example pipeline with Spring could look like this: <beans> <bean id="1" class="com.abc.jdbc" dbRef="db1" querySql="select * from table1"> <property name="next"> <ref bean="2"> </property> </bean> <bean id="2" class="com.abc.concatenate" method1="getFirstName" method2="getLastName" result="setName"> <property name="next"> <ref bean="3"> </property> </bean> <bean id="3" class="com.abc.jmsObject2MapMessage> <property name="next"> <ref bean="4"> </property> </bean> <bean id="4" class="com.abc.jmsPublish" topic="T.PERSON"/> </beans> The order of beans is defined explicitly although XML already provides an order.
A nasty user of this system could even reorder the beans in a different order :-(
So maybe some XSLT to transform from a 'pipes&filters' language to a Spring configuration ?
Or extend Spring with this kind of processing step ?
Or how about a XML-aware bean reference: <beans> <bean id="1" class="com.abc.jdbc" dbRef="db1" querySql="select * from table1"> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="2" class="com.abc.concatenate" method1="getFirstName" method2="getLastName" result="setName"> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="3" class="com.abc.jmsObject2MapMessage> <property name="next"> <ref bean="following-sibling"> </property> </bean> <bean id="4" class="com.abc.jmsPublish" topic="T.PERSON"/> </beans>
Currently, I think XSLT would be the best solution since it keeps the clarity of the pipes&filters language.