From 60718b3ae7dee447baff47504eb25e1af6170517 Mon Sep 17 00:00:00 2001 From: Johannes Dicks Date: Wed, 3 Feb 2016 17:17:19 +0100 Subject: [PATCH] #358 : LibraryExtensions refactored to allow standalone execution (without eclipse extensions) --- .../core/extensions/ILibraryDescriptor.java | 14 +++ .../core/extensions/LibraryExtensions.java | 109 ++++++++++-------- 2 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java diff --git a/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java b/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java new file mode 100644 index 0000000000..c5b7bf3359 --- /dev/null +++ b/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java @@ -0,0 +1,14 @@ +package org.yakindu.sct.generator.core.extensions; + +import org.eclipse.emf.common.util.URI; +import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; + +public interface ILibraryDescriptor { + + URI getURI(); + + String getLibraryId(); + + IDefaultFeatureValueProvider createFeatureValueProvider(); + +} \ No newline at end of file diff --git a/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java b/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java index d06a703e8f..ef6fe906f4 100644 --- a/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java +++ b/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java @@ -10,9 +10,6 @@ */ package org.yakindu.sct.generator.core.extensions; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Lists.newArrayList; - import java.util.List; import org.eclipse.core.runtime.CoreException; @@ -22,9 +19,9 @@ import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; import org.yakindu.sct.model.sgen.FeatureTypeLibrary; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; /** * @@ -40,25 +37,47 @@ public class LibraryExtensions { public static final String GLOBAL_ID = "ALL"; - public static class LibraryDescriptor { + private static List descriptors; + + private static class LibraryDescriptor implements ILibraryDescriptor { private final IConfigurationElement configElement; LibraryDescriptor(IConfigurationElement configElement) { this.configElement = configElement; } + /* + * (non-Javadoc) + * + * @see + * org.yakindu.sct.generator.core.extensions.ILibraryDescriptor#getURI() + */ + @Override public URI getURI() { return URI.createURI(configElement.getAttribute(ATTRIBUTE_URI)); } - + + /* + * (non-Javadoc) + * + * @see org.yakindu.sct.generator.core.extensions.ILibraryDescriptor# + * getLibraryId() + */ + @Override public String getLibraryId() { return configElement.getAttribute(ATTRIBUTE_LIBRARY_ID); } + /* + * (non-Javadoc) + * + * @see org.yakindu.sct.generator.core.extensions.ILibraryDescriptor# + * createFeatureValueProvider() + */ + @Override public IDefaultFeatureValueProvider createFeatureValueProvider() { try { - return (IDefaultFeatureValueProvider) configElement - .createExecutableExtension(DEFAULT_PROVIDER); + return (IDefaultFeatureValueProvider) configElement.createExecutableExtension(DEFAULT_PROVIDER); } catch (CoreException e) { e.printStackTrace(); } @@ -66,51 +85,49 @@ public IDefaultFeatureValueProvider createFeatureValueProvider() { } } - public static Iterable getLibraryDescriptors() { - IConfigurationElement[] configurationElements = Platform - .getExtensionRegistry().getConfigurationElementsFor( - EXTENSION_POINT_ID); - return transform(newArrayList(configurationElements), - new CreateLibraryDescriptor()); + public static List getLibraryDescriptors() { + + if (descriptors == null) { + descriptors = Lists.newArrayList(); + if (Platform.isRunning()) { + initFromExtensions(); + } + } + return descriptors; + } + + protected static void initFromExtensions() { + IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() + .getConfigurationElementsFor(EXTENSION_POINT_ID); + for (IConfigurationElement iConfigurationElement : configurationElements) { + descriptors.add(new LibraryDescriptor(iConfigurationElement)); + } } - - public static Iterable getLibraryDescriptors( - final List libraryIds) { - Iterable libraryDescriptor = LibraryExtensions - .getLibraryDescriptors(); - return Iterables.filter(libraryDescriptor, - new Predicate() { - public boolean apply(LibraryDescriptor input) { - for (String libId : libraryIds) { - if (input.getLibraryId().equals(libId)) { - return true; - } - } - return false; + + public static List getLibraryDescriptors(final List libraryIds) { + Iterable libraryDescriptor = getLibraryDescriptors(); + return Lists.newArrayList(Iterables.filter(libraryDescriptor, new Predicate() { + public boolean apply(ILibraryDescriptor input) { + for (String libId : libraryIds) { + if (input.getLibraryId().equals(libId)) { + return true; } - }); + } + return false; + } + })); } - - public static IDefaultFeatureValueProvider getDefaultFeatureValueProvider( - List libraryId, FeatureTypeLibrary library) { - Iterable libraryDescriptor = getLibraryDescriptors(libraryId); - for (LibraryDescriptor desc : libraryDescriptor) { - IDefaultFeatureValueProvider defaultProvider = desc - .createFeatureValueProvider(); - if (defaultProvider != null - && defaultProvider.isProviderFor(library)) { + + public static IDefaultFeatureValueProvider getDefaultFeatureValueProvider(List libraryId, + FeatureTypeLibrary library) { + List libraryDescriptor = getLibraryDescriptors(libraryId); + for (ILibraryDescriptor desc : libraryDescriptor) { + IDefaultFeatureValueProvider defaultProvider = desc.createFeatureValueProvider(); + if (defaultProvider != null && defaultProvider.isProviderFor(library)) { return defaultProvider; } } return null; } - private static final class CreateLibraryDescriptor implements - Function { - - public LibraryDescriptor apply(IConfigurationElement from) { - return new LibraryDescriptor(from); - } - } - }