0

目前正在尝试通过 SAMS 的第 4 章 - 在 14 天内自学 CORBA。我使用 idlj 作为 IDL-To-JAVA 编译器,我得到了一个与使用 idl2Java“JDK 预版本”不同的类列表,我尝试进行一些编辑,但出现了很多错误。

这是我输入和修改的代码,而不是用 idlj “编译”(生成):

// StockMarket.idl

 // The StockMarket module consists of definitions useful
 // for building stock market-related applications.
 module StockMarket {

     // The StockSymbol type is used for symbols (names)
     // representing stocks.
     typedef string StockSymbol;

     // A StockSymbolList is simply a sequence of
     // StockSymbols.
     typedef sequence<StockSymbol> StockSymbolList;

     // The StockServer interface is the interface for a
     // server which provides stock market information.
     // (See the comments on the individual methods for
     // more information.)
     interface StockServer {

         // getStockValue() returns the current value for
         // the given StockSymbol. If the given StockSymbol
         // is unknown, the results are undefined (this
         // would be a good place to raise an exception).
         float getStockValue(in StockSymbol symbol);

         // getStockSymbols() returns a sequence of all
         // StockSymbols known by this StockServer.
         StockSymbolList getStockSymbols();
     };
 }; 

服务器实现

// StockServerImpl.java

  package StockMarket;

  import java.util.Vector;
import org.omg.CORBA.Context;
import org.omg.CORBA.ContextList;
import org.omg.CORBA.DomainManager;
import org.omg.CORBA.ExceptionList;
import org.omg.CORBA.NVList;
import org.omg.CORBA.NamedValue;

 import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CORBA.Policy;
import org.omg.CORBA.Request;
import org.omg.CORBA.SetOverrideType;
 import org.omg.CosNaming.NameComponent;
 import org.omg.CosNaming.NamingContext;
 import org.omg.CosNaming.NamingContextHelper;

 // StockServerImpl implements the StockServer IDL interface.
 public class StockServerImpl extends StockServerPOA 
{

     // Stock symbols and their respective values.
     private Vector myStockSymbols;
     private Vector myStockValues;

     // Characters from which StockSymbol names are built.
     private static char ourCharacters[] = { 'A', 'B', 'C', 'D', 'E', 'F','G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R','S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

     // Path name for StockServer objects.
     private static String ourPathName = "StockServer";

     // Create a new StockServerImpl.
     public StockServerImpl() {

         myStockSymbols = new Vector();
         myStockValues = new Vector();

         // Initialize the symbols and values with some random values.
         for (int i = 0; i < 10; i++) {

             // Generate a string of four random characters.
             StringBuffer stockSymbol = new StringBuffer("    ");
             for (int j = 0; j < 4; j++) {

                 stockSymbol.setCharAt(j, ourCharacters[(int)(Math.random()
                         * 26f)]);
             }

             myStockSymbols.addElement(stockSymbol.toString());

             // Give the stock a value between 0 and 100. In this example,
             // the stock will retain this value for the duration of the
             // application.
             myStockValues.addElement(new Float(Math.random() * 100f));
         }

         // Print out the stock symbols generated above.
         System.out.println("Generated stock symbols:");
         for (int i = 0; i < 10; i++) {
             System.out.println("  " + myStockSymbols.elementAt(i) + " " +
                     myStockValues.elementAt(i));
         }
         System.out.println();
     }

     // Return the current value for the given StockSymbol.
     public float getStockValue(String symbol) {

         // Try to find the given symbol.
         int stockIndex = myStockSymbols.indexOf(symbol);
         if (stockIndex != -1) {

             // Symbol found; return its value.
             return ((Float)myStockValues.elementAt(stockIndex)).
                     floatValue();
         } else {

             // Symbol was not found.
             return 0f;
         }
     }

     // Return a sequence of all StockSymbols known by this StockServer.
     public String[] getStockSymbols() {

         String[] symbols = new String[myStockSymbols.size()];
         myStockSymbols.copyInto(symbols);

         return symbols;
     }

     // Create and initialize a StockServer object.
     public static void main(String args[]) {

         try {

             // Initialize the ORB.
             ORB orb = ORB.init(args, null);

             // Create a StockServerImpl object and register it with the
             // ORB.
            StockServerImpl stockServer = new StockServerImpl();
             orb.connect((Object)stockServer);

             // Get the root naming context.
// Get the root naming context.
org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
NamingContext namingContext = NamingContextHelper.narrow(obj);

             // Bind the StockServer object reference in the naming
             // context.
             NameComponent nameComponent = new NameComponent(ourPathName,
                     "");
             NameComponent path[] = { nameComponent };
             namingContext.rebind(path, (Object)stockServer);

            // Wait for invocations from clients.
             java.lang.Object waitOnMe = new java.lang.Object();
             synchronized (waitOnMe) {
                 waitOnMe.wait();
             }
         } catch (Exception ex) {
             System.err.println("Couldn't bind StockServer: " + ex.
                    getMessage());
         }
     }

    @Override
    public boolean _is_a(String repositoryIdentifier) {
        throw new UnsupportedOperationException("Not supported yet.");
    }



    @Override
    public boolean _non_existent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public Object _get_interface_def() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


 } 

-- 客户端实现

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StockMarket;

/**
 *
 * @author ygalila
 */
 import org.omg.CORBA.ORB;
 import org.omg.CosNaming.NameComponent;
 import org.omg.CosNaming.NamingContext;
 import org.omg.CosNaming.NamingContextHelper;

 // StockMarketClient is a simple client of a StockServer.
 public class StockMarketClient {

     // Create a new StockMarketClient.
     StockMarketClient() {

     }

     // Run the StockMarketClient.
     public void run() {

         connect();

         if (myStockServer != null) {
             doSomething();
         }
     }

  // Connect to the StockServer.
    protected void connect() {
        try {

             // Get the root naming context.
            org.omg.CORBA.Object obj = ourORB.
                     resolve_initial_references("NameService");
             NamingContext namingContext = NamingContextHelper.narrow(obj);

             // Attempt to locate a StockServer object in the naming
            // context.
             NameComponent nameComponent = new NameComponent("StockServer","");
             NameComponent path[] = { nameComponent };
             myStockServer = StockServerHelper.narrow(namingContext.
                     resolve(path));
         } catch (Exception ex) {
             System.err.println("Couldn't resolve StockServer: " + ex);
             myStockServer = null;
             return;
        }

         System.out.println("Succesfully bound to a StockServer.");
     }

     // Do some cool things with the StockServer.
     protected void doSomething() {

         try {

             // Get the valid stock symbols from the StockServer.
             String[] stockSymbols = myStockServer.getStockSymbols();

             // Display the stock symbols and their values.
             for (int i = 0; i < stockSymbols.length; i++) {
                 System.out.println(stockSymbols[i] + " " +
                         myStockServer.getStockValue(stockSymbols[i]));
             }
         } catch (org.omg.CORBA.SystemException ex) {
             System.err.println("Fatal error: " + ex);
         }
     }

     // Start up a StockMarketClient.
     public static void main(String args[]) {

         // Initialize the ORB.
         ourORB = ORB.init(args, null);

         StockMarketClient stockClient = new StockMarketClient();

         stockClient.run();

         // This simply waits forever so that the DOS window doesn't
         // disappear (for developers using Windows IDEs).
         while (true)
             ;
     }

     // My ORB.
     public static ORB ourORB;

     // My StockServer.
     private StockServer myStockServer;
 } 

在netbeans中运行后,我得到以下信息:

@ server

run:
Generated stock symbols:
  HDWB 79.99867
  XITG 7.3477798
  BLPP 48.922062
  PDRK 89.32173
  IQAQ 30.236895
  ZSSE 2.7638085
  XDAQ 39.625706
  CEJD 93.04881
  GCYC 64.20899
  LMWN 6.168055

Couldn't bind StockServer: StockMarket.StockServerImpl cannot be cast to org.omg.CORBA.Object
BUILD SUCCESSFUL (total time: 27 seconds)

@ Client

run:
نوف 13, 2012 11:56:33 ص com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl <init>
WARNING: "IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 10.100.221.94; port: 900"
org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 201  completed: No
    at com.sun.corba.se.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2200)
    at com.sun.corba.se.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2221)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:223)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:236)
    at com.sun.corba.se.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:119)
    at com.sun.corba.se.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:185)
    at com.sun.corba.se.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:136)
    at com.sun.corba.se.impl.resolver.BootstrapResolverImpl.invoke(BootstrapResolverImpl.java:99)
    at com.sun.corba.se.impl.resolver.BootstrapResolverImpl.resolve(BootstrapResolverImpl.java:132)
    at com.sun.corba.se.impl.resolver.CompositeResolverImpl.resolve(CompositeResolverImpl.java:47)
    at com.sun.corba.se.impl.resolver.CompositeResolverImpl.resolve(CompositeResolverImpl.java:47)
    at com.sun.corba.se.impl.resolver.CompositeResolverImpl.resolve(CompositeResolverImpl.java:47)
    at com.sun.corba.se.impl.orb.ORBImpl.resolve_initial_references(ORBImpl.java:1170)
    at StockMarket.StockMarketClient.connect(StockMarketClient.java:39)
    at StockMarket.StockMarketClient.run(StockMarketClient.java:27)
    at StockMarket.StockMarketClient.main(StockMarketClient.java:84)
Caused by: java.net.ConnectException: Connection refused: connect
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:364)
    at sun.nio.ch.Net.connect(Net.java:356)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:623)
    at java.nio.channels.SocketChannel.open(SocketChannel.java:184)
    at com.sun.corba.se.impl.transport.DefaultSocketFactoryImpl.createSocket(DefaultSocketFactoryImpl.java:78)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:206)
    ... 13 more

Couldn't resolve StockServer: org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 201  completed: No

那么任何人都可以帮我解决这些问题吗?如果我从命令行运行这些,我在尝试编译 serverimpl 时会得到以下信息:-

G:\CORBA\StockMarket>javac StockServerImpl.java
StockServerImpl.java:21: error: cannot find symbol
    extends StockServerPOA
            ^
  symbol: class StockServerPOA
StockServerImpl.java:23: error: cannot find symbol
    implements StockServer
               ^
  symbol: class StockServer
StockServerImpl.java:85: error: cannot find symbol
    public float getStockValue(String symbol) throws InvalidStockSymbolException

                                                     ^
  symbol:   class InvalidStockSymbolException
  location: class StockServerImpl
StockServerImpl.java:98: error: cannot find symbol
            throw new InvalidStockSymbolException();
                      ^
  symbol:   class InvalidStockSymbolException
  location: class StockServerImpl
StockServerImpl.java:84: error: method does not override or implement a method f
rom a supertype
    @Override
    ^
StockServerImpl.java:103: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:133: error: cannot find symbol
        StockServer href = null;
        ^
  symbol:   class StockServer
  location: class StockServerImpl
StockServerImpl.java:136: error: cannot find symbol
        ORB orb = StockServerORBHelper.getORB(args);
                  ^
  symbol:   variable StockServerORBHelper
  location: class StockServerImpl
StockServerImpl.java:137: error: cannot find symbol
        POA rootpoa = StockServerPOAHelper.getRoot(orb);
                      ^
  symbol:   variable StockServerPOAHelper
  location: class StockServerImpl
StockServerImpl.java:152: error: method servant_to_reference in interface POAOpe
rations cannot be applied to given types;
        try { ref = rootpoa.servant_to_reference(stockServerImpl); }
                           ^
  required: Servant
  found: StockServerImpl
  reason: actual argument StockServerImpl cannot be converted to Servant by meth
od invocation conversion
StockServerImpl.java:156: error: cannot find symbol
        try { href = StockServerHelper.narrow(ref); }
                     ^
  symbol:   variable StockServerHelper
  location: class StockServerImpl
StockServerImpl.java:199: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:206: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:213: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:219: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:225: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:231: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:237: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:243: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:249: error: method does not override or implement a method
from a supertype
    @Override
    ^
StockServerImpl.java:255: error: method does not override or implement a method
from a supertype
    @Override
    ^
21 errors

G:\CORBA\StockMarket>
4

1 回答 1

1

看看这个问题,它处理类似的异常。

看起来客户端 ORB 没有连接到 NamingService 的正确连接详细信息,默认情况下它假定 NamingService 正在侦听端口 900。Sun/Oracle ORB 具有许多预定义的 -ORB 属性,可以当服务位于非默认位置时使用。

在此处-ORBInitRef查看 Oracle ORB的属性和详细信息。

至于您在编译服务器代码时遇到的各种编译错误。看起来服务器端骨架代码(从 idl 编译器生成的生成的服务器端代码)要么没有被编译,要么在编译类路径中丢失,所以 java 编译器找不到它。

于 2012-11-13T15:36:09.463 回答