Pages

Wednesday, December 17, 2008

SSH Tips-and-Tricks

I have some ssh tips that I'm using time to time to connect to the university servers. This is cool because I can use the software from the university labs while being at home. So I don't need to drive 20 km both ways just for a single experiment.

X11 forwarding

You can encrypt X sessions over SSH. Not only is the traffic encrypted, but the DISPLAY environment variable on the remote system is set properly. So, if you are running X on your local computer, your remote X applications magically appear on your local screen.

You can enable X11 forwarding with ssh -X host.


Compressing data

SSH can use gzip compression on any connection. The default compression level is equivalent to approximately 4x compression for text. Compression is a great idea if you are forwarding X sessions on a dial-up or slow network. Turn on compression with ssh -C.


Roaming behind the firewall.

Suppose you have to connect to machines, that are located in some remote place behind a firewall. The gateway "G" is a remote server that has the authority to connect to those machines.

What we would like to do is to use any kind of X11 UI on workstation A, for instance. We need two ssh tunnels to connect to the workstations. With the first tunnel we will remap the local port using -L option. The general syntax will be:

ssh -L{local_port}:{workstation}:{remote_port} {user}@{hostname}

After this the gateway will become transparent for us. The second tunnel created for localhost will actually go forward to the gateway and connect to {workstation}.

ssh -X -o "HostKeyAlias {workstation}" {user}@localhost

Given the example above, let's try to connect user "ant" to the workstation "corona" via "aragorn" gateway. Note that we want to use X11 forwarding to be able to work with graphical environment. There are several ways to do this. I'll describe two possible solutions here (assuming that you have M$ Windows running on your PC).


Cygwin

With Cygwin environment one may get the connection to the workstation using following steps.

  1. Start cygwing environment

  2. Start X11 environment, type strartx

  3. Ensure you have at least two xterm windows. Type xterm & to start another xterm

  4. Create first SSH tunnel: ssh -L2222:corona:22 ant@aragorn

  5. In another xterm window create the second SSH tunnel: ssh -X -C -o "HostKeyAlias corona" -p 2222 ant@localhost




To verify is the X forwarding works fine, just type any app. name to the remote xterm, like xclock - and the xclock application should open on your machine while actually running on the remote host.


WinAxe + Putty

If you don't have cygwin environment installed and you're unwilling to install it, then we have another solution here. WinAxe and putty can be used to create the same SSH tunnels as with cygwin. Follow the steps:


  1. Install WinAxe, and run XSession

  2. Start putty, specify the connection parameters and create a tunnel as shown on the pictures below.



  3. Start another putty window and create new tunnel against localhost with X-forwarding enabled.




After putty sessions are started you can do the same trick again as with cygwin, start a remote application so that it looks like running on your machine.

Tuesday, December 2, 2008

Oracle 10g JDBC driver and BigDecimal.toString()

I would like to share an issue with you regarding Oracle 10g JDBC driver and migration to Java 5.

Recently we encountered a production bug where a client got some extra money on his/her account. The bug appeared with Oracle JDBC driver (ojdbc14.jar) version 10.1.0.4 once we migrated the application to Java 5 from Java 1.4.

In order to safe the precision in Java it is the common practice to use BigDecimals. Oracle driver, while binding the objects to SQL types, is calling BigDecimal.toString() method. In Java 5, the method was altered in order to support JSR-13:Decimal Arithmetic Enhancement standard. This issue exists in Sun bug database

Here's a piece of code to reproduce this problem:
-- table for the test
create table test_table ( value number )


// java code to reproduce the behaviour
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class HelloFromOracle {
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:user/passwd@DB:1521:SID");
String query = "insert into test_table values (?)";
PreparedStatement stmt = connection.prepareStatement(query);
BigDecimal bd = new BigDecimal("0.000000000000001");
stmt.setBigDecimal(1, bd);
stmt.execute();
stmt.close();
connection.close();
}
}


Now, with Oracle driver v 10.1.0.4 the value in database will be:
> select * from test_table
VALUE
-------------------------
5115

... And with Oracle driver v 10.1.0.5
> select * from test_table
VALUE
-------------------------
0.000000000000001


So my message is to replace the Oracle driver 10.1.0.4 to 10.1.0.5, especially with migration to Java 5 from Java 1.4. What is interesting is that oracle driver 8.1.7.4 doesn't suffer from this problem.

Thursday, November 20, 2008

Code Kata: The Business Rules

I'm thinking of an application for JBoss Drools. We are given a task to design a system for order management (OMS), which will sit behind a FIX gateway and serve several internal and external systems. One thing is for sure - we are to take a challenge to organize and maintain a large set of business rules and this brings up some memories.

In 2004 a colleague told me about code kata. These are the good exercises to do when learning a new language, or just empirically solve some common problems. One of the puzzlers sit in my mind permanently since then - The Business Rules Kata, which is about the hard to maintain complex business rules.
How can you tame these wild business rules? How can you build a system that will be flexible enough to handle both the complexity and the need for change? And how can you do it without condemming yourself to years and years of mindless support?

We face all these problems daily while coding the business logic for our banking system. One of the approaches some developers have taken is that
the if-the statements can be avoided while using appropriate OOP techniques

The problem is that sometimes while avoiding those if-statements the code becomes over-engineered due to a large number of classes. And it is still doesn't solve the problem of quick changes that are very hard to implement in a big organization with the "release process".

Recently I've found a discussion at joelonsoftware.com. It is dated 2005 but I think not much have changed since then. I think that most of the developers are still fighting the complexity of the business rules and in future it will still be a challenge.

My next step is to create a demo using JBoss Drools, QuickFIX/J. Apache Camel can be used to integrate both into one flow.

Also what is really cool about Drools is that it would allow our clients to maintain the order validation rules apart of the overall release process using Guvnor. So the changes in order validation logic could be introduced rapidly.

The TAGRI Approach To Ducumentation

I found a nice blog post called Developers Aren't Gonna Read It about the software requirements documentation principles, which actually refers to Scott W. Ambler's post called The TAGRI (They Aren't Gonna Read It) Principle of Software Development.
The idea behind these posts is that the developers do not read documentation. And to be honest - yes! it is true! I don't remember myself reading the documentation for a long time. In fact, I don't remember if I really have read a single document produced by analyst from start to end. Thinking of the man-hours spent on the documentation by analysts it may be horrible to calculate the total cost that was spent to produce these documents.
I was responsible for the web UI part of this system (100 pages) but had to understand also how the backend works (200 pages). ... So, did I read the documentation? I didn't. I didn't have to because I preferred direct communication with the guys who know the system throughout.

Indeed, a very familiar situation.
Not all documentation sucks. ... the best documentation is the one that is very easily changeable. Wiki is the best option here - it's easily searchable (through many projects) and changeable.

I love wiki! IMHO, it is one of the best approaches to the communication and to hold the documentation, tips-and-tricks, tutorials, etc.

To me, the best solution is to really communicate verbally. Make it to the whiteboard and draw some concepts together with the analyst. It ensures we both use the same terms, and both understand the domain of the problem. I hope our team members feel the same.

Tuesday, November 18, 2008

Creating Database Link (aka DBLINK) in Oracle

I had to mess with Oracle database links recently. Here's a good reference to the "CREATE DATABASE LINK" sentence syntax.

In my case it was:
CREATE PUBLIC DATABASE LINK MYLINK CONNECT TO USER IDENTIFIED BY PASSWORD USING 'mydb';


'mydb' has to be an entry in ORACLE_HOME/network/admin/tnsnames.ora file on the server side Oracle client installation, so that the host database can see the foreign one (which is 'mydb').

Next, make sure that sqlnet is configured to use tnsnames.ora file. Its configuration is defined ORACLE_HOME/network/admin/sqlnet.ora, which contains client side network configuration parameters (again, on the server side!). For instance:

NAMES.DIRECTORY_PATH=(TNSNAMES, LDAP)


Next, we could test this database link:

SELECT * FROM dual@mydb;


Now it makes sense to use a synonym to point to the foreign table.

CREATE SYNONYM MYSYNONYM FOR dual@mydb;


Now the initial query will look as follows:

SELECT * FROM MYSYNONYM;

Saturday, November 8, 2008

GWT vs Flex

We have now some experience with GWT while implementing an enterprise price management system's UI. There are some nice features that GWT provides, but there are a lot of pain also while using some 3rd party widget library, like GWT-Ext. In my mind, the main problem is that while coding the solution using GWT, you loose the feeling of the application (especially if using a wrapper around a native JavaScript library). But this is just my feeing and doesn't really show if GWT is bad or good. Related to the real problems, when we had too many records to render in a data grid, we faced some performance problems - rendering all the records took quite a time and stalled the browser instance until the end of rendering process.
The 3rd issue I find relevant is the amount of code had to be produced for the layout and interactive behavior of the application. The Swing-style coding is a real blocker. IMHO, GWT probably make heavier use of annotations, perhaps even introduce some custom annotations for simplifying the implementation of user actions (i.e. listeners as inner classes).

After we almost finished the prototype, I started to think that probably, GWT is not really what we'd like to use in our application for the UI. I'm still strongly convinced that for this application we need a rich UI to satisfy the user. Adobe Flex was the next technology in our list. More than that, Flex is approved for our internal development by the architects council. So one our colleague presented us how is he using Flex to implement a RIA for loan management.. and smashing!!! I really liked the code separation for the UI: layout is defined in MXML files, events for the UI components are defined in a separate ActionScript files, and some utility code can also be separated into other scripts. Further more, the speed of making a convenient application was really fast. And also the amount of code to be written is much less than with GWT. No layout problems in the resulted swf, rendering a data grid with thousands of rows was just blizzard-fast compared to JavaScript analog.
I found some comparisons of GWT, Flex and Echo2 on the web: one and two
Both GWT and Flex 2 are Rich Internet Application (RIA) frameworks whose code is downloaded and executed on the client side, in the browser. GWT compiles Java code to JavaScript whereas Flex compiles a combination of ActionScript and MXML code to a Flash swf file.

And here's the quote I was waiting for:
...both GWT and Flex 2 have different situation where they each are better suited. HTML, CSS, JavaScript are still the best option when developing a web application intended for the general public. Flex 2 is would fit well when developing flashy business minded applications with a targeted audience.

I think this describes our case the most - a targeted audience.

Friday, October 3, 2008

Python.bind(C++)

Lately I've started to put pressure on my studies. One task I have taken for now is to extend an old academic application (C++), which involves improving the internal format, as well as the interfaces between sub-programs, etc. It is also due to some integration possibilities that we have to rethink the design of the application.

Now, so far I have no experience with C/C++ application development. Java is no-go, as the other academic tools in our landscape (embedded systems) are mostly C++. Therefore I'm thinking that I need some workaround here in order no to violate the overall structure and do it as quick as if I would do it in Java.

As one of my goals is to create a grammar-based input to the application, the natural choice for me is ANTLR. It provides many targets for the input grammar, C++ included. Now if I'm trying to escape C++ coding, and Java is not the language that is appropriate for this task (I think), then I'd rather pick some scripting language.
Python has a nice extension mechanism as well as ANTLR v3 supports the Python target. Also, it is claimed that the Python learning curve is quite flat, which makes sense in my case.

I've found a great tutorial on the C extensions for Python. What amazes me the most, is the ease of the extension compilation. One way is to compile it using gcc from command line. But the other way is to create a simple Python script (setup.py) which will do everything for you:

from distutils.core import setup, Extension
module1 = Extension('extension_name', sources = ['myfile.c'])
setup (name = 'PackageName',
version = '1.0',
description = 'my demo package',
ext_modules = [module1])


Now the script can be executed in the same directory where the myfile.c resides: python setup.py build.

The only thing I had to do extra on my machine is to install the python-dev package via the Synaptic Package Manager.

What I want to do now, is to create a C++ class hierarchy to model the internal format of the current application, then wrap it with some C++ code that will convert Python types to C++ types and vice versa. And after that I can create a Python binding and ANTLR grammar with Python target to convert the input format using Python code into the C++ classes. Bingo! :)

Thursday, August 28, 2008

JetBrains MPS

Now this is a killer! The Meta Programming System brought by JetBrains is a really nice full-featured DSL development environment where you can design your own language from ground up, including the specific editor and other features along with code generation and even refactoring support!


MPS Screencast by Sebastien Arbogast.

Saturday, August 9, 2008

More Widgets for Guvnor

For properties and XML editors in Guvnor I'm trying to make use of GWT-Ext components. The widgets from this library look really nice. Also the API looks OK. For the properties widget, theres a PropertiesGridPanel available, which provides some nice features for editing the properties (see the screenshot below).



.. and a piece of code that assembles the panel:

PropertyGridPanel grid = new PropertyGridPanel();
GridView view = new GridView();
grid.setView(view);
Map map = new HashMap();
PropertyHolder[] props = getProps(); //RPC call here
for (PropertyHolder holder : props.list) {
map.put(holder.name,holder.value);
}
grid.setSource(map);

How does it know which editor should be attached to the value column? Simple the type is known at the compile time! One of the benefits of GWT is claimed to be its performance. It is fast, but this power comes at a price - you cannot leave the type undefined, i.e. you cannot use java.lang.Object for the type of your class members.

Consider the following class, which is intended for exchanging the information via RPC calls:

public class PropertyHolder implements IsSerializable {
public String name;
public Object value; // coudn't use Object type here
}


While trying to compile this code down to javaScript, we'll get the following messages from the GWT's compiler:
org.drools.guvnor.client.ruleeditor.PropertyHolder
[java] Analyzing the fields of type 'org.drools.guvnor.client.ruleeditor.PropertyHolder' that qualify for serialization
[java] public java.lang.Object value
[java] java.lang.Object
[java] [ERROR] In order to produce smaller client-side code, 'Object' is not allowed; consider using a more specific type
[java] [ERROR] Type 'org.drools.guvnor.client.ruleeditor.PropertyHolder' was not serializable and has no concrete serializable subtypes

So that's it! I couldn't think of any solution how to cheat the compiler and leave the value's type undefined, so I have to use strings for now.

Disqus for Code Impossible