Adolfo Eloy

Ranch Hand
+ Follow
since Mar 21, 2009
Adolfo likes ...
Eclipse IDE Java Linux
Experience with development since 1997 starting with Visual Basic and SQL Server. After 2003 started developing web applications with PHP under Linux with Oracle Database. After 2005 had started with JAVA developing desktop programs and now working with web development with JAVA.
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Adolfo Eloy

Hi all.
I've already search trying to understand what's happening in the following snippet of code but I didn't find anything helpful yet.

I'm trying to output something like follows:

But, I'm getting:

I've been trying to use GMT and UTC Timezones when creating a calendar but it didn't solve this problem.
I think that there is something related to Epoch but I'm not getting it.

Notes: I know that an instance of Date would never have a date defined with day equals to 0 or month equals to 0.
But 1000 milliseconds is equal to 1 second and 1 second is far enough to complete 1 day. :P

Thanks in advance for some help.

7 years ago

I'm developing a spring based application which I'd like to organize in separated projects like follows:

books-service: provides all services binded to repositories working with some relational database
books-web: provides all controllers and web resources

books-service has a configuration using java class annotated with @Configuration which scans for package
books-web has a configuration using java class annotated with @Configuration which scans for package

When I set the base package to @ComponentScan with I have all requests being executed twice because beans were created twice.
So to solve this, I'm using @ComponentScan.Filter as follows:

Configuration for web app:

Configuration for services app:

So even using this configuration, my services are being executed twice.
Does anybody knows what's happening here?

Thanks in advance

Adolfo Eloy
8 years ago
The way to getting around with that is using a List of Animal so you can pass through your method which expects a List of animal.
The point is: you can't deal with polymorphism the same way to ordinary classes.
Base type (List, ArrayList) can be polymorphic as whatever class in java. But generic types can't be treated the same way.
If you really need to use something near to polymorphic generic types you will need to use wildcards with <? extends Something> or <? super Something>.

<? extends Something> you can't use add with collections
<? super Something> you can use add with collections

To see more about it take a look at Generics post from Suresh.

The code bellow is getting around this problem using a list of Animals:

Suresh KumarPandey wrote:1.NavigableSet<? extends Object> set6=new TreeSet<Integer>();
set6.add(new Integer(32));
why this add gives error because i am adding a integer object only which extends Object.

When declaring a reference using wildcard (?) with extends, the reference can point to anything that is a subtype of generic declaration.

At the example above, animals can reference a List of Dogs, or Cats (regarding that Dog and Cat extends Animal).
But it does not mean that you can use the method add from animals, because it's totally unsafe.
If java compiler allowed that you could fall into something like this:

Creating a list of dogs and passing dogs to getMoreAnimals method:

getMoreAnimals method:

If using a reference List<? extends Animal> allowed to add things to this, the code above will add something wrong into the list.
It will be inserting a cat inside a dogs list. The compiler will think you were using an animal list.

Suresh KumarPandey wrote:2.
NavigableSet<? super Object> set5=new TreeSet<Object>();
set5.add(new Double(3.14));

Why the add method does not throw compile error here.

Second question is using super which was created to allow insertions. Not only navigating through a collection for example.
With super you can pass a list of Dogs or Animals or even Objects and can add Dogs to this. But remember, ONLY Dog can be inserted.

Consider the following example (changing the getMoreAnimals a little bit):

And you can pass a list of animals through this method:

I hope it can clarify your doubts about polymorphism using generics.

Mark Spritzler wrote:There are many approaches to resolve ambiguity.

1) Use @Qualifier next to your @Autowired so you can tell which one to inject by name.

2) Put @Primary on one of the implementations, then Spring will inject that one.

3) Only have one of the two implementations in your classpath at runtime, so that there will ever only be one implementation.

For 3, I have yet to see any good examples of why you would need multiple implementations of a Service or a Repository or a Controller, EndPoint, or any other class that you write.

However, there are cases where you might have multiple DataSources, TransactionManagers, or other infrastructure beans. In which case, you only have option #1, because you can't put @Primary into a class you didn't write and only have the .class of.

Good Luck


I have an example of using multiple implementations of a Service.
I'm working on a project that need fake service implementations. I do not need mock at this case... Suppose I have a specific service which collect data from any database but I can't use this service when working at some new functionality that depends on this service. I'd like to use another implementation which returns fake data only to continue working.

Even considering this example, I think using Qualifier or Primary is not a better approach because I don't want that programmers need to change code but instead some kind of config file.

Any ideas?

8 years ago
Hi all.
Considering Oracle's documentation for ServletContext.setInitParameter method, there is the following assertion when describing the exceptions that could be thrown when setInitParameter is invoked:

IllegalStateException - if this ServletContext has already been initialized

To test this, I've created a context parameter at DD (Web Deployment Descriptor - which should be initialized when the application is started) .
So I've tried to change the attribute value expecting for an exception (IllegalStateException in such case).

I'd like to know why this exception isn't thrown?

Context parameter defined at DD:

And the servlet's code that tries to change the context parameter through setInitParameter.

Thanks in advance.

Question from enthuware: enthuware.jwpv6.2.1045:
Which of the following events allow you to access the HttpSession attribute that cause the event to be fired?

The doubt lies among these two answers:
- HttpSessionBindingEvent
- HttpSessionAttributeEvent

When I create a listener of type HttpSessionAttributeListener, I can retrieve the same data as I can when using HttpSessionBindingEvent. And further, I can get the attribute that cause the event considering all objects/values that can be added to the Session attribute.

I still think that the right answer is HttpSessionAttributeEvent.

Anybody could help me with this question?
Hi Frits. Thanks for replying. I've tried what you post, but no success...
Instead of posting my web.xml, I've change the code to use Servlet 3.0's annotations to ease this post, ok?

This is a bit modified example of Compression filter available at Head First Servlet & JSP.

My servlet code:

This is the code of the filter:

Compression response wrapper:

and gzip servlet output stream wrapper:

Thanks in advance.
Hi all.

I'm studying chapter 13 from HF Servlets and JSP and I'm trouble with the Compression Filter example.
I've written all the code for this example (regarding the Errata for HF Servlets And JSP).

When requesting the most pages (either via servlet's URL pattern or directly JSP) I got no problems.
But when I try to call a specific Servlet all I get on the browser is a blank page.

This is the code of my Servlet:

Did anybody here already see this kind of problem?
Any ideas?

Thanks in advance.

You can use a4j within RichFaces to return a JSON structure through an assync method calling.
Regarding a4j you can use the a4:jsFunction like follows:

action: the method to call assync
data: a getter method defined at myBean (which returns the List parsed with JSONObject or JSONArray)
name: the name of the javascript function to call on your jsp
oncomplete: the javascript function that will receive the JSON data to be processed as you wish

I hope you can use Richfaces on your project, or newer JSF versions.

Another way to use AJAX: take a look at DWR

9 years ago
Hello Tim Holloway!

Thanks for replying.
I need to do this because of a really specific scenario.
The project I'm working on use the JSF 1.1 spec and I need to invoke dynamically some methods from backing beans which implements some kind of interface.
But there is some times that a specific method cannot be executed dynamically and I'd like to detect when it was invoked.

I can't change this architecture, so I'd like to use some JSF hooks to achieve that.

But, I can get the "events" attribute now from the UIViewRoot.
I forgot to use the code below before retrieving the value of the attribute.

I know that I'm breaking this encapsulation but I don't have any idea to deal with this so specific scenario.

9 years ago
Hi all.

I've been struggling to get the name of the method that started invoking my BackingBean.
I'm using a PhaseListener who is listening to invoke_app phase id.
Through this approach, I'd like to know the name of the method that fired this event.

I've been searching for the method name within the FacesContext , Application and ExternalContext but with no success.
I also tried to use reflection to get access to the "events" attribute of UIViewRoot (I saw inside the myfaces sourcecode). But... no success again...

Anybody know any way to achieve this? either by reflection or better using the API without any hacking.

The source code using reflection with no success

9 years ago
Hello all.

I've created a web application which has some artifacts that I'd like to distribute to be used by another web application.
There is some JSP, Sevlets and even tiles-definitions on that project.
I was searching some way to do that, but the problem is that Servlets must be declared within the web.xml.

Does anybody could tell me if what I'm searching for is possible at least?

Any suggestions on where to find more about it will be appreciated.

9 years ago
Hi all.

I'm studying about patterns with the "Head First Design Patterns Book".
At the factory pattern chapter, one bullet point says: "Abstract Factory relies on object composition"

I'd like to get clear where the composition is being done with Abstract Factory.
I always saw composition when an aggregation is present when modeling a class.

Thank you folks!
Hi all.

I have a conceptual doubt about threads. If I have an object that has the method that follows:

I'd like to confirm that the object whose method is synchronized is the object that has been locked.
So the instance variables such as acct who is a reference to an object isn't locked.

Am I right?