Skip to content

Commit 14e6ed9

Browse files
committed
fix plugin id
1 parent 1b4c9c5 commit 14e6ed9

File tree

6 files changed

+48
-86
lines changed

6 files changed

+48
-86
lines changed

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/JavaArtifactNames.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,17 @@
1616

1717
package com.exonum.binding.core.runtime;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
20-
2119
import java.util.regex.Matcher;
2220
import java.util.regex.Pattern;
2321

2422
final class JavaArtifactNames {
25-
26-
private static final String DELIMITER = ":";
27-
private static final Pattern FORBIDDEN_CHARS_PATTERN = Pattern.compile("[\\s:]");
28-
private static final int KEEP_EMPTY = -1;
23+
private static final Pattern FORBIDDEN_CHARS_PATTERN = Pattern.compile("[\\s]");
2924

3025
/**
31-
* Checks that a Java artifact name is in format "groupId/artifactId:version".
32-
*
33-
* @param name a Java artifact name
34-
* @return an array with two elements: artifact name and artifact version
35-
* @throws IllegalArgumentException if the name format is not correct
26+
* Validates artifact id for forbidden characters.
27+
* @throws IllegalArgumentException if any forbidden characters found
3628
*/
37-
static String[] checkPluginArtifact(String name) {
38-
String[] coordinates = name.split(DELIMITER, KEEP_EMPTY);
39-
checkArgument(coordinates.length == 2,
40-
"Invalid artifact name (%s), must have 'groupId/artifactId:version' format",
41-
name);
42-
for (String c : coordinates) {
43-
checkNoForbiddenChars(c);
44-
}
45-
return coordinates;
46-
}
47-
48-
/**
49-
* Returns plugin artifact id for the given service artifact.
50-
*/
51-
static String getPluginArtifactId(ServiceArtifactId artifactId) {
52-
return artifactId.getName() + DELIMITER + artifactId.getVersion();
53-
}
54-
55-
private static void checkNoForbiddenChars(String s) {
29+
static void checkNoForbiddenChars(String s) {
5630
Matcher matcher = FORBIDDEN_CHARS_PATTERN.matcher(s);
5731

5832
if (matcher.find()) {

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/Pf4jServiceLoader.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void startPlugin(String pluginId) throws ServiceLoadingException {
119119

120120
/** Loads the service definition from the already loaded plugin with the given id. */
121121
private LoadedServiceDefinition loadDefinition(String pluginId) throws ServiceLoadingException {
122-
ServiceArtifactId artifactId = extractServiceId(pluginId);
122+
ServiceArtifactId artifactId = ServiceArtifactId.parseFrom(pluginId);
123123
Supplier<ServiceModule> serviceModuleSupplier = findServiceModuleSupplier(pluginId);
124124
LoadedServiceDefinition serviceDefinition =
125125
LoadedServiceDefinition.newInstance(artifactId, serviceModuleSupplier);
@@ -129,20 +129,6 @@ private LoadedServiceDefinition loadDefinition(String pluginId) throws ServiceLo
129129
return serviceDefinition;
130130
}
131131

132-
private static ServiceArtifactId extractServiceId(String pluginId)
133-
throws ServiceLoadingException {
134-
try {
135-
String[] result = JavaArtifactNames.checkPluginArtifact(pluginId);
136-
String name = result[0];
137-
String version = result[1];
138-
return ServiceArtifactId.newJavaId(name, version);
139-
} catch (IllegalArgumentException e) {
140-
String message = String.format(
141-
"Invalid plugin id (%s) is specified in service artifact metadata", pluginId);
142-
throw new ServiceLoadingException(message, e);
143-
}
144-
}
145-
146132
private Supplier<ServiceModule> findServiceModuleSupplier(String pluginId)
147133
throws ServiceLoadingException {
148134
List<Class<? extends ServiceModule>> extensionClasses = pluginManager
@@ -192,8 +178,7 @@ public Optional<LoadedServiceDefinition> findService(ServiceArtifactId artifactI
192178
@Override
193179
public void unloadService(ServiceArtifactId artifactId) {
194180
checkArgument(loadedServices.containsKey(artifactId), "No such artifactId: %s", artifactId);
195-
196-
String pluginId = JavaArtifactNames.getPluginArtifactId(artifactId);
181+
String pluginId = artifactId.toString();
197182
try {
198183
boolean stopped = pluginManager.unloadPlugin(pluginId);
199184
// The docs don't say why it may fail to stop the plugin.
@@ -213,7 +198,7 @@ public void unloadAll() {
213198
// Unload the plugins
214199
List<Exception> errors = new ArrayList<>();
215200
for (ServiceArtifactId artifactId : loadedServices.keySet()) {
216-
String pluginId = JavaArtifactNames.getPluginArtifactId(artifactId);
201+
String pluginId = artifactId.toString();
217202
try {
218203
unloadPlugin(pluginId);
219204
} catch (Exception e) {

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/ServiceArtifactId.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public abstract class ServiceArtifactId {
3535

3636
private static final String DELIMITER = ":";
37-
private static final int NUM_FIELDS = 3;
37+
private static final int KEEP_EMPTY = -1;
3838

3939
/**
4040
* Returns the runtime id in which the service shall be deployed.
@@ -52,15 +52,16 @@ public abstract class ServiceArtifactId {
5252
public abstract String getVersion();
5353

5454
/**
55-
* Parses a service id in format "runtimeId:serviceName" as {@link #toString()} produces.
55+
* Parses a service id in format "runtimeId:serviceName:version" as {@link #toString()} produces.
5656
*
5757
* @param serviceArtifactId a string in format "runtimeId:serviceName:version". Whitespace
5858
* characters, including preceding and trailing, are not allowed
5959
* @return a ServiceArtifactId with the given coordinates
6060
* @throws IllegalArgumentException if the format is not correct
6161
*/
6262
public static ServiceArtifactId parseFrom(String serviceArtifactId) {
63-
String[] coordinates = serviceArtifactId.split(DELIMITER, NUM_FIELDS);
63+
JavaArtifactNames.checkNoForbiddenChars(serviceArtifactId);
64+
String[] coordinates = serviceArtifactId.split(DELIMITER, KEEP_EMPTY);
6465
checkArgument(coordinates.length == 3,
6566
"Invalid artifact id (%s), must have 'runtimeId:groupId/artifactId:version' format",
6667
serviceArtifactId);
@@ -92,7 +93,7 @@ public static ServiceArtifactId valueOf(int runtimeId, String name, String versi
9293
}
9394

9495
/**
95-
* Returns an artifact id in the following format: "runtimeId:serviceName".
96+
* Returns an artifact id in the following format: "runtimeId:serviceName:version".
9697
*/
9798
@Override
9899
public final String toString() {

exonum-java-binding/core/src/test/java/com/exonum/binding/core/runtime/JavaArtifactNamesTest.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,22 @@
1616

1717
package com.exonum.binding.core.runtime;
1818

19+
import static com.exonum.binding.core.runtime.JavaArtifactNames.checkNoForbiddenChars;
1920
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

2122
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.params.ParameterizedTest;
23-
import org.junit.jupiter.params.provider.ValueSource;
2423

2524
class JavaArtifactNamesTest {
2625

2726
@Test
2827
void checkValidName() {
2928
String name = "com.acme/foo:1.0";
30-
JavaArtifactNames.checkPluginArtifact(name);
29+
checkNoForbiddenChars(name);
3130
}
3231

33-
@ParameterizedTest
34-
@ValueSource(strings = {
35-
"",
36-
"too-few:components:1.0",
37-
"com.acme:foo-service:0.1.0:extra-component",
38-
" : : ",
39-
"com acme:foo:1.0",
40-
"com.acme:foo service:1.0",
41-
"com.acme:foo-service:1 0",
42-
"com.acme:foo-service: 1.0",
43-
"com.acme:foo-service:1.0 ",
44-
})
45-
void checkInvalidName(String pluginId) {
46-
assertThrows(IllegalArgumentException.class,
47-
() -> JavaArtifactNames.checkPluginArtifact(pluginId));
32+
@Test
33+
void checkInvalidName() {
34+
String name = "com.acme foo:1.0";
35+
assertThrows(IllegalArgumentException.class, () -> checkNoForbiddenChars(name));
4836
}
4937
}

exonum-java-binding/core/src/test/java/com/exonum/binding/core/runtime/Pf4jServiceLoaderIntegrationTestable.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656

5757
abstract class Pf4jServiceLoaderIntegrationTestable {
5858

59-
private static final String PLUGIN_ID = "com.acme/foo-service:1.0.1";
60-
private static final String PLUGIN_ID_2 = "org.acme/bar-service:3.2.1";
59+
private static final String PLUGIN_ID = "1:com.acme/foo-service:1.0.1";
60+
private static final String PLUGIN_ID_2 = "1:org.acme/bar-service:3.2.1";
6161
private static final Map<String, Class<?>> TEST_DEPENDENCY_REFERENCE_CLASSES = ImmutableMap.of(
6262
"exonum-java-binding", Service.class,
6363
"vertx", Vertx.class,
@@ -95,7 +95,7 @@ void canLoadService() throws ServiceLoadingException, IOException {
9595

9696
// Check the definition
9797
ServiceArtifactId serviceId = serviceDefinition.getId();
98-
ServiceArtifactId expectedId = getServiceArtifact(pluginId);
98+
ServiceArtifactId expectedId = ServiceArtifactId.parseFrom(pluginId);
9999
assertThat(serviceId).isEqualTo(expectedId);
100100
Supplier<ServiceModule> moduleSupplier = serviceDefinition.getModuleSupplier();
101101
ServiceModule module = moduleSupplier.get();
@@ -123,7 +123,7 @@ void cannotLoadIfPluginManagerFailsToLoad() throws IOException {
123123
assertThat(e).hasMessageContaining("Failed to load the service from");
124124

125125
// Check the definition is inaccessible
126-
ServiceArtifactId serviceId = getServiceArtifact(PLUGIN_ID);
126+
ServiceArtifactId serviceId = ServiceArtifactId.parseFrom(PLUGIN_ID);
127127
assertThat(serviceLoader.findService(serviceId)).isEmpty();
128128
}
129129

@@ -153,7 +153,7 @@ void cannotLoadIfPluginFailedToStart() throws IOException {
153153
assertThat(e).hasMessageContaining("Failed to start the plugin");
154154

155155
// Check the definition is inaccessible
156-
ServiceArtifactId serviceId = getServiceArtifact(pluginId);
156+
ServiceArtifactId serviceId = ServiceArtifactId.parseFrom(pluginId);
157157
assertThat(serviceLoader.findService(serviceId)).isEmpty();
158158

159159
// Check it is unloaded if failed to start
@@ -163,7 +163,7 @@ void cannotLoadIfPluginFailedToStart() throws IOException {
163163
@ParameterizedTest
164164
@ValueSource(strings = {
165165
"foo-service",
166-
"com.acme:foo-service:1.0:extra-coordinate",
166+
"1:com.acme/foo-service:1.0:extra-coordinate",
167167
})
168168
void cannotLoadIfInvalidPluginIdInMetadata(String invalidPluginId) throws IOException {
169169
anArtifact()
@@ -173,8 +173,9 @@ void cannotLoadIfInvalidPluginIdInMetadata(String invalidPluginId) throws IOExce
173173
// Try to load the service
174174
Exception e = assertThrows(ServiceLoadingException.class,
175175
() -> serviceLoader.loadService(artifactLocation));
176-
assertThat(e).hasMessageContaining("Invalid plugin id");
176+
assertThat(e).hasMessageContaining("Failed to load plugin");
177177
assertThat(e).hasMessageContaining(invalidPluginId);
178+
assertThat(e.getCause()).hasMessageContaining("Invalid artifact id");
178179

179180
// Check it is unloaded if failed to start
180181
verify(pluginManager).unloadPlugin(invalidPluginId);
@@ -253,14 +254,14 @@ void canLoadUnloadService() throws Exception {
253254

254255
@Test
255256
void unloadServiceNonLoaded() {
256-
ServiceArtifactId unknownPluginId = getServiceArtifact(PLUGIN_ID);
257+
ServiceArtifactId unknownPluginId = ServiceArtifactId.parseFrom(PLUGIN_ID);
257258
assertThrows(IllegalArgumentException.class,
258259
() -> serviceLoader.unloadService(unknownPluginId));
259260
}
260261

261262
@Test
262263
void findServiceNonLoaded() {
263-
ServiceArtifactId unknownPluginId = getServiceArtifact(PLUGIN_ID);
264+
ServiceArtifactId unknownPluginId = ServiceArtifactId.parseFrom(PLUGIN_ID);
264265
Optional<?> serviceDefinition = serviceLoader.findService(unknownPluginId);
265266
assertThat(serviceDefinition).isEmpty();
266267
}
@@ -304,16 +305,11 @@ private static ServiceArtifactBuilder anArtifact() {
304305
private void verifyUnloaded(String pluginId) {
305306
verify(pluginManager).unloadPlugin(pluginId);
306307

307-
ServiceArtifactId serviceId = getServiceArtifact(pluginId);
308+
ServiceArtifactId serviceId = ServiceArtifactId.parseFrom(pluginId);
308309
assertThat(serviceLoader.findService(serviceId)).isEmpty();
309310
}
310311

311312
private static void assertNamesEqual(Class<?> actual, Class<?> expected) {
312313
assertThat(actual.getName()).isEqualTo(expected.getName());
313314
}
314-
315-
private ServiceArtifactId getServiceArtifact(String pluginId) {
316-
String[] result = JavaArtifactNames.checkPluginArtifact(pluginId);
317-
return ServiceArtifactId.newJavaId(result[0], result[1]);
318-
}
319315
}

exonum-java-binding/core/src/test/java/com/exonum/binding/core/runtime/ServiceArtifactIdTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.exonum.binding.core.runtime;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.params.ParameterizedTest;
@@ -28,7 +29,7 @@ class ServiceArtifactIdTest {
2829
@ValueSource(strings = {
2930
"0:land-registry:v1",
3031
"1:com.acme/foo-service:1.1.1-beta1",
31-
"100500:foo:bar:",
32+
"100500:foo:bar",
3233
})
3334
void parseFromRoundtrip(String serviceId) {
3435
ServiceArtifactId parsedId = ServiceArtifactId.parseFrom(serviceId);
@@ -65,4 +66,21 @@ void toStringTest() {
6566

6667
assertThat(id.toString()).isEqualTo("0:full-name:0.1");
6768
}
69+
70+
@ParameterizedTest
71+
@ValueSource(strings = {
72+
"",
73+
"too-few:components:1.0",
74+
"com.acme:foo-service:0.1.0:extra-component",
75+
" : : ",
76+
"com acme:foo:1.0",
77+
"com.acme:foo service:1.0",
78+
"com.acme:foo-service:1 0",
79+
"com.acme:foo-service: 1.0",
80+
"com.acme:foo-service:1.0 ",
81+
})
82+
void checkInvalidName(String artifactId) {
83+
assertThrows(IllegalArgumentException.class,
84+
() -> ServiceArtifactId.parseFrom(artifactId));
85+
}
6886
}

0 commit comments

Comments
 (0)