Pages

Monday, November 28, 2011

Recognizing The Letters as Being Drawn With The Mouse

While playing-out with a Kinect device, I think I will be writing more about this later, I had an idea. I could detect letters being drawn with the mouse and convert them to key presses enabling the user, use the mouse  like a keyboard. It's basically same problem like they do with these pens.
So while drawing the above with the mouse my app. prints "BABA" on where the cursor is. There are two parts to this problem, hard part is obviously recognizing what actually is written. Easy part is producing the key presses. Here are the libraries I used :
java.awt.Robot
A Robot is actually created with automating tests in mind. It has the methods for moving, clicking the mouse, producing key presses and capturing the screen.
java.awt.MouseInfo
MouseInfo class gives you the location of the mouse. Unfortunately with just Java I couldn't find a way to tell if any of the mouse key's are being pressed out side the java application window. Here are some discussions on that. Apparently one would have to use a native library. Anyway I didn't really need to handle clicks at the beginning anyway.
How to Recognize The Characters
My approach is capturing the changes in the movement of 'x' and 'y' axis.
Here is how 'x' and 'y' axis moves while a the letter 'A' is being drawn :


One might also choose to compare the values of increases and decreases in order to eliminate some false matches.
I use 90ms mouse position samples to compute the differences from the previous location. If the differences are less than 10 pixel I ignore them.
Here is how the 'B' recognition function looks like :

  @Override  
     public void execute(Double x, Double y) {  
       if (state == 0 && (y < 0 || (y == 0 && prevY < 0))) {  
         state++;  
       } else if (state == 1 && x > 0 && y > 0) {  
         state++;  
       } else if (state == 2 && x < 0 && y > 0) {  
         state++;  
       } else if (state == 3 && x > 0 && y > 0) {  
         state++;  
       } else if (state == 4 && x < 0 && y > 0) {  
         state++;  
       } else {  
         state = 0;  
         setIndex(saveI);  
         saveI++;  
       }  
       if (state == 5) {  
         setResult(getIndex());  
         breaked();  
       }  
       prevY = y;  
     }  
This function is continuously fed with ordered X and Y axis values. State variable is increased if the difference is expected, reseted otherwise. When the state 5 is reached than we know that it's a 'B'.
I found this approach working quite well even with not taking account if the mouse button is pressed, though that would be required to match some simpler characters like 'I' or '.'.

Monday, October 31, 2011

Hot R&D Topics At Itea 2011 co-summit

Last week I attended the Itea2/Artemis co-summit. It's a showcase of Itea funded R&D projects.
This year apparently most projects are  focused on embeded software (Artemis), M2M Networks and Green Energy.
There are several projects that focus on M2M architecture/applications, including ours A2Nets, which would allow M2M devices to connect and exchange information with each other. Notable applications would be determining when your bus will be at the bus stop or cars passing by sharing traffic data with each other.
It seems, at least in europe, smart readers will be more common in the future. There are numerous projects trying to build and manage different metering networks such as electric and gas metering.
MetaVerse1 was also an interesting project which attempts to enable interoperability among virtual worlds. One of the products of the project is the MPEG-V standard.

Friday, September 23, 2011

Creating a JSF2 Console Component

Since most of the people coming to my site are still looking for JSF how-to's here is one more. This is an how-to on creating a console component, like the one here, with just the JSF2 api.
A shell like console's basic requirements look like this ;
  • Should prompt the user to enter a command. 
  • Evaluate the command and print the result.
  • Should limit the text to be entered only on the prompt line limiting the movement of the caret. 
  • The new command line should be empty for user to enter a new command.
  • Should support AJAX
Since all a JSF component does is rendering HTML and managing state we should actually find a way to implement this with HTML and JavaScript. 
Here are two options I could think of ;
  • Render a textarea, try to limit the movement of the caret through JavaScript.
  • Render a input text for the user command. Print outputs within a 'div'. Blend them together using CSS.
I chose the second approach since JavaScript needed for he first approach is a bit tricky to implement.
Now to implement these as a JSF component we need these :
  • A taglib defining our component.
  • A State manager component that manages inputs and outputs of the component and beans.
  • A Renderer that will output the html depending on components state
The taglib is actually quite simple :
 <?xml version="1.0" encoding="UTF-8"?>  
 <facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"  
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"  
           version="2.0">  
   <namespace>http://mca.org/qmass</namespace>  
   <tag>  
     <tag-name>console</tag-name>  
     <component>  
       <component-type>UIConsole</component-type>  
       <renderer-type>UIConsole</renderer-type>  
     </component>  
   </tag>  
 </facelet-taglib>  
We will refence this taglib through our xhtml's using namespace and our component tag. Component-type references to what I called state-manager and renderer-type references to the renderer.
The state manager :
 @FacesComponent(value = "UIConsole")  
 public class UIConsole extends UICommand {  
   public String getCommand() {  
     return (String) getStateHelper().eval("command");  
   }  
   public void setCommand(String command) {  
     getStateHelper().put("command", command);  
     getValueExpression("command").setValue(FacesContext.getCurrentInstance().getELContext(), command);  
   }  
   public String getOutput() {  
     return (String) getStateHelper().eval("output");  
   }  
   public void setOutput(String output) {  
     getStateHelper().put("output", output);  
   }  
   @Override  
   public String getFamily() {  
     return "qmass.jsf.Console";  
   }  
 }  
Above code does not do much but actually doing it is a bit trickier. We mark this class as a JSF component, and give it an id with @FacesComponent annotation. The means to access your components inputs and outputs are defined with in this class. Get/Set command is for the command that our shell will implement. The output is for printing the result of our shell. And since we will need to perform an action we are extending from UICommand and use it's bindings.
Lastly the renderer :
 @FacesRenderer(rendererType = "UIConsole", componentFamily = "qmass.jsf.Console")  
 @ResourceDependencies({  
     @ResourceDependency(name = "qconsole.css", library = "org.mca.qmass", target = "head"),  
     @ResourceDependency(name = "jsf.js", library = "javax.faces", target = "body")})  
 public class ConsoleRenderer extends Renderer {  
   @Override  
   public void decode(FacesContext context, UIComponent component) {  
     UIConsole console = (UIConsole) component;  
     String val = context.getExternalContext().getRequestParameterMap().get(getCommandId(console));  
     console.setCommand(val);  
     console.queueEvent(new ActionEvent(component));  
   }  
   private String getCommandId(UIConsole console) {  
     return console.getClientId() + "_in";  
   }  
   @Override  
   public void encodeBegin(FacesContext context, UIComponent component) throws IOException {  
     UIConsole comp = (UIConsole) component;  
     String inId = getCommandId(comp);  
     String lines = comp.getOutput();  
     ResponseWriter writer = context.getResponseWriter();  
     writer.startElement("div", null);  
     writer.writeAttribute("id", comp.getClientId(), null);  
     writer.writeAttribute("name", comp.getClientId(), null);  
     writer.writeAttribute("class", "qconsole", null);  
     writer.writeAttribute("onmouseover", "document.getElementById('" + inId + "').focus();", null);  
     String[] lineRay = lines.split("\n");  
     for (int i = 0; i < lineRay.length; i++) {  
       writer.startElement("div", null);  
       writer.writeAttribute("class", "qconsolerow", null);  
       writer.write(lineRay[i].replaceAll(" ", "&nbsp;"));  
       if (i + 1 == lineRay.length) {  
         writer.startElement("input", null);  
         writer.writeAttribute("id", inId, null);  
         writer.writeAttribute("name", inId, null);  
         writer.writeAttribute("onkeypress",  
             "if(event.keyCode == 13){" +  
                 "jsf.ajax.request(this,event,{execute:'" + comp.getClientId() + "'," +  
                 "render:'" + comp.getClientId() + "'," +  
                 "onevent:function(e) {if(e.status=='success')" +  
                 "document.getElementById('" + inId + "').focus();}});" +  
                 "return false;" +  
                 "}",  
             null);  
         writer.writeAttribute("class", "qconsole", null);  
         writer.writeAttribute("autocomplete", "off", null);  
         writer.writeAttribute("type", "text", null);  
         writer.endElement("input");  
       }  
       writer.endElement("div");  
     }  
   }  
   @Override  
   public void encodeEnd(FacesContext context, UIComponent component) throws IOException {  
     ResponseWriter writer = context.getResponseWriter();  
     writer.endElement("div");  
   }  
 }  
We start by defining this class as a Faces Renderer and specify the necessary resources it needs. We need he ajax library and css for custom look. Decode method both sets the new command entered by the user and schedules the action listener attached for execution if necessary. Encode part outputs the HTML we use.
Finally here is how I use the component on a xhtml :
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml"  
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:q="http://mca.org/qmass">  
 ...  
       <q:console id="q" output="#{consoleBean.output}" command="#{consoleBean.input}"  
             actionListener="#{consoleBean.handleCommand}"/>  
 ...  
 </html>  
You can find out the full source code available here as a part of the QMass project here.
Cheers

Saturday, August 27, 2011

QMass, P2P Communication over TCP

I have done significant refactoring and added new features to QMass :
  • P2P, TCP communication, UDP for cluster discovery code
  • A new console app working online or at command shell
  • Significant performance improvements
You may try out the console at http://qmass.cloudfoundry.com , which spans 8 instances.
And here is my new logo :

Tuesday, June 7, 2011

QMass as an Data Grid

Memcached defines it's self as in-memory object system. Hazelcast defines it's self as a distributed data grid. What they mean is there is cluster of instances that form a data grid, where a key value pair is hold on only one instance.
I started developing QMass with cache data in mind. Particularly for use with Hibernate 2nd level cache where "a key value pair is hold on only one instance" approach is not that useful where there are too many access on the cache creating to many network trips. So the cache I designed broke that rule.
While that was useful, forming a data-grid used as a in-memory db also is. So there starts the QMass Data Grid Service where you can do that. There is also a console app. where you can fire up multiple instances and test your grid whit put, get and remove.
And more to come :)
QMass Project page, Wiki, Downloads

Tuesday, May 24, 2011

QMass for Sharing Sessions

I have just committed code that adds session sharing support to QMass.
By defining a filter QMass will share session attributes amongst cluster. It serializes/deserializes the values and does not work synchronously just yet.
QMass project page, QMass wiki

Tuesday, May 17, 2011

Tip to Detect Connection Leaks

To detect potential connections on a Weblogic backed connection pool set the "Inactive Connection Timeout" close to zero. Setting this feature will cause the application server to forcibly close the connections which have been left inactive for the timeout period. It will print out a stack trace which will guide you where the connection is acquired and not yet released so that you could investigate.

Saturday, May 14, 2011

Paging with Hibernate

Hibernate query api has setFirstResults and setMaxResults methods that restricts the result set for ceratain amaount. Typical usage inside a generic method would be like :
public List find(String queryString, Object[] queryArgs, int first, int max) {
Query q = getEntityManager().createQuery(queryString);
if (queryArgs != null) {
setParameters(q, queryArgs);
}
q.setFirstResult(first);
q.setMaxResults(max);
return q.getResultList();
}
Here If you are using Hibernate with Oracle it will wrap the original query with a select query which will filter the result based on rowid's.
One rule of thumb while using paging is that your query shouldn't have any joins on collections. Than Hibernate will just execute the original query claiming it can't exactly determine the number of results returned. The warning "firstResult/maxResults specified with collection fetch; applying in memory!" is logged and paging only occurs on the ResultSet object.
Luckily the queries I came across mostly could be refactored so that they don't use joins on collections. Like :
   EJBQuery query = new EJBQueryImpl(Foo.class, "f");
query.join(Joins.joinFetch("f.fooBooSet", "fbs"));
if(sorguKriteri.getBoo() != null){
query.addRestriction(Restrictions.addEquals("fbs.boo", sorguKriteri.getBoo()));
}
Could be rewritten as :
  EJBQuery query = new EJBQueryImpl(Foo.class, "f");
if (sorguKriteri.getBoo() != null) {
query.addIn("f", new EJBQueryImpl(FooBoo.class,"fb", (EJBQueryImpl) query)
.addProjections(Projections.add("fb.foo"))
.addRestriction(Restrictions.addEquals("fb.boo",sorguKriteri.getBoo())));
}
A sub select is used to filter the result instead of join.
Another common pattern used with hibernate is using a lazy list which loads results, page by page basis which looks like :
 public class PagingList<E> extends AbstractList<E> implements Serializable {
private Query query;
private int numOfResults = 0;
private int pageSize = 10;
private List<E> currentResults = new ArrayList<E>();
private int currentPageIndex = -1;
public E get(int index) {
int pageIndex = index / pageSize;
if (currentPageIndex != pageIndex) {
currentPageIndex = pageIndex;
currentResults = getService().find(query, currentPageIndex * pageSize, pageSize);
}
return currentResults.get(index % pageSize);
}
public int size() {
return numOfResults;
}
public PagingList(Query query, long numOfResults, int pageSize) {
this.query = query;
this.numOfResults = (int) numOfResults;
this.pageSize = pageSize;
}
public Service getService() {
return (Service) SpringUtils.getBean("service");
}
...
}
Here the query is executed when the get method is called on the list object. Down side of this approach is that query size must be determined with a separate select.

Tuesday, April 19, 2011

QMass, An API for Cluster Wide Operations

I have started a new clustering api for Java. You can access the project page at http://code.google.com/p/qmass/. Very first version is ready to be downloaded.
It's first application is an hibernate cache provider implementation. You can start using it by simply defining org.mca.qmass.cache.hibernate.provider.QMassHibernateCacheProvider as the hibernate cache provider.
By default it will try to cluster applications running on localhost in the port range 6661-6670. To configure QMass you need to set the qmass.cluster property through your persistence.xml. An example persistence.xml might look like this :
<persistence>
<persistence-unit name="app">
...
<properties>
<property name="hibernate.cache.use_second_level_cache"
value="true"/>
<property name="hibernate.cache.provider_class"
value="org.mca.qmass.cache.hibernate.provider.QMassHibernateCacheProvider"/>
<property name="qmass.cluster"
value="localhost,6661,6670/"/>
<property name="qmass.name"
value="myproject"/>
...
</properties>
</persistence-unit>
</persistence>
Cluster definition 10.10.10.112,6661,6670/10.10.10.113,6661,6670/ will define a cluster spanning two different machines with in the port range. qmass.name is the name of your cluster. By setting it you might have two or more clusters running together with out messing with each other.

Thursday, April 14, 2011

New Languages on the JVM

In case you didn't notice there is a trend for developing new languages that run on top of JVM. Jython, JRuby, Scala, Groovy, Clojure are examples. It seems latest addition will be the RedHat's Ceylon.
I have tried some of these languages. I wrote my impressions on Groovy here.
After 6 years of professional experience with Java, here is what I would do to fix it and what I wont touch.
Don't touch the packages.
Packages are convenient and simple ways to define the namespace. So I wont touch the way it is. You have to define the package keyword for every class/file and specify imports below that. I would farther force the constraint and remove the default package.
Improve the imports with alias.
Only problem I had with imports are on the rare case that, when I want to import more than one class with the same name. Alias's could be introduce to improve that. Here is how I would have used them ;
import org.apache.commons.lang.StringUtils as CommonsStringUtils;
import org.springframework.util.StringUtils as SpringStringUtils;
...
if(CommonsStringUtils.isEmpty(str)) {
...
One more rule I would have on defining an alias is that you can't define alias if there is not any naming collision. This would prevent the abuse of use.
One public class per file.
This is a rule that most of the languages remove, which I object. I like it and won't touch it. With the packaging rules and this, I can find the source code of the class easily.
Access modifiers.
Remove the private keyword from language and make the default visibility private for classes and variables. If needed introduce a separate keyword for package level visibility.
No headless files.
Another common trade of the new languages are letting the users code without any class definition. Like a single lined print statement without any class definition is a valid program. I don't think enabling this freely would encourage anything more than some garbage scripts.
Improve Getters Setters, Simplify Initializing Constructors
Java Beans spec. dictates that a bean should define private fields and make these fields accessible through what we call getter/setter methods. This is widely used in today's apps. and frameworks. Hibernate and other JPA frameworks use it extensibly.
I believe getter/setter methods are mostly useless and clutters the code.
Again another source for boilerplate code is the initializers.
I think best way to solve this is by introducing new meta information.
public class City {
public get String name; // public getter no setter
public get set Integer population; // public getter - setter
public get protected set Integer foo; // public getter - protected setter
}
...
tokyo.getName(); // to use
So public or package level visibility for a field must be used with get/set keywords. Specifically defining the get/set methods will override the default methods. And at last I believe that setters should return 'this'.
For the constructors I don't have any solution that I like for now. Groovy and some others might too, have a map syntax for constructors.
No Primitives
Java has distinction between 1L and the Java Long object. They should be same. Constructors for Long object must be removed and typing 1L should create the Long object. Again method calls could be made like 1L.toString()
Null checks
Again a common source for boilerplate code. I like the Groovy solution :
if(state != null &&
state.getTransitions() != null &&
state.getTransitions().size() > 0) {
states = state.getTransitions().values();
} else {
states = new ArrayList();
}
Could be written as :
 states = state?.transitions?.values() ?: new ArrayList();  
? is the null check and ?: is called the elvis operator.
Closures
Another popular construct that people try to add is the closures. Closures are simply function blocks that could be passed as parameters to methods or other functions. This is what closures look like in Java :
 jdbcTemplate.doInConnection(new ConnectionClosure<Long>() {
@Override
public Long execute(Connection con) throws Exception {
ResultSet set = con.createStatement().executeQuery("select max(id) from cacheoperations");
while (set.next()) {
return new Long(set.getLong(1));
}
return new Long(0);
}
});
That is the OO way to define a closure. I am sure that on many enterprise apps. some similar code for accessing a Connection object exists. We don't want unclosed connections so we employ this pattern to be sure that connection is closed after the closure is done. While this works it's cumbersome to define. Every closure needs an interface with just one method to be defined. In this case the ConnectionClosure interface. Improved version with no interface might look like this :
public class JDBCTemplate {
public Object doInConnection(Object execute(Connection con)) {
...
// use
jdbcTemplate.doInConnection(
Long execute(Connection con) throws Exception {
ResultSet set = con.createStatement().executeQuery("select max(id) from cacheoperations");
while (set.next()) {
return new Long(set.getLong(1));
}
return new Long(0);
});
To improve code use any object containing a method with the mentioned signature could be passed as argument to this function.
What needs to be thought further is how this would effect the Java api design.
Biggest Don'ts
Don't change the Java's basic syntax. Don't add pascal like begin and end keywords for blocks. Curly braces are enough. Same thing for loops and if statements. I don't think there is a need for introducing a new assignment operator like :=.

Tuesday, March 29, 2011

Comparing different cache products

My app. relies heavily on cache to function. Although I use cache on the view, I use it mostly as Hibernate 2nd level cache. Here are some of the products I used and what I think about them.
Ehcache
I think this was used to be called Easy Hibernate Cache and Hibernate people promoted it's use. Now it's owned by Terracotta. I used this quite for a while, it's light, fast and mostly easy to use. However it didn't worked that good as a distributed cache environment.
It has multicast support for peer discovery which works pretty well for most of the time. Except it didn't worked on our prod environment where we have 10 nodes across the network. Some of the nodes just didn't connect to each other which is unacceptable.
It also has manual peer discovery, which needs different ehcache.xml's which would point to other nodes. I think you could tell hibernate, which xml to use at persistence.xml file.
Still I had hard time tracking which node joined to the cluster and which failed to.
Memcached
Main reason I tried memcached was, its use at facebook, wikipedia and such. Memcached is designed a bit different from the ehcache. This is written in 'C', had to be build and run as a daemon process. Cache clients can connect to it through telnet and perform. First problem I ran into was building it. Although It build easily on a Linux environment It required some editing to headers on a HP-UX in order to compile. Second and greater problem is, it doesn't have support for Hibernate. I used the library here http://code.google.com/p/hibernate-memcached/ which is not being actively developed anymore. It also has some nasty bugs...
Hazelcast
Hazelcast is what I use now. At first look it's design looks similar to ehcache but this one is made distribution in mind. It's distributed by default. I have enabled back-up reads, and near-cache settings for better performance.
It comes whit a decent monitoring app. which you can monitor your cluster and see the active nodes.
It's much easier to configure especially compared whit ehcache.