tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.remote.core / src / org / eclipse / tracecompass / tmf / remote / core / proxy / RemoteSystemProxy.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
12 * Bernd Hufmann - Update to org.eclipse.remote API 2.0
13 **********************************************************************/
14 package org.eclipse.tracecompass.tmf.remote.core.proxy;
15
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.remote.core.IRemoteConnection;
21 import org.eclipse.remote.core.IRemoteConnectionChangeListener;
22 import org.eclipse.remote.core.RemoteConnectionChangeEvent;
23 import org.eclipse.remote.core.exception.RemoteConnectionException;
24 import org.eclipse.tracecompass.internal.tmf.remote.core.shell.CommandShell;
25 import org.eclipse.tracecompass.tmf.remote.core.shell.ICommandShell;
26
27 /**
28 * <p>
29 * RemoteSystemProxy implementation.
30 * </p>
31 *
32 * @author Bernd Hufmann
33 */
34 @NonNullByDefault
35 public class RemoteSystemProxy implements IRemoteConnectionChangeListener {
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40 private final IRemoteConnection fHost;
41 private boolean fExplicitConnect;
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46
47 /**
48 * Constructor
49 *
50 * @param host
51 * The host of this proxy
52 */
53 public RemoteSystemProxy(IRemoteConnection host) {
54 fHost = host;
55 fHost.addConnectionChangeListener(this);
56 }
57
58 /**
59 * Returns the connection instance.
60 *
61 * @return the @link{IRemoteConnection} instance
62 */
63 public IRemoteConnection getRemoteConnection() {
64 return fHost;
65 }
66
67 // ------------------------------------------------------------------------
68 // Operations
69 // ------------------------------------------------------------------------
70
71 /**
72 * Connects the remote connection.
73 *
74 * @param monitor
75 * a monitor to report progress.
76 *
77 * @throws ExecutionException
78 * If the connection fails
79 */
80 public void connect(IProgressMonitor monitor) throws ExecutionException {
81 try {
82 if (!fHost.isOpen()) {
83 // Note that open() may trigger a RemoteConnectionChangeEvent
84 fHost.open(monitor);
85 fExplicitConnect = true;
86 }
87 } catch (RemoteConnectionException e) {
88 throw new ExecutionException("Cannot connect " + fHost.getName(), e); //$NON-NLS-1$
89 }
90 }
91
92 /**
93 * Disconnects from the remote connection, may close the connection.
94 */
95 public void disconnect() {
96 if (fExplicitConnect) {
97 fHost.close();
98 fExplicitConnect = false;
99 }
100 }
101
102 /**
103 * Disposes the proxy, may close the connection.
104 */
105 public void dispose() {
106 fHost.removeConnectionChangeListener(this);
107 disconnect();
108 }
109
110 /**
111 * Creates a command shell.
112 *
113 * @return the command shell implementation
114 */
115 public ICommandShell createCommandShell() {
116 return new CommandShell(fHost);
117 }
118
119 /**
120 * Returns the connection state.
121 *
122 * @return whether the remote host is currently connected.
123 */
124 public boolean isConnected() {
125 return fHost.isOpen();
126 }
127
128 @Override
129 public void connectionChanged(@Nullable RemoteConnectionChangeEvent event) {
130 if (event != null) {
131 int type = event.getType();
132 if (type == RemoteConnectionChangeEvent.CONNECTION_ABORTED ||
133 type == RemoteConnectionChangeEvent.CONNECTION_CLOSED) {
134 fExplicitConnect = false;
135 }
136 }
137 }
138
139 }
This page took 0.037031 seconds and 5 git commands to generate.