Skip to main content
summaryrefslogtreecommitdiffstats
blob: abc144c1679458e415a67de3428391a11fae78b2 (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
/*******************************************************************************
 * 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.sharedobject;

import java.util.*;
import org.eclipse.ecf.core.sharedobject.provider.ISharedObjectInstantiator;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.internal.core.sharedobject.*;

/**
 * Factory for creating {@link ISharedObject} instances. This class provides ECF
 * clients an entry point to constructing {@link ISharedObject} instances. <br>
 */
public class SharedObjectFactory implements ISharedObjectFactory {

	private static Hashtable sharedobjectdescriptions = new Hashtable();

	protected static ISharedObjectFactory instance = null;

	static {
		instance = new SharedObjectFactory();
	}

	protected SharedObjectFactory() {
		// null constructor
	}

	public static ISharedObjectFactory getDefault() {
		return instance;
	}

	private static void trace(String msg) {
		Trace.trace(Activator.PLUGIN_ID, msg);
	}

	private static void dumpStack(String msg, Throwable e) {
		Trace.catching(Activator.PLUGIN_ID, SharedObjectDebugOptions.EXCEPTIONS_CATCHING, SharedObjectFactory.class, "dumpStack", e); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectFactory#addDescription(org.eclipse.ecf.core.SharedObjectTypeDescription)
	 */
	public SharedObjectTypeDescription addDescription(SharedObjectTypeDescription description) {
		trace("addDescription(" + description + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		return addDescription0(description);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectFactory#getDescriptions()
	 */
	public List getDescriptions() {
		return getDescriptions0();
	}

	protected List getDescriptions0() {
		return new ArrayList(sharedobjectdescriptions.values());
	}

	protected SharedObjectTypeDescription addDescription0(SharedObjectTypeDescription n) {
		if (n == null)
			return null;
		return (SharedObjectTypeDescription) sharedobjectdescriptions.put(n.getName(), n);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectFactory#containsDescription(org.eclipse.ecf.core.SharedObjectTypeDescription)
	 */
	public boolean containsDescription(SharedObjectTypeDescription scd) {
		return containsDescription0(scd);
	}

	protected boolean containsDescription0(SharedObjectTypeDescription scd) {
		if (scd == null)
			return false;
		return sharedobjectdescriptions.containsKey(scd.getName());
	}

	protected SharedObjectTypeDescription getDescription0(SharedObjectTypeDescription scd) {
		if (scd == null)
			return null;
		return (SharedObjectTypeDescription) sharedobjectdescriptions.get(scd.getName());
	}

	protected SharedObjectTypeDescription getDescription0(String name) {
		if (name == null)
			return null;
		return (SharedObjectTypeDescription) sharedobjectdescriptions.get(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectContainerFactory#getDescriptionByName(java.lang.String)
	 */
	public SharedObjectTypeDescription getDescriptionByName(String name) throws SharedObjectCreateException {
		trace("getDescriptionByName(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		SharedObjectTypeDescription res = getDescription0(name);
		if (res == null) {
			//throw new SharedObjectCreateException(Messages.SharedObjectFactory_Exception_Create_Shared_Object + name + Messages.SharedObjectFactory_Exception_Create_Shared_Object_Not_Found);
			throw new SharedObjectCreateException(Messages.bind(Messages.SharedObjectFactory_SharedObjectCreateException_X_Not_Found, name));
		}
		return res;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectContainerFactory#createSharedObject(org.eclipse.ecf.core.SharedObjectTypeDescription,
	 *      java.lang.Object[])
	 */
	public ISharedObject createSharedObject(SharedObjectTypeDescription desc, Object[] args) throws SharedObjectCreateException {
		trace("createSharedObject(" + desc + "," //$NON-NLS-1$ //$NON-NLS-2$
				+ Trace.getArgumentsString(args) + ")"); //$NON-NLS-1$
		if (desc == null)
			throw new SharedObjectCreateException(Messages.SharedObjectFactory_Description_Not_Null);
		SharedObjectTypeDescription cd = getDescription0(desc);
		if (cd == null)
			//throw new SharedObjectCreateException(Messages.SharedObjectFactory_Exception_Create_Shared_Objec + desc.getName() + Messages.SharedObjectFactory_Exception_Create_Shared_Object_Not_Found);
			throw new SharedObjectCreateException(Messages.bind(Messages.SharedObjectFactory_SharedObjectDescription_X_Not_Found, desc.getName()));
		ISharedObjectInstantiator instantiator = null;
		try {
			instantiator = cd.getInstantiator();
		} catch (Exception e) {
			SharedObjectCreateException newexcept = new SharedObjectCreateException(Messages.SharedObjectFactory_Exception_Create_With_Description + desc + ": " + e.getClass().getName() + ": " //$NON-NLS-1$ //$NON-NLS-2$
					+ e.getMessage(), e);
			dumpStack("Exception in createSharedObject", newexcept); //$NON-NLS-1$
			throw newexcept;
		}
		if (instantiator == null)
			//throw new SharedObjectCreateException(Messages.SharedObjectFactory_Exception_Create_Instantiator + cd.getName() + Messages.SharedObjectFactory_Exception_Create_Instantiator_Null);
			throw new SharedObjectCreateException(Messages.bind(Messages.SharedObjectFactory_Exception_Create_Instantiator_X_Null, cd.getName()));
		// Ask instantiator to actually create instance
		return instantiator.createInstance(desc, args);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectContainerFactory#createSharedObject(java.lang.String)
	 */
	public ISharedObject createSharedObject(String descriptionName) throws SharedObjectCreateException {
		return createSharedObject(getDescriptionByName(descriptionName), null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectContainerFactory#createSharedObject(java.lang.String,
	 *      java.lang.Object[])
	 */
	public ISharedObject createSharedObject(String descriptionName, Object[] args) throws SharedObjectCreateException {
		return createSharedObject(getDescriptionByName(descriptionName), args);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObjectContainerFactory#removeDescription(org.eclipse.ecf.core.SharedObjectTypeDescription)
	 */
	public SharedObjectTypeDescription removeDescription(SharedObjectTypeDescription scd) {
		trace("removeDescription(" + scd + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		return removeDescription0(scd);
	}

	protected SharedObjectTypeDescription removeDescription0(SharedObjectTypeDescription n) {
		if (n == null)
			return null;
		return (SharedObjectTypeDescription) sharedobjectdescriptions.remove(n.getName());
	}
}

Back to the top