📄 cb3.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>JAX-RPC Distributor Service</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="CB2.html" /> <link rel="Next" href="CB4.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <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"> <blockquote><a name="wp85780"> </a><h2 class="pHeading1">JAX-RPC Distributor Service</h2><a name="wp70641"> </a><p class="pBody">The Coffee Break server is a client of the JAX-RPC distributor service. The service code consists of the service interface, service implementation class, and several JavaBeans components that are used for method parameters and return types.</p><a name="wp64997"> </a><h3 class="pHeading2">Service Interface</h3><a name="wp65917"> </a><p class="pBody">The service interface, <code class="cCode"><a href="../examples/cb/jaxrpc/src/server/com/sun/cb/SupplierIF.java" target="_blank">SupplierIF</a></code>, defines the methods that can be called by remote clients. The parameters and return types of these methods are the JavaBeans components listed in the previous section.</p><a name="wp85777"> </a><p class="pBody">The source code for the <code class="cCode">SupplierIF</code> interface, which follows, resides in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/cb/jaxrpc/src/</code> directory.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package com.sun.cb;import java.rmi.Remote;import java.rmi.RemoteException;public interface SupplierIF extends Remote { public ConfirmationBean placeOrder(OrderBean order) throws RemoteException; public PriceListBean getPriceList() throws RemoteException;}<a name="wp65078"> </a></pre></div><a name="wp65001"> </a><h3 class="pHeading2">Service Implementation</h3><a name="wp65002"> </a><p class="pBody">The <code class="cCode"><a href="../examples/cb/jaxrpc/src/server/com/sun/cb/SupplierImpl.java" target="_blank">SupplierImpl</a></code> class implements the <code class="cCode">placeOrder</code> and <code class="cCode">getPriceList</code> methods, which are defined by the <code class="cCode">SupplierIF</code> interface. So that you can focus on the code related to JAX-RPC, these methods are short and simplistic. In a real-world application, these methods would access databases and interact with other services, such as shipping, accounting, and inventory.</p><a name="wp65003"> </a><p class="pBody">The <code class="cCode">placeOrder</code> method accepts as input a coffee order and returns a confirmation for the order. To keep things simple, the <code class="cCode">placeOrder</code> method confirms every order and sets the ship date in the confirmation to the next day. The source code for the <code class="cCode">placeOrder</code> method follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ConfirmationBean placeOrder(OrderBean order) { Date tomorrow = DateHelper.addDays(new Date(), 1); ConfirmationBean confirmation = new ConfirmationBean(order.getId(), DateHelper.dateToCalendar(tomorrow)); return confirmation;}<a name="wp65101"> </a></pre></div><a name="wp65005"> </a><p class="pBody">The <code class="cCode">getPriceList</code> method returns a <code class="cCode">PriceListBean</code> object, which lists the name and price of each type of coffee that can be ordered from this service. The <code class="cCode">getPriceList</code> method creates the <code class="cCode">PriceListBean</code> object by invoking a private method named <code class="cCode">loadPrices</code>. In a production application, the <code class="cCode">loadPrices</code> method would fetch the prices from a database. However, our <code class="cCode">loadPrices</code> method takes a shortcut by getting the prices from the <code class="cCode">SupplierPrices.properties</code> file. Here are the <code class="cCode">getPriceList</code> and <code class="cCode">loadPrices</code> methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public PriceListBean getPriceList() { PriceListBean priceList = loadPrices(); return priceList;}private PriceListBean loadPrices() { String propsName = "com.sun.cb.SupplierPrices"; Date today = new Date(); Date endDate = DateHelper.addDays(today, 30); PriceItemBean[] priceItems = PriceLoader.loadItems(propsName); PriceListBean priceList = new PriceListBean(DateHelper.dateToCalendar(today), DateHelper.dateToCalendar(endDate), priceItems); return priceList;}<a name="wp65455"> </a></pre></div><a name="wp65449"> </a><h3 class="pHeading2">Publishing the Service in the Registry</h3><a name="wp106050"> </a><p class="pBody">Because we want customers to find our service, we publish it in a registry. When the JAX-RPC Web application is started and stopped, the context listener object <code class="cCode">ContextListener</code> publishes and removes the service in the <code class="cCode">contextInitialized</code> and <code class="cCode">contextDestroyed</code> methods respectively.</p><a name="wp90372"> </a><p class="pBody">The <code class="cCode">contextInitialized</code> <code class="cCode">method</code> begins by loading <code class="cCode">String</code> values from the <code class="cCode">URLHelper</code> class and <code class="cCode">CoffeeRegistry.properties</code> file. Next, the program instantiates a utility class named <code class="cCode">JAXRPublisher</code>. <code class="cCode">The</code> <code class="cCode">contextInitialized</code> <code class="cCode">method</code> connects to the registry by invoking the <code class="cCode">makeConnection</code> method of <code class="cCode">JAXRPublisher</code>. To publish the service, <code class="cCode">the</code> <code class="cCode">contextInitialized</code> <code class="cCode">method</code> invokes the <code class="cCode">executePublish</code> method, which accepts as input <code class="cCode">username</code>, <code class="cCode">password</code>, and <code class="cCode">endpoint</code>. The <code class="cCode">username</code> and <code class="cCode">password</code> values are required by the Registry Server. The <code class="cCode">endpoint</code> value is the URL that remote clients will use to contact our JAX-RPC service. The <code class="cCode">executePublish</code> method of <code class="cCode">JAXRPublisher</code> returns a key that uniquely identifies the service in the registry. <code class="cCode">The</code> <code class="cCode">contextInitialized</code> <code class="cCode">method </code>saves this key in a text file named <code class="cCode">orgkey.txt</code>. The <code class="cCode">contextDestroyed</code> <code class="cCode">method</code> reads the key from <code class="cCode">orgkey.txt</code> so that it can delete the service. See <a href="CB3.html#wp65013">Deleting the Service From the Registry</a>. The source code for the <code class="cCode">contextInitialized</code> <code class="cCode">method</code> follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void contextInitialized(ServletContextEvent event) { String queryURL = URLHelper.getQueryURL(); String publishURL = URLHelper.getPublishURL(); String endpoint = URLHelper.getEndpointURL(); 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"); JAXRPublisher publisher = new JAXRPublisher(); publisher.makeConnection(queryURL, publishURL); String key = publisher.executePublish(username, password, endpoint); try { FileWriter out = new FileWriter(keyFile); out.write(key); out.flush(); out.close(); } catch (IOException ex) { System.out.println(ex.getMessage()); } }<a name="wp106095"> </a></pre></div><a name="wp67372"> </a><p class="pBody">The <code class="cCode"><a href="../examples/cb/jaxrpc/src/registry/com/sun/cb/JAXRPublisher.java" target="_blank">JAXRPublisher</a></code> class is almost identical to the sample program <code class="cCode"><a href="../examples/jaxr/simple/src/JAXRPublish.java" target="_blank">JAXRPublish.java</a></code>, which is described in <a href="JAXR3.html#wp66325">Managing Registry Data</a>.</p><a name="wp67384"> </a><p class="pBody">First, the <code class="cCode">makeConnection</code> method creates a connection to the Registry Server. See <a href="JAXR3.html#wp64057">Establishing a Connection</a> for more information. To do this, it first specifies a set of connection properties using the query and publish URLs retrieved from <code class="cCode">URLHelper</code>. For the Registry Server, the query and publish URLs are actually the same.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Properties props = new Properties();props.setProperty("javax.xml.registry.queryManagerURL", queryUrl);props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishUrl);<a name="wp67388"> </a></pre></div><a name="wp67389"> </a><p class="pBody">Next, the <code class="cCode">makeConnection</code> method creates the connection using a connection factory it looks up using JNDI, using the connection properties:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCode">context = new InitialContext();factory = (ConnectionFactory) context.lookup("java:comp/env/eis/JAXR");factory.setProperties(props);connection = factory.createConnection();</code><a name="wp90619"> </a></pre></div><a name="wp67391"> </a><p class="pBody">The <code class="cCode">executePublish</code> method takes three arguments: a username, a password, and an endpoint. It begins by obtaining a <code class="cCode">RegistryService</code> object, then a <code class="cCode">BusinessQueryManager</code> object and a <code class="cCode">BusinessLifeCycleManager</code> object, which enable it to perform queries and manage data:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">rs = connection.getRegistryService();blcm = rs.getBusinessLifeCycleManager();bqm = rs.getBusinessQueryManager();<a name="wp67392"> </a></pre></div><a name="wp67393"> </a><p class="pBody">Because it needs password authentication in order to publish data, it then uses the username and password arguments to establish its security credentials:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray());Set creds = new HashSet();creds.add(passwdAuth);connection.setCredentials(creds);<a name="wp67394"> </a></pre></div><a name="wp74494"> </a><p class="pBody">It then creates an <code class="cCode">Organization</code> object with the name "JAXRPCCoffeeDistributor," then a <code class="cCode">User</code> object that will serve as the primary contact. This code is almost identical to the code in the JAXR examples.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ResourceBundle bundle =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -