Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c34adcac4459a70676c54bfe0738df0d1834109 (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
/*******************************************************************************
 * 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.ote.define.utilities;

import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
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.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.plugin.core.util.ExtensionPoints;
import org.eclipse.osee.ote.define.OteDefinePlugin;
import org.eclipse.osee.ote.define.parser.BaseOutfileParser;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public class OutfileParserExtensionManager {

   private static final String OUTFILE_PARSER = "OutfileParser";
   private static final String CLASS_NAME = "classname";
   private static final String SUPPORTED_EXTENSIONS = "supportedExtensions";
   private final Map<String, BaseOutfileParser> contributions;
   private final HashCollection<String, BaseOutfileParser> extensionsToParsers;

   private static OutfileParserExtensionManager instance = null;

   private OutfileParserExtensionManager() {
      contributions = new HashMap<String, BaseOutfileParser>();
      extensionsToParsers = new HashCollection<String, BaseOutfileParser>(false, HashSet.class);
   }

   public static OutfileParserExtensionManager getInstance() {
      if (instance == null) {
         instance = new OutfileParserExtensionManager();
      }
      return instance;
   }

   public String[] getSupportedExtensions() throws OseeCoreException {
      checkObjectsLoaded();
      Set<String> set = extensionsToParsers.keySet();
      return set.toArray(new String[set.size()]);
   }

   public BaseOutfileParser getOutfileParserFor(URL fileToImport) throws OseeCoreException {
      checkObjectsLoaded();
      BaseOutfileParser toReturn = null;
      for (BaseOutfileParser parser : getOutfileParsers()) {
         if (parser.isValidParser(fileToImport)) {
            toReturn = parser;
            break;
         }
      }
      if (toReturn == null) {
         throw new OseeStateException(String.format("Unsupported outfile type [%s] no valid outfile parser found",
            fileToImport));
      }
      return toReturn;
   }

   public BaseOutfileParser getOutfileParserById(String id) throws OseeCoreException {
      checkObjectsLoaded();
      return contributions.get(id);
   }

   public Collection<BaseOutfileParser> getOutfileParsers() throws OseeCoreException {
      checkObjectsLoaded();
      return contributions.values();
   }

   private void checkObjectsLoaded() throws OseeCoreException {
      if (contributions.isEmpty()) {
         List<IConfigurationElement> elements =
            ExtensionPoints.getExtensionElements(OteDefinePlugin.PLUGIN_ID + "." + OUTFILE_PARSER, OUTFILE_PARSER);
         for (IConfigurationElement element : elements) {
            IExtension extension = (IExtension) element.getParent();
            String identifier = extension.getUniqueIdentifier();
            String bundleName = element.getContributor().getName();
            String parserClassName = element.getAttribute(CLASS_NAME);
            String supportedExtensions = element.getAttribute(SUPPORTED_EXTENSIONS);
            Set<String> supportedSet = getSupportedExtensions(supportedExtensions);
            BaseOutfileParser parser = (BaseOutfileParser) loadClass(bundleName, parserClassName);
            if (parser != null) {
               contributions.put(identifier, parser);
               for (String extensionType : supportedSet) {
                  extensionsToParsers.put(extensionType, parser);
               }
            }
         }
      }
   }

   private Set<String> getSupportedExtensions(String rawExtensions) {
      Set<String> toReturn = new HashSet<String>();
      for (String value : rawExtensions.split(";")) {
         value = value.trim();
         if (Strings.isValid(value)) {
            toReturn.add(value);
         }
      }
      return toReturn;
   }

   private Object loadClass(String bundleName, String className) throws OseeCoreException {
      Object object = null;
      if (Strings.isValid(bundleName) && Strings.isValid(className)) {
         try {
            Bundle bundle = Platform.getBundle(bundleName);
            Class<?> taskClass = bundle.loadClass(className);
            try {
               Method getInstance = taskClass.getMethod("getInstance", new Class[] {});
               object = getInstance.invoke(null, new Object[] {});
            } catch (Exception ex) {
               object = taskClass.newInstance();
            }
         } catch (Exception ex) {
            throw new OseeCoreException(String.format("Unable to Load: [%s - %s]", bundleName, className), ex);
         }
      }
      return object;
   }
}

Back to the top