Binding to an InitialContext in JBoss 7.1.1
madchedar0 May 22, 2012 12:47 PMI've been scouring the forums/web for a way to bind objects to names using an InitialContext object. I'm in the process of migrating our code from JBoss 5.1.0GA to JBoss 7.1.1Final.
I have a few services with constructors like the following:
public SomeServiceManager() { context = new InitialContext(); try { context.bind(SERVICE_NAME, this); // where SERVICE_NAME is "java:/SomeServiceManager" or similar } catch(NamingException e ) { logger.error("Failed to bind name in context", e); } }
But I get the following error during startup (I have an ear called nexus.ear that contains the sar I'm porting over to as7.1.1 called nexus.sar):
10:57:33,683 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-13) MSC00001: Failed to start service jboss.deployment.subunit."nexus.ear"."nexus.sar".INSTALL: org.jboss.msc.service.StartExcepti
on in service jboss.deployment.subunit."nexus.ear"."nexus.sar".INSTALL: Failed to process phase INSTALL of subdeployment "nexus.sar" of deployment "nexus.ear"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_30]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_30]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_30]
Caused by: java.lang.IllegalArgumentException: JBAS017221: Class not instantiated
at org.jboss.as.service.ReflectionUtils.newInstance(ReflectionUtils.java:107)
at org.jboss.as.service.ParsedServiceDeploymentProcessor.newInstance(ParsedServiceDeploymentProcessor.java:204)
at org.jboss.as.service.ParsedServiceDeploymentProcessor.addServices(ParsedServiceDeploymentProcessor.java:109)
at org.jboss.as.service.ParsedServiceDeploymentProcessor.deploy(ParsedServiceDeploymentProcessor.java:99)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
... 5 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_30]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_30]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_30]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_30]
at org.jboss.as.service.ReflectionUtils.newInstance(ReflectionUtils.java:105)
... 9 more
Caused by: java.lang.UnsupportedOperationException: JBAS011859: Naming context is read-only
at org.jboss.as.naming.WritableServiceBasedNamingStore.requireOwner(WritableServiceBasedNamingStore.java:126)
at org.jboss.as.naming.WritableServiceBasedNamingStore.bind(WritableServiceBasedNamingStore.java:56)
at org.jboss.as.naming.NamingContext.bind(NamingContext.java:221)
at org.jboss.as.naming.InitialContext.bind(InitialContext.java:161)
at org.jboss.as.naming.NamingContext.bind(NamingContext.java:230)
at javax.naming.InitialContext.bind(InitialContext.java:400) [rt.jar:1.6.0_30]
Now I have seen similar topics start to address the read-only naming context issue:
https://community.jboss.org/thread/177141
https://issues.jboss.org/browse/AS7-2755
And the solution is apparently by doing the following here:
The code that's supposed to do the trick:
WritableServiceBasedNamingStore.pushOwner(context.getChildTarget()); try { jmsManager.createConnectionFactory(false, configuration, configuration.getBindings()); } finally { WritableServiceBasedNamingStore.popOwner(); }
But isn't clear to me is:
- The type of context object that is being used that has method "getChildTarget". Is it not an InitialContext object? Is it an OperationContext object?
- Are services supposed to instantiate their own WritableServiceBasedNamingStore to accomplish this?
I'm unclear because I was stepping through the WritableServiceBasedNamingStore code and it seems like unless there is a WritableServiceBasedNamingStore.WRITE_OWNER (http://grepcode.com/file/repo1.maven.org/maven2/org.jboss.as/jboss-as-naming/7.1.1.Final/org/jboss/as/naming/WritableServiceBasedNamingStore.java#WritableServiceBasedNamingStore.0WRITE_OWNER) associated with a ServiceTarget, then you get the "Naming Context is read-only" exception thrown here: http://grepcode.com/file/repo1.maven.org/maven2/org.jboss.as/jboss-as-naming/7.1.1.Final/org/jboss/as/naming/WritableServiceBasedNamingStore.java#WritableServiceBasedNamingStore.requireOwner%28%29
When I've tried incorporating the above code into my services I've run into issues because I guess I'm supposed to create ServiceTarget objects and maybe have an OperationContext instead of an InitialContext object? Problem is those classes (ServiceTargetImpl, ServiceBuilderImpl, OperationContext) are relagated to having only package access and aren't accessible. So I'm unsure of where to turn next.
Is there a way to bind objects to an InitialContext similar to the way it was done in JBoss 5.1.0GA? If not, what is the correct way?
Thank you