Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 96555812e6d9cac3655586c50967263307327513 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*******************************************************************************
 * Copyright (c) 2004 Composent, 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: Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.core;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.ecf.core.provider.IContainerInstantiator;
import org.eclipse.ecf.core.provider.IRemoteServiceContainerInstantiator;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.internal.core.ECFDebugOptions;
import org.eclipse.ecf.internal.core.ECFPlugin;

/**
 * Description of an {@link IContainer} type.  Instances of this class are used to represent {@link IContainerInstantiator}s
 * in the {@link ContainerFactory}
 * 
 * @see ContainerFactory IContainerInstantiator
 */
public class ContainerTypeDescription {

	protected String name = null;

	protected String instantiatorClass = null;

	protected IContainerInstantiator instantiator = null;

	protected String description = null;

	protected int hashCode = 0;

	protected boolean server;

	protected boolean hidden;

	public ContainerTypeDescription(String name, String instantiatorClass, String description) {
		this(name, instantiatorClass, description, false, false);
	}

	public ContainerTypeDescription(String name, String instantiatorClass, String description, boolean server, boolean hidden) {
		Assert.isNotNull(name, "ContainerTypeDescription<init> name cannot be null"); //$NON-NLS-1$
		this.name = name;
		this.hashCode = name.hashCode();
		Assert.isNotNull(instantiatorClass, "ContainerTypeDescription<init> instantiatorClass cannot be null"); //$NON-NLS-1$
		this.instantiatorClass = instantiatorClass;
		this.description = description;
		this.server = server;
		this.hidden = hidden;
	}

	/**
	 * @since 3.4
	 */
	public ContainerTypeDescription(String name, IContainerInstantiator instantiator) {
		this(name, instantiator, null);
	}

	public ContainerTypeDescription(String name, IContainerInstantiator instantiator, String description) {
		this(name, instantiator, description, false, false);
	}

	public ContainerTypeDescription(String name, IContainerInstantiator inst, String desc, boolean server, boolean hidden) {
		Assert.isNotNull(name, "ContainerTypeDescription<init> name cannot be null"); //$NON-NLS-1$
		this.name = name;
		this.hashCode = name.hashCode();
		Assert.isNotNull(inst, "ContainerTypeDescription<init> instantiator instance cannot be null"); //$NON-NLS-1$
		this.instantiator = inst;
		this.description = desc;
		this.server = server;
		this.hidden = hidden;
	}

	/**
	 * Get ContainerTypeDescription name
	 * 
	 * @return String name for the ContainerTypeDescription. Will not be null.
	 */
	public String getName() {
		return name;
	}

	public boolean equals(Object other) {
		if (!(other instanceof ContainerTypeDescription))
			return false;
		ContainerTypeDescription scd = (ContainerTypeDescription) other;
		return scd.name.equals(name);
	}

	public int hashCode() {
		return hashCode;
	}

	public String toString() {
		StringBuffer b = new StringBuffer("ContainerTypeDescription["); //$NON-NLS-1$
		b.append("name=").append(name).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
		if (instantiator == null)
			b.append("instantiatorClass=").append(instantiatorClass) //$NON-NLS-1$
					.append(";"); //$NON-NLS-1$
		else
			b.append("instantiator=").append(instantiator).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
		b.append("desc=").append(description).append(";"); //$NON-NLS-1$ //$NON-NLS-2$
		return b.toString();
	}

	protected IContainerInstantiator getInstantiator() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		synchronized (this) {
			if (instantiator == null)
				initializeInstantiator();
			return instantiator;
		}
	}

	private void initializeInstantiator() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		// Load instantiator class
		Class clazz = Class.forName(instantiatorClass);
		// Make new instance
		instantiator = (IContainerInstantiator) clazz.newInstance();
	}

	/**
	 * Get the String description associated with this ContainerTypeDescription
	 * instance
	 * 
	 * @return String description. May be null.
	 */
	public String getDescription() {
		return description;
	}

	public boolean isServer() {
		return server;
	}

	public boolean isHidden() {
		return hidden;
	}

	/**
	 * Get array of supported adapters for this container type description. The
	 * returned array entries will be the fully qualified names of the adapter
	 * classes.
	 * 
	 * Note that the returned types do not guarantee that a subsequent call to
	 * {@link IContainer#getAdapter(Class)} with the same type name as a
	 * returned value will return a non-<code>null</code result. In other words, even if the
	 * class name is in the returned array, subsequent calls to
	 * {@link IContainer#getAdapter(Class)} may still return <code>null</code>.
	 * 
	 * @return String[] of supported adapters. The entries in the returned array
	 *         will be the fully qualified class names of adapters supported by
	 *         the given description. An empty string array (String[0]) will be
	 *         returned if no adapters are supported.
	 */
	public String[] getSupportedAdapterTypes() {
		String method = "getSupportedAdapterTypes"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		String[] result = new String[0];
		try {
			String[] r = getInstantiator().getSupportedAdapterTypes(this);
			if (r != null)
				result = r;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
		}
		List resultList = new ArrayList();
		for (int i = 0; i < result.length; i++) {
			resultList.add(result[i]);
		}
		if (!resultList.contains(IContainer.class.getName()))
			resultList.add(IContainer.class.getName());
		Trace.exiting(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_EXITING, this.getClass(), method, result);
		return (String[]) resultList.toArray(new String[] {});
	}

	protected void traceAndLogException(int code, String method, Throwable e) {
		Trace.catching(ECFPlugin.PLUGIN_ID, ECFDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), method, e);
		ECFPlugin.getDefault().log(new Status(IStatus.ERROR, ECFPlugin.PLUGIN_ID, code, method, e));
	}

	/**
	 * Get array of parameter types for this ContainerTypeDescription. Each of
	 * the rows of the returned array specifies a Class[] of parameter types.
	 * These parameter types correspond to the types of Objects that can be
	 * passed into the second parameter of
	 * {@link IContainerInstantiator#createInstance(ContainerTypeDescription, Object[])}.
	 * For example, if this method returns a Class [] = {{ String.class,
	 * String.class }, { String.class }} this indicates that a call to
	 * createInstance(description,new String[] { "hello", "there" }) and a call
	 * to createInstance(description,new String[] { "hello" }) will be
	 * understood by the underlying provider implementation.
	 * 
	 * @return Class[][] array of Class arrays. Each row corresponds to a
	 *         Class[] that describes the types of Objects for second parameter
	 *         to
	 *         {@link IContainerInstantiator#createInstance(ContainerTypeDescription, Object[])}.
	 *         If no parameter types are understood as arguments, a Class[0][0]
	 *         array will be returned
	 */
	public Class[][] getSupportedParameterTypes() {
		String method = "getParameterTypes"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		Class[][] result = new Class[0][0];
		try {
			Class[][] r = getInstantiator().getSupportedParameterTypes(this);
			if (r != null)
				result = r;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
		}
		Trace.exiting(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_EXITING, this.getClass(), method, result);
		return result;
	}

	/**
	 * @return String[] of container's intents.
	 * 
	 * @since 3.0
	 */
	public String[] getSupportedIntents() {
		String method = "getSupportedIntents"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		try {
			IContainerInstantiator ci = getInstantiator();
			return (ci instanceof IRemoteServiceContainerInstantiator) ? ((IRemoteServiceContainerInstantiator) ci).getSupportedIntents(this) : null;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
			return null;
		}
	}

	/**
	 * @since 3.1
	 */
	public String[] getSupportedConfigs() {
		String method = "getSupportedConfigs"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		try {
			IContainerInstantiator ci = getInstantiator();
			return (ci instanceof IRemoteServiceContainerInstantiator) ? ((IRemoteServiceContainerInstantiator) ci).getSupportedConfigs(this) : null;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
			return null;
		}
	}

	/**
	 * @since 3.1
	 */
	public String[] getImportedConfigs(String[] exporterSupportedConfigs) {
		String method = "getImportedConfigs"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		if (exporterSupportedConfigs == null)
			return null;
		try {
			IContainerInstantiator ci = getInstantiator();
			return (ci instanceof IRemoteServiceContainerInstantiator) ? ((IRemoteServiceContainerInstantiator) ci).getImportedConfigs(this, exporterSupportedConfigs) : null;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
			return null;
		}
	}

	/**
	 * @since 3.1
	 */
	public Dictionary getPropertiesForImportedConfigs(String[] importedConfigs, Dictionary exportedProperties) {
		String method = "getPropertiesForImportedConfigs"; //$NON-NLS-1$
		Trace.entering(ECFPlugin.PLUGIN_ID, ECFDebugOptions.METHODS_ENTERING, this.getClass(), method);
		if (importedConfigs == null)
			return null;
		try {
			IContainerInstantiator ci = getInstantiator();
			return (ci instanceof IRemoteServiceContainerInstantiator) ? ((IRemoteServiceContainerInstantiator) ci).getPropertiesForImportedConfigs(this, importedConfigs, exportedProperties) : null;
		} catch (Exception e) {
			traceAndLogException(IStatus.ERROR, method, e);
			return null;
		}
	}

}

Back to the top