Skip to main content
summaryrefslogtreecommitdiffstats
blob: 27ddccfe84bdee4f28afe18711effe918fe7003c (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * 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.skynet.core.attribute;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.plugin.core.util.ExtensionPoints;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.providers.AbstractAttributeDataProvider;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public class AttributeExtensionManager {
   private static final String CLASS_ID = "class";
   private static final String ATTRIBUTE_TYPE = Activator.PLUGIN_ID + ".AttributeType";
   private static final String ATTRIBUTE_DATA_PROVIDER_TYPE = Activator.PLUGIN_ID + ".AttributeDataProvider";

   private static final String[] attributeBaseTypes = new String[] {
      "CharacterBackedAttributeType",
      "BinaryBackedAttributeType"};
   private static final String[] attributeProviderBaseTypes = new String[] {
      "CharacterAttributeDataProvider",
      "BinaryAttributeDataProvider"};

   private static final AttributeExtensionManager instance = new AttributeExtensionManager();

   private Map<String, Pair<String, String>> attributeTypeClasses;
   private Map<String, Pair<String, String>> attributeDataProviderClasses;

   private AttributeExtensionManager() {
      this.attributeTypeClasses = null;
      this.attributeDataProviderClasses = null;
   }

   public static String resolveAttributeBaseTypeId(String id) {
      return resolveAttributeProviderTypeId(id);
   }

   public static String resolveAttributeProviderTypeId(String id) {
      String newType = id;
      if (!newType.contains(".")) {
         newType = Activator.PLUGIN_ID + "." + newType;
      }
      return newType;
   }

   public static Class<? extends Attribute<?>> getAttributeClassFor(String name) throws OseeCoreException {
      if (instance.attributeTypeClasses == null) {
         instance.attributeTypeClasses = instance.loadExtensions(ATTRIBUTE_TYPE, attributeBaseTypes, CLASS_ID);
      }
      String resolved = resolveAttributeBaseTypeId(name);
      Pair<String, String> entry = instance.attributeTypeClasses.get(resolved);
      if (entry == null) {
         throw new OseeArgumentException(String.format("Unable to find class for: [%s]", resolved));
      }

      return instance.loadClass(entry.getFirst(), entry.getSecond());
   }

   public static Class<? extends AbstractAttributeDataProvider> getAttributeProviderClassFor(String name) throws OseeCoreException {
      if (instance.attributeDataProviderClasses == null) {
         instance.attributeDataProviderClasses =
            instance.loadExtensions(ATTRIBUTE_DATA_PROVIDER_TYPE, attributeProviderBaseTypes, CLASS_ID);
      }
      String resolved = resolveAttributeProviderTypeId(name);
      Pair<String, String> entry = instance.attributeDataProviderClasses.get(resolved);
      if (entry == null) {
         throw new OseeArgumentException(String.format("Unable to find class for: [%s]", resolved));
      }
      return instance.loadClass(entry.getFirst(), entry.getSecond());
   }

   @SuppressWarnings("unchecked")
   private <T> Class<T> loadClass(String bundleName, String className) throws OseeCoreException {
      Class<T> toReturn = null;
      try {
         Bundle bundle = Platform.getBundle(bundleName);
         toReturn = bundle.loadClass(className);
      } catch (ClassNotFoundException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return toReturn;
   }

   private Map<String, Pair<String, String>> loadExtensions(String extensionPointId, String[] elementNames, String classNameAttribute) throws OseeStateException {
      Map<String, Pair<String, String>> toReturn = new HashMap<String, Pair<String, String>>();
      for (String elementName : elementNames) {
         List<IConfigurationElement> elements = ExtensionPoints.getExtensionElements(extensionPointId, elementName);
         for (IConfigurationElement element : elements) {
            IExtension extension = (IExtension) element.getParent();
            String name = extension.getUniqueIdentifier();
            String className = Strings.intern(element.getAttribute(classNameAttribute));
            String bundleName = Strings.intern(element.getContributor().getName());

            if (Strings.isValid(bundleName) && Strings.isValid(className)) {
               toReturn.put(name, new Pair<String, String>(bundleName, className));
            }
         }
      }
      if (toReturn.isEmpty()) {
         throw new OseeStateException(String.format(
            "No Objects loaded for [%s] with element names %s and attribute [%s]", extensionPointId,
            Arrays.asList(elementNames), classNameAttribute));
      }
      return toReturn;
   }

   @Override
   public String toString() {
      return super.toString();
   }

   public static Set<String> getAttributeProviders() throws OseeStateException {
      if (instance.attributeDataProviderClasses == null) {
         instance.attributeDataProviderClasses =
            instance.loadExtensions(ATTRIBUTE_DATA_PROVIDER_TYPE, attributeProviderBaseTypes, CLASS_ID);
      }
      return instance.attributeDataProviderClasses.keySet();
   }

   public static Set<String> getAttributeClasses() throws OseeStateException {
      if (instance.attributeTypeClasses == null) {
         instance.attributeTypeClasses = instance.loadExtensions(ATTRIBUTE_TYPE, attributeBaseTypes, CLASS_ID);
      }
      return instance.attributeTypeClasses.keySet();
   }
}

Back to the top