📄 cb3.html
字号:
ResourceBundle.getBundle("com.sun.cb.CoffeeRegistry");// Create organization name and description Organization org = blcm.createOrganization(bundle.getString("org.name"));InternationalString s = blcm.createInternationalString (bundle.getString("org.description"));org.setDescription(s);// Create primary contact, set name User primaryContact = blcm.createUser();PersonName pName = blcm.createPersonName(bundle.getString("person.name"));primaryContact.setPersonName(pName);<a name="wp67402"> </a></pre></div><a name="wp67403"> </a><p class="pBody">It adds a telephone number and email address for the user, then makes the user the primary contact:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">org.setPrimaryContact(primaryContact);<a name="wp67404"> </a></pre></div><a name="wp67405"> </a><p class="pBody">It gives JAXRPCCoffeeDistributor a classification using the North American Industry Classification System (NAICS). In this case it uses the classification "Other Grocery and Related Products Wholesalers".</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Classification classification = (Classification) blcm.createClassification(cScheme, bundle.getString("classification.name"), bundle.getString("classification.value"));Collection classifications = new ArrayList();classifications.add(classification);org.addClassifications(classifications);<a name="wp67406"> </a></pre></div><a name="wp67407"> </a><p class="pBody">Next, it adds the JAX-RPC service, called "JAXRPCCoffee Service," and its service binding. The access URI for the service binding contains the endpoint URL that remote clients will use to contact our service:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">http://localhost:8080/jaxrpc-coffee-supplier/jaxrpc<a name="wp67408"> </a>Collection services = new ArrayList();Service service = blcm.createService(bundle.getString("service.name"));InternationalString is = blcm.createInternationalString (bundle.getString("service.description"));service.setDescription(is);// Create service bindingsCollection serviceBindings = new ArrayList();ServiceBinding binding = blcm.createServiceBinding();is = blcm.createInternationalString (bundle.getString("service.binding"));binding.setDescription(is);binding.setValidateURI(false);binding.setAccessURI(endpoint);serviceBindings.add(binding);// Add service bindings to serviceservice.addServiceBindings(serviceBindings);// Add service to services, then add services to organizationservices.add(service);org.addServices(services);<a name="wp90109"> </a></pre></div><a name="wp67422"> </a><p class="pBody">Then it saves the organization to the registry:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Collection orgs = new ArrayList();orgs.add(org);BulkResponse response = blcm.saveOrganizations(orgs);<a name="wp67423"> </a></pre></div><a name="wp67382"> </a><p class="pBody">The <code class="cCode">BulkResponse</code> object returned by <code class="cCode">saveOrganizations</code> includes the <code class="cCode">Key</code> object containing the unique key value for the organization. The <code class="cCode">executePublish</code> method first checks to make sure the <code class="cCode">saveOrganizations</code> call succeeded. </p><a name="wp67429"> </a><p class="pBody">If the call succeeded, the method extracts the value from the <code class="cCode">Key</code> object and displays it:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Collection keys = response.getCollection();Iterator keyIter = keys.iterator();if (keyIter.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) keyIter.next(); id = orgKey.getId(); System.out.println("Organization key is " + id);}<a name="wp67430"> </a></pre></div><a name="wp67431"> </a><p class="pBody">Finally, the method returns the string <code class="cCode">id</code> so that the <code class="cCode">OrgPublisher</code> program can save it in a file for use by the <code class="cCode">OrgRemover</code> program.</p><a name="wp65013"> </a><h3 class="pHeading2">Deleting the Service From the Registry</h3><a name="wp65014"> </a><p class="pBody">The <code class="cCode"><a href="../examples/cb/jaxrpc/src/registry/com/sun/cb/OrgRemover.java" target="_blank">contextDestroyed</a></code> <code class="cCode">method</code> deletes the service from the Registry Server. Like the <code class="cCode">contextInitialized</code> <code class="cCode">method</code>, the <code class="cCode">contextDestroyed</code> <code class="cCode">method</code> starts by fetching URLs from <code class="cCode">URLHelper</code> and other values from the <code class="cCode">CoffeeRegistry.properties</code> file. One these values, <code class="cCode">keyFile</code>, is the name of the file that contains the key that uniquely identifies the service. <code class="cCode">The</code> <code class="cCode">contextDestroyed</code> <code class="cCode">method</code> reads the key from the file, connects to the Registry Server by invoking <code class="cCode">makeConnection</code>, and then deletes the service from the registry by calling <code class="cCode">executeRemove</code>. Here is the source code for the <code class="cCode">contextDestroyed</code> <code class="cCode">method</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void contextDestroyed(ServletContextEvent event) { String keyStr = null; String queryURL = URLHelper.getQueryURL(); String publishURL = URLHelper.getPublishURL(); ResourceBundle registryBundle = ResourceBundle.getBundle( "com.sun.cb.CoffeeRegistry"); String username = registryBundle.getString("registry.username"); String password = registryBundle.getString("registry.password"); String keyFile = registryBundle.getString("key.file"); try { FileReader in = new FileReader(keyFile); char[] buf = new char[512]; while (in.read(buf, 0, 512) >= 0) { } in.close(); keyStr = new String(buf).trim(); } catch (IOException ex) { System.out.println(ex.getMessage()); } JAXRRemover remover = new JAXRRemover(); remover.makeConnection(queryURL, publishURL); javax.xml.registry.infomodel.Key modelKey = null; modelKey = remover.createOrgKey(keyStr); remover.executeRemove(modelKey, username, password);}<a name="wp90723"> </a></pre></div><a name="wp90803"> </a><p class="pBody">Instantiated by the <code class="cCode">contextDestroyed</code> <code class="cCode">method</code>, the <code class="cCode"><a href="../examples/cb/jaxrpc/src/registry/com/sun/cb/JAXRRemover.java" target="_blank">JAXRRemover</a></code> class contains the <code class="cCode">makeConnection</code>, <code class="cCode">createOrgKey</code>, and <code class="cCode">executeRemove</code> methods. It is almost identical to the sample program <code class="cCode"><a href="../examples/jaxr/simple/src/JAXRDelete.java" target="_blank">JAXRDelete.java</a></code>, which is described in <a href="JAXR3.html#wp64166">Removing Data from the Registry</a>.</p><a name="wp90804"> </a><p class="pBody">The <code class="cCode">makeConnection</code> method is identical to the <code class="cCode">JAXRPublisher</code> method of the same name.</p><a name="wp90805"> </a><p class="pBody">The <code class="cCode">createOrgKey</code> method is a utility method that takes one argument, the string value extracted from the key file. It obtains the <code class="cCode">RegistryService</code> object and the <code class="cCode">BusinessLifeCycleManager</code> object, then creates a <code class="cCode">Key</code> object from the string value.</p><a name="wp90806"> </a><p class="pBody">The <code class="cCode">executeRemove</code> method takes three arguments: a username, a password, and the <code class="cCode">Key</code> object returned by the <code class="cCode">createOrgKey</code> method. It uses the username and password arguments to establish its security credentials with the Registry Server, just as the <code class="cCode">executePublish</code> method does.</p><a name="wp90807"> </a><p class="pBody">The method then wraps the <code class="cCode">Key</code> object in a <code class="cCode">Collection</code> and uses the <code class="cCode">BusinessLifeCycleManager</code> object's <code class="cCode">deleteOrganizations</code> method to delete the organization. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Collection keys = new ArrayList();keys.add(key);BulkResponse response = blcm.deleteOrganizations(keys);<a name="wp90808"> </a></pre></div><a name="wp67455"> </a><p class="pBody">The <code class="cCode">deleteOrganizations</code> method returns the keys of the organizations it deleted, so the <code class="cCode">executeRemove</code> method then verifies that the correct operation was performed and displays the key for the deleted organization.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Collection retKeys = response.getCollection();Iterator keyIter = retKeys.iterator();javax.xml.registry.infomodel.Key orgKey = null; if (keyIter.hasNext()) { orgKey = (javax.xml.registry.infomodel.Key) keyIter.next(); id = orgKey.getId(); System.out.println("Organization key was " + id);}<a name="wp67456"> </a></pre></div> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="CB2.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="CB4.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -