public code v1
This commit is contained in:
+405
@@ -0,0 +1,405 @@
|
||||
package flintstones.helper.extensionpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IConfigurationElement;
|
||||
import org.eclipse.core.runtime.IExtensionRegistry;
|
||||
import org.eclipse.core.runtime.Platform;
|
||||
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
||||
import org.eclipse.e4.core.contexts.IEclipseContext;
|
||||
|
||||
import flintstones.entity.extensionenum.ExtensionEnum;
|
||||
|
||||
/**
|
||||
* The Class BaseRegistry.
|
||||
*/
|
||||
public class BaseRegistry {
|
||||
|
||||
/** The registers. */
|
||||
private final ArrayList<ExtensionRegistry> registers;
|
||||
|
||||
/** The extension point. */
|
||||
private final String extensionPoint;
|
||||
|
||||
/** The label id. */
|
||||
private String label_id = "uid"; //$NON-NLS-1$
|
||||
|
||||
/** The label implementation. */
|
||||
private String label_implementation = "implementation"; //$NON-NLS-1$
|
||||
|
||||
/**
|
||||
* Instantiates a new domain service provider.
|
||||
* @param extensionPoint The extensionPoint
|
||||
*/
|
||||
public BaseRegistry(String extensionPoint) {
|
||||
this.extensionPoint = extensionPoint;
|
||||
this.registers = new ArrayList<>();
|
||||
this.loadRegisters();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the registry id.
|
||||
*
|
||||
* @return the registry id
|
||||
*/
|
||||
public String getRegistryId() {
|
||||
return this.extensionPoint;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load registers.
|
||||
*/
|
||||
private void loadRegisters() {
|
||||
IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
|
||||
IConfigurationElement[] extensionElements = extensionRegistry.getConfigurationElementsFor(this.extensionPoint); // Domain.EXTENSION_POINT
|
||||
Arrays.stream(extensionElements)
|
||||
.forEach(this::loadRegistry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load registry.
|
||||
*
|
||||
* @param extensionElement the extension element
|
||||
*/
|
||||
private void loadRegistry(IConfigurationElement extensionElement) {
|
||||
ExtensionRegistry registry = new ExtensionRegistry(extensionElement);
|
||||
this.registers.add(registry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first registry where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @return The first ExtensionRegistry with the given key,value, null if the value does not exists for the given key. If the ke
|
||||
*/
|
||||
public ExtensionRegistry getFirstRegistryWhere(ExtensionEnum key, String value) {
|
||||
return this.getFirstRegistryWhere(key.toString(), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first registry where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @return The first ExtensionRegistry with the given key,value, null if the value does not exists for the given key. If the ke
|
||||
*/
|
||||
public ExtensionRegistry getFirstRegistryWhere(String key, String value) {
|
||||
for (ExtensionRegistry er : this.registers) {
|
||||
if (er.getAttribute(key) == null)
|
||||
throw new InvalidKeyIdentifier(key,value);
|
||||
if (er.getAttribute(key)
|
||||
.equals(value))
|
||||
return er;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all registries where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @return all the ExtensionRegistry with the given key
|
||||
*/
|
||||
public ExtensionRegistry[] getAllRegistriesWhere(ExtensionEnum key, String value) {
|
||||
return this.getAllRegistriesWhere(key.toString(), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all registries where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @return all the ExtensionRegistry with the given key
|
||||
*/
|
||||
public ExtensionRegistry[] getAllRegistriesWhere(String key, String value) {
|
||||
ArrayList<ExtensionRegistry> erl = new ArrayList<>();
|
||||
for (ExtensionRegistry er : this.registers)
|
||||
if (er.getAttribute(key)
|
||||
.equals(value))
|
||||
erl.add(er);
|
||||
return erl.toArray(new ExtensionRegistry[erl.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @param attribute The attribute to get from the obtained registers
|
||||
* @return An array with the wanted attribute from the registers where key=value.
|
||||
*/
|
||||
public String[] getAllAttributesWhere(ExtensionEnum key, String value, ExtensionEnum attribute) {
|
||||
return this.getAllAttributesWhere(key.toString(), value, attribute.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes where.
|
||||
*
|
||||
* @param key The key of the item you are looking for
|
||||
* @param value The value of the key.
|
||||
* @param attribute The attribute to get from the obtained registers
|
||||
* @return An array with the wanted attribute from the registers where key=value.
|
||||
*/
|
||||
public String[] getAllAttributesWhere(String key, String value, String attribute) {
|
||||
ExtensionRegistry[] arr = this.getAllRegistriesWhere(key, value);
|
||||
return Arrays.asList(arr)
|
||||
.stream()
|
||||
.map(k -> k.getAttribute(attribute))
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all registries.
|
||||
*
|
||||
* @return Every ExtensionRegistry available.
|
||||
*/
|
||||
public ExtensionRegistry[] getAllRegistries() {
|
||||
return this.registers.toArray(new ExtensionRegistry[this.registers.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes.
|
||||
*
|
||||
* @param attr The attribute of the ExtensionRegistry you are looking for
|
||||
* @return The value for the given attribute of every ExtensionRegistry.
|
||||
*/
|
||||
public String[] getAllAttributes(ExtensionEnum attr) {
|
||||
return this.getAllAttributes(attr.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes.
|
||||
*
|
||||
* @param attr The attribute of the ExtensionRegistry you are looking for
|
||||
* @return The value for the given attribute of every ExtensionRegistry.
|
||||
*/
|
||||
public String[] getAllAttributes(String attr) {
|
||||
ArrayList<String> attrs = new ArrayList<>();
|
||||
for (ExtensionRegistry er : this.registers) {
|
||||
String val = er.getAttribute(attr);
|
||||
if (val != null)
|
||||
attrs.add(val);
|
||||
}
|
||||
return attrs.toArray(new String[attrs.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes unique.
|
||||
*
|
||||
* @param attr The attribute of the ExtensionRegistry you are looking for
|
||||
* @return The value for the given attribute of every ExtensionRegistry, deleting the duplicated ones.
|
||||
*/
|
||||
public String[] getAllAttributesUnique(ExtensionEnum attr) {
|
||||
return this.getAllAttributesUnique(attr.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the all attributes unique.
|
||||
*
|
||||
* @param attr The attribute of the ExtensionRegistry you are looking for
|
||||
* @return The value for the given attribute of every ExtensionRegistry, deleting the duplicated ones.
|
||||
*/
|
||||
public String[] getAllAttributesUnique(String attr) {
|
||||
return this.unique(this.getAllAttributes(attr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attribute from an ExtensionRegistry id
|
||||
* @param attrId The ExtensionRegistry id
|
||||
* @param attr The attribute you are looking for
|
||||
* @return
|
||||
*/
|
||||
public String getAttributeFromId(String attrId, ExtensionEnum attr) {
|
||||
ExtensionRegistry extensionRegistry = this.getFirstRegistryWhere(this.getLabel_id(), attrId);
|
||||
return extensionRegistry.getAttribute(attr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique.
|
||||
*
|
||||
* @param arr An array of items
|
||||
* @return The same items without repeats
|
||||
*/
|
||||
private String[] unique(String[] arr) {
|
||||
int end = arr.length;
|
||||
|
||||
Set<String> set = new HashSet<>();
|
||||
for (int i = 0; i < end; i++)
|
||||
set.add(arr[i]);
|
||||
return set.toArray(new String[set.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate.
|
||||
*
|
||||
* @param id The ID of the extension registry to instantiate
|
||||
* @return The instantiated item
|
||||
*/
|
||||
public Object instantiate(String id) {
|
||||
return this.instantiate(id, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate using the extension Registry directy. Pretty usefull for creating child items.
|
||||
* @param extensionRegistry The extension registry to instantiate
|
||||
* @return The instantiated item
|
||||
*/
|
||||
public Object instantiate(ExtensionRegistry extensionRegistry) {
|
||||
return this.instantiate(extensionRegistry, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate.
|
||||
*
|
||||
* @param id The ID of the extension registry to instantiate
|
||||
* @param context The context to register the object in. If it is null, it won't be registered.
|
||||
* @return The instantiated item
|
||||
*/
|
||||
public Object instantiate(String id, IEclipseContext context) {
|
||||
|
||||
// Grab
|
||||
ExtensionRegistry extensionRegistry = this.getFirstRegistryWhere(this.getLabel_id(), id);
|
||||
if (extensionRegistry == null) {
|
||||
System.err.println("Extensión no encontrada: ID: " + id); //$NON-NLS-1$
|
||||
System.err.println("Extensiones disponibles para el punto de extension " + this.extensionPoint); //$NON-NLS-1$
|
||||
System.err.println(this.toString());
|
||||
throw new InvalidExtensionIdentifier(id);
|
||||
}
|
||||
|
||||
return this.instantiate(extensionRegistry, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate using the extension Registry directy. Pretty usefull for creating child items.
|
||||
* @param extensionRegistry The extension registry to instantiate
|
||||
* @param context The context to register the object in. If it is null, it won't be registered.
|
||||
* @return The instantiated item
|
||||
*/
|
||||
public Object instantiate(ExtensionRegistry extensionRegistry, IEclipseContext context) {
|
||||
|
||||
// Grab
|
||||
IConfigurationElement conf = extensionRegistry.getConfiguration();
|
||||
try {
|
||||
|
||||
Object item = conf.createExecutableExtension(this.getLabel_implementation());
|
||||
if (context != null)
|
||||
ContextInjectionFactory.inject(item, context);
|
||||
|
||||
return item;
|
||||
} catch (CoreException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (ExtensionRegistry de : this.getAllRegistries()) {
|
||||
sb.append(de.toString());
|
||||
sb.append("\n"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the label id.
|
||||
*
|
||||
* @return The label for the extension point ID. The default value is "id".
|
||||
*/
|
||||
public String getLabel_id() {
|
||||
return this.label_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label id.
|
||||
*
|
||||
* @param label_id The new label for the extension point ID. The default value is "id".
|
||||
*/
|
||||
public void setLabel_id(String label_id) {
|
||||
this.label_id = label_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the label implementation.
|
||||
*
|
||||
* @return The label for the extension point implementation. The default value is "implementation".
|
||||
*/
|
||||
public String getLabel_implementation() {
|
||||
return this.label_implementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label implementation.
|
||||
*
|
||||
* @param label_implementation The new label for the extension point implementation. The default value is "implementation".
|
||||
*/
|
||||
public void setLabel_implementation(ExtensionEnum label_implementation) {
|
||||
this.setLabel_implementation(label_implementation.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label implementation.
|
||||
*
|
||||
* @param label_implementation The new label for the extension point implementation. The default value is "implementation".
|
||||
*/
|
||||
public void setLabel_implementation(String label_implementation) {
|
||||
this.label_implementation = label_implementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Class InvalidExtensionIdentifier.
|
||||
*/
|
||||
public class InvalidExtensionIdentifier extends RuntimeException {
|
||||
|
||||
/** The Constant serialVersionUID. */
|
||||
private static final long serialVersionUID = -517005676614953393L;
|
||||
|
||||
/**
|
||||
* Instantiates a new invalid extension identifier.
|
||||
*
|
||||
* @param text the text
|
||||
*/
|
||||
private InvalidExtensionIdentifier(String text) {
|
||||
super("Identificador de extensión no encontrado: " + text); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Class InvalidKeyIdentifier.
|
||||
*/
|
||||
private class InvalidKeyIdentifier extends RuntimeException {
|
||||
|
||||
/** The Constant serialVersionUID. */
|
||||
private static final long serialVersionUID = 924357389433409796L;
|
||||
|
||||
/**
|
||||
* Instantiates a new invalid key identifier.
|
||||
*
|
||||
* @param text the text
|
||||
*/
|
||||
private InvalidKeyIdentifier(String text) {
|
||||
super("Clave no encontrada en el punto de extensión: " + text); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
private InvalidKeyIdentifier(String text, String t2) {
|
||||
super("Clave no encontrada en el punto de extensión: " + text + "// " + t2); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user