Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf4a76342ebe3e9c568265fbb3323c83663cb3b3 (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
/*******************************************************************************
 * Copyright (c) 1997-2009 by ProSyst Software GmbH
 * http://www.prosyst.com
 * 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:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.ds.impl;

import java.util.*;
import org.eclipse.equinox.internal.ds.*;
import org.eclipse.equinox.internal.ds.model.ServiceComponentProp;
import org.eclipse.equinox.internal.util.ref.Log;
import org.osgi.service.component.*;

/**
 * ComponentFactoryImpl.java
 * 
 * @author Valentin Valchev
 * @author Stoyan Boshev
 * @author Pavlin Dobrev
 */

public class ComponentFactoryImpl implements ComponentFactory {

	static boolean security = false;

	private ServiceComponentProp sci;

	/**
	 * ComponentFactoryImpl
	 * 
	 * @param component
	 *            the ComponentDescription Object with Properties
	 */
	public ComponentFactoryImpl(ServiceComponentProp component) {
		security = Log.security();
		this.sci = component;
	}

	/**
	 * Create a new instance of the component. Additional properties may be
	 * provided for the component instance.
	 * 
	 * @param properties
	 *            Additional properties for the component instance.
	 * @return A ComponentInstance object encapsulating the component instance.
	 *         The returned component instance has been activated.
	 */
	public ComponentInstance newInstance(Dictionary additionalProps) {
		ComponentInstanceImpl instance = null;
		try {
			if (Activator.DEBUG) {
				Activator.log.debug("ComponentFactoryImpl.newInstance(): " + sci.name, null); //$NON-NLS-1$
			}

			// merge properties
			Hashtable props = (Hashtable) sci.getProperties().clone();
			SCRUtil.copyTo(props, additionalProps);

			// create a new SCP (adds to resolver scpEnabled list)
			ServiceComponentProp newSCP = InstanceProcess.resolver.mapNewFactoryComponent(sci.serviceComponent, props);

			// register the component and make instance if immediate
			Vector toBuild = new Vector(1);
			toBuild.addElement(newSCP);
			InstanceProcess.staticRef.buildComponents(toBuild, security);
			if (!newSCP.instances.isEmpty()) {
				// an instance was built because the component is either
				// immediate
				// or someone has got it as service (if provides one)
				instance = (ComponentInstanceImpl) newSCP.instances.firstElement();
			}
			if (instance == null) {
				// finally build an instance if not done yet
				instance = InstanceProcess.staticRef.buildComponent(null, newSCP, null, security);
			}
			instance.factory = this;
		} catch (Throwable e) {
			if (e instanceof ComponentException) {
				throw (ComponentException) e;
			}
			Activator.log.error("ComponentFactoryImpl.newInstance(): failed for " + sci.name + " with properties " + additionalProps, e); //$NON-NLS-1$ //$NON-NLS-2$
			throw new ComponentException(Messages.COULD_NOT_CREATE_NEW_INSTANCE, e);
		}
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "ComponentFactory for " + sci.name; //$NON-NLS-1$
	}

}

Back to the top