Re: AppModule: using injected service in ContributeWebSecurityManager
Hi, I created a JNDI object provider to directly inject JNDI resources into managed services : public class JNDIObjectProvider implements ObjectProvider { @Override public T provide(Class objectType, AnnotationProvider annotationProvider, ObjectLocator locator) { JNDIResource annotation = annotationProvider.getAnnotation(JNDIResource.class); if (annotation == null) { return null; } String name = annotation.value(); try { Context ctx = new InitialContext(); T resource = (T) ctx.lookup(name); return resource; } catch (NamingException e) { throw new RuntimeException("JNDI lookup failed for name: " + name, e); } } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface JNDIResource { String value(); } Used in a service : @Inject @JNDIResource("java:comp/env/jdbc/myDataSource") private DataSource dataSource; Hope it can help Regards, <http://www.financeactive.com/> Guillaume Bodet Directeur de la Technologie - CTO T. +33 177 721 613 M. +33 620 888 665 www.financeactive.com <http://www.financeactive.com/> <https://twitter.com/#!/Financeactive> <http://www.linkedin.com/company/finance-active> <http://fr.viadeo.com/fr/profile/finance.active> <http://fr-fr.facebook.com/pages/Finance-active/426171734065585> P Avant d¹imprimer, pensez à l¹environnement Ce message et toutes les pièces jointes sont établis à l'intention exclusive de ses destinataires et sont confidentiels. L'internet ne permettant pas d'assurer l'intégrité de ce message, Finance active décline toute responsabilité au titre de ce message, dans l'hypothèse où il aurait été intercepté ou modifié. Le 21/06/12 07:01, « cablepuff » a écrit : >Thanks. > >It works. > >I created > >public static DataSource buildMyDataSource() { >// ... implemenation. >} > >then inject the code using @Local @Service("myDataSource") DataSource >dataSource like you suggested. > > > > >-- >View this message in context: >http://tapestry.1045711.n5.nabble.com/AppModule-using-injected-service-in- >ContributeWebSecurityManager-tp5713989p5714000.html >Sent from the Tapestry - User mailing list archive at Nabble.com. > >- >To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org >For additional commands, e-mail: users-h...@tapestry.apache.org > - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
Re: AppModule: using injected service in ContributeWebSecurityManager
Thanks. It works. I created public static DataSource buildMyDataSource() { // ... implemenation. } then inject the code using @Local @Service("myDataSource") DataSource dataSource like you suggested. -- View this message in context: http://tapestry.1045711.n5.nabble.com/AppModule-using-injected-service-in-ContributeWebSecurityManager-tp5713989p5714000.html Sent from the Tapestry - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
Re: AppModule: using injected service in ContributeWebSecurityManager
Hi! I'm pretty sure your dataSource is being injected, otherwise you'd receive a different error and not an NPE. This looks wrong to me: binder.bind(DataSource.class, AppModule.getJndiDataSource().getClass()).withId("blog.dataSource"); Specifically the: AppModule.getJndiDataSource().getClass() Do you not know what the class is at compile time? Rather than binding, you may want a use Builder method. Also, the binding gives an ID of "blog.dataSource" but you're injecting "myDataSource"...? As an aside, I usually inject services in my Module with this: public void contributeWebSecurityManager( final Configuration configuration @Local DataSource dataSource) { ... } No need for @InjectService. The @Local tells T5-IoC to look for a service defined in *that* module class. Because DataSource is a popular interface, it could be defined in other modules. The annotation helps to dis-ambiguate between them. Steve. On 20 June 2012 20:47, cablepuff wrote: > Hi i have the following problem. I have set the following code. > > public void contributeWebSecurityManager( > final Configuration configuration > //, @InjectService("myDataSource")final DataSource > dataSource > ) { > final JdbcRealm realm = new JdbcSaltedRealm(); > realm.setDataSource(getJndiDataSource()); > // realm.setDataSource(dataSource); -- does not work > realm.setAuthenticationQuery(AppModule.AUTHENTICATION_QUERY); > realm.setUserRolesQuery(AppModule.USER_ROLES_QUERY); > realm.setPermissionsQuery(AppModule.PERMISSION_QUERY); > realm.setPermissionsLookupEnabled(true); > realm.setCredentialsMatcher(this.credentialsMatcher); > configuration.add(realm); > } > > In the same AppModule class I have this > > public static void bind(final ServiceBinder binder) { > binder.bind(DataSource.class, > AppModule.getJndiDataSource().getClass()) > .withId("blog.dataSource"); > } > > When i run my app it seems the datasource is not injected. How am I able to > inject the datasource binded into ContributeWebSecurityManager. > > This is error i am getting when using service injection instead of just > manually getting the datasource. > > NFO: Cannot create JDBC driver of class '' for connect URL 'null' > java.lang.NullPointerException > at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524) > at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493) > at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307) > at java.sql.DriverManager.getDriver(DriverManager.java:273) > at > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) > at > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) > at > org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) > at $DataSource_210503fddf1.getConnection(Unknown Source) > at > org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) > at > org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) > at > org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573) > at > org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637) > at > org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666) > at > org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674) > at > org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729) > at > org.apache.shiro.realm.JdbcSaltedRealm.getPasswordForUser(JdbcSaltedRealm.java:242) > at > org.apache.shiro.realm.JdbcSaltedRealm.doGetAuthenticationInfo(JdbcSaltedRealm.java:170) > at > org.apache.shiro.realm.JdbcSaltedRealm.doGetAuthenticationInfo(JdbcSaltedRealm.java:61) > > -- > View this message in context: > http://tapestry.1045711.n5.nabble.com/AppModule-using-injected-service-in-ContributeWebSecurityManager-tp5713989.html > Sent from the Tapestry - User mailing list archive at Nabble.com. > > - > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org > For additional commands, e-mail: users-h...@tapestry.apache.org > - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org
AppModule: using injected service in ContributeWebSecurityManager
Hi i have the following problem. I have set the following code. public void contributeWebSecurityManager( final Configuration configuration //, @InjectService("myDataSource")final DataSource dataSource ) { final JdbcRealm realm = new JdbcSaltedRealm(); realm.setDataSource(getJndiDataSource()); // realm.setDataSource(dataSource); -- does not work realm.setAuthenticationQuery(AppModule.AUTHENTICATION_QUERY); realm.setUserRolesQuery(AppModule.USER_ROLES_QUERY); realm.setPermissionsQuery(AppModule.PERMISSION_QUERY); realm.setPermissionsLookupEnabled(true); realm.setCredentialsMatcher(this.credentialsMatcher); configuration.add(realm); } In the same AppModule class I have this public static void bind(final ServiceBinder binder) { binder.bind(DataSource.class, AppModule.getJndiDataSource().getClass()) .withId("blog.dataSource"); } When i run my app it seems the datasource is not injected. How am I able to inject the datasource binded into ContributeWebSecurityManager. This is error i am getting when using service injection instead of just manually getting the datasource. NFO: Cannot create JDBC driver of class '' for connect URL 'null' java.lang.NullPointerException at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524) at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493) at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307) at java.sql.DriverManager.getDriver(DriverManager.java:273) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) at $DataSource_210503fddf1.getConnection(Unknown Source) at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573) at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637) at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666) at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729) at org.apache.shiro.realm.JdbcSaltedRealm.getPasswordForUser(JdbcSaltedRealm.java:242) at org.apache.shiro.realm.JdbcSaltedRealm.doGetAuthenticationInfo(JdbcSaltedRealm.java:170) at org.apache.shiro.realm.JdbcSaltedRealm.doGetAuthenticationInfo(JdbcSaltedRealm.java:61) -- View this message in context: http://tapestry.1045711.n5.nabble.com/AppModule-using-injected-service-in-ContributeWebSecurityManager-tp5713989.html Sent from the Tapestry - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org For additional commands, e-mail: users-h...@tapestry.apache.org