java - 在 IDL + Java 中访问变量和参数

标签 java variables arguments corba idl

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

在 IDL 中定义 - 一个账户作为一个结构体 - 账户作为账户的序列 - 具有一些帐户操作的接口(interface)客户: 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 方法中的参数...

最佳答案

您可以添加外观或工厂来允许您访问客户/管理员界面,因此在您的IDL中您还可以:

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

该接口(interface)的实现可以让您从数据库等中查找任何详细信息。

使用此功能,您可能不需要 getAccounts() 中的 name 字段(除非用于过滤),您可以执行以下操作:

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

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

关于java - 在 IDL + Java 中访问变量和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219692/

相关文章:

java - CSS 未加载 || Spring

java - 尝试将 XML 值转换为映射

java - GWT 按钮中的图像

java - 使用批处理作业的 FlatFileItemReader 和 FlatFileItemWriter

Javascript 变量问题

c - 在 C 中声明一个包含另一个变量内容的变量名

linux - Bash 脚本添加参数中收到的数字

python - 将函数参数打包到字典中——与 **kwargs 相反

c - 递归函数探索矩阵的最小参数数

java - 我可以在 IntelliJ 中使用 Java 更新和设置多个变量吗?不,我不能