Skip to content

Commit

Permalink
feat: add binding to service provider for use in Groovy script
Browse files Browse the repository at this point in the history
Add an additional binding for the Service Provider to Groovy scripts.

ING-4265
  • Loading branch information
florianesser authored and emanuelaepure10 committed May 3, 2024
1 parent 97b0df6 commit 0b84ee3
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
7 changes: 7 additions & 0 deletions common/plugins/eu.esdihumboldt.hale.common.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@
id="eu.esdihumboldt.hale.common.core.qname">
</alias>
</extension>
<extension
point="eu.esdihumboldt.util.service.provider.delegate">
<allow
allowAll="false"
class="eu.esdihumboldthale.hale.common.core.service.DelegateServiceProvider">
</allow>
</extension>
<extension
point="eu.esdihumboldt.util.groovy.meta">
<metaClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 wetransform GmbH
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* wetransform GmbH <http://www.wetransform.to>
*/

package eu.esdihumboldt.hale.common.core.service;

/**
* A DelegateServiceProvider delegates service requests to an underlying
* ServiceProvider. It implements the ServiceProvider interface and forwards
* getService calls to the specified underlying ServiceProvider.
*
* @author EmanuelaEpure
*/
public class DelegateServiceProvider implements ServiceProvider {

private final ServiceProvider serviceProvider;

/**
* Constructs a new DelegateServiceProvider with the specified
* ServiceProvider.
*
* @param serviceProvider the underlying ServiceProvider to delegate to
*/
public DelegateServiceProvider(ServiceProvider serviceProvider) {
this.serviceProvider = serviceProvider;
}

@Override
public <T> T getService(Class<T> serviceInterface) {
return serviceProvider.getService(serviceInterface);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public interface GroovyConstants {
*/
public static final String BINDING_INSTANCE_INDEX = "_instanceIndex";

/**
* Name of the service provider in the binding.
*/
public static final String BINDING_SERVICE_PROVIDER = "_serviceProvider";

/**
* Name of the helper functions accessor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import eu.esdihumboldt.hale.common.core.io.Text;
import eu.esdihumboldt.hale.common.core.io.project.ProjectInfoService;
import eu.esdihumboldt.hale.common.core.report.SimpleLog;
import eu.esdihumboldt.hale.common.core.service.DelegateServiceProvider;
import eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder;
import eu.esdihumboldt.hale.common.instance.index.InstanceIndexService;
import eu.esdihumboldt.hale.common.instance.index.spatial.SpatialIndexService;
Expand Down Expand Up @@ -305,6 +306,9 @@ public static Binding createBinding(InstanceBuilder builder, Cell cell, Cell typ
binding.setVariable(BINDING_INSTANCE_INDEX,
executionContext.getService(InstanceIndexService.class));

binding.setVariable(BINDING_SERVICE_PROVIDER,
new DelegateServiceProvider(executionContext));

return binding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<?eclipse version="3.4"?>
<plugin>
<extension-point id="eu.esdihumboldt.util.groovy.sandbox" name="Groovy Sandbox restriction configuration" schema="schema/eu.esdihumboldt.util.groovy.sandbox.exsd"/>
<extension-point id="eu.esdihumboldt.util.service.provider.delegate" name="Service Provider Delegate" schema="schema/eu.esdihumboldt.util.service.provider.delegate.exsd"/>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="eu.esdihumboldt.util.groovy.sandbox" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appinfo>
<meta.schema plugin="eu.esdihumboldt.util.groovy.sandbox" id="eu.esdihumboldt.util.service.provider.delegate" name="Service Provider Delegate"/>
</appinfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>

<element name="extension">
<annotation>
<appinfo>
<meta.element />
</appinfo>
</annotation>
<complexType>
<choice minOccurs="1" maxOccurs="unbounded">
<element ref="delegate"/>
</choice>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute translatable="true"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="delegate">
<complexType>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="eu.esdihumboldt.hale.common.core.service.DelegateServiceProvider:"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appinfo>
<meta.section type="since"/>
</appinfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="examples"/>
</appinfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="apiinfo"/>
</appinfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="implementation"/>
</appinfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>


</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@

import org.codehaus.groovy.runtime.GStringImpl;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.kohsuke.groovy.sandbox.GroovyInterceptor;

import de.fhg.igd.eclipse.util.extension.ExtensionUtil;
import eu.esdihumboldt.util.groovy.sandbox.GroovyRestrictionException;
import groovy.lang.Closure;
import groovy.lang.GString;
Expand Down Expand Up @@ -107,6 +112,11 @@ public class RestrictiveGroovyInterceptor extends GroovyInterceptor {
*/
private static final Set<String> disallowedClosureWriteProperties = new HashSet<>();

/**
* The extension point identifier.
*/
public static final String EXTENSION_ID = "eu.esdihumboldt.util.service.provider.delegate";

/**
* Allowed packages.
*/
Expand Down Expand Up @@ -170,6 +180,7 @@ public class RestrictiveGroovyInterceptor extends GroovyInterceptor {
// Binding classes
allowedClasses.add(Timestamp.class);
allowedClasses.add(java.sql.Date.class);
addAllowedClassesForID(EXTENSION_ID);

// Number formatting
allowedClasses.add(NumberFormat.class);
Expand Down Expand Up @@ -206,6 +217,34 @@ public class RestrictiveGroovyInterceptor extends GroovyInterceptor {
disallowedClosureWriteProperties.add("directive");
}

/**
* add allowed classes from the extension point ID
*
* @param extension_point ID
*/
private static void addAllowedClassesForID(String extension_point) {
// Get the extension point
IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
.getExtensionPoint(extension_point);

// Get all extensions contributed to the extension point
IExtension[] extensions = extensionPoint.getExtensions();
for (IExtension extension : extensions) {
// Get all configuration elements of the extension
IConfigurationElement[] configElements = extension.getConfigurationElements();
for (IConfigurationElement configElement : configElements) {
try {
for (IConfigurationElement element : configElement.getChildren("delegate")) {
Class<?> loadedClass = ExtensionUtil.loadClass(element, "class");
allowedClasses.add(loadedClass);
}
} catch (Exception e) {
// Handle any exceptions
}
}
}
}

/**
* AllowedPackage saves information to allow the use of all classes with a
* given prefix.
Expand Down

0 comments on commit 0b84ee3

Please sign in to comment.