Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2e2c6f53288859647dfb4caf99e5562fcc4bc816 (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
/*******************************************************************************
 * 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.provider.remoteservice.generic;

import java.io.Serializable;
import java.util.*;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.remoteservice.*;

public class RemoteServiceRegistryImpl implements Serializable {

	private static long nextServiceId = 1;

	private static final long serialVersionUID = -291866447335444599L;

	protected static final String REMOTEOBJECTCLASS = Constants.OBJECTCLASS;

	protected static final String REMOTESERVICE_ID = Constants.SERVICE_ID;

	protected static final String REMOTESERVICE_RANKING = Constants.SERVICE_RANKING;

	protected ID containerID;

	public RemoteServiceRegistryImpl() {
		//
	}

	/**
	 * Published services by class name. Key is a String class name; Value is a
	 * ArrayList of IRemoteServiceRegistrations
	 */
	protected HashMap publishedServicesByClass = new HashMap(50);

	/** All published services */
	protected ArrayList allPublishedServices = new ArrayList(50);

	public RemoteServiceRegistryImpl(ID localContainerID) {
		this();
		this.containerID = localContainerID;
	}

	protected long getNextServiceId() {
		return nextServiceId++;
	}

	public ID getContainerID() {
		return containerID;
	}

	public void publishService(RemoteServiceRegistrationImpl serviceReg) {

		// Add the ServiceRegistration to the list of Services published by
		// Class Name.
		final String[] clazzes = (String[]) serviceReg.getReference().getProperty(REMOTEOBJECTCLASS);
		final int size = clazzes.length;

		for (int i = 0; i < size; i++) {
			final String clazz = clazzes[i];

			ArrayList services = (ArrayList) publishedServicesByClass.get(clazz);

			if (services == null) {
				services = new ArrayList(10);
				publishedServicesByClass.put(clazz, services);
			}

			services.add(serviceReg);
		}

		// Add the ServiceRegistration to the list of all published Services.
		allPublishedServices.add(serviceReg);
	}

	public void unpublishService(RemoteServiceRegistrationImpl serviceReg) {

		// Remove the ServiceRegistration from the list of Services published by
		// Class Name.
		final String[] clazzes = (String[]) serviceReg.getReference().getProperty(REMOTEOBJECTCLASS);
		final int size = clazzes.length;

		for (int i = 0; i < size; i++) {
			final String clazz = clazzes[i];
			final ArrayList services = (ArrayList) publishedServicesByClass.get(clazz);
			services.remove(serviceReg);
		}

		// Remove the ServiceRegistration from the list of all published
		// Services.
		allPublishedServices.remove(serviceReg);

	}

	public void unpublishServices() {
		publishedServicesByClass.clear();
		allPublishedServices.clear();
	}

	public IRemoteServiceReference[] lookupServiceReferences(String clazz, IRemoteFilter filter) {
		int size;
		ArrayList references;
		ArrayList serviceRegs;
		if (clazz == null) {
			serviceRegs = allPublishedServices;
		} else {
			/* services registered under the class name */
			serviceRegs = (ArrayList) publishedServicesByClass.get(clazz);
		}

		if (serviceRegs == null) {
			return (null);
		}

		size = serviceRegs.size();

		if (size == 0) {
			return (null);
		}

		references = new ArrayList(size);
		for (int i = 0; i < size; i++) {
			final IRemoteServiceRegistration registration = (IRemoteServiceRegistration) serviceRegs.get(i);

			final IRemoteServiceReference reference = registration.getReference();
			if ((filter == null) || filter.match(reference)) {
				// Must be RemoteServiceReferenceImpl
				final RemoteServiceReferenceImpl impl = (RemoteServiceReferenceImpl) reference;
				impl.setRemoteClass(clazz);
				references.add(reference);
			}
		}

		if (references.size() == 0) {
			return null;
		}

		return (IRemoteServiceReference[]) references.toArray(new RemoteServiceReferenceImpl[references.size()]);

	}

	public IRemoteServiceReference[] lookupServiceReferences() {
		int size;
		ArrayList references;
		size = allPublishedServices.size();

		if (size == 0) {
			return (null);
		}

		references = new ArrayList(size);
		for (int i = 0; i < size; i++) {
			final IRemoteServiceRegistration registration = (IRemoteServiceRegistration) allPublishedServices.get(i);

			final IRemoteServiceReference reference = registration.getReference();
			references.add(reference);
		}

		if (references.size() == 0) {
			return null;
		}

		return (IRemoteServiceReference[]) references.toArray(new RemoteServiceReferenceImpl[references.size()]);
	}

	protected RemoteServiceRegistrationImpl[] getRegistrations() {
		return (RemoteServiceRegistrationImpl[]) allPublishedServices.toArray(new RemoteServiceRegistrationImpl[allPublishedServices.size()]);
	}

	protected RemoteServiceRegistrationImpl findRegistrationForServiceId(long serviceId) {
		for (final Iterator i = allPublishedServices.iterator(); i.hasNext();) {
			final RemoteServiceRegistrationImpl reg = (RemoteServiceRegistrationImpl) i.next();
			if (serviceId == reg.getServiceId()) {
				return reg;
			}
		}
		return null;
	}

	/**
	 * @since 3.0
	 */
	protected RemoteServiceRegistrationImpl findRegistrationForRemoteServiceId(IRemoteServiceID remoteServiceID) {
		for (final Iterator i = allPublishedServices.iterator(); i.hasNext();) {
			final RemoteServiceRegistrationImpl reg = (RemoteServiceRegistrationImpl) i.next();
			if (remoteServiceID.equals(reg.getID()))
				return reg;
		}
		return null;
	}

	public String toString() {
		final StringBuffer buf = new StringBuffer("RemoteServiceRegistryImpl["); //$NON-NLS-1$
		buf.append("all=").append(allPublishedServices).append(";").append("byclass=").append(publishedServicesByClass).append("]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		return buf.toString();
	}

	/**
	 * @since 3.0
	 */
	public IRemoteServiceID createRemoteServiceID(long serviceid) {
		return (IRemoteServiceID) IDFactory.getDefault().createID(IDFactory.getDefault().getNamespaceByName(RemoteServiceNamespace.NAME), new Object[] {containerID, new Long(serviceid)});
	}

}

Back to the top