Looking for software experts?
Need an expert advice on software development? Need consulting work done in time and at high standards? Tremend has the right solution for you.

We can provide expertise in:
  •    » high traffic and complex content website infrastructures using Java, PHP or .NET. More here ...
  •    » mobile applications for iPhone, Android or J2ME. More here ...

For an enquiry, send an email to contact [at] tremend [dot] ro.

Enumerating LAN and WLAN clients on a Linux router

April 20th, 2007 by Bogdan Nitulescu in General

A while ago I was asked to take a WiFi router and add some bell and whistles to its interface. It was one of those small NAT boxes – nothing more than a LAN switch and a WLAN access point strapped to a 200 MHz system-on-a-chip running Linux. ucLinux, to be exact.

The exact request was “list all the computers connected to our box – wired and wireless”. Not a lot of info – MAC, IP address, Windows computer name. But what does “connected” actually mean?

This wasn’t much of a question for the marketing people. Ideally, if you put a cable in the router, the computer will show as connected. Take the cable out, and it should show no more. Well, a lot easier said than done.

It’s easy to be accurate on the wireless side. The driver for the Marvell wireless chipset can be queried for the active connections, and you can retrieve the active MACs from /proc/net/mvwlan.

Read the rest of this entry »

read more ...

NotSerializable exception: PersonManagerImpl

April 20th, 2007 by spostelnicu in Java, General

I recently stumbled across a NotSerializableException in the project I was working, and I decided to share this experience with you because the cause of the problem it’s quite interesting and some of you might come across the same problem.

First of all, the first thing noticed was that the exception was thrown when trying to serialize the http servlet session. By some sort of “accident”, the session contained an object that was not serializable. So far everything seemed normal. The one thing that was wierd about it was that the object in question was an instance of PersonManagerImpl. Let’s say that this is the name of one of the classes in our project. It’s a class that implements some business service interface, named PersonManager.

“Now, wait a minute!”, I said. “How the f*** did that come to be on the session?”

After a closer look, I noticed that the name of the offending class was not actually PersonManagerImpl, but PersonManagerImpl$1.

“A-ha!” That just made it a little easier for me. All I had to look for was to find an anonymous inner class of PersonManagerImpl, and discover how it got to be put on session.

After some digging in the code, I found the following lines (Note: these are not the actual names of classes and methods, but you get the point):


// Get a set of persons by some criteria
Set<Person> persons = personDao.findSomePersons(someCriteria);
// Sort the set of persons by some criteria (because the DAO method returned an unsorted Set)
TreeSet sortedSet = new TreeSet(new Comparator<Person>() {
public int compare(Person p1, Person o2) {
return p1.getPosition().compareTo(p2.getPosition());
}
});
sortedSet.addAll(persons);
session.setAttribute(SOME_PERSONS, sortedSet);

So, to make it all clear: what happened here was that the sorted set was saved as a session attribute, but along with it was also saved the Comparator, which was declared as an anonymous inner class of PersonManagerImpl.

To fix this annoying bug, the easy solution was to make the Person class implement Comparable:

public class Person implements Serializable, Comparable {
…….

public int compareTo(Object o) {
Person that = (Person) o;
return this.position.compareTo(that.position);
}
}

and change the sorting code to:


Set sortedSet = new TreeSet();
sortedSet.addAll(persons);

read more ...

Avoid Spring circular references and over eager type matching using lazy initialization

April 5th, 2007 by Marius Hanganu in Java, General, Spring

Circular dependencies between beans managed by Spring is usually caused by a logic error, but it may be sometimes exactly what developers intend to obtain.



We recently ran into this problem: Spring seems to “over-eagerly” initialize the beans, resulting in an circular dependency error.



2007-04-05 13:24:41,310 ERROR [main] context.ContextLoader (ContextLoader.java:205) – Context initialization failed

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘articleManager’: Bean with name ‘*********’ has been injected into other beans [******, **********, **********, **********] in its raw version as part of a circular reference,

but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching – consider using ‘getBeanNamesOfType’ with the ‘allowEagerInit’ flag turned off, for example.




We managed to overcome the problem by using the “lazy-init” attribute for our Spring beans. Other techniques for avoiding/fixing the problems are described by Costin Leau and Andreas Senft on the spring framework’s forums here and here.




read more ...