📄 jndiutils.html
字号:
<a name="91" href="#91">91</a> r.add(<strong>new</strong> StringRefAddr(JmxUtils.NAME, (String)ht.get(JmxUtils.NAME)));<a name="92" href="#92">92</a> <em class="comment">// Put in a value to serve as a unique 'key'.</em><a name="93" href="#93">93</a> r.add(<strong>new</strong> StringRefAddr(JmxUtils.KEY,<a name="94" href="#94">94</a> on.getCanonicalKeyPropertyListString()));<a name="95" href="#95">95</a> <strong>return</strong> r;<a name="96" href="#96">96</a> }<a name="97" href="#97">97</a> <a name="98" href="#98">98</a> <em>/**<em>*</em></em><a name="99" href="#99">99</a> <em> * Get subcontext. Only looks down one level.</em><a name="100" href="#100">100</a> <em> * @param subContext Name of subcontext to return.</em><a name="101" href="#101">101</a> <em> * @return Sub context.</em><a name="102" href="#102">102</a> <em> * @throws NamingException </em><a name="103" href="#103">103</a> <em> */</em><a name="104" href="#104">104</a> <strong>public</strong> <strong>static</strong> Context getSubContext(<strong>final</strong> String subContext)<a name="105" href="#105">105</a> throws NamingException {<a name="106" href="#106">106</a> <strong>return</strong> getSubContext(getCompoundName(subContext));<a name="107" href="#107">107</a> }<a name="108" href="#108">108</a> <a name="109" href="#109">109</a> <em>/**<em>*</em></em><a name="110" href="#110">110</a> <em> * Get subcontext. Only looks down one level.</em><a name="111" href="#111">111</a> <em> * @param subContext Name of subcontext to return.</em><a name="112" href="#112">112</a> <em> * @return Sub context.</em><a name="113" href="#113">113</a> <em> * @throws NamingException </em><a name="114" href="#114">114</a> <em> */</em><a name="115" href="#115">115</a> <strong>public</strong> <strong>static</strong> Context getSubContext(<strong>final</strong> CompoundName subContext)<a name="116" href="#116">116</a> throws NamingException {<a name="117" href="#117">117</a> Context context = <strong>new</strong> InitialContext();<a name="118" href="#118">118</a> <strong>try</strong> {<a name="119" href="#119">119</a> context = (Context)context.lookup(subContext);<a name="120" href="#120">120</a> } <strong>catch</strong> (NameNotFoundException e) {<a name="121" href="#121">121</a> context = context.createSubcontext(subContext); <a name="122" href="#122">122</a> }<a name="123" href="#123">123</a> <strong>return</strong> context;<a name="124" href="#124">124</a> }<a name="125" href="#125">125</a> <a name="126" href="#126">126</a> <em>/**<em>*</em></em><a name="127" href="#127">127</a> <em> * </em><a name="128" href="#128">128</a> <em> * @param context A subcontext named for the <code>on.getDomain()</code></em><a name="129" href="#129">129</a> <em> * (Assumption is that caller already setup this subcontext).</em><a name="130" href="#130">130</a> <em> * @param on The ObjectName we're to base our bind name on.</em><a name="131" href="#131">131</a> <em> * @return Returns key we used binding this ObjectName.</em><a name="132" href="#132">132</a> <em> * @throws NamingException</em><a name="133" href="#133">133</a> <em> * @throws NullPointerException</em><a name="134" href="#134">134</a> <em> */</em><a name="135" href="#135">135</a> <strong>public</strong> <strong>static</strong> CompoundName bindObjectName(Context context,<a name="136" href="#136">136</a> <strong>final</strong> ObjectName on)<a name="137" href="#137">137</a> throws NamingException, NullPointerException {<a name="138" href="#138">138</a> <em class="comment">// I can't call getNameInNamespace in tomcat. Complains about</em><a name="139" href="#139">139</a> <em class="comment">// unsupported operation -- that I can't get absolute name.</em><a name="140" href="#140">140</a> <em class="comment">// Therefore just skip this test below -- at least for now.</em><a name="141" href="#141">141</a> <em class="comment">// Check that passed context has the passed ObjectNames' name.</em><a name="142" href="#142">142</a> <em class="comment">//</em><a name="143" href="#143">143</a> <em class="comment">// String name = getCompoundName(context.getNameInNamespace()).toString();</em><a name="144" href="#144">144</a> <em class="comment">// if (!name.equals(on.getDomain())) {</em><a name="145" href="#145">145</a> <em class="comment">// throw new NamingException("The current context is " + name +</em><a name="146" href="#146">146</a> <em class="comment">// " but domain is " + on.getDomain() + " (Was expecting " +</em><a name="147" href="#147">147</a> <em class="comment">// "them to be the same).");</em><a name="148" href="#148">148</a> <em class="comment">// }</em><a name="149" href="#149">149</a> CompoundName key = getCompoundName(on);<a name="150" href="#150">150</a> context.rebind(key, getReference(on));<a name="151" href="#151">151</a> <strong>return</strong> key;<a name="152" href="#152">152</a> }<a name="153" href="#153">153</a> <a name="154" href="#154">154</a> <strong>public</strong> <strong>static</strong> CompoundName unbindObjectName(<strong>final</strong> Context context,<a name="155" href="#155">155</a> <strong>final</strong> ObjectName on)<a name="156" href="#156">156</a> throws NullPointerException, NamingException {<a name="157" href="#157">157</a> CompoundName key = getCompoundName(on);<a name="158" href="#158">158</a> context.unbind(key);<a name="159" href="#159">159</a> <strong>return</strong> key;<a name="160" href="#160">160</a> }<a name="161" href="#161">161</a> <a name="162" href="#162">162</a> <a name="163" href="#163">163</a> <em>/**<em>*</em></em><a name="164" href="#164">164</a> <em> * Testing code.</em><a name="165" href="#165">165</a> <em> * @param args Command line arguments.</em><a name="166" href="#166">166</a> <em> * @throws NullPointerException </em><a name="167" href="#167">167</a> <em> * @throws MalformedObjectNameException </em><a name="168" href="#168">168</a> <em> * @throws NamingException </em><a name="169" href="#169">169</a> <em> * @throws InvalidNameException </em><a name="170" href="#170">170</a> <em> */</em><a name="171" href="#171">171</a> <strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args)<a name="172" href="#172">172</a> throws MalformedObjectNameException, NullPointerException,<a name="173" href="#173">173</a> InvalidNameException, NamingException {<a name="174" href="#174">174</a> <strong>final</strong> ObjectName on = <strong>new</strong> ObjectName(<span class="string">"org.archive.crawler:"</span> +<a name="175" href="#175">175</a> <span class="string">"type=Service,name=Heritrix00,host=debord.archive.org"</span>);<a name="176" href="#176">176</a> Context c = getSubContext(getCompoundName(on.getDomain()));<a name="177" href="#177">177</a> CompoundName key = bindObjectName(c, on);<a name="178" href="#178">178</a> Reference r = (Reference)c.lookup(key);<a name="179" href="#179">179</a> <strong>for</strong> (Enumeration e = r.getAll(); e.hasMoreElements();) {<a name="180" href="#180">180</a> System.out.println(e.nextElement());<a name="181" href="#181">181</a> }<a name="182" href="#182">182</a> unbindObjectName(c, on);<a name="183" href="#183">183</a> }<a name="184" href="#184">184</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -