Tuesday, November 9, 2010

Palm Pre Plus

So yesterday, after months of waiting I got my Palm Pre Plus.
Meanwhile cool! :)

But! I could not activate it without data service from my cellular provider. Ughm :(
So I purchased it. Most of the day I have some kind of internet connection around: either wired or wireless. The only time I don't have such, is when I'm driving, and I don't really need it - I should drive. So currently I have a feeling that I'm going to pay for something I'm not going to use...

Anyway, I purchased the data plan and activated my phone, and even managed to make/receive few calls.

But it has not Hebrew or Russian. Why?? Some internet search discovered the following guide.

Another problem: it seems that I cannot connect to App Catalog even for free apps. Why??! If you know how to solve it, please let me know...

Another problem: Skype doesn't support it... Well, it becomes really annoying now. I hoped that a new Palm Pre will replace the outdated Asus pocket-pc, but currently it doesn't :(

Actually currently my feeling is a sort of disappointment: I waited to get this gadget for a loooong time, and now it seems that it doesn't covers my very basic needs for some cool applications and Skype. May be I should have go for some cool HTC device with Android? Rhetorical question.

P.S. In these comments (Russian) you'll find another discussion.

Thursday, August 5, 2010

Spring - Which resources are actually loaded in Spring Context?

Today I got a really weird behavior: some spring context file was loaded into the same context twice. This caused exception, since some beans are expected to be singletons, while I got two instances.

In this post I'm not going to describe what exactly was the problem. But rather how I investigated it.

So in order to see what are the actual files being loaded and what is the source of the definition that caused them to be loaded I put a breakpoint in org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource, Resource).

That's all. Try it yourself :)

Sunday, June 27, 2010

tarlog-plugins 1.4.1

I'm happy to announce the 1.4.1 release of the tarlog-plugins.
The main feature of this release is an ability to customize the actual commands being run via the preferences page.
This may make the plugin useful for non-Windows environments.

Enjoy!

Tuesday, June 22, 2010

Eclipse Helios RC4 Review

Some time ago I have downloaded the JEE edition of the Eclipse Helios RC4.
And here comes a review in order to win a Helios t-shirt.
I'll start with bugs I found till now. I didn't find many. Continue with features I still miss, and try to end with some positive note about the features I liked.

Bugs

For a RC release there are really few bugs I found till now.
  • In Html Editor the div element does not get idented. Really annoying...
  • Sometimes the key bindings does not work in Package Explorer. For example, when I press F5 I expect "Refresh" to run. Sometimes it happens, sometimes it doesn't.

Missing Features

And not the features I still miss. Please notice: I'm a Java Developer. Meaning I'm talking here about features I miss mainly as a Java Developer and it may be irrelevant for the C/C++/Php and other users. So if you are not a Java Developer, you can probably skip this section.
  • Built-in Maven Support - It's 2010. Most projects I'm involved in the last 5 (five!) years, use Maven for builds, but Eclipse till today ships with Ant plugin only. Hey! Wake up! Almost nobody uses it anymore! And if even some does, there are a lot of Maven users, who need to install the Maven plugins manually. So can you do a favor and add it Maven plugin at least to the distributions for Java and JEE developers?
  • Subversion/Git support - same as Maven: most of the open source projects I know, use Subversion or Git. So may be it's time to include these plugins in the Eclipse distributions?
  • I'm really fond of the "Quick Fix" (Ctrl+1) feature of Eclipse. One suggestion that can be quite easily added to this feature: when there are not initialized final fields in the class, the following error is displayed: "The blank final field may not have been initialized". The quick fix may be to open "Generate Constructor using Fields" dialog.
  • It would be cool to collapse the generics in the fields declaration
    Instead of:
    private Map<String, Set<String>> map = new HashMap<String, Set<String>>();
    it could be:
    private Map<String, Set<String>> map = new HashMap<~>();

New Cool Features

Actually I didn't find many cool features in this release.
  • I'm really fond of "Quick Fix" (Ctrl+1) feature. One of the most annoying "bugs" that is had till now, was placing @SuppressWarnings annotation only on the method level. So now it has two options: place it on method and place it on variable.
  • Refactoring: Move Type to a New File - finally! I was missing this feature for a loooong time!
  • Java debugger has two new neat features: Breakpoints view now contains more details, like "hit count", "conditions" and so on. Variables view contains "instance count", so it is easy to check how many instances of the specific type were created.

Conclusion

I didn't find any new really cool features in this version of Eclipse. 3.5 was a great release, and this release looks like mainly bug fixes. Don't get me wrong: having a stable SDK without bugs is more important than having unstable one with a lot of features.
So nice work, guys! You are doing a great job!


If you got that far and read all this, you may be interested in the plugins I developed for Eclipse:
Tarlog plugins
Encoder Tool

Thanks for reading.

Monday, May 31, 2010

Palm: Game of Life - Reading the Comments

Getting the data about the published application is somewhat annoying. From the developer's portal you can see the amount of downloads and the average user rating.
From the rss feed you can additionally see the number of comments, but you cannot read them.
Fortunately, http://www.myappbox.com shows the comments, once you find your application.

I don't know if there is any normal way to answer the user comments and I will be really surprised, if any of the users bumps into my blog, but currently I see no other way to post replies.

First, all the guys who said that the game is cool: thank you!!! It's really a pleasure to read your comments ;-)

Aaron B and Stanley T: you both mentioned that "Ability to save favorite initial conditions would be a great feature." I'm totally agree with you. Moreover, I developed it in a very first version and even mentioned it in "Help". Yes, it's on the last help's screen that talks about shortcuts.

Stanley T: "The builtin ability to share saved starting patterns would be very cool." Totally agree. I'll think about it :-)

Michael M: "Not playable at all on the Pixi due to the screen formatting. Please fix." What does this mean? Are cells too small? The cell's size is adjustable. Once again: reading "Help" may help.

Sunday, May 23, 2010

Conway's Game of Life for PALM WebOS

And here comes my first WebOS application: Conway's Game of Life.
Actually I don't own palm myself and I developed it using emulator, so all of you, who own palm: please! please! please! Download it and see if it runs, how is your experience, and what are the bugs.

The project is open source at Google Code. You are welcome to report bugs.

Thursday, March 25, 2010

Security Questions

I hate the sites that require you to insert the security question that must be chosen out of an enumeration. My problem with an enumeration is simple: most of the answers to these questions can be googled very easily (in my case they extremely easily if you are Russian speaking).
It's very easy to know what primary school I've attended, which city I currently live in (or was born) and what is my nickname.
Why is it such a difficult task to allow custom security questions? After all these questions intend to provide a security and not a security breach...

Wednesday, March 24, 2010

Hibernate/JPA Best Practices

Here come some of Hibernate/JPA best practices.
Please notice that this guide does not intend to cover Hibernate/JPA at all, but only to provide some best practices. Personally I learned Hibernate using Hibernate in Action.

I really appreciate any comments saying what do you think about this practice, why it's wrong, and what additional practices should be added.

So let's start:

Override hashCode() and equals()

The Hibernate reference states that "It is recommended that you implement equals() and hashCode() to compare the natural key properties of the entity."
The reason is simple: different instances of the class may represent the same record in the database. Therefore, when comparing these two instances, you'd like to get equal result, while default equals implementation will return not-equal since it's not the same instance.

This becomes really important, when working with collections, especially with Sets. You don't want the same object to appear twice in set, right?

Overriding the hashCode() and equals() is not a very complex goal, but you must be very careful:
1. Remember that two equal objects must return the same hash code. Therefore, you cannot use auto-generated Hibernate id in hashCode() - this value is not assigned for the newly created objects. After persistence occurs, the value is assigned, so the hashCode will change, when the object actually wasn't changed!
2. Changing the fields that participate in hashCode() will change the hash code value. So if your object is stored in a Set (or it's key of a Map), you won't be able to retrieve it from the set anymore - one hash was used for insert and another one was used for retrieve.
So basically you'll need to remember not to change objects that are stored in sets! And this is really important!
So you may ask: how will I know who stored my object in a set?
My answer is simple: you cannot know this, unless you don't give your objects out. So you are the only person who is using these objects, so you know how they are kept, right?
Storing the objects without giving them out is not so weird idea: keep the persistence layer away from the business logic and return a copy of object when required.
Another option: return immutable objects to the business tier. So the business tier won't be able to change them. When the change is required - provide a special API. Thus the objects won't change accidentally.

Try to Make All Object Immutable

This may sound weird, how exactly the persistent objects can be immutable. But in the previous part I described why it's important. And actually it can be quite easily achieved:
1. Make all setters private. Thus it will be impossible to call them without using reflection. (Hibernate will use reflection and populate the properties during the object retrieval)
2. When returning collections, wrap them with Collections.unmodifiable. So the user won't be able to modify your collections.
3. Allow changes only via special methods.

Return Copy of Persistent Objects to the Business Tier

So no accidental change in hash code may occur.
Additionally when filling the business objects, a lot of potential problems may be resolved. Consider that the persistent object contains a lazy collection. If the object is returned as is to the business tier, the lazy elements in the collections can be accessed after the transaction was closed, therefore the query to the database will fail and user will get an exception.

h3. Change Data in Collections Only via Special Methods
When having associations, take care of this association via special method: for example, Parent class will have method addChild(Child child).
When returning the values of collections wrap them using Collections.unmodifiable to prevent accidental changes.
This is useful both to handle bidirectional associations correctly and to prevent accidental changes in hash code.

Summary

1. Override hashCode() and equals() of the entities using the natural key properties of the entity.
2. Don't compare auto-generated id in hashCode().
3. Don't change the properties that participate in the hashCode() for objects stored in collections that use hash code (especially Sets or Maps). If such a change must occur, reinsert object into collection. Remember that remove must occur before the property is updated.
4. Keep the persistence tier away from the business logic as much as possible. Don't pass the persistence objects to the business at all.
5. Make your objects immutable (or semi-immutable): make all setters private, return collection values only wrapped with Collections.unmodifiable, make changes in collections only via special methods.


Recommended Reading

1. Hibernate in Action (In Action series)
2. Java Persistence with Hibernate
3.The Best Software Writing I: Selected and Introduced by Joel Spolsky (v. 1)

Sunday, March 7, 2010

Maven: Copy Dependencies to a folder

So if you need to copy all project dependencies to a folder using Maven, just type
mvn dependency:copy-dependencies -DoutputDirectory=<folder name>

Can it be more simple?
So why I'm googleing for it over and over again?


Recommended Reading

1. Maven: The Definitive Guide
2. Pro Git
3. Smart and Gets Things Done: Joel Spolsky's Concise Guide to Finding the Best Technical TalentProgramming Language & Tool Books)

Tuesday, March 2, 2010

Apache Wink - Dynamic Resources

Quite often people ask me what is so different about Apache Wink, and why another JAX-RS framework. Usually I answer that Apache Wink started in HP long before there was any REST open-source framework. It was developed internally for two years before joining Apache.

This may be interesting from historical point of view, but then people ask: so why should we use it now? Are there any unique features?
And the answer here is: yes, there are some unique features beyond the JAX-RS spec and this post will describe one of the most cool features (IMO) that Wink contains:

Dynamic Resources


The idea of Dynamic Resource is quite similar to Template pattern and it's simple: many resource classes have more or less the same logic. However, they manipulate different entities, so obviously they have different URLs.

Consider the following example: a resource that simply a facade to the database - it allows the CRUD operation and it uses Hibernate as the persistence layer.

So basically the template class will look something like this:

public class CrudResource {

private Session session;
@Context
private UriInfo uriInfo;

private Class clazz;

@GET
public Object getEntity(@PathParam("id") String id) {
return session.get(clazz, id);
}

@POST
public Response createEntity(Object newEntity) {
session.save(newEntity);
String id = newEntity.getId(); // this line will not compile, but the idea is that id is assigned by hibernate
URI location = uriInfo.getAbsolutePathBuilder().segment(String.valueOf(id)).build();
return Response.created(location).build();
}

@PUT
public void updateEntity(Object entity) {
session.update(entity);
}

@DELETE
public void delete(@PathParam("id") String id) {
session.delete(session.get(clazz, id));
}

}

Notice: This class doesn't contain the full logic, like transaction management, and won't even compile. It should provide the idea how to implement the "template" style resource and not how to really work with Hibernate or even Wink.

So as you can see, this class can basically perform the CRUD operations on any entity. It would be a pity, if for each entity, we'll need to extend this class just to assign a different path. And here come the Dynamic Resources. They allow to skip the @Path annotation and implement the DynamicResource interface instead (or extend from the AbstractDynamicResource class).

So now our resource will look something like this:
public class CrudResource extends AbstractDynamicResource  {
...
}

Now it's possible to create multiple instances of this class and assign it a different paths and other members (like 'clazz', which should hold the value of actual class).

Notice: Dynamic Resources can be returned only by Application.getSingletons() method. Thus these resources are actually singletons and must be coded accordingly (for example think about synchronization issues).

Spring Integration


Wink contains the Spring Integration extension. I won't describe its features here, but only want to mention the usage of Dynamic Resources with Spring.
First, Dynamic Resource can be defined and registered via Spring Context, thus you can create new resources using the same class, while updating the configuration only. Pretty nice feature, when you think about it.
Second, Dynamic Resources benefit from different scopes. Meaning, they may not be singletons anymore.