Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5686c3661e754ba7f7bfc99770f2ade03439d12b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**********************************************************************
 * Copyright (c) 2012, 2013 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Bernd Hufmann - Initial API and implementation
 **********************************************************************/
package org.eclipse.linuxtools.internal.lttng2.control.ui.views.remote;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.core.model.IRSECallback;
import org.eclipse.rse.core.subsystems.ICommunicationsListener;
import org.eclipse.rse.core.subsystems.IConnectorService;
import org.eclipse.rse.core.subsystems.ISubSystem;
import org.eclipse.rse.services.IService;
import org.eclipse.rse.services.shells.IShellService;
import org.eclipse.rse.services.terminals.ITerminalService;
import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;

/**
 * <p>
 * RemoteSystemProxy implementation.
 * </p>
 *
 * @author Bernd Hufmann
 */
public class RemoteSystemProxy implements IRemoteSystemProxy {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private final IHost fHost;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    /**
     * Constructor
     *
     * @param host
     *            The host of this proxy
     */
    public RemoteSystemProxy(IHost host) {
        fHost = host;
    }

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    @Override
    public IShellService getShellService() {
        ISubSystem ss = getShellServiceSubSystem();
        if (ss != null) {
            return (IShellService)ss.getSubSystemConfiguration().getService(fHost).getAdapter(IShellService.class);
        }
        return null;
    }

    @Override
    public ITerminalService getTerminalService() {
        ISubSystem ss = getTerminalServiceSubSystem();
        if (ss != null) {
            return (ITerminalService)ss.getSubSystemConfiguration().getService(fHost).getAdapter(ITerminalService.class);
        }
        return null;
    }

    @Override
    public ISubSystem getShellServiceSubSystem() {
        if (fHost == null) {
            return null;
        }
        ISubSystem[] subSystems = fHost.getSubSystems();
        IShellService ssvc = null;
        for (int i = 0; subSystems != null && i < subSystems.length; i++) {
            IService svc = subSystems[i].getSubSystemConfiguration().getService(fHost);
            if (svc!=null) {
                ssvc = (IShellService)svc.getAdapter(IShellService.class);
                if (ssvc != null) {
                    return subSystems[i];
                }
            }
        }
        return null;
    }

    @Override
    public ISubSystem getTerminalServiceSubSystem() {
        if (fHost == null) {
            return null;
        }
        ISubSystem[] subSystems = fHost.getSubSystems();
        ITerminalService ssvc = null;
        for (int i = 0; subSystems != null && i < subSystems.length; i++) {
            IService svc = subSystems[i].getSubSystemConfiguration().getService(fHost);
            if (svc!=null) {
                ssvc = (ITerminalService)svc.getAdapter(ITerminalService.class);
                if (ssvc != null) {
                    return subSystems[i];
                }
            }
        }
        return null;
    }

    @Override
    public IFileServiceSubSystem getFileServiceSubSystem() {
        if (fHost == null) {
            return null;
        }
        ISubSystem[] subSystems = fHost.getSubSystems();
        for (int i = 0; subSystems != null && i < subSystems.length; i++) {
            if (subSystems[i] instanceof IFileServiceSubSystem) {
                return (IFileServiceSubSystem)subSystems[i];
            }
        }
        return null;
    }

    @Override
    public int getPort() {
        if (getShellServiceSubSystem() != null) {
            return getShellServiceSubSystem().getConnectorService().getPort();
        }
        return IRemoteSystemProxy.INVALID_PORT_NUMBER;
    }

    @Override
    public void setPort(int port) {
        if ((getShellServiceSubSystem() != null) && (port > 0)) {
            getShellServiceSubSystem().getConnectorService().setPort(port);
        }
    }

    @Override
    public void connect(IRSECallback callback) throws ExecutionException {
        ISubSystem shellSubSystem = getShellServiceSubSystem();
        if (shellSubSystem != null) {
            if (!shellSubSystem.isConnected()) {
                try {
                    shellSubSystem.connect(false, callback);
                } catch (OperationCanceledException e) {
                    callback.done(Status.CANCEL_STATUS, null);
                }
                catch (Exception e) {
                    throw new ExecutionException(e.toString(), e);
                }
            } else {
                callback.done(Status.OK_STATUS, null);
            }
        }
    }

    @Override
    public void disconnect() throws ExecutionException {
            ISubSystem shellSubSystem = getShellServiceSubSystem();
            if (shellSubSystem != null) {
                try {
                    shellSubSystem.disconnect();
                } catch (Exception e) {
                    throw new ExecutionException(e.toString(), e);
                }
            }
    }

    @Override
    public ICommandShell createCommandShell() throws ExecutionException {
        ICommandShell shell = new CommandShell(this);
        shell.connect();
        return shell;
    }

    @Override
    public void addCommunicationListener(ICommunicationsListener listener) {
        IConnectorService[] css = fHost.getConnectorServices();
        for (IConnectorService cs : css) {
            cs.addCommunicationsListener(listener);
        }
    }

    @Override
    public void removeCommunicationListener(ICommunicationsListener listener) {
        IConnectorService[] css = fHost.getConnectorServices();
        for (IConnectorService cs : css) {
            cs.removeCommunicationsListener(listener);
        }
    }
}

Back to the top