【问题标题】:Accessing variables and arguments in IDL + Java在 IDL + Java 中访问变量和参数
【发布时间】:2012-10-24 13:20:37
【问题描述】:

我现在正在研究分布式系统,所以...我必须学习 IDL 的东西。 :D 我有这个要求:

在 IDL 中定义 - 作为结构的帐户 - 帐户作为帐户的序列 - 具有一些帐户操作的客户界面: payIn (amount, accountId), - 如果成功则返回 true,否则返回 false getAccounts (name), - 返回一个账户序列(属于这个人) - 具有帐户操作的界面管理员: 创建一个新帐户 删除帐户

这是我的 .idl 文件:

module AccountNaming
{
    struct Account
    {
        long id;
        double balance;
        string name;    
    };

    typedef sequence<Account> accounts;

    interface Customer
    {
        boolean payIn(in double amount, in long accountId);
        accounts getAccounts(in string name);
        string helloCust();
    };
    interface Administrator
    {
        void createAcc();
        void deleteAcc();
        string helloAdmin();
    };
};

我已经为 IDL 生成了所有的 POA、Helper、Holder 类。

在我的 Java 代码中,我有一个使用 IDL 代码的类:

import AccountNaming.*;
public class CustomerImpl extends CustomerPOA
{
   public String helloCust()
   {
      return "Hello, dear Customer! :) ";
   }

   @Override
   public boolean payIn(double amount, int accountId)
   {
      // how to get to the Customer's local variables ?
      super.Customer.setAmount... // or something like that, because this doesn't work... etc.
      return false;
   }

   @Override
   public Account[] getAccounts(String name)
   {
      // TODO Auto-generated method stub
      return null;
   }
}    

我知道 .idl 文件是正确的。 “helloCust/admin”方法有效。我的问题是如何访问客户/管理员的变量,以便我可以将它们设置为 payIn 、 getAccounts 方法中的参数...

【问题讨论】:

  • Customer 是一个没有任何变量的接口,对吧?
  • 我认为您的“工作流程”还不清楚。谁创造了客户?识别客户的关键是什么...管理员应如何管理客户?

标签: java variables arguments corba idl


【解决方案1】:

您可以添加外观或工厂以允许您访问您的Customer/Administrator 接口,因此在您的 IDL 中您还可以:

interface UserFactory 
{
   Customer getCustomer(String customerName);
   Administrator getAdministrator(String credentials); 
}

这个接口的实现会让你从数据库等中查找任何细节。

使用它,您可能不需要getAccounts() 中的name 字段(除非它用于过滤),您可以这样做:

Account[] accounts = getAccounts("not-needed-anymore");
for (Account account : accounts) {
   if (account.id == accountId) {
      account.balance += amount;
      break;
   }
}

这将更新您的 Account 数组信息,但您仍需要保留数据结构。

【讨论】:

    【解决方案2】:

    感谢 Reimeus 的快速回答,但我想到了一个更简单的解决方案:

    public boolean payIn(double amount, int accountNumber)
        {
            boolean success = false;
            for(int i = 0; i < accountList.length; i++)
            {
                if(accountList[i].accountNumber == accountNumber)
                {
                    accountList[i].balance += amount;
                    success = true;
                }
            }
            return success;
        }
    
        @Override
        public account[] getAccounts(String name)
        {
            account[] accounts;
            int numberOfAccounts = 0;
            int count = 0;
            for(int i = 0; i < accountList.length; i++)
            {
               if(accountList[i] != null)
                if(accountList[i].name.equalsIgnoreCase(name))
                {
                    numberOfAccounts++;
                }
            }
            if(numberOfAccounts != 0)
            {
                accounts = new account[numberOfAccounts];
                for(int i = 0; i < accountList.length; i++)
                {
                    if(accountList[i].name.equalsIgnoreCase(name))
                    {
                        accounts[count] = accountList[i];
                        count++;
                    }
                }
                return accounts;
            }
            else
            {
                return null;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多