Commit | Line | Data |
---|---|---|
f14957a3 MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 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 | * Marc-Andre Laperle - Initial API and implementation | |
417a4110 | 11 | * Bernd Hufmann - Add connection handling |
f14957a3 MAL |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model; | |
15 | ||
417a4110 BH |
16 | import java.net.URI; |
17 | import java.net.URISyntaxException; | |
18 | import java.text.MessageFormat; | |
19 | ||
20 | import org.eclipse.core.commands.ExecutionException; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | import org.eclipse.core.runtime.IStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.core.runtime.URIUtil; | |
25 | import org.eclipse.jdt.annotation.NonNull; | |
26 | import org.eclipse.remote.core.exception.RemoteConnectionException; | |
f14957a3 MAL |
27 | import org.eclipse.swt.graphics.Image; |
28 | import org.eclipse.tracecompass.internal.tmf.remote.ui.Activator; | |
417a4110 | 29 | import org.eclipse.tracecompass.internal.tmf.remote.ui.messages.RemoteMessages; |
f14957a3 | 30 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement; |
417a4110 | 31 | import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy; |
40692116 | 32 | import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory; |
f14957a3 MAL |
33 | |
34 | /** | |
35 | * An RemoteImportConnectionNodeElement representing a connection node. | |
36 | * | |
37 | * @author Marc-Andre Laperle | |
38 | */ | |
39 | public class RemoteImportConnectionNodeElement extends TracePackageElement { | |
40 | ||
41 | private static final String IMAGE_PATH = "icons/obj/connection_node.gif"; //$NON-NLS-1$ | |
42 | ||
43 | private String fName; | |
44 | private String fURI; | |
417a4110 | 45 | private RemoteSystemProxy fRemoteProxy; |
f14957a3 MAL |
46 | |
47 | /** | |
48 | * Constructs an instance of RemoteImportConnectionNodeElement. | |
49 | * | |
50 | * @param parent | |
51 | * the parent of this element, can be set to null | |
52 | * @param name | |
53 | * the node name | |
54 | * @param uri | |
55 | * the URI in string form to connect to the node | |
56 | */ | |
57 | public RemoteImportConnectionNodeElement(TracePackageElement parent, | |
58 | String name, String uri) { | |
59 | super(parent); | |
60 | fName = name; | |
61 | fURI = uri; | |
62 | } | |
63 | ||
64 | @Override | |
65 | public String getText() { | |
66 | return fName + " (" + fURI + ")"; //$NON-NLS-1$//$NON-NLS-2$ | |
67 | } | |
68 | ||
69 | @Override | |
70 | public Image getImage() { | |
71 | return Activator.getDefault().getImageFromImageRegistry(IMAGE_PATH); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Get the name of the connection. | |
76 | * | |
77 | * @return the name of the connection | |
78 | */ | |
79 | public String getName() { | |
80 | return fName; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Set the name of the connection. | |
85 | * | |
86 | * @param name the name of the connection | |
87 | */ | |
88 | public void setName(String name) { | |
89 | fName = name; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Get the URI of the connection. | |
94 | * | |
95 | * @return the URI of the connection | |
96 | */ | |
97 | public String getURI() { | |
98 | return fURI; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Set the URI of the connection. | |
103 | * | |
104 | * @param uri the URI of the connection | |
105 | */ | |
106 | public void setURI(String uri) { | |
107 | fURI = uri; | |
108 | } | |
109 | ||
417a4110 BH |
110 | /** |
111 | * Connects to the remote host | |
112 | * | |
113 | * @param monitor | |
114 | * a progress monitor | |
115 | * | |
116 | * @return status of the executions | |
117 | */ | |
118 | public IStatus connect(@NonNull IProgressMonitor monitor) { | |
119 | // Use local variables to avoid null annotation warning | |
120 | RemoteSystemProxy proxy = fRemoteProxy; | |
121 | String name = fName; | |
122 | if (name == null) { | |
123 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, RemoteMessages.RemoteImportConnectionNodeElement_NodeNameNullError); | |
124 | } | |
125 | if (proxy == null) { | |
126 | try { | |
127 | URI hostUri = null; | |
128 | hostUri = URIUtil.fromString(fURI); | |
129 | if (hostUri == null) { | |
130 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, RemoteMessages.RemoteImportConnectionNodeElement_UriNullError); | |
131 | } | |
40692116 | 132 | proxy = new RemoteSystemProxy(TmfRemoteConnectionFactory.createConnection(hostUri, name)); |
417a4110 BH |
133 | fRemoteProxy = proxy; |
134 | } catch (URISyntaxException e) { | |
135 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(RemoteMessages.RemoteImportConnectionNodeElement_InvalidUriString, fURI), e); | |
136 | } catch (RemoteConnectionException e) { | |
137 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(RemoteMessages.RemoteImportConnectionNodeElement_ConnectionFailure, fURI), e); | |
138 | } | |
139 | } | |
140 | try { | |
141 | proxy.connect(monitor); | |
142 | return Status.OK_STATUS; | |
143 | } catch (ExecutionException e) { | |
144 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(RemoteMessages.RemoteImportConnectionNodeElement_ConnectionFailure, fURI), e); | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * Disconnects the remote host | |
150 | */ | |
151 | public void disconnect() { | |
152 | if (fRemoteProxy != null) { | |
153 | fRemoteProxy.disconnect(); | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
158 | * Returns the remote system proxy implementation | |
159 | * | |
160 | * @return the remote system proxy or null | |
161 | */ | |
162 | public RemoteSystemProxy getRemoteSystemProxy() { | |
163 | return fRemoteProxy; | |
164 | } | |
f14957a3 | 165 | } |