Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 997cbe52aa4645082178325215af23d08a35583a (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/****************************************************************************
 * 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
 *****************************************************************************/

/*
 * Created on Dec 20, 2004
 *  
 */
package org.eclipse.ecf.provider.generic;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.ecf.core.ISharedObject;
import org.eclipse.ecf.core.ISharedObjectConnector;
import org.eclipse.ecf.core.ISharedObjectManager;
import org.eclipse.ecf.core.SharedObjectAddException;
import org.eclipse.ecf.core.SharedObjectConnectException;
import org.eclipse.ecf.core.SharedObjectCreateException;
import org.eclipse.ecf.core.SharedObjectDescription;
import org.eclipse.ecf.core.SharedObjectDisconnectException;
import org.eclipse.ecf.core.events.SharedObjectActivatedEvent;
import org.eclipse.ecf.core.events.SharedObjectManagerAddEvent;
import org.eclipse.ecf.core.events.SharedObjectManagerConnectEvent;
import org.eclipse.ecf.core.events.SharedObjectManagerCreateEvent;
import org.eclipse.ecf.core.events.SharedObjectManagerDisconnectEvent;
import org.eclipse.ecf.core.events.SharedObjectManagerRemoveEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.security.ISharedObjectPolicy;
import org.eclipse.ecf.core.util.AbstractFactory;
import org.eclipse.ecf.core.util.IQueueEnqueue;
import org.eclipse.ecf.provider.Trace;

/**
 * 
 */
public class SOManager implements ISharedObjectManager {
	static Trace debug = Trace.create("sharedobjectmanager");

	SOContainer container = null;
	Vector connectors = null;

	public SOManager(SOContainer cont) {
		super();
		this.container = cont;
		connectors = new Vector();
	}

	protected void debug(String msg) {
		if (Trace.ON && debug != null) {
			debug.msg(msg + ":" + container.getID());
		}
	}

	protected void dumpStack(String msg, Throwable e) {
		if (Trace.ON && debug != null) {
			debug.dumpStack(e, msg + ":" + container.getID());
		}
	}

	protected void addConnector(ISharedObjectConnector conn) {
		connectors.add(conn);
	}

	protected boolean removeConnector(ISharedObjectConnector conn) {
		return connectors.remove(conn);
	}

	protected List getConnectors() {
		return connectors;
	}

	protected Class[] getArgTypes(String[] argTypes, Object[] args,
			ClassLoader cl) throws ClassNotFoundException {
		return AbstractFactory.getClassesForTypes(argTypes, args, cl);
	}

	protected ISharedObject makeSharedObjectInstance(final Class newClass,
			final Class[] argTypes, final Object[] args) throws Exception {
		Object newObject = null;
		try {
			newObject = AccessController
					.doPrivileged(new PrivilegedExceptionAction() {
						public Object run() throws Exception {
							Constructor aConstructor = newClass
									.getConstructor(argTypes);
							aConstructor.setAccessible(true);
							return aConstructor.newInstance(args);
						}
					});
		} catch (java.security.PrivilegedActionException e) {
			throw e.getException();
		}
		return verifySharedObject(newObject);
	}

	protected ISharedObject verifySharedObject(Object newSharedObject) {
		if (newSharedObject instanceof ISharedObject)
			return (ISharedObject) newSharedObject;
		else
			throw new ClassCastException("Object " + newSharedObject.toString()
					+ " does not implement " + ISharedObject.class.getName());
	}

	protected ISharedObject loadSharedObject(SharedObjectDescription sd)
			throws Exception {
		// First get classloader
		ClassLoader cl = container.getClassLoaderForSharedObject(sd);
		// Then get args array from properties
		Object[] args = container.getArgsFromProperties(sd);
		// And arg types
		String[] types = container.getArgTypesFromProperties(sd);
		Class[] argTypes = getArgTypes(types, args, cl);
		// Now load top-level class
		final Class newClass = Class.forName(sd.getClassname(), true, cl);
		return makeSharedObjectInstance(newClass, argTypes, args);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#getSharedObjectIDs()
	 */
	public ID[] getSharedObjectIDs() {
		return container.getSharedObjectIDs();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#createSharedObject(org.eclipse.ecf.core.SharedObjectDescription,
	 *      org.eclipse.ecf.core.ISharedObjectContainerTransaction)
	 */
	public ID createSharedObject(SharedObjectDescription sd)
			throws SharedObjectCreateException {
		debug("createSharedObject(" + sd + ")");
		// notify listeners
		if (sd == null)
			throw new SharedObjectCreateException(
					"SharedObjectDescription cannot be null");
		ID sharedObjectID = sd.getID();
		if (sharedObjectID == null)
			throw new SharedObjectCreateException(
					"New object ID cannot be null");
		container.fireContainerEvent(new SharedObjectManagerCreateEvent(
				container.getID(), sd));
		ISharedObject newObject = null;
		ID result = sharedObjectID;
		try {
			newObject = loadSharedObject(sd);
			result = addSharedObject(sharedObjectID, newObject, sd
					.getProperties());
		} catch (Exception e) {
			dumpStack("Exception in createSharedObject", e);
			SharedObjectCreateException newExcept = new SharedObjectCreateException(
					"Container " + container.getID()
							+ " had exception creating shared object "
							+ sharedObjectID + ": " + e.getClass().getName()
							+ ": " + e.getMessage());
			newExcept.setStackTrace(e.getStackTrace());
			throw newExcept;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#addSharedObject(org.eclipse.ecf.core.identity.ID,
	 *      org.eclipse.ecf.core.ISharedObject, java.util.Map,
	 *      org.eclipse.ecf.core.ISharedObjectContainerTransaction)
	 */
	public ID addSharedObject(ID sharedObjectID, ISharedObject sharedObject,
			Map properties) throws SharedObjectAddException {
		debug("addSharedObject(" + sharedObjectID + "," + sharedObject + ","
				+ properties + ")");
		// notify listeners
		container.fireContainerEvent(new SharedObjectManagerAddEvent(container
				.getID(), sharedObjectID, sharedObject, properties));
		ID result = sharedObjectID;
		try {
			ISharedObject so = sharedObject;
			SharedObjectDescription sd = new SharedObjectDescription(
					sharedObject.getClass().getClassLoader(), sharedObjectID,
					container.getID(), sharedObject.getClass().getName(),
					properties, 0);
			container.addSharedObjectAndWait(sd, so);
		} catch (Exception e) {
			dumpStack("Exception in addSharedObject", e);
			SharedObjectAddException newExcept = new SharedObjectAddException(
					"Container " + container.getID()
							+ " had exception adding shared object "
							+ sharedObjectID + ": " + e.getClass().getName()
							+ ": " + e.getMessage());
			newExcept.setStackTrace(e.getStackTrace());
			throw newExcept;
		}
		// notify listeners
		container.fireContainerEvent(new SharedObjectActivatedEvent(container
				.getID(), result, container.getGroupMemberIDs()));
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#getSharedObject(org.eclipse.ecf.core.identity.ID)
	 */
	public ISharedObject getSharedObject(ID sharedObjectID) {
		return container.getSharedObject(sharedObjectID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#removeSharedObject(org.eclipse.ecf.core.identity.ID)
	 */
	public ISharedObject removeSharedObject(ID sharedObjectID) {
		debug("removeSharedObject(" + sharedObjectID + ")");
		// notify listeners
		container.fireContainerEvent(new SharedObjectManagerRemoveEvent(
				container.getID(), sharedObjectID));
		return container.removeSharedObject(sharedObjectID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#connectSharedObjects(org.eclipse.ecf.core.identity.ID,
	 *      org.eclipse.ecf.core.identity.ID[])
	 */
	public ISharedObjectConnector connectSharedObjects(ID sharedObjectFrom,
			ID[] sharedObjectsTo) throws SharedObjectConnectException {
		debug("connectSharedObjects(" + sharedObjectFrom + ","
				+ sharedObjectsTo + ")");
		// notify listeners
		container.fireContainerEvent(new SharedObjectManagerConnectEvent(
				container.getID(), sharedObjectFrom, sharedObjectsTo));
		if (sharedObjectFrom == null)
			throw new SharedObjectConnectException("sender cannot be null");
		if (sharedObjectsTo == null)
			throw new SharedObjectConnectException("receivers cannot be null");
		ISharedObjectConnector result = null;
		synchronized (container.getGroupMembershipLock()) {
			// Get from to make sure it's there
			SOWrapper wrap = container.getSharedObjectWrapper(sharedObjectFrom);
			if (wrap == null)
				throw new SharedObjectConnectException("sender object "
						+ sharedObjectFrom.getName() + " not found");
			IQueueEnqueue[] queues = new IQueueEnqueue[sharedObjectsTo.length];
			for (int i = 0; i < sharedObjectsTo.length; i++) {
				SOWrapper w = container
						.getSharedObjectWrapper(sharedObjectsTo[i]);
				if (w == null)
					throw new SharedObjectConnectException("receiver object "
							+ sharedObjectsTo[i].getName() + " not found");
				queues[i] = new QueueEnqueueImpl(w.getQueue());
			}
			// OK now we've got ids and wrappers, make a connector
			result = new SOConnector(sharedObjectFrom, sharedObjectsTo, queues);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#disconnectSharedObjects(org.eclipse.ecf.core.ISharedObjectConnector)
	 */
	public void disconnectSharedObjects(ISharedObjectConnector connector)
			throws SharedObjectDisconnectException {
		if (connector != null) {
			debug("disconnectSharedObjects(" + connector.getSender() + ")");
			// notify listeners
			container
					.fireContainerEvent(new SharedObjectManagerDisconnectEvent(
							container.getID(), connector.getSender()));
		}
		if (connector == null)
			throw new SharedObjectDisconnectException("connect cannot be null");
		if (!removeConnector(connector)) {
			throw new SharedObjectDisconnectException("connector " + connector
					+ " not found");
		}
		connector.dispose();
	}

	protected void dispose() {
		debug("dispose()");
		for (Enumeration e = connectors.elements(); e.hasMoreElements();) {
			ISharedObjectConnector conn = (ISharedObjectConnector) e
					.nextElement();
			conn.dispose();
		}
		connectors.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectManager#getSharedObjectConnectors(org.eclipse.ecf.core.identity.ID)
	 */
	public List getSharedObjectConnectors(ID sharedObjectFrom) {
		debug("getSharedObjectConnectors(" + sharedObjectFrom + ")");
		List results = new ArrayList();
		for (Enumeration e = connectors.elements(); e.hasMoreElements();) {
			ISharedObjectConnector conn = (ISharedObjectConnector) e
					.nextElement();
			if (sharedObjectFrom.equals(conn.getSender())) {
				results.add(conn);
			}
		}
		return results;
	}

	public static Class[] getClassesForTypes(String[] argTypes, Object[] args,
			ClassLoader cl) throws ClassNotFoundException {
		Class clazzes[] = null;
		if (args == null || args.length == 0)
			clazzes = new Class[0];
		else if (argTypes != null) {
			clazzes = new Class[argTypes.length];
			for (int i = 0; i < argTypes.length; i++) {
				clazzes[i] = Class.forName(argTypes[i], true, cl);
			}
		} else {
			clazzes = new Class[args.length];
			for (int i = 0; i < args.length; i++) {
				if (args[i] == null)
					clazzes[i] = null;
				else
					clazzes[i] = args[i].getClass();
			}
		}
		return clazzes;
	}

	public void setRemoteAddPolicy(ISharedObjectPolicy policy) {
		container.setRemoteAddPolicy(policy);
	}
}

Back to the top