Re: t5: @Inject hibernate Session into a service?
Angelo, Thanks Angelo. What my service needs is an access control list stored in a database, but it doesn't actually need to query the database as long as it can be passed the list (as some kind of object). So I think what I need is to use @Session in my AppModule, query in my builder method, and then pass the list to the service (constructor injection). Would that seem like a sane way? I ask because I'm just getting started on tapestry-hibernate and am not sure if this is considered good or bad practice. It seems perfectly sane to me, but opinions are most welcome. I also wanted to reiterate the concern lasitha brought up about injecting the Session into a service, as you mentioned. See the previous messages for them, but you may experience issues using it like that. thanks for any input! Angelo Chen wrote: Hi Chris, If what you want is, accessing the hibernate session from your service, Davor has answer to my similar question before: declare Session as parameter in you service constructor you dont even need to call any inject annotation ... MyServiceImpl(Session session){ this.session=session; } in your module use: public static void bind(ServiceBinder binder) { binder.bind(MyService.class, MyServiceImpl.class); } It works very well. A.C. Chris Lewis-5 wrote: Hi all, So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
Hi Chris, If what you want is, accessing the hibernate session from your service, Davor has answer to my similar question before: declare Session as parameter in you service constructor you dont even need to call any inject annotation ... MyServiceImpl(Session session){ this.session=session; } in your module use: public static void bind(ServiceBinder binder) { binder.bind(MyService.class, MyServiceImpl.class); } It works very well. A.C. Chris Lewis-5 wrote: Hi all, So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/t5%3A-%40Inject-hibernate-Session-into-a-service--tf4501533.html#a12852161 Sent from the Tapestry - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
t5: @Inject hibernate Session into a service?
Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
I've made a bit of progress. First of all, I realized that I don't need database access for my AccessController (Dispatcher) since it will simply need to check a User object for roles, etc. This object would be created/destroyed by a login page. I did figure out an acceptable way to get at the Session at any rate, so here's what I did. tapestry-hibernate provides the HibernateSessionManager service, which is represented by an interface of the same name. So in my AppModule I have a builder method that basically does this: public Dispatcher buildAccessController(@Inject HibernateSessionManager sessionManager) { return new AccessControllerImpl(sessionManager); } The builder is provided with the tapestry-ioc HibernateSessionManager service, which then passes this instance to my AccessController. According to the source of HibernateSessionManager, the implementation provides a Session for the current thread via: HibernateSessionManager#getSession So now that my AccessController has this service, it can simply call getSession() anytime it needs a Session. I'm now dealing with a different (architectural) issue, but that's for another message ;). I hope someone finds this useful! sincerely, chris Chris Lewis wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
why implementing an access control service ? ^_^ maybe = a wery T5 way? Chris Lewis-5 wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/t5%3A-%40Inject-hibernate-Session-into-a-service--tf4501533.html#a12838907 Sent from the Tapestry - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
AFAIK, @Inject only works on pages and components. Services are injected into via their constructors, without need of any annotations. Services are singletons by default, but you can use the @Scope annotation to make them per-thread. If however you want the dispatcher to be a singleton, you've got a little more work to do :) This recent thread about getting an ASO into a singleton service might give you some ideas: T5 - Inject an Application State Object into a Service (Sep 12) [1] In your case, you need to create something similar to the ApplicationStateManager to get a hold of the Session for the current thread. Unfortunately, the HibernateSessionManager [2] service won't work because its also per-thread. Take a look at 'Shadow Services' [3] in the ioc. I think you could create a service that shadows the HibernateSessionManager.getSession() method and have this injected into your dispatcher. Your dispatcher and shadow service remain singletons. I'm afraid i don't have time to test it out, but it seems to work in my head :) Cheers, lasitha. [1] http://mail-archives.apache.org/mod_mbox/tapestry-users/200709.mbox/[EMAIL PROTECTED] [2] http://tapestry.apache.org/tapestry5/tapestry-hibernate/apidocs/index.html?org/apache/tapestry/hibernate/HibernateSessionManager.html [3] http://tapestry.apache.org/tapestry5/tapestry-ioc/shadow.html On 9/22/07, Chris Lewis [EMAIL PROTECTED] wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
Oh, you snuck in while i was typing :) Please test whether your solution works - i don't think it will... The HibernateSessionManager is a per-thread service, and the current implementation creates a session on construction. So your AccessController constructor will be handed the HibernateSessionManager (and thereby the Session) for the first thread that accesses the AccessController. All subsequent threads will get the very same HibernateSessionManager and Session instances. If i'm not getting this all muddled, i think you have to use a shadow to get a different session per thread from your service (see my previous post). Cheers. On 9/22/07, lasitha [EMAIL PROTECTED] wrote: AFAIK, @Inject only works on pages and components. Services are injected into via their constructors, without need of any annotations. Services are singletons by default, but you can use the @Scope annotation to make them per-thread. If however you want the dispatcher to be a singleton, you've got a little more work to do :) This recent thread about getting an ASO into a singleton service might give you some ideas: T5 - Inject an Application State Object into a Service (Sep 12) [1] In your case, you need to create something similar to the ApplicationStateManager to get a hold of the Session for the current thread. Unfortunately, the HibernateSessionManager [2] service won't work because its also per-thread. Take a look at 'Shadow Services' [3] in the ioc. I think you could create a service that shadows the HibernateSessionManager.getSession() method and have this injected into your dispatcher. Your dispatcher and shadow service remain singletons. I'm afraid i don't have time to test it out, but it seems to work in my head :) Cheers, lasitha. [1] http://mail-archives.apache.org/mod_mbox/tapestry-users/200709.mbox/[EMAIL PROTECTED] [2] http://tapestry.apache.org/tapestry5/tapestry-hibernate/apidocs/index.html?org/apache/tapestry/hibernate/HibernateSessionManager.html [3] http://tapestry.apache.org/tapestry5/tapestry-ioc/shadow.html On 9/22/07, Chris Lewis [EMAIL PROTECTED] wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
Let me restate - I do NOT need the HibernateSessionManager, but I DO need an ASO. ASOs are per client/request/thread, so I will have to figure this part out as my service is a Singleton (and I'm not convinced it needs to be per thread). So... couldn't I just inject a reference to the ApplicationStateManager into my Dispatcher, and then use it to grab ASOs? As long as the ApplicationStateManager accesses the current thread, this should work. I haven't yet put much thought into this, but I'm reading up now. Does my logic appear flawed? lasitha wrote: AFAIK, @Inject only works on pages and components. Services are injected into via their constructors, without need of any annotations. Services are singletons by default, but you can use the @Scope annotation to make them per-thread. If however you want the dispatcher to be a singleton, you've got a little more work to do :) This recent thread about getting an ASO into a singleton service might give you some ideas: T5 - Inject an Application State Object into a Service (Sep 12) [1] In your case, you need to create something similar to the ApplicationStateManager to get a hold of the Session for the current thread. Unfortunately, the HibernateSessionManager [2] service won't work because its also per-thread. Take a look at 'Shadow Services' [3] in the ioc. I think you could create a service that shadows the HibernateSessionManager.getSession() method and have this injected into your dispatcher. Your dispatcher and shadow service remain singletons. I'm afraid i don't have time to test it out, but it seems to work in my head :) Cheers, lasitha. [1] http://mail-archives.apache.org/mod_mbox/tapestry-users/200709.mbox/[EMAIL PROTECTED] [2] http://tapestry.apache.org/tapestry5/tapestry-hibernate/apidocs/index.html?org/apache/tapestry/hibernate/HibernateSessionManager.html [3] http://tapestry.apache.org/tapestry5/tapestry-ioc/shadow.html On 9/22/07, Chris Lewis [EMAIL PROTECTED] wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: t5: @Inject hibernate Session into a service?
Your logic is fine with regard to the ApplicationStateManager. I wanted to mention the difference with the HibernateSessionManager for the sake of anyone else that happens upon this thread. All the best, lasitha. On 9/23/07, Chris Lewis [EMAIL PROTECTED] wrote: Let me restate - I do NOT need the HibernateSessionManager, but I DO need an ASO. ASOs are per client/request/thread, so I will have to figure this part out as my service is a Singleton (and I'm not convinced it needs to be per thread). So... couldn't I just inject a reference to the ApplicationStateManager into my Dispatcher, and then use it to grab ASOs? As long as the ApplicationStateManager accesses the current thread, this should work. I haven't yet put much thought into this, but I'm reading up now. Does my logic appear flawed? lasitha wrote: AFAIK, @Inject only works on pages and components. Services are injected into via their constructors, without need of any annotations. Services are singletons by default, but you can use the @Scope annotation to make them per-thread. If however you want the dispatcher to be a singleton, you've got a little more work to do :) This recent thread about getting an ASO into a singleton service might give you some ideas: T5 - Inject an Application State Object into a Service (Sep 12) [1] In your case, you need to create something similar to the ApplicationStateManager to get a hold of the Session for the current thread. Unfortunately, the HibernateSessionManager [2] service won't work because its also per-thread. Take a look at 'Shadow Services' [3] in the ioc. I think you could create a service that shadows the HibernateSessionManager.getSession() method and have this injected into your dispatcher. Your dispatcher and shadow service remain singletons. I'm afraid i don't have time to test it out, but it seems to work in my head :) Cheers, lasitha. [1] http://mail-archives.apache.org/mod_mbox/tapestry-users/200709.mbox/[EMAIL PROTECTED] [2] http://tapestry.apache.org/tapestry5/tapestry-hibernate/apidocs/index.html?org/apache/tapestry/hibernate/HibernateSessionManager.html [3] http://tapestry.apache.org/tapestry5/tapestry-ioc/shadow.html On 9/22/07, Chris Lewis [EMAIL PROTECTED] wrote: Hi all, I'm implementing an access control service as a Dispatcher, and contributing it to the MasterDispatcher service. This dispatcher runs just before PageRender... and ComponentAction..., so that it can check if the user is allowed to access the page/resource. This seems to be a very T5 way of doing things and completely removes the task of access control from the pages (ie i don't have to extend a base page that implements control logic). I want this service to use a database and am already using tapestry-hibernate in this project. I figured I could just @Inject the session into my service just like I would a page or component, but that doesn't work. In a way that makes sense; services are singletons if I'm not mistaken (which makes sense), and Sessions exist (and possibly injected?) per-thread. Being that my service will be started in a different thread than any request, I think @Inject is ignored. So my question is, how should I go about getting access to my database from my service? I'd like to use the blinding simplicity of of IoC just giving it to me, but I;m not sure that's an option. Any ideas? thanks, chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]