/*
 * $Header$
 *
 * Copyright (c) IBM Corporation (2005)
 *
 * These materials have been contributed  to the OSGi Alliance as 
 * "MEMBER LICENSED MATERIALS" as defined in, and subject to the terms of, 
 * the OSGi Member Agreement, specifically including but not limited to, 
 * the license rights and warranty disclaimers as set forth in Sections 3.2 
 * and 12.1 thereof, and the applicable Statement of Work. 
 *
 * All company, brand and product names contained within this document may be 
 * trademarks that are the sole property of the respective owners.
 */
package org.eclipse.osgi.impl.service.component;

import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.osgi.component.Main;
import org.eclipse.osgi.component.model.ComponentDescriptionProp;
import org.osgi.service.component.ComponentException;
import org.osgi.service.component.ComponentFactory;
import org.osgi.service.component.ComponentInstance;

/**
 * When a component is declared with the <code>factory</code> attribute on its
 * <code>component</code> element, the Service Component Runtime will register
 * a ComponentFactory service to allow new component configurations to be
 * created and activated rather than automatically creating and activating
 * component configuration as necessary.
 * 
 * @version $Revision$
 */
public class ComponentFactoryImpl implements ComponentFactory {

	/* set this to true to compile in debug messages */
	static final boolean				DEBUG	= true;

	private ComponentDescriptionProp	cdp;
	private Main						main;

	/**
	 * ComponentFactoryImpl
	 * 
	 * @param context the SC bundle context
	 * @param componentDescriptionProp the ComponentDescription Object with
	 *        Properties
	 * @param buildDispose
	 */
	public ComponentFactoryImpl(ComponentDescriptionProp cdp, Main main) {
		this.cdp = cdp;
		this.main = main;
	}

	/**
	 * Create and activate a new component configuration. Additional properties
	 * may be provided for the component configuration.
	 * 
	 * @param properties Additional properties for the component configuration.
	 * @return A ComponentInstance object encapsulating the component
	 *         configuration. The returned component configuration has been
	 *         activated and, if the component specifies a <code>service</code>
	 *         element, the component configuration has been registered as a
	 *         service.
	 * @throws ComponentException If the Service Component Runtime is unable to
	 *         activate the component configuration.
	 */
	public ComponentInstance newInstance(Dictionary newProperties) {

		// merge properties
		Hashtable properties = (Hashtable) cdp.getProperties().clone();
		Enumeration propsEnum = newProperties.keys();
		while (propsEnum.hasMoreElements()) {
			Object key = propsEnum.nextElement();
			properties.put(key, newProperties.get(key));
		}

		// create a new cdp (adds to resolver enabledCDPs list)
		ComponentDescriptionProp newCDP = main.resolver.mapFactoryInstance(cdp
				.getComponentDescription(), properties);

		// try to resolve new cdp - adds to resolver's satisfied list
		if (!main.resolver.justResolve(newCDP)) {
			main.resolver.enabledCDPs.remove(newCDP); // was added by
														// mapFactoryInstance
			throw new ComponentException("Could not resolve instance of " + cdp
					+ " with properties " + properties);
		}

		// if new cdp resolves, send it to instance process (will register
		// service
		// if it has one)
		main.resolver.instanceProcess.buildInstances(Collections
				.singletonList(newCDP));

		// get instance of new cdp to return

		if (newCDP.getComponentDescription().isImmediate()) {
			// if cdp is immediate then instanceProcess created one
			return (ComponentInstance) newCDP.getInstances().get(0);
		}

		return main.resolver.instanceProcess.buildDispose.build(null, newCDP);
	}
}

@@ -1009,17 +1009,25 @@
  When a 
-c
+Service C
 omponent
@@ -1083,17 +1083,18 @@
 ute 
+%0A * 
 on its
-%0A *
  %3Cco
@@ -1157,24 +1157,28 @@
 me will 
+%0A * 
 register
 %0A * a Co
@@ -1169,19 +1169,16 @@
 register
-%0A *
  a Compo
@@ -1239,16 +1239,17 @@
 ons 
+%0A * 
 to be
-%0A *
  cre
@@ -1270,98 +1270,8 @@
 ated
- rather than automatically creating and activating%0A * component configuration as necessary
 .%0A *
@@ -2106,29 +2106,35 @@
 ulating 
-the component
+an instance of the 
 %0A%09 *    
@@ -2138,16 +2138,26 @@
         
+component 
 configur
@@ -2176,17 +2176,17 @@
 eturned 
-c
+C
 omponent
@@ -2178,33 +2178,33 @@
 urned Component 
-c
+C
 onfiguration has
@@ -2200,24 +2200,16 @@
 uration 
-has been
 %0A%09 *    
@@ -2205,32 +2205,50 @@
 on %0A%09 *         
+instance has been 
 activated and, i
@@ -2253,25 +2253,46 @@
  if the 
-component
+Service Component %0A%09 *        
  specifi
@@ -2316,28 +2316,16 @@
 e%3C/code%3E
-%0A%09 *        
  element
@@ -2326,25 +2326,25 @@
 lement, the 
-c
+C
 omponent con
@@ -2332,33 +2332,46 @@
 , the Component 
-c
+%0A%09 *         C
 onfiguration has
@@ -2391,28 +2391,16 @@
 red as a
-%0A%09 *        
  service
@@ -2499,25 +2499,25 @@
 ctivate the 
-c
+C
 omponent con
@@ -2505,33 +2505,33 @@
 e the Component 
-c
+C
 onfiguration.%0A%09 
@@ -2522,24 +2522,33 @@
 onfiguration
+ instance
 .%0A%09 */%0A%09publ

update javadoc comments