Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe4c9a938122d9a512f1a9801dc2468d1273c09c (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package org.eclipse.ecf.provider.remoteservice.generic.registry;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.provider.remoteservice.generic.RemoteCallImpl;
import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
import org.eclipse.ecf.remoteservice.IRemoteServiceRegistration;

public class RemoteServiceRegistrationImpl implements
		IRemoteServiceRegistration, Serializable {

	private static final long serialVersionUID = -3206899332723536545L;

	transient Object service;

	/** service classes for this registration. */
	protected String[] clazzes;

	/** service id. */
	protected long serviceid;

	/** properties for this registration. */
	protected Properties properties;

	/** service ranking. */
	protected int serviceranking;

	/* internal object to use for synchronization */
	transient protected Object registrationLock = new Object();

	/** The registration state */
	protected int state = REGISTERED;

	protected ID containerID = null;

	public static final int REGISTERED = 0x00;

	public static final int UNREGISTERING = 0x01;

	public static final int UNREGISTERED = 0x02;

	public RemoteServiceRegistrationImpl() {

	}

	public void publish(RemoteServiceRegistryImpl registry, Object service,
			String[] clazzes, Dictionary properties) {
		this.service = service;
		this.clazzes = clazzes;
		this.containerID = registry.getContainerID();

		synchronized (registry) {
			serviceid = registry.getNextServiceId();
			this.properties = createProperties(properties);
			registry.publishService(this);
		}
		notifyPublishServiceEvent();
	}

	private void notifyPublishServiceEvent() {
		// XXX should notify via event here
		System.out.println("TBD notification of publish service event needed");
	}

	public Object getService() {
		return service;
	}

	public ID getContainerID() {
		return containerID;
	}

	public IRemoteServiceReference getReference() {
		return new RemoteServiceReferenceImpl(this);
	}

	public void setProperties(Dictionary properties) {
		synchronized (registrationLock) {
			/* in the process of unregistering */
			if (state != REGISTERED) {
				throw new IllegalStateException("Service already registered");
			}
			this.properties = createProperties(properties);
		}

		// XXX Need to notify that registration modified
	}

	public void unregister() {
		// TODO Auto-generated method stub

	}

	/**
	 * Construct a properties object from the dictionary for this
	 * ServiceRegistration.
	 * 
	 * @param props
	 *            The properties for this service.
	 * @return A Properties object for this ServiceRegistration.
	 */
	protected Properties createProperties(Dictionary props) {
		Properties properties = new Properties(props);

		properties.setProperty(RemoteServiceRegistryImpl.REMOTEOBJECTCLASS,
				clazzes);

		properties.setProperty(RemoteServiceRegistryImpl.REMOTESERVICE_ID,
				new Long(serviceid));

		Object ranking = properties
				.getProperty(RemoteServiceRegistryImpl.REMOTESERVICE_RANKING);

		serviceranking = (ranking instanceof Integer) ? ((Integer) ranking)
				.intValue() : 0;

		return (properties);
	}

	static class Properties extends Hashtable {

		/**
		 * 
		 */
		private static final long serialVersionUID = -3684607010228779249L;

		/**
		 * Create a properties object for the service.
		 * 
		 * @param props
		 *            The properties for this service.
		 */
		private Properties(int size, Dictionary props) {
			super((size << 1) + 1);

			if (props != null) {
				synchronized (props) {
					Enumeration keysEnum = props.keys();

					while (keysEnum.hasMoreElements()) {
						Object key = keysEnum.nextElement();

						if (key instanceof String) {
							String header = (String) key;

							setProperty(header, props.get(header));
						}
					}
				}
			}
		}

		/**
		 * Create a properties object for the service.
		 * 
		 * @param props
		 *            The properties for this service.
		 */
		protected Properties(Dictionary props) {
			this((props == null) ? 2 : Math.max(2, props.size()), props);
		}

		/**
		 * Get a clone of the value of a service's property.
		 * 
		 * @param key
		 *            header name.
		 * @return Clone of the value of the property or <code>null</code> if
		 *         there is no property by that name.
		 */
		protected Object getProperty(String key) {
			return (cloneValue(get(key)));
		}

		/**
		 * Get the list of key names for the service's properties.
		 * 
		 * @return The list of property key names.
		 */
		protected synchronized String[] getPropertyKeys() {
			int size = size();

			String[] keynames = new String[size];

			Enumeration keysEnum = keys();

			for (int i = 0; i < size; i++) {
				keynames[i] = (String) keysEnum.nextElement();
			}

			return (keynames);
		}

		/**
		 * Put a clone of the property value into this property object.
		 * 
		 * @param key
		 *            Name of property.
		 * @param value
		 *            Value of property.
		 * @return previous property value.
		 */
		protected synchronized Object setProperty(String key, Object value) {
			return (put(key, cloneValue(value)));
		}

		/**
		 * Attempt to clone the value if necessary and possible.
		 * 
		 * For some strange reason, you can test to see of an Object is
		 * Cloneable but you can't call the clone method since it is protected
		 * on Object!
		 * 
		 * @param value
		 *            object to be cloned.
		 * @return cloned object or original object if we didn't clone it.
		 */
		protected static Object cloneValue(Object value) {
			if (value == null)
				return null;
			if (value instanceof String) /* shortcut String */
				return (value);

			Class clazz = value.getClass();
			if (clazz.isArray()) {
				// Do an array copy
				Class type = clazz.getComponentType();
				int len = Array.getLength(value);
				Object clonedArray = Array.newInstance(type, len);
				System.arraycopy(value, 0, clonedArray, 0, len);
				return clonedArray;
			}
			// must use reflection because Object clone method is protected!!
			try {
				return (clazz.getMethod("clone", null).invoke(value, null));
			} catch (Exception e) {
				/* clone is not a public method on value's class */
			} catch (Error e) {
				/* JCL does not support reflection; try some well known types */
				if (value instanceof Vector)
					return (((Vector) value).clone());
				if (value instanceof Hashtable)
					return (((Hashtable) value).clone());
			}
			return (value);
		}

		public synchronized String toString() {
			String keys[] = getPropertyKeys();

			int size = keys.length;

			StringBuffer sb = new StringBuffer(20 * size);

			sb.append('{');

			int n = 0;
			for (int i = 0; i < size; i++) {
				String key = keys[i];
				if (!key.equals(RemoteServiceRegistryImpl.REMOTEOBJECTCLASS)) {
					if (n > 0)
						sb.append(", "); //$NON-NLS-1$

					sb.append(key);
					sb.append('=');
					Object value = get(key);
					if (value.getClass().isArray()) {
						sb.append('[');
						int length = Array.getLength(value);
						for (int j = 0; j < length; j++) {
							if (j > 0)
								sb.append(',');
							sb.append(Array.get(value, j));
						}
						sb.append(']');
					} else {
						sb.append(value);
					}
					n++;
				}
			}

			sb.append('}');

			return (sb.toString());
		}
	}

	public Object getProperty(String key) {
		return properties.getProperty(key);
	}

	public String[] getPropertyKeys() {
		return properties.getPropertyKeys();
	}

	public long getServiceId() {
		return serviceid;
	}

	public Object callService(RemoteCallImpl call) throws Exception {
		return call.invoke(service);
	}
}

Back to the top