PriceServerImpl.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.rmi.example2;
- import java.util.*;
- import javax.naming.*;
- import weblogic.rmi.*;
- import java.rmi.RemoteException;
- /**
- * PriceServerImpl implements the PriceServer remote interface, providing
- * prices on securities for remote clients.
- */
- public class PriceServerImpl implements PriceServer {
- // Stock price database, implemented as a Hashtable.
- /** stockTable */
- private Hashtable stockTable = null;
- /**
- * Creates the stock price 'database'.
- */
- public PriceServerImpl() {
- stockTable = new Hashtable();
- Float f = new Float(0);
- stockTable.put("SUNW", f.valueOf("20"));
- stockTable.put("INTC", f.valueOf("30"));
- stockTable.put("BEAS", f.valueOf("40"));
- }
- /**
- * Returns the price of the given security.
- * @param symbol
- */
- public float getPrice(String symbol) throws java.rmi.RemoteException{
- Float f = (Float) stockTable.get(symbol);
- System.out.println("PriceServer: Call to getPrice for symbol : "
- + symbol);
- return f.floatValue();
- }
- /**
- * Sets the price for the given security.
- * @param symbol
- * @param price
- */
- public void setPrice(String symbol, float price) throws java.rmi.RemoteException{
- Float f = new Float(price);
- stockTable.put(symbol, f);
- System.out.println("PriceServer: Call to setPrice for " + symbol + " to "
- + price);
- }
- /**
- * Returns all ticker symbols in this server's database.
- */
- public String[] getSecurities() throws java.rmi.RemoteException{
- if (stockTable.size() > 0) {
- String[] securities = new String [stockTable.size()];
- Enumeration enum = stockTable.keys();
- int i = 0;
- while (enum.hasMoreElements()) {
- securities [i] = (String) enum.nextElement();
- i++;
- }
- return securities;
- }
- return null;
- }
- /**
- * Instanciates and binds a PriceServer.
- * @param args
- */
- public static void main(String[] args) {
- try {
- PriceServerImpl server = new PriceServerImpl();
- Context ctx = new InitialContext();
- ctx.bind(PRICESERVERNAME, server);
- System.out.println("PriceServer was started and bound in the registry "
- + "to the name " + PRICESERVERNAME);
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- }