Skip to content

Commit

Permalink
Fix parsing of service provider file (#3841).
Browse files Browse the repository at this point in the history
  • Loading branch information
karelmaxa committed Feb 7, 2024
1 parent 356c008 commit 9b93148
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
5 changes: 5 additions & 0 deletions modules/flowable-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
Expand Down Expand Up @@ -367,9 +369,13 @@ public void unregister() {
@Override
public ScriptEngine resolveScriptEngine(String name) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
String className = in.readLine();
in.close();
String className;
try (var input = configFile.openStream()) {
className = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
.map(line -> line.split("#", 2)[0].trim())
.filter(Predicate.not(String::isBlank))
.findFirst().orElse(null);
}
Class<?> cls = bundle.loadClass(className);
if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.osgi;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.net.URL;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

import org.flowable.osgi.Extender.BundleScriptEngineResolver;
import org.junit.Test;
import org.osgi.framework.Bundle;

/**
* Test processing of service provider configuration file.
*/
public class BundleScriptEngineResolverTest {

@Test
public void testResolveScriptEngine() throws Exception {
ScriptEngineFactory factory = mock(ScriptEngineFactoryMock.class);
Bundle bundle = mock(Bundle.class);
when(bundle.loadClass(eq("org.flowable.ScriptEngineFactoryMock"))).thenAnswer(answer -> factory.getClass());
URL configFile = getClass().getClassLoader().getResource("META-INF/services/javax.script.ScriptEngineFactory");
BundleScriptEngineResolver resolver = new BundleScriptEngineResolver(bundle, configFile);
ScriptEngine resolvedEngine = resolver.resolveScriptEngine("mockengine");
assertNotNull(resolvedEngine);
assertEquals("mockengine", resolvedEngine.get("name"));
}

/**
* Script engine factory providing mock script engine.
*/
static abstract class ScriptEngineFactoryMock implements ScriptEngineFactory {

@Override
public List<String> getNames() {
return List.of("mockengine");
}

@Override
public ScriptEngine getScriptEngine() {
ScriptEngine engine = mock(ScriptEngine.class);
when(engine.get(eq("name"))).thenReturn("mockengine");
return engine;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Comments, tabs and spaces should be ignored
# See java.util.ServiceLoader

org.flowable.ScriptEngineFactoryMock # should be also ignored

0 comments on commit 9b93148

Please sign in to comment.