Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d5e635fe15bcfbd5d2e574526d6fa11a500851d7 (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
/*******************************************************************************
 * Copyright (c) 2013 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.te.tcf.locator.steps;

import java.util.concurrent.TimeoutException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.runtime.utils.ProgressHelper;
import org.eclipse.tcf.te.runtime.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.nls.Messages;

/**
 * WaitForReadyStep
 */
public class WaitForReadyStep extends AbstractPeerModelStep {

	/**
	 * Constructor.
	 */
	public WaitForReadyStep() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.interfaces.IStep#validateExecute(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void validateExecute(IStepContext context, IPropertiesContainer data, IFullQualifiedId fullQualifiedId, IProgressMonitor monitor) throws CoreException {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.interfaces.IStep#execute(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer, org.eclipse.tcf.te.runtime.stepper.interfaces.IFullQualifiedId, org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	public void execute(final IStepContext context, final IPropertiesContainer data, final IFullQualifiedId fullQualifiedId, final IProgressMonitor monitor, final ICallback callback) {
		final IPeerModel peerModel = getActivePeerModelContext(context, data, fullQualifiedId);

		if (peerModel != null && !Boolean.getBoolean("WaitForReadyStep.skip")) { //$NON-NLS-1$
			Protocol.invokeLater(new Runnable() {
				final Runnable thisRunnable = this;
				int refreshCount = 0;
				@Override
				public void run() {
					if (ProgressHelper.isCancel(WaitForReadyStep.this, monitor, callback)) {
						return;
					}
					else if (refreshCount >= getTotalWork(context, data)) {
						@SuppressWarnings("synthetic-access")
                        String message = NLS.bind(Messages.WaitForReadyStep_error_timeout, getActivePeerContext(context, data, fullQualifiedId).getName());
						callback(data, fullQualifiedId, callback, StatusHelper.getStatus(new TimeoutException(message)), null);
					}
					else {
						// Try to open a channel to the target and check for errors
						Tcf.getChannelManager().openChannel(peerModel.getPeer(), null, new IChannelManager.DoneOpenChannel() {
							@Override
							public void doneOpenChannel(final Throwable error, final IChannel channel) {
								if (ProgressHelper.isCancel(WaitForReadyStep.this, monitor, callback)) {
									return;
								}
								IStatus status = null;

								// If the channel open succeeded, we are done
								if (error == null && channel != null && channel.getState() == IChannel.STATE_OPEN) {
									status = Status.OK_STATUS;
								}

								// Close the channel right away
								if (channel != null) Tcf.getChannelManager().closeChannel(channel);

								// If we have an OK status, we are done
								if (status != null && status.isOK()) {
									callback(data, fullQualifiedId, callback, status, null);
									return;
								}

								// License errors are reported to the user and breaks the wait immediately
								if (error != null) {
									String message = error.getLocalizedMessage();
									if (message != null && (message.contains("LMAPI error occured:") //$NON-NLS-1$
												|| message.contains("Failed to read output from value-add"))) { //$NON-NLS-1$
										callback(data, fullQualifiedId, callback, StatusHelper.getStatus(error), null);
										return;
									}
								}

								// Try again until timed out
								refreshCount++;
								ProgressHelper.worked(monitor, 1);
								Protocol.invokeLater(refreshCount < 20 ? 500 : 1000, thisRunnable);
							}
						});
					}
				}
			});
		}
		else {
			callback(data, fullQualifiedId, callback, Status.OK_STATUS, null);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.stepper.steps.AbstractStep#getTotalWork(org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext, org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	public int getTotalWork(IStepContext context, IPropertiesContainer data) {
	    return 100;
	}
}

Back to the top