KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > FetchTaskFactoriesRegistry


1 /**********************************************************************
2  * Copyright (c) 2004, 2006 Eclipse Foundation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Gunnar Wagenknecht - Initial API and implementation
10  * IBM - Initial API and implementation
11  **********************************************************************/

12 package org.eclipse.pde.internal.build;
13
14 import java.util.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.pde.build.IFetchFactory;
17
18 /**
19  * A registry for acessing fetch task factories.
20  * @since 3.2
21  */

22 public class FetchTaskFactoriesRegistry implements IPDEBuildConstants {
23
24     // Map of registered factories. key: factoryId, value: configuration element or corresponding instance
25
private Map factories;
26     
27     public FetchTaskFactoriesRegistry() {
28         factories = new HashMap();
29         initializeRegistry();
30     }
31
32     /**
33      * Returns the factory instance with the specified id.
34      * <p>
35      * The instance is not cached. Each time this method is called, a new
36      * instance is created.
37      * </p>
38      *
39      * @param id
40      * @return the factory instance (maybe <code>null</code>)
41      */

42     public IFetchFactory getFactory(String JavaDoc id) {
43         Object JavaDoc result = factories.get(id);
44         if (result instanceof IFetchFactory)
45             return (IFetchFactory) result;
46         
47         IConfigurationElement extension = (IConfigurationElement) factories.get(id);
48         if (null != extension) {
49             try {
50                 IFetchFactory toCache = (IFetchFactory) extension.createExecutableExtension(ATTR_CLASS);
51                 factories.put(id, toCache);
52                 return toCache;
53             } catch (CoreException e) {
54                 BundleHelper.getDefault().getLog().log(e.getStatus());
55             }
56         }
57         return null;
58     }
59
60     /**
61      * Returns a collection of registered factory ids.
62      *
63      * @return a collection of registered factory ids
64      */

65     public Collection getFactoryIds() {
66         return factories.keySet();
67     }
68
69     /**
70      * Initializes the registry
71      */

72     private void initializeRegistry() {
73         IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor(EXT_FETCH_TASK_FACTORIES);
74         for (int i = 0; i < extensions.length; i++) {
75             IConfigurationElement extension = extensions[i];
76             if (ELEM_FACTORY.equals(extension.getName())) {
77                 String JavaDoc id = extension.getAttribute(ATTR_ID);
78                 if (null != id && id.trim().length() > 0) {
79                     factories.put(id, extension);
80                 }
81             }
82         }
83     }
84 }
85
Popular Tags