G
Guest
Gast
Hallo,
ich habe ein rmi client - server programm und bekomme beim ausführen immer folgende exception:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
Usage: java RemoteBankServer
Java Result: 1
Das passiert wenn ich den Server starten möchte!?
ich habe ein rmi client - server programm und bekomme beim ausführen immer folgende exception:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
Usage: java RemoteBankServer
Java Result: 1
Das passiert wenn ich den Server starten möchte!?
Code:
import java.util.*;
import java.rmi.*;
import common.BankKontoException;
import common.Geld;
import common.BankKonto;
public class Client
{
public static void main(String[] args){
if (System.getSecurityManager() == null) {
System.setProperty("java.security.policy", "security.policy");
System.setSecurityManager (new RMISecurityManager());
}
try
{
BankKonto bank = (BankKonto) Naming.lookup("rmi:///RemoteBank");
String cmd = args[0].toLowerCase();
if (cmd.equals("open"))
{
bank.openAccount(args[1], args[2]);
System.out.println("Konto eröffnet.");
}
else if (cmd.equals("close"))
{
Geld money = bank.closeAccount(args[1], args[2]);
System.out.println(money.amount + " Euro wurden ihnen ausgezahlt.");
System.out.println("Vielen Dank beehren Sie uns bald wieder.");
}
else if (cmd.equals("einzahlen"))
{
Geld money = new Geld(Integer.parseInt(args[3]));
bank.einzahlen(args[1], args[2], money);
System.out.println("Sie haben " + money.amount + " Euro eingezahlt.");
}
else if (cmd.equals("abheben"))
{
Geld money = bank.abheben(args[1], args[2], Integer.parseInt(args[3]));
System.out.println("Sie haben " + money.amount + " Euro abgehoben.");
}
else if (cmd.equals("kontostand"))
{
int amt = bank.kontostand(args[1], args[2]);
System.out.println("Sie haben " + amt + " Euro auf Ihrem Konto.");
}
else if (cmd.equals("history"))
{
Vector transactions = bank.getTransactionHistory(args[1], args[2]);
for(int i = 0; i < transactions.size(); i++)
System.out.println(transactions.elementAt(i));
}
else System.out.println("Unbekannter Eingabeparameter");
}
catch (RemoteException e)
{
System.err.println(e);
}
catch (BankKontoException e)
{
System.err.println(e.getMessage());
}
catch (Exception e)
{
System.err.println(e);
System.err.println("Usage: java Client cmd account password [options]");
System.err.println("bei Parameter: open, close, einzahlen ... [Euro], abheben ... [Euro], Kontostand, history");
}
}
}
Code:
package server;
import java.rmi.*;
public class Server{
public static void main(String[] args)
{
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
try
{
BankKontoImpl bank = new BankKontoImpl();
Naming.rebind("RemoteBank", bank);
System.out.println("Bankserver läuft.....");
} catch (Exception e)
{
System.err.println(e);
System.err.println("Usage: java RemoteBankServer");
System.exit(1);
}
}
}
Code:
package server;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import common.BankKontoException;
import common.Geld;
import common.BankKonto;
public class BankKontoImpl extends UnicastRemoteObject implements BankKonto{
class Account
{
String passwort;
int balance;
Vector transactions = new Vector();
Account(String passwort)
{
this.passwort = passwort;
transactions.addElement("Konto eröffnet am " + new Date());
}
}
Hashtable accounts = new Hashtable();
public BankKontoImpl()
throws RemoteException
{
super();
}
public synchronized void openAccount(String name, String passwort)
throws RemoteException, BankKontoException
{
if (accounts.get(name) != null) throw new BankKontoException("Konto existiert bereits");
Account acct = new Account(passwort);
accounts.put(name, acct);
}
public Account verify(String name, String passwort)
throws BankKontoException
{
synchronized(accounts)
{
Account acct = (Account)accounts.get(name);
if (acct == null) throw new BankKontoException("Konto nicht vorhanden");
if (!passwort.equals(acct.passwort)) throw new BankKontoException("ungültiges Passwort");
return acct;
}
}
public synchronized Geld closeAccount(String name, String passwort)
throws RemoteException, BankKontoException
{
Account acct;
acct = verify(name, passwort);
accounts.remove(name);
synchronized (acct)
{
int balance = acct.balance;
acct.balance = 0;
return new Geld(balance);
}
}
public void einzahlen(String name, String passwort, Geld money)
throws RemoteException, BankKontoException
{
Account acct = verify(name, passwort);
synchronized(acct) {
acct.balance += money.amount;
acct.transactions.addElement("eingezahlt " + money.amount + " am " + new Date());
}
}
public Geld abheben(String name, String passwort, int amount)
throws RemoteException, BankKontoException
{
Account acct = verify(name, passwort);
synchronized(acct)
{
if (acct.balance < amount) throw new BankKontoException("Keine genügende Kontodeckung");
acct.balance -= amount;
acct.transactions.addElement("abgehoben " + amount + " am " + new Date());
return new Geld(amount);
}
}
public int kontostand(String name, String passwort)
throws RemoteException, BankKontoException
{
Account acct = verify(name, passwort);
synchronized(acct)
{
return acct.balance;
}
}
public Vector getTransactionHistory(String name, String passwort)
throws RemoteException, BankKontoException
{
Account acct = verify(name, passwort);
synchronized(acct)
{
return acct.transactions;
}
}
}
Code:
package common;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.*;
public interface BankKonto extends Remote {
public void openAccount(String name, String passwort)
throws RemoteException, BankKontoException;
public Geld closeAccount(String name, String passwort)
throws RemoteException, BankKontoException;
public void einzahlen(String name, String passwort, Geld money)
throws RemoteException, BankKontoException;
public Geld abheben(String name, String passwort, int amount)
throws RemoteException, BankKontoException;
public int kontostand(String name, String passwort)
throws RemoteException, BankKontoException;
public Vector getTransactionHistory(String name, String passwort)
throws RemoteException, BankKontoException;
}
Code:
package common;
public class Geld implements java.io.Serializable
{
public int amount;
public Geld(int amount)
{
this.amount = amount;
}
}
Code:
package common;
public class BankKontoException extends Exception
{
public BankKontoException(String msg)
{
super(msg);
}
}