Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
binding date using properties
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
egolan  
View profile  
 More options Jan 31 2012, 9:42 am
From: egolan <egola...@gmail.com>
Date: Tue, 31 Jan 2012 06:42:19 -0800 (PST)
Local: Tues, Jan 31 2012 9:42 am
Subject: binding date using properties
Hi,
I am using  Names.bindProperties(binder(), properties); where my
properties are taken from a pre-initialized configuration.
One of the properties represent java.util.Date.
We have a simple date format class that sets the value:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy
HH:mm:ss");
when I just use the Names binding, I get the Guice creation exception
that the named field was not bound.
This is my injected constructor:

    @Inject
    public CoreFetchOperationsImpl(@Named("MpsQueryFilter") String
genericQuery, @Named("MpsIterationBulkSize")int bulkSizeFetch,
            @Named("MpsLastProcessingTime") Date lastProcessingTime)

In order to solve it, I am getting from the pre-initialized
configuration the concrete Date as string, parse it using the format
and bind specifically:
bind(Date.class).annotatedWith(Names.named("MpsLastProcessingTime")).toInst ance(parsedDate));

Is there a better way?

Thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart McCulloch  
View profile  
 More options Jan 31 2012, 10:10 am
From: Stuart McCulloch <mccu...@gmail.com>
Date: Tue, 31 Jan 2012 15:10:16 +0000
Local: Tues, Jan 31 2012 10:10 am
Subject: Re: binding date using properties
On 31 Jan 2012, at 14:42, egolan wrote:

> Hi,
> I am using  Names.bindProperties(binder(), properties); where my
> properties are taken from a pre-initialized configuration.
> One of the properties represent java.util.Date.
> We have a simple date format class that sets the value:
> SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy
> HH:mm:ss");
> when I just use the Names binding, I get the Guice creation exception
> that the named field was not bound.

Names.bindProperties(...) creates constant bindings for the properties map - constant bindings are converted from Strings to actual instances by TypeConverters:

   http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec...

Guice provides built-in TypeConverters for primitive types, enums, and class literals - but not for Date, hence the creation exception. However, you can add your own:

   http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec...)

See also http://99soft.github.com/rocoto/ which provides a range of useful TypeConverters, including one for Date: http://99soft.github.com/rocoto/converters.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Peierls  
View profile  
 More options Jan 31 2012, 10:55 am
From: Tim Peierls <t...@peierls.net>
Date: Tue, 31 Jan 2012 10:55:38 -0500
Local: Tues, Jan 31 2012 10:55 am
Subject: Re: binding date using properties

I recommend using Rocoto, too, but here's an example that shows how to do
it yourself, expressed as a JUnit test:

http://pastebin.com/mWx9xG4M

--tim

On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eyal Golan  
View profile  
 More options Jan 31 2012, 11:20 am
From: Eyal Golan <egola...@gmail.com>
Date: Tue, 31 Jan 2012 18:20:54 +0200
Local: Tues, Jan 31 2012 11:20 am
Subject: Re: binding date using properties

Thanks for the inputs.
The code example is what I needed.
However, using it will convert all Date classes that get injected.
Is there a way to create a converter only for parameters annotated with
Named ?

Eyal Golan
egola...@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74
Skype: egolan74

P  Save a tree. Please don't print this e-mail unless it's really necessary


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jordi  
View profile  
 More options Jan 31 2012, 11:39 am
From: jordi <jo...@donky.org>
Date: Tue, 31 Jan 2012 17:39:08 +0100
Local: Tues, Jan 31 2012 11:39 am
Subject: Re: binding date using properties

use a regular guice Matcher [1], I guess this would do the trick:

import static com.google.inject.matcher.Matchers.annotatedWith;

// ...
protected void configure() {
  convertToTypes(annotatedWith(Names.named("YOUR_KEY")), typeConverter);

}

jordi

[1]
http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
egolan  
View profile  
 More options Jan 31 2012, 11:41 am
From: egolan <egola...@gmail.com>
Date: Tue, 31 Jan 2012 08:41:18 -0800 (PST)
Local: Tues, Jan 31 2012 11:41 am
Subject: Re: binding date using properties
jordi,
I tried that. the convertToTypes needs TypeLiteral.

Tim & Stuart,
This is great !!
I think it is a good idea to inject all dates with that converter.
However, just to understand. Is it possible to convert base on
Annotation (or Named)

Thanks,

On Jan 31, 6:39 pm, jordi <jo...@donky.org> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart McCulloch  
View profile  
 More options Jan 31 2012, 12:13 pm
From: Stuart McCulloch <mccu...@gmail.com>
Date: Tue, 31 Jan 2012 17:13:06 +0000
Local: Tues, Jan 31 2012 12:13 pm
Subject: Re: binding date using properties
On 31 Jan 2012, at 16:41, egolan wrote:

> jordi,
> I tried that. the convertToTypes needs TypeLiteral.

> Tim & Stuart,
> This is great !!
> I think it is a good idea to inject all dates with that converter.
> However, just to understand. Is it possible to convert base on
> Annotation (or Named)

That's right - Type conversion only matches by TypeLiteral (not by Key) so it's not possible to select different TypeConverters for different annotations.

What's your use-case for selecting different String->Date conversions by annotation?  There are several approaches you could take involving the SPI, etc. but a lot depends on your use-case...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eyal Golan  
View profile  
 More options Jan 31 2012, 12:21 pm
From: Eyal Golan <egola...@gmail.com>
Date: Tue, 31 Jan 2012 19:21:38 +0200
Local: Tues, Jan 31 2012 12:21 pm
Subject: Re: binding date using properties

I'm not sure exactly yet what are ALL of my use cases :)

This is what I have right now:
property named LastProcessingTime , which has a specific representation of
Date (base on a format we have been using).
A class (the service) that needs to use this property as Date, and that
property is injected by @Named

I will also need to update DB with date (different one) with the same
format, but this will be using a method call.

In the future (we are migrating to Guice just now), I will have other
services that will get injected with date, but they'll have different
@Named.

Well,
After explaining, I think that conversion will be good for the future
use-cases as well :)

Thanks for the help !!
The code is much cleaner now.

Eyal Golan
egola...@gmail.com

Visit: http://jvdrums.sourceforge.net/
LinkedIn: http://www.linkedin.com/in/egolan74
Skype: egolan74

P  Save a tree. Please don't print this e-mail unless it's really necessary


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
igor.petrouk  
View profile  
 More options Jan 31 2012, 11:25 am
From: "igor.petrouk" <igor.petr...@gmail.com>
Date: Tue, 31 Jan 2012 08:25:31 -0800 (PST)
Local: Tues, Jan 31 2012 11:25 am
Subject: Re: binding date using properties
It is used for all String params that are injected to Date variables
and need conversion. This looks like what you need.

On 31 Січ, 18:20, Eyal Golan <egola...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
igor.petrouk  
View profile  
 More options Jan 31 2012, 10:37 am
From: "igor.petrouk" <igor.petr...@gmail.com>
Date: Tue, 31 Jan 2012 07:37:41 -0800 (PST)
Local: Tues, Jan 31 2012 10:37 am
Subject: Re: binding date using properties
Take a look at this test. It runs successfully. Is it very different
from how you do your code?

class MyClass{
  Date date;

  @Inject
  public MyClass(@Named("date") Date dt){
    this.date = dt;
  }

}

class DateTypeConverter implements TypeConverter{
  public Object convert(String value, TypeLiteral<?> toType) {
    return new Date();
  }

}

public class SimpleTest extends TestCase {
  class MyModule extends AbstractModule{
    @Override
    protected void configure() {
      Map<String, String> map = new HashMap<String, String>();
      map.put("date","something");
      Names.bindProperties(binder(), map);
      bind(MyClass.class);
      convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), new
DateTypeConverter());
    }
  }

  public void test(){
      Injector injector = Guice.createInjector(new MyModule());
      MyClass aClass = injector.getInstance(MyClass.class);
 }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Igor Petrouk  
View profile  
 More options Jan 31 2012, 10:31 am
From: Igor Petrouk <igor.petr...@gmail.com>
Date: Tue, 31 Jan 2012 17:31:29 +0200
Local: Tues, Jan 31 2012 10:31 am
Subject: Re: binding date using properties

Take a look at this test. It runs successfully. Is it very different from
how you do your code?

class MyClass{
  Date date;

  @Inject
  public MyClass(@Named("date") Date dt){
    this.date = dt;
  }

}

class DateTypeConverter implements TypeConverter{
  public Object convert(String value, TypeLiteral<?> toType) {
    return new Date();
  }

}

public class SimpleTest extends TestCase {
  class MyModule extends AbstractModule{
    @Override
    protected void configure() {
      Map<String, String> map = new HashMap<String, String>();
      map.put("date","something");
      Names.bindProperties(binder(), map);
      bind(MyClass.class);
      convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), new
DateTypeConverter());
    }
  }

  public void test(){
      Injector injector = Guice.createInjector(new MyModule());
      MyClass aClass = injector.getInstance(MyClass.class);
  }

}

2012/1/31 Stuart McCulloch <mccu...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Igor Petrouk  
View profile  
 More options Jan 31 2012, 9:49 am
From: Igor Petrouk <igor.petr...@gmail.com>
Date: Tue, 31 Jan 2012 16:49:55 +0200
Local: Tues, Jan 31 2012 9:49 am
Subject: Re: binding date using properties

Hi.

What do you mean by this?

> We have a simple date format class that sets the value:
> SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");

Where does this format class appear?

2012/1/31 egolan <egola...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »