Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 52a907f62724279ab90d9ba31967002fe0580c67 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2011 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.core.connection;

import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.te.core.activator.CoreBundleActivator;
import org.eclipse.tcf.te.core.connection.interfaces.IConnectionType;
import org.eclipse.tcf.te.core.connection.interfaces.IConnectionTypeConstants;
import org.eclipse.tcf.te.core.utils.text.StringUtil;
import org.eclipse.tcf.te.runtime.nls.Messages;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;

/**
 * Connection type implementation.
 */
public class ConnectionType extends PropertiesContainer implements IConnectionType {

	/**
	 * Initialize the default connection type properties.
	 * <p>
	 * <b>Note:</b> This method is called from {@link #setInitializationData(IConfigurationElement, String, Object)}.
	 */
	protected void initDefaultProperties() {
		setProperty(IConnectionTypeConstants.PROPERTY_DEFINING_BUNDLE, null);
		setProperty(IConnectionTypeConstants.PROPERTY_ID, null);
		setProperty(IConnectionTypeConstants.PROPERTY_LABEL, ""); //$NON-NLS-1$
		setProperty(IConnectionTypeConstants.PROPERTY_SHORTNAME, ""); //$NON-NLS-1$
		setProperty(IConnectionTypeConstants.PROPERTY_DESCRIPTION, ""); //$NON-NLS-1$
		setProperty(IConnectionTypeConstants.PROPERTY_CATEGORY_ID, null);
		setProperty(IConnectionTypeConstants.PROPERTY_SUPPORTS_EARLY_FINISH, false);
		setProperty(IConnectionTypeConstants.PROPERTY_ENABLED, true);
		setProperty(IConnectionTypeConstants.PROPERTY_LAST_INVALID_CAUSE, null);
		setProperty(IConnectionTypeConstants.PROPERTY_CONNECT_IMMEDIATELY, true);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
	 */
	@Override
    @SuppressWarnings("unchecked")
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		// Initialize the default properties first
		initDefaultProperties();

		// read the connection type attributes from the configuration element and apply
		if (config != null && config.getName().equals("connectionType")) { //$NON-NLS-1$

			// Set the id if an id attribute is specified and the id had been not
			// yet set by the connection type implementation class.
			// Throws an exception if the id is empty or null.
			String id = config.getAttribute("id"); //$NON-NLS-1$
			if (getId() == null && id != null && id.length() > 0) {
				setProperty(IConnectionTypeConstants.PROPERTY_ID, id);
			} else {
				throw createMissingMandatoryAttributeException("id", config.getContributor().getName()); //$NON-NLS-1$
			}

			// Initialize the label field by reading the <label> extension attribute if present.
			String label = config.getAttribute("label"); //$NON-NLS-1$
			if (label != null && label.length() > 0) {
				setProperty(IConnectionTypeConstants.PROPERTY_LABEL, label);
			} else {
				throw createMissingMandatoryAttributeException("label", config.getContributor().getName()); //$NON-NLS-1$
			}

			// Initialize the description field by reading the "<description>" extension child element if present.
			IConfigurationElement[] children = config.getChildren("description"); //$NON-NLS-1$
			// Only one description element is allow. All other will be ignored
			if (children.length > 0) {
				IConfigurationElement description = children[0];
				String value = description.getValue();
				setProperty(IConnectionTypeConstants.PROPERTY_DESCRIPTION, value != null ? value.trim() : ""); //$NON-NLS-1$
			}

			// Get the enabled attribute
			String isEnabled = config.getAttribute("isEnabled"); //$NON-NLS-1$

			// We allow to overwrite the enabled attribute from the plugin.xml
			// via a .options file property:
			//
			// The options file key is: <contribution plugin>/connectionType/<id>/enabled = true
			StringBuilder debugKey = new StringBuilder(config.getContributor().getName());
			debugKey.append("/connectionType/"); //$NON-NLS-1$
			debugKey.append(id.replaceAll("\\s", "_")); //$NON-NLS-1$ //$NON-NLS-2$
			debugKey.append("/enabled"); //$NON-NLS-1$

			if (Boolean.parseBoolean(Platform.getDebugOption(debugKey.toString()))) {
				isEnabled = "true"; //$NON-NLS-1$
			}

			// Apply the enabled attribute
			if (isEnabled != null && isEnabled.length() > 0 && (isEnabled.equalsIgnoreCase("true") || isEnabled.equalsIgnoreCase("false"))) { //$NON-NLS-1$ //$NON-NLS-2$
				setProperty(IConnectionTypeConstants.PROPERTY_ENABLED, Boolean.valueOf(isEnabled).booleanValue());
			}

			// Get the short name attribute
			String shortName = config.getAttribute("shortName"); //$NON-NLS-1$
			if (shortName != null && shortName.length() > 0) {
				setProperty(IConnectionTypeConstants.PROPERTY_SHORTNAME, shortName);
			}

			// Get the new connection wizard category id
			String categoryId = config.getAttribute("categoryId"); //$NON-NLS-1$
			if (categoryId != null && categoryId.length() > 0) {
				setProperty(IConnectionTypeConstants.PROPERTY_CATEGORY_ID, categoryId);
			}
		}

		// Check the initialization data object. Can be either a string or a hash table.
		if (data instanceof String) {
			// Tokenize the string and take the pairs as properties
			String[] params = StringUtil.tokenize((String)data, 0, false);
			for (int i = 0; i < params.length - 1; i++) {
				// Parameter key is the first parameter
				String paramKey = params[i];
				// Parameter value is the second parameter
				String paramValue = (i + 1) < params.length ? params[++i] : null;
				// Don't apply a property with the same name if already set (protects
				// explicit attributes like "shortName", "isEnabled" or "categoryId").
				if (paramKey != null && getProperty(paramKey) != null) continue;
				// Store it
				if (paramKey != null) setProperty(paramKey, paramValue);
			}
		}
		else if (data instanceof Map<?,?>) {
			// Just copy the map content to the properties
			Map<String, String> params = (Map<String, String>)data;
			for (String paramKey : params.keySet()) {
				// If the short name is already set, don't overwrite it
				if (IConnectionTypeConstants.PROPERTY_SHORTNAME.equals(paramKey) && getProperty(paramKey) != null) continue;
				// Store it
				if (paramKey != null) setProperty(paramKey, params.get(paramKey));
			}
		}
	}

	/**
	 * Creates a new {@link CoreException} to be thrown if a mandatory extension attribute
	 * is missing.
	 *
	 * @param attributeName The attribute name. Must be not <code>null</code>.
	 * @param extensionId The extension id. Must be not <code>null</code>.
	 *
	 * @return The {@link CoreException} instance.
	 */
	protected CoreException createMissingMandatoryAttributeException(String attributeName, String extensionId) {
		Assert.isNotNull(attributeName);
		Assert.isNotNull(extensionId);

		return new CoreException(new Status(IStatus.ERROR,
		                					CoreBundleActivator.getUniqueIdentifier(),
		                					0,
		                					NLS.bind(Messages.Extension_error_missingRequiredAttribute, attributeName, extensionId),
		                					null));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.extensions.IExecutableExtension#getId()
	 */
	@Override
    public final String getId() {
		return getStringProperty(IConnectionTypeConstants.PROPERTY_ID);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.extensions.IExecutableExtension#getLabel()
	 */
	@Override
    public final String getLabel() {
		return getStringProperty(IConnectionTypeConstants.PROPERTY_LABEL);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.extensions.IExecutableExtension#getDescription()
	 */
	@Override
    public final String getDescription() {
		return getStringProperty(IConnectionTypeConstants.PROPERTY_DESCRIPTION);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.core.connection.interfaces.IConnectionType#isEnabled()
	 */
	@Override
    public boolean isEnabled() {
		return getBooleanProperty(IConnectionTypeConstants.PROPERTY_ENABLED) /*&& ConnectionTypeBindingsManager.getInstance().isConnectionTypeEnabled(getId())*/;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.core.connection.interfaces.IConnectionType#isValid()
	 */
	@Override
    public boolean isValid() {
		setProperty(IConnectionTypeConstants.PROPERTY_LAST_INVALID_CAUSE, null);
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {
		// If the requested adapter is a connection service type,
		// forward to the connection type service manager
//		if (IConnectionTypeService.class.isAssignableFrom(adapter)) {
//			IConnectionTypeService service = ConnectionTypeServiceManager.getInstance().getService(getId(), adapter);
//			if (service != null) return service;
//		}
		return super.getAdapter(adapter);
	}

}

Back to the top