Re: JpaPersistModule, Hibernate and URL Variables
If you always want to use h2, you could just simply build the string using https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getenv--? If you wanted to inject different JPA providers, you could certainly use Guice for that (since you posted here) - I have a HibernateSessionFactory that provides a session per request like this: @Provides @RequestScoped private Session provideSession() { And then for my jUnit tests I have: @Provides @Singleton private Session provideSession() { in a public class TestGuiceModule extends AbstractModule Would that fit your use case? Cheers, -S- On Wednesday, February 14, 2018 at 10:38:16 AM UTC, Evan Ruff wrote: > > Hey guys, > > I've got what I think is a pretty straightforward task but I'm having a > little issue figuring out where to attach in my code. > > I've got an embedded H2 database being used in an installed application to > manage state of some objects. I'm using Hibernate as my JPA implementation > and everything is working great. The one issue I have is that the path of > the database file is set in my URL as: > > jdbc:h2:file:./data/application_db;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE; > > which changes depending on how the application is executed. I'd like to > have the database file be written in a location set by an environment > variable, so that I'd have: > > jdbc:h2:file:*$APP_DB_PATH* > /data/application_db;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE; > > I've tried several different forms and it doesn't seem to be taking. > > Has anyone encountered this and could give me a pointer on how to > implement? > > Thanks! > > E > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6e5d4d2f-5f5c-430b-93ef-aca089efb688%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Configuring a GenericServlet (not HttpServlet)
> little problem: I want to map a GenericServlet (one that extends > com.caucho.hessian.server.HessianServlet, so I cannot simply opt for > changing it) rather than a HttpServlet. No takers? Guess I will bite the dust and add a web.xml-configured servlet for this for now, then... -S- -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Configuring a GenericServlet (not HttpServlet)
Hi, I am configuring my servlets with Guice (a joy!), but ran into this little problem: I want to map a GenericServlet (one that extends com.caucho.hessian.server.HessianServlet, so I cannot simply opt for changing it) rather than a HttpServlet. How can I do that? The ServletKeyBindingBuilder interface seems to be all HttpServlet? Still on Guice2 btw, but this might be a good excuse to make the move (everything I have needed is working smoothly, so have just not felt the need!) if it is needed. Cheers, -S- -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Re: Is a global Injector reference bad practice?
Sorry to comment on my own post, but I guess a main obligation could be that the dependency on Guice should not be scattered around the code base and the practical implication being ease of switching DI frameworks. Would still welcome comments (not least on what would be best practice for the samples given), though. Thanks. -S- On Nov 4, 9:07 pm, Sondre Bjornebekk wrote: > Hi, > > Sometimes it can be very convenient to just do > injector.getInstance(InjectedObject.class), for instance if you have a > object you don't need to be able to mock, but want to send a > dependency that you _would_ be like to be able to mock[1]. Currently I > do this by setting a global reference to the Injector when my module > is set up (typically either in a junit setUp() or extending > GuiceServletContextListener, see bottom for the code for the > latter[2]). > > Is this considered bad practice? If so, (a) what is the kosher way? > and (b) any practical consequences? > > Also, a somewhat related question: Instead of doing the assisted > inject hoopla (which Guice admittedly makes much much more elegant > than manual factories), I sometimes find it easier to do: > > new NonInjectedObject("param that could have been annotated > assisted", > injector.getInstance(InjectedObject.class)); > > Will this be frowned upon by you DI gurus? :-) > > Cheers, > > -S- > > [1] - For example for some simple ad-hoc JSPs, I have a JSPFacade > class that will never be anything else, but it is indeed useful to > switch the implementations it uses or at least get all the > dependencies autowired according to the active module config. > > [2] - Code for global reference in the guice servlet context listener: > > /** Don't know if this is bad practice, but sets up and keeps a > global reference that can be used to use the Injector other places. */ > public static Injector getGlobalInjectorReference(){ > return injector; > } > > @Override > protected Injector getInjector() { > injector = Guice.createInjector(new ServletModule() { > @Override > protected void configureServlets() { > log.info("configureServlets starting"); > > } > }); > > return injector; > } -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Is a global Injector reference bad practice?
Hi, Sometimes it can be very convenient to just do injector.getInstance(InjectedObject.class), for instance if you have a object you don't need to be able to mock, but want to send a dependency that you _would_ be like to be able to mock[1]. Currently I do this by setting a global reference to the Injector when my module is set up (typically either in a junit setUp() or extending GuiceServletContextListener, see bottom for the code for the latter[2]). Is this considered bad practice? If so, (a) what is the kosher way? and (b) any practical consequences? Also, a somewhat related question: Instead of doing the assisted inject hoopla (which Guice admittedly makes much much more elegant than manual factories), I sometimes find it easier to do: new NonInjectedObject("param that could have been annotated assisted", injector.getInstance(InjectedObject.class)); Will this be frowned upon by you DI gurus? :-) Cheers, -S- [1] - For example for some simple ad-hoc JSPs, I have a JSPFacade class that will never be anything else, but it is indeed useful to switch the implementations it uses or at least get all the dependencies autowired according to the active module config. [2] - Code for global reference in the guice servlet context listener: /** Don't know if this is bad practice, but sets up and keeps a global reference that can be used to use the Injector other places. */ public static Injector getGlobalInjectorReference(){ return injector; } @Override protected Injector getInjector() { injector = Guice.createInjector(new ServletModule() { @Override protected void configureServlets() { log.info("configureServlets starting"); } }); return injector; } -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Re: "Parameter based singleton scope" in AssistedInject factory?
Neat. In fact the MapMaker[1] that takes a Function and provides different eviction policies etc. that you used for the map of caches will probably also save me from some manual work in the actual local heap cache implementation around synchronization etc. Thanks! Since I found it less than smooth to integrate Guice 3.0 in my maven build at this point (Stuart McCulloch's snapshot build that was discussed here seems to only have 2.1.8?), I replaced the @AlwaysCreatesNewInstances with a simple @Named annotation to differentiate the injection for the "actual cache factory" from the Multiton giving me: @Inject public MultitonCacheFactory(@Named("underlyingFactory") CacheFactory cacheFactory) { this.underlyingFactory = cacheFactory; } And: bind(CacheFactory.class). to(MultitonCacheFactory.class).in(Singleton.class); bind(CacheFactory.class). annotatedWith(Names.named("underlyingFactory")). toProvider( FactoryProvider.newFactory(CacheFactory.class, NamedCacheLocal.class) ); This seems to work in the unit testing (and with some simple concurrency testing). Any comments on that approach? Will do a quick feasibility test, but cannot see why I should not be able to wrap Coherence (or something similar) by just changing the last of the two bindings. -S- [1] - This Google guava library was in fact all new to me and seems really useful. Kudos goes to MV for releasing it under an Apache license. On Oct 14, 12:27 pm, Fred Faber wrote: > Basically you have a factory that should return a singleton instance for > each unique string value passed into it? > > FactoryProvider won't do this for you out of the box, as it will create a > new instance per invocation (as you've illustrated). > > What I would do in this case is to decorate your factory with an > implementation that controls singletonness: > > interface CacheFactory { > // This can be named anything you want, not just "create" > NamedCache create(String name); > > } > > class CachingCacheFactory implements CacheFactory { > > private final CacheFactory underlyingFactory; > > //http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/co... > private final Map caches = new MapMaker() > .makeComputingMap(new Function(){ > �...@override public NamedCache apply(String name) { > return underlyingFactory.create(name); > } > }); > > @Inject > CachingCacheFactory { > @AlwaysCreatesNewInstances CacheFactory cacheFactory) { > this.underlyingFactory = cacheFactory; > } > > @Override > NamedCache create(String name) { > return caches.get(name); > } > > } > > * * * > > public class YourModule extends AbstractModule { > > �...@override protected void configure() { > // as an alternative to binding this as a singleton, you could inject > the map of caches > // as a singleton instance. 6 or 1/2 dozen, unless you want to > unittest your > // implementation, in which case you'll want to inject the singleton > map > bind(CacheFactory.class) > .to(CachingCacheFactory.class) > .in(Singleton.class); > > > //http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec... > // this binds a "CacheFactory" instance, annotated with > @AlwaysCreatesNewInstances, to > // a default factory implementation which will return instances of > NamedCacheLocal when > // its create() method is invoked > install(new FactoryModuleBuilder() > .implement(NamedCache, NamedCacheLocal) > .build(Key.get(NamedCache.class, > AlwaysCreatesNewInstances.class)); > } > > } > > That should do the trick. > > -Fred > > On Thu, Oct 14, 2010 at 4:25 AM, Sondre Bjornebekk < > > sondre.bjorneb...@gmail.com> wrote: > > Hi, > > > Trying to use assisted inject and find it pretty neat, the minor > > detail is that it does not quite work for me yet :-) > > > I have created a Map wrapper to ease switching of caches (typically to > > something like Coherence) for my application. I want the Map to be > > returned "in a Singleton scope, based on the value of the (assisted) > > parameter". I am doing: > > > bind(CacheFactory.class).toProvider( > > FactoryProvider.newFactory(CacheFactory.class, > > NamedCacheLocal.class) ); > > > And I have: > > /** > > * A factory for named caches, that later might hide its > > implementation using a cluster wide storage. > > */ > > > public interface CacheFactory { > >
"Parameter based singleton scope" in AssistedInject factory?
Hi, Trying to use assisted inject and find it pretty neat, the minor detail is that it does not quite work for me yet :-) I have created a Map wrapper to ease switching of caches (typically to something like Coherence) for my application. I want the Map to be returned "in a Singleton scope, based on the value of the (assisted) parameter". I am doing: bind(CacheFactory.class).toProvider( FactoryProvider.newFactory(CacheFactory.class, NamedCacheLocal.class) ); And I have: /** * A factory for named caches, that later might hide its implementation using a cluster wide storage. */ public interface CacheFactory { //had to be named create to work with guice assisted inject, so renamed from getNamedCache public NamedCache create(String name); } public interface NamedCache extends Map { public int getHitPercent(); public String getHitPercentString(); ... } /** * A named cache with a local map as implementation for storage. */ public class NamedCacheLocal implements NamedCache { ... /** * Constructs a NamedCacheLocal with its dependencies. */ @Inject public NamedCacheLocal(@Assisted String cacheName) { ... } I have this (failing) unit test that checks that the same cache is used for the same name: System.out.println("doing put / get test"); NamedCache namedCache = namedCacheFactory.create("myCache"); namedCache.put("fisk","fesk"); for(int i=0;i<10; i++){ Object o = namedCache.get("non-hit"); namedCache.put("test"+i,"testval"+i); } Object hit = namedCache.get("fisk"); System.out.println("cache " + namedCache + " delivered value " + hit + " to a hit percent of " + namedCache.getHitPercentString()); assertTrue(namedCache.getHitPercent() > 8 && namedCache.getHitPercent() < 10); NamedCache namedCacheWithSameName = namedCacheFactory.create("myCache"); for(int i=0;i<10; i++){ String val = namedCacheWithSameName.get("test"+i); System.out.println("got val from cache: " + val); } //should have used the same reference, thus hit pct now higher System.out.println("done with cache " + namedCacheWithSameName); assertTrue(namedCache.getHitPercent() > 10); assertTrue(namedCache.equals(namedCacheWithSameName)); So is the thing I put in quotation marks in the intro ("in a Singleton scope, based on the value of the (assisted) parameter") even possible just by Guice config, or do I have to go two steps back and one forward and just implement the CacheFactory myself with this logic? The first thing I tried was bind( CacheFactory.class ). toProvider( FactoryProvider.newFactory(CacheFactory.class, NamedCacheLocal.class) ) . in ( Singleton.class ); But that (of course) just gives me a singleton CacheFactory, not the NamedCacheLocal "singleton" based on the assisted factory parameter. Cheers, Sondre -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.