Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0c05b7a55b041979abbc83595152f9a79cfa3ae9 (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
/*******************************************************************************
 * 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.identity;

import java.security.AccessController;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.internal.core.identity.Activator;
import org.eclipse.ecf.internal.core.identity.IdentityDebugOptions;
import org.eclipse.ecf.internal.core.identity.Messages;
import org.eclipse.osgi.util.NLS;

/**
 * A factory class for creating ID instances. This is the factory for plugins to
 * manufacture ID instances.
 * 
 */
public class IDFactory implements IIDFactory {
	public static final String SECURITY_PROPERTY = IDFactory.class.getName() + ".security"; //$NON-NLS-1$

	private static final int IDENTITY_CREATION_ERRORCODE = 2001;

	private static Hashtable namespaces = new Hashtable();

	private static boolean securityEnabled = false;

	protected static IIDFactory instance = null;

	static {
		instance = new IDFactory();
		addNamespace0(new StringID.StringIDNamespace());
		addNamespace0(new GUID.GUIDNamespace());
		addNamespace0(new LongID.LongNamespace());
	}

	protected IDFactory() {
	}

	public static IIDFactory getDefault() {
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#addNamespace(org.eclipse.ecf.core.identity.Namespace)
	 */
	public Namespace addNamespace(Namespace namespace) throws SecurityException {
		if (namespace == null)
			return null;
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "addNamespace", namespace); //$NON-NLS-1$
		checkPermission(new NamespacePermission(namespace.toString(), NamespacePermission.ADD_NAMESPACE));
		Namespace result = addNamespace0(namespace);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "addNamespace", result); //$NON-NLS-1$
		return result;
	}

	protected final static Namespace addNamespace0(Namespace namespace) {
		if (namespace == null)
			return null;
		return (Namespace) namespaces.put(namespace.getName(), namespace);
	}

	protected final static void checkPermission(NamespacePermission namespacepermission) throws SecurityException {
		if (securityEnabled)
			AccessController.checkPermission(namespacepermission);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#containsNamespace(org.eclipse.ecf.core.identity.Namespace)
	 */
	public boolean containsNamespace(Namespace namespace) throws SecurityException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "containsNamespace", namespace); //$NON-NLS-1$
		if (namespace == null)
			return false;
		checkPermission(new NamespacePermission(namespace.toString(), NamespacePermission.CONTAINS_NAMESPACE));
		boolean result = containsNamespace0(namespace);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "containsNamespace", new Boolean(result)); //$NON-NLS-1$
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#getNamespaces()
	 */
	public List getNamespaces() {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "getNamespaces"); //$NON-NLS-1$
		return new ArrayList(namespaces.values());
	}

	protected final static boolean containsNamespace0(Namespace n) {
		if (n == null)
			return false;
		return namespaces.containsKey(n.getName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#getNamespace(org.eclipse.ecf.core.identity.Namespace)
	 */
	public Namespace getNamespace(Namespace namespace) throws SecurityException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "getNamespace", namespace); //$NON-NLS-1$
		if (namespace == null)
			return null;
		checkPermission(new NamespacePermission(namespace.toString(), NamespacePermission.GET_NAMESPACE));
		Namespace result = getNamespace0(namespace);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "getNamespace", result); //$NON-NLS-1$
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#getNamespaceByName(java.lang.String)
	 */
	public Namespace getNamespaceByName(String name) throws SecurityException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "getNamespaceByName", name); //$NON-NLS-1$
		Namespace result = getNamespace0(name);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "getNamespaceByName", result); //$NON-NLS-1$
		return result;
	}

	protected final static Namespace getNamespace0(Namespace n) {
		if (n == null)
			return null;
		return (Namespace) namespaces.get(n.getName());
	}

	protected final static Namespace getNamespace0(String name) {
		if (name == null)
			return null;
		else
			return (Namespace) namespaces.get(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createGUID()
	 */
	public ID createGUID() throws IDCreateException {
		return createGUID(GUID.DEFAULT_BYTE_LENGTH);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createGUID(int)
	 */
	public ID createGUID(int length) throws IDCreateException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "createGUID", new Integer(length)); //$NON-NLS-1$
		Namespace namespace = new GUID.GUIDNamespace();
		ID result = createID(namespace, new Integer[] {new Integer(length)});
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "createGUID", result); //$NON-NLS-1$
		return result;
	}

	protected static void logAndThrow(String s, Throwable t) throws IDCreateException {
		IDCreateException e = null;
		if (t != null) {
			e = new IDCreateException(s + ": " + t.getClass().getName() + ": " //$NON-NLS-1$ //$NON-NLS-2$
					+ t.getMessage(), t);
		} else {
			e = new IDCreateException(s);
		}
		Trace.throwing(Activator.PLUGIN_ID, IdentityDebugOptions.EXCEPTIONS_THROWING, IDFactory.class, "logAndThrow", e); //$NON-NLS-1$
		Activator.getDefault().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IDENTITY_CREATION_ERRORCODE, s, e));
		throw e;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createID(org.eclipse.ecf.core.identity.Namespace,
	 *      java.lang.Object[])
	 */
	public ID createID(Namespace n, Object[] args) throws IDCreateException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "createID", new Object[] {n, Trace.getArgumentsString(args)}); //$NON-NLS-1$
		// Verify namespace is non-null
		if (n == null)
			logAndThrow(Messages.IDFactory_Namespace_Not_Null, null);
		// Make sure that namespace is in table of known namespace. If not,
		// throw...we don't create any instances that we don't know about!
		Namespace ns = getNamespace0(n);
		if (ns == null)
			logAndThrow(NLS.bind(Messages.IDFactory_Namespace_Not_Found, n.getName()), null);
		// We're OK, go ahead and setup array of classes for call to
		// instantiator
		// Ask instantiator to actually create instance
		ID result = ns.createInstance(args);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "createID", result); //$NON-NLS-1$
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createID(java.lang.String,
	 *      java.lang.Object[])
	 */
	public ID createID(String namespaceName, Object[] args) throws IDCreateException {
		Namespace n = getNamespaceByName(namespaceName);
		if (n == null)
			throw new IDCreateException(NLS.bind(Messages.IDFactory_Namespace_Not_Found, namespaceName));
		return createID(n, args);
	}

	public ID createID(Namespace namespace, String uri) throws IDCreateException {
		return createID(namespace, new Object[] {uri});
	}

	public ID createID(String namespace, String uri) throws IDCreateException {
		return createID(namespace, new Object[] {uri});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createStringID(java.lang.String)
	 */
	public ID createStringID(String idstring) throws IDCreateException {
		if (idstring == null)
			throw new IDCreateException(Messages.IDFactory_StringID_Not_Null);
		Namespace n = new StringID.StringIDNamespace();
		return createID(n, new String[] {idstring});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#createLongID(long)
	 */
	public ID createLongID(long l) throws IDCreateException {
		Namespace n = new LongID.LongNamespace();
		return createID(n, new Long[] {new Long(l)});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.IIDFactory#removeNamespace(org.eclipse.ecf.core.identity.Namespace)
	 */
	public Namespace removeNamespace(Namespace n) throws SecurityException {
		Trace.entering(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_ENTERING, IDFactory.class, "removeNamespace", n); //$NON-NLS-1$
		if (n == null)
			return null;
		checkPermission(new NamespacePermission(n.toString(), NamespacePermission.REMOVE_NAMESPACE));
		Namespace result = removeNamespace0(n);
		Trace.exiting(Activator.PLUGIN_ID, IdentityDebugOptions.METHODS_EXITING, IDFactory.class, "removeNamespace", result); //$NON-NLS-1$
		return result;
	}

	protected final static Namespace removeNamespace0(Namespace n) {
		if (n == null)
			return null;
		return (Namespace) namespaces.remove(n.getName());
	}
}

Back to the top