1 /*
2  * @(#) $Id: JSTKShellRMIServerImpl.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.jstksh;
11
12import java.rmi.Remote;
13import java.rmi.Naming;
14import java.rmi.RemoteException;
15import java.rmi.server.*;
16import javax.security.auth.Subject;
17
18public class JSTKShellRMIServerImpl extends java.rmi.server.UnicastRemoteObject
19            implements JSTKShell {
20    protected JSTKShell shell = null;
21    protected Subject sub = null;
22
23    public JSTKShellRMIServerImpl() throws java.rmi.RemoteException {
24        super(0);
25        shell = new JSTKShellServer();
26    }
27
28    public JSTKShellRMIServerImpl(int port) throws java.rmi.RemoteException {
29        super(port);
30        shell = new JSTKShellServer();
31    }
32
33    public JSTKShellRMIServerImpl(int port, RMIClientSocketFactory clientFactory,
34            RMIServerSocketFactory serverFactory) throws java.rmi.RemoteException {
35        super(port, clientFactory, serverFactory);
36        shell = new JSTKShellServer();
37    }
38
39    public void setSubject(Subject sub){
40        this.sub = sub;
41    }
42
43    public String execCommand(String[] cmdargs) throws RemoteException {
44        try {
45            return shell.execCommand(cmdargs);
46        } catch (Exception e){
47            throw new RemoteException("Exception at RMI Server", e);
48        }
49    }
50
51    public String createSession() throws RemoteException {
52        try {
53            return shell.createSession();
54        } catch (Exception e){
55            throw new RemoteException("Exception at RMI Server", e);
56        }
57    }
58
59    public void destroySession(String sessId) throws RemoteException {
60        try {
61            shell.destroySession(sessId);
62        } catch (Exception e){
63            throw new RemoteException("Exception at RMI Server", e);
64        }
65    }
66
67    public void initialize(String[] args, String serverId) throws Exception{
68        int regPort = 1099;
69        try {
70            java.rmi.registry.LocateRegistry.createRegistry(regPort);
71        } catch (Exception exc){
72            System.err.println("Could not create rmiregistry in local JVM: " + exc);
73            System.exit(1);
74        }
75        System.out.println("RMI Registry started. Listens for requests on port: " + regPort);
76
77        Naming.rebind("//localhost/" + serverId, this);
78        System.out.println(serverId + " bound in registry ...");
79    }
80
81    public static void main(String[] args) throws Exception{
82        JSTKShellRMIServerImpl impl = new JSTKShellRMIServerImpl();
83        impl.initialize(args, "JSTKShellRMIServer");
84    }
85}
86