RSS
 

Client for OAuth with Spring Security

20 Nov

About one year ago I wrote an article in which I described how can one implement a client and a server that uses the OAuth security protocol. The implementation was using Spring Security and a plugin for OAuth. The article was intended to focus on the 2Legged flavor of the OAuth 1.0 protocol. However, because of a misunderstanding from my part, that article is just a normal OAuth 1.0 implementation of Client and Server, with a small hack for the second step. Thanks to all the people that commented on that article, I was able to understand how the 2Legged mode should work.

Therefore, this article presents a small client application that uses both the normal OAuth 1.0 mode and the 2Legged mode (also know as “signed fetch”) for accessing a protected resource. Read the rest of this entry »

 
 

Unit testing with Spring – a bad mix

20 Mar

There has been a discussion recently, on the JUnit group, about doing unit testing with Spring. The arguments provided in the answers to that question reminded me of a problem that we faced in the project that I am currently working on: the usage of Spring for unit tests was a bad idea. Let me tell you how we started, and why we got to realise that it is not good to mix the well-known framework in the unit tests. Read the rest of this entry »

 

Ehcache event listeners as Spring beans

02 Feb

When working with Ehcache and Spring, one might get to the point where he/she needs to implement cache event listeners: objects that are observing the cache and want to be notified by the cache when an element is put, removed or expires in/from the cache. However, Ehcache has a requirement that for each such listener you need to write 2 classes: a cache listener factory and the actual cache listener. Moreover, Ehcache takes care by itself of the instantiations of the factory and the listeners, so if one wants to have the listeners as Spring beans and have some other beans autowired in one’s listeners, then there is certainly a problem. Here is a solution to this problem. Read the rest of this entry »

 
No Comments

Posted in Spring

 

Access Spring context from static methods

22 Jan

In this post I will present a very simple example that attempts to access an existing Spring application context from a class that has nothing to do with the Spring context. For example from a static method of a class. This is, of course, quite trivial, but I might make use of this little technique in some next posts about Spring, so I think it is due to first start with this small example.

Therefore, what I want to do is to have an up-and-running application context, and access the beans in that context from a static method. The best approach is to never have to do this. In an ideal case, all your classes are Spring beans, and therefore subjects to autowiring and dependency injection. However, from time to time you need to write a completely independent class, that needs to access some Spring beans. Read the rest of this entry »

 
1 Comment

Posted in Spring

 

Transactional code blocks in Java

11 Oct

Recently I read about a feature available in the .NET frametwork 2.0 or later that allows you to write transactional code blocks: that is, code blocks in which you can execute some actions or change the state of some objects and then, if something goes wrong, have the entire changes reverted to the initial state. The objects are taken to the state before the start of the transactional blocks and any other changes you performed are rolled back. The article is here.

Reading this got me thinking about how can I implement this in Java. I formulate the problem as it follows:  “How transparent for the user can one implement transactional code blocks?”. Please note that I don’t say “Can you…” but “How transparent…”. I believe that allowing any amount of stress for the user, you can implement anything. The question is, how transparent and easy to use can one do it. Read the rest of this entry »

 
 

King, Stephen – Under the Dome

26 Sep

Reading Stephen King’s “Under the Dome has been a nice experience. At first you get to know a lot of characters that are living in the unfortunate local town, and this might be a little confusing, at first. However, the author comes back to each character on a somehow regular basis, so that you don’t forget what was that character doing and how he or she relates to the other people involved in the action. Also, all these character get to bring their share of participation and changing in the course of events in a manner that I really got to enjoy.

What got me by surprise in the book is the end of the story. The book has a considerable size, and the action goes in a fast pace. However, the actual explanation for the dome (the reason why the dome is there, who or what is powering it up, why that city and not a different one, perhaps why not a more important one) as well as the end of the entire story happens very fast, in less than 50 pages. You wait the entire book for getting the explanation for it, and when you actually get it you realise it is not quite the one you expected, or the one that you considered as a possible option. Sure, the alien element of the dome is presented to the user at about 1/3 of the book, but I think everyone reading the book expects the dome to have a more earthly nature. However I consider this as a good element of the book, as I was not disappointed by the outcome of the story.

The end of the book also tends to be philosophical and get a 2nd sub-level of interpreting the events, which is something that you don’t encounter in the rest of the book. All in all, though, an excellent reading and some great times that I had with it.

 
 

Developing Swing applications with Eclipse and Netbeans

19 Aug

I recently worked on some personal projects that required Swing development. They were some desktop applications and some Java applets. For the development, I used primarily Eclipse, because I like the IDE and I am very accustomed with it. However, for the Swing development, I didn’t wanted to write the code by myself. Therefore, I had to either search for a Swing plugin for Eclipse or revert to some other solution (read IDE) for developing the Swing parts. Here is the combination that works for me very well. Read the rest of this entry »

 
 

Q1: Get the star! … if you can – solution

04 Aug

Quizz of the weekThe first issue of the Quiz of the week category involved the writing of a method that could throw a checked exception without declaring it in the throws clause. Here are the 3 solutions to this problem. Read the rest of this entry »

 

Q1: Get the star! … if you can

22 Jul

Quizz of the weekI started this category because I like to solve problems, of any type, especially if they are related to the languages and the tools that I am using on a daily basis. This type of posts is not something new, of course. You can find probably hundreds of sites and blogs that are doing pretty much the same thing. A simple search on google reveals this, but you could find some other sites for sure.

The problems that I will post here are not completely invented by me. Most of them are actually found in other books, on the internet, in the language specs (such as JLS), in the APIs, in the code that I am working on every day. Some of them (albeit only a few) are actually invented by me, but I don’t exclude the possibility that they are already posted somewhere else. Anyway, the source of the problem is irrelevant.

The answer(s) to each problem will be posted in the next question, next week.

Since I am posting these problems, I would also love to get comments with possible solutions, ideas for improving the problem or just general remarks about the discussed problem. So feel free to join the club, and solve the issues :)

Here is the first issue of this series. Read the rest of this entry »

 

Combining JUnit Theories/Parameterized tests with Spring

07 Jul

In almost any project that is using Spring, you get to the point where the Spring’s dependency injection mechanism is required also for unit tests. Combining this with JUnit leads one to the usage of the SpringJUnit4Runner to enable the initialization of the Spring context, as well as the autowiring of various beans in the test object that is just being executed.

However, for more specialized unit testing purposes, such as implementing parameterized tests or theories, one must use a specialized runner (Parameterized and Theories, respectively). This, of course, gets in conflict with Spring, which needs to use it’s own runner. JUnit does not support multiple runners for the same unit test (and except for the Spring runner, all the other runners are pretty excluding one another so it kind of makes sense to allow only one runner at a time). Read the rest of this entry »

 
7 Comments

Posted in Spring