package org.jboss.test.classloader.scoping.naming.service;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Attribute;
import org.jboss.mx.util.MBeanServerLocator;
public class BindingService
{
private String[] names = {"Name0", "Name1", "Name2"};
private Boolean origCallByValue;
public String[] getNames()
{
return names;
}
public void setNames(String[] names)
{
this.names = names;
}
public void start() throws Exception
{
MBeanServer server = MBeanServerLocator.locateJBoss();
ObjectName namingService = new ObjectName("jboss:service=Naming");
origCallByValue = (Boolean) server.getAttribute(namingService, "CallByValue");
Attribute callByValue = new Attribute("CallByValue", Boolean.TRUE);
server.setAttribute(namingService, callByValue);
System.out.println("NamingService.CallByValue set to true");
InitialContext ctx = new InitialContext();
Context testCtx = ctx.createSubcontext("shared-context");
System.out.println("Created shared-context");
testCtx.bind("KeyCount", new Integer(names.length));
System.out.println("Bound KeyCount");
for(int n = 0; n < names.length; n ++)
{
String key = "Key#" + n;
BindValue value = new BindValue();
value.setValue("Value#"+n);
testCtx.bind(key, value);
System.out.println("Bound "+key);
}
}
public void stop() throws Exception
{
MBeanServer server = MBeanServerLocator.locateJBoss();
ObjectName namingService = new ObjectName("jboss:service=Naming");
Attribute callByValue = new Attribute("CallByValue", origCallByValue);
server.setAttribute(namingService, callByValue);
System.out.println("NamingService.CallByValue restored to: "+origCallByValue);
InitialContext ctx = new InitialContext();
Context testCtx = (Context) ctx.lookup("shared-context");
testCtx.unbind("KeyCount");
System.out.println("Unbound KeyCount");
for(int n = 0; n < names.length; n ++)
{
String key = "Key#" + n;
testCtx.unbind(key);
System.out.println("Unbound "+key);
}
ctx.unbind("shared-context");
System.out.println("Destroyed shared-context");
}
}