01: /**********************************************************************
02: * Copyright (c) 2004, 2006 Eclipse Foundation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Gunnar Wagenknecht - Initial API and implementation
10: * IBM - Initial API and implementation
11: **********************************************************************/package org.eclipse.pde.internal.build;
12:
13: import java.util.*;
14: import org.eclipse.core.runtime.*;
15: import org.eclipse.pde.build.IFetchFactory;
16:
17: /**
18: * A registry for acessing fetch task factories.
19: * @since 3.2
20: */
21: public class FetchTaskFactoriesRegistry implements IPDEBuildConstants {
22:
23: // Map of registered factories. key: factoryId, value: configuration element or corresponding instance
24: private Map factories;
25:
26: public FetchTaskFactoriesRegistry() {
27: factories = new HashMap();
28: initializeRegistry();
29: }
30:
31: /**
32: * Returns the factory instance with the specified id.
33: * <p>
34: * The instance is not cached. Each time this method is called, a new
35: * instance is created.
36: * </p>
37: *
38: * @param id
39: * @return the factory instance (maybe <code>null</code>)
40: */
41: public IFetchFactory getFactory(String id) {
42: Object result = factories.get(id);
43: if (result instanceof IFetchFactory)
44: return (IFetchFactory) result;
45:
46: IConfigurationElement extension = (IConfigurationElement) factories
47: .get(id);
48: if (null != extension) {
49: try {
50: IFetchFactory toCache = (IFetchFactory) extension
51: .createExecutableExtension(ATTR_CLASS);
52: factories.put(id, toCache);
53: return toCache;
54: } catch (CoreException e) {
55: BundleHelper.getDefault().getLog().log(e.getStatus());
56: }
57: }
58: return null;
59: }
60:
61: /**
62: * Returns a collection of registered factory ids.
63: *
64: * @return a collection of registered factory ids
65: */
66: public Collection getFactoryIds() {
67: return factories.keySet();
68: }
69:
70: /**
71: * Initializes the registry
72: */
73: private void initializeRegistry() {
74: IConfigurationElement[] extensions = Platform
75: .getExtensionRegistry().getConfigurationElementsFor(
76: EXT_FETCH_TASK_FACTORIES);
77: for (int i = 0; i < extensions.length; i++) {
78: IConfigurationElement extension = extensions[i];
79: if (ELEM_FACTORY.equals(extension.getName())) {
80: String id = extension.getAttribute(ATTR_ID);
81: if (null != id && id.trim().length() > 0) {
82: factories.put(id, extension);
83: }
84: }
85: }
86: }
87: }
|