Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8b0986e638fe3ee61526d5d42411b74b78457eaf (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.framework.plugin.core.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.internal.PluginCoreActivator;
import org.osgi.framework.Bundle;

/**
 * Use ExtensionDefinedObjects if possible
 * 
 * @author Ryan D. Brooks
 */
public class ExtensionPoints {
   public static List<IConfigurationElement> getExtensionElements(Plugin plugin, String extensionPointName, String elementName) {
      Bundle bundle = plugin.getBundle();
      return getExtensionElements(bundle.getSymbolicName() + "." + extensionPointName, elementName);
   }

   public static List<IConfigurationElement> getExtensionElements(String extensionPointId, String elementName) {
      IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
      if (extensionRegistry == null) {
         throw new IllegalStateException("The extension registry is unavailable");
      }

      IExtensionPoint point = extensionRegistry.getExtensionPoint(extensionPointId);
      if (point == null) {
         throw new IllegalArgumentException("The extension point " + extensionPointId + " does not exist");
      }

      IExtension[] extensions = point.getExtensions();
      ArrayList<IConfigurationElement> elementsList = new ArrayList<IConfigurationElement>(extensions.length * 3);

      for (IExtension extension : extensions) {
         IConfigurationElement[] elements = extension.getConfigurationElements();
         for (IConfigurationElement element : elements) {
            if (element.getName().equalsIgnoreCase(elementName)) {
               elementsList.add(element);
            }
         }
      }
      return elementsList;
   }

   /**
    * Return extension point unique ids if type extensionPointId
    * 
    * @param extensionPointId <plugin>.Point Id
    * @param extensionPointUniqueIds array of unique ids
    */
   public static List<IExtension> getExtensionsByUniqueId(String extensionPointId, Collection<String> extensionPointUniqueIds) {
      List<IExtension> extensions = new ArrayList<IExtension>();
      for (String entensionPointUniqueId : extensionPointUniqueIds) {
         IExtension extension = Platform.getExtensionRegistry().getExtension(entensionPointUniqueId);
         if (extension == null) {
            OseeLog.log(PluginCoreActivator.class, Level.SEVERE,
               "Unable to locate extension [" + entensionPointUniqueId + "]");
         } else {
            String thisPointId = extension.getExtensionPointUniqueIdentifier();
            if (extensionPointId.equals(thisPointId)) {
               extensions.add(extension);
            } else {
               OseeLog.log(PluginCoreActivator.class, Level.SEVERE,
                  "Unknown extension id [" + thisPointId + "] from extension [" + entensionPointUniqueId + "]");
            }
         }
      }
      return extensions;
   }

   public static List<String> getExtensionsPointUniqueIds(String extensionPointId) {
      List<String> extensionPointIds = new ArrayList<String>();
      IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId);
      if (point == null) {
         throw new IllegalArgumentException("The extension point " + extensionPointId + " does not exist");
      }

      IExtension[] extensions = point.getExtensions();
      for (IExtension extension : extensions) {
         extensionPointIds.add(extension.getUniqueIdentifier());
      }
      return extensionPointIds;
   }

   public static IConfigurationElement getExtensionElement(String extensionPointId, String elementName) {
      List<IConfigurationElement> elements = ExtensionPoints.getExtensionElements(extensionPointId, elementName);

      if (elements.isEmpty()) {
         throw new IllegalArgumentException(
            "no elements named " + elementName + " for " + extensionPointId + " where found.");
      }
      if (elements.size() > 1) {
         throw new IllegalArgumentException(
            elements.size() + " elements named " + elementName + " for " + extensionPointId + " where found.  Expected exactly one.");
      }
      return elements.get(0);
   }

}

Back to the top