Hi William
The problem is the following:
You can't inject an EntityManager or an EntityManagerFactory until
the PersistService.start() method has run.
This is because the start method creates the EntityManagerFactory.
So to fix your code you can inject Provider<XXX> instead of an
instance of XXX.
Hope this helps.
On 08/04/2012 12:47 AM, William Osmond
wrote:
Hello group,
I've seen other people with similar problems to mine, however none
of the solutions presented are working here. Hopefully one of you
can point me in the right direction.
I'm attempting to get Guice, JPA, and Shiro all to play nice with
each other (and Wicket, once this is nailed down). I'm trying to
deploy the following onto resin-4.0.29:
web.xml -
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>MyApp</display-name>
<filter>
<filter-name>GuiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GuiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>my.app.guice.ServletBootstrap</listener-class>
</listener>
</web-app>
ServletBootstrap.class -
public class ServletBootstrap extends
GuiceServletContextListener {
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent
servletContextEvent) {
servletContext =
servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
ServletModule servletModule = new ServletModule() {
@Override
protected void configureServlets() {
super.configureServlets();
install(new
JpaPersistModule("serverPersistenceUnit"));
install(new WebSecurityModule(servletContext));
filter("/*").through(PersistFilter.class);
filter("/*").through(GuiceShiroFilter.class);
}
};
return Guice.createInjector(servletModule);
}
}
WebSecurityModule.class -
public class WebSecurityModule extends ShiroWebModule {
public WebSecurityModule(ServletContext servletContext) {
super(servletContext);
}
@Override
protected void configureShiroWeb() {
bindRealm().to(BaseRealm.class);
addFilterChain("/**", ANON);
}
}
BaseRealm.class -
public class BaseRealm extends AuthorizingRealm {
@Inject private SecurityDAO securityDAO;
@Inject private UserDAO userDAO;
public BaseRealm() {
setName("BaseRealm");
}
@Override
protected SimpleAuthenticationInfo
doGetAuthenticationInfo(AuthenticationToken authenticationToken)
throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken)
authenticationToken;
String username = token.getUsername();
User user = securityDAO.findUserByUsername(username);
if (user != null) {
return new SimpleAuthenticationInfo(user.getId(),
user.getPasswordHash(), getName());
} else {
return null;
}
}
@Override
protected SimpleAuthorizationInfo
doGetAuthorizationInfo(PrincipalCollection principalCollection)
{
Long userId = (Long)
principalCollection.fromRealm(getName()).iterator().next();
User user = userDAO.find(userId);
if (user != null) {
SimpleAuthorizationInfo info = new
SimpleAuthorizationInfo();
List<String> permissions =
identifyPermissions(user);
info.addStringPermissions(permissions);
return info;
} else {
return null;
}
}
private List<String> identifyPermissions(User user) {
List<String> permissions = new
ArrayList<String>();
if (user.isCreator()) {
permissions.add("repository:create");
}
for (Long id : securityDAO.findReposOwnedByUser(user)) {
permissions.add("repository:delete:" + id);
permissions.add("repository:" + id);
}
for (Long id :
securityDAO.findReposAdministerableByUser(user)) {
permissions.add("repository:" + id);
}
for (Long id :
securityDAO.findReposContributableByUser(user)) {
permissions.add("repository:" + id + ":create");
permissions.add("repository:" + id + ":view");
}
for (Long id :
securityDAO.findReposViewableByUser(user)) {
permissions.add("repository:" + id + ":view");
permissions.add("repository:" + id +
":create:note");
}
return permissions;
}
}
And when I run it,
I get this:
03 15:42:02.859] {main} com.google.inject.CreationException:
Guice creation errors:
1) Error in custom provider,
java.lang.NullPointerException
while locating
com.google.inject.persist.jpa.JpaPersistService
while locating
javax.persistence.EntityManager
for field at
com.brightandbrown.bbfiles.data.daos.SecurityDAO.em(SecurityDAO.java:10)
while locating
com.brightandbrown.bbfiles.data.daos.SecurityDAO
for field at
com.brightandbrown.bbfiles.shiro.BaseRealm.securityDAO(BaseRealm.java:15)
while locating
com.brightandbrown.bbfiles.shiro.BaseRealm
while locating
org.apache.shiro.realm.Realm annotated with
@com.google.inject.multibindings.Element(setName=,uniqueId=3)
at
org.apache.shiro.guice.ShiroModule.bindRealm(ShiroModule.java:103)
while locating
java.util.Set<org.apache.shiro.realm.Realm>
while locating
java.util.Collection<org.apache.shiro.realm.Realm>
for parameter 0 at
org.apache.shiro.web.mgt.DefaultWebSecurityManager.<init>(DefaultWebSecurityManager.java:85)
at
org.apache.shiro.guice.ShiroModule.configure(ShiroModule.java:55)
while locating
org.apache.shiro.mgt.SecurityManager
Caused by: java.lang.NullPointerException
at
com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)
at
com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:50)
at
com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:34)
at
com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:55)
at
com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
at
com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
at
com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
at
com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at
com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
at
com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:110)
at
com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
at
com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at
com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
at
com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
at
com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at
com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
at
com.google.inject.multibindings.Multibinder$RealMultibinder.get(Multibinder.java:326)
at
com.google.inject.multibindings.Multibinder$RealMultibinder.get(Multibinder.java:220)
at
com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at
com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:54)
at
com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
at
com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
at
com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:84)
at
com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at
com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
at
com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at
com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
at
com.google.inject.Scopes$1$1.get(Scopes.java:65)
at
com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at
com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:204)
at
com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:198)
at
com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
at
com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:198)
at
com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:179)
at
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
at
com.google.inject.Guice.createInjector(Guice.java:95)
at
com.google.inject.Guice.createInjector(Guice.java:72)
at
com.google.inject.Guice.createInjector(Guice.java:62)
at
com.brightandbrown.bbfiles.guice.ServletBootstrap.getInjector(ServletBootstrap.java:37)
at
com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
at
com.brightandbrown.bbfiles.guice.ServletBootstrap.contextInitialized(ServletBootstrap.java:20)
at
com.caucho.server.webapp.WebApp.start(WebApp.java:3575)
at
com.caucho.env.deploy.DeployController.startImpl(DeployController.java:680)
at
com.caucho.env.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:77)
at
com.caucho.env.deploy.DeployController.startOnInit(DeployController.java:530)
at
com.caucho.env.deploy.DeployContainer.start(DeployContainer.java:170)
at
com.caucho.server.webapp.WebAppContainer.start(WebAppContainer.java:728)
at
com.caucho.server.host.Host.start(Host.java:677)
at
com.caucho.env.deploy.DeployController.startImpl(DeployController.java:680)
at
com.caucho.env.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:77)
at
com.caucho.env.deploy.DeployController.startOnInit(DeployController.java:530)
at
com.caucho.env.deploy.DeployContainer.start(DeployContainer.java:170)
at
com.caucho.server.host.HostContainer.start(HostContainer.java:542)
at
com.caucho.server.cluster.ServletService.start(ServletService.java:1337)
at
com.caucho.server.cluster.ServletSystem.start(ServletSystem.java:72)
at
com.caucho.env.service.ResinSystem.startServices(ResinSystem.java:529)
at
com.caucho.env.service.ResinSystem.start(ResinSystem.java:496)
at
com.caucho.server.resin.Resin.start(Resin.java:817)
at
com.caucho.server.resin.Resin.initMain(Resin.java:949)
at
com.caucho.server.resin.Resin.main(Resin.java:1398)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
java.lang.reflect.Method.invoke(Method.java:597)
at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Any help would be
greatly appreciated!
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/u9HN_8RvJCEJ.
To post to this group, send email to
[email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
|