📄 ch13s26.html
字号:
import java.io.InputStream;
import java.util.HashMap;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import org.jboss.naming.NonSerializableFactory;
public class JNDIMap extends org.jboss.util.ServiceMBeanSupport implements JNDIMapMBean
{
private String jndiName;
private HashMap contextMap = new HashMap();
public String getJndiName()
{
return jndiName;
}
public void setJndiName(String jndiName) throws NamingException
{
String oldName = this.jndiName;
this.jndiName = jndiName;
if( super.getState() == STARTED )
{
unbind(oldName);
try
{
rebind();
}
catch(Exception e)
{
NamingException ne = new NamingException("Failed to update jndiName");
ne.setRootCause(e);
throw ne;
}
}
}
public String getName()
{
return "JNDIMap(" + jndiName + ")";
}
public void startService() throws Exception
{
rebind();
}
public void stopService()
{
unbind(jndiName);
}
private static Context createContext(Context rootContext, Name name) throws NamingException
{
Context subctx = rootContext;
for(int n = 0; n < name.size(); n ++)
{
String atom = name.get(n);
try
{
Object obj = subctx.lookup(atom);
subctx = (Context) obj;
}
catch(NamingException e)
{ // No binding exists, create a subcontext
subctx = subctx.createSubcontext(atom);
}
}
return subctx;
}
private void rebind() throws NamingException
{
InitialContext rootCtx = new InitialContext();
// Get the parent context into which we are to bind
Name fullName = rootCtx.getNameParser("").parse(jndiName);
log.debug("fullName="+fullName);
Name parentName = fullName;
if( fullName.size() > 1 )
parentName = fullName.getPrefix(fullName.size()-1);
else
parentName = new CompositeName();
Context parentCtx = createContext(rootCtx, parentName);
Name atomName = fullName.getSuffix(fullName.size()-1);
String atom = atomName.get(0);
NonSerializableFactory.rebind(parentCtx, atom, contextMap);
}
private void unbind(String jndiName)
{
try
{
Context rootCtx = (Context) new InitialContext();
rootCtx.unbind(jndiName);
NonSerializableFactory.unbind(jndiName);
}
catch(NamingException e)
{
log.exception(e);
}
}
}
</pre></div></div></div><div class="section"><a name="d0e9795"></a><div class="titlepage"><div><h3 class="title"><a name="d0e9795"></a>Sample jboss.jcml mbean entry</h3></div></div><p>A sample jboss.jcml mbean entry for the JNDIMap MBean is given in <a href="ch13s26.html#howto.mbeans.config" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s26.html#howto.mbeans.config" title="Figure 13.4. Sample jboss.jcml entry for the JNDIMap MBean">Figure 13.4</a>. This binds a HashMap object under the "inmemory/maps/MapTest" JNDI name.
</p><div class="figure"><p><a name="howto.mbeans.config"></a><b>Figure 13.4. Sample jboss.jcml entry for the JNDIMap MBean</b></p><pre class="programlisting">
<server>
...
<mbean code="org.jboss.naming.NamingService" name="DefaultDomain:service=Naming">
<attribute name="Port">1099</attribute>
</mbean>
<!-- Add the JNDIMap entry after the NamingService since the NamingService must
be running in order for the JNDIMap bean to start.
-->
<mbean code="JNDIMap" name="DefaultDomain:service=JNDIMap,jndiName=inmemory/maps/MapTest">
<attribute name="JndiName">inmemory/maps/MapTest</attribute>
</mbean>
...
</server>
</pre><pre class="programlisting">
// Sample lookup code
InitialContext ctx = new InitialContext();
HashMap map = (HashMap) ctx.lookup("inmemory/maps/MapTest");
</pre></div></div></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch13s21.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s21.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch13s39.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s39.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -