From 78a6ac3e3802abf5a2f2d022a9a1a28483dbc7b9 Mon Sep 17 00:00:00 2001 From: EL MASRI Chadi Date: Tue, 18 Oct 2016 16:31:53 +0200 Subject: [PATCH] Implement the method to read the KieModule from classpath. --- drools-compiler/pom.xml | 9 +++++ .../kie/builder/impl/KieRepositoryImpl.java | 30 +++++++++++++-- .../drools/compiler/kproject/ReleaseIdImpl.java | 36 ++++++++++++------ .../kie/builder/impl/KieRepositoryImplTest.java | 41 +++++++++++++++++++++ .../src/test/resources/artifact-1.0.jar | Bin 0 -> 2490 bytes 5 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 drools-compiler/src/test/java/org/drools/compiler/kie/builder/impl/KieRepositoryImplTest.java create mode 100644 drools-compiler/src/test/resources/artifact-1.0.jar diff --git a/drools-compiler/pom.xml b/drools-compiler/pom.xml index 7076a42..c335bf6 100644 --- a/drools-compiler/pom.xml +++ b/drools-compiler/pom.xml @@ -361,6 +361,15 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + ${basedir}/src/test/resources/artifact-1.0.jar + + + diff --git a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java index c7089f6..3b6747f 100644 --- a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java +++ b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java @@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.math.BigInteger; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -144,9 +145,32 @@ public class KieRepositoryImpl } private KieModule checkClasspathForKieModule(ReleaseId releaseId) { - // TODO - // ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - // URL url = classLoader.getResource( ((ReleaseIdImpl)releaseId).getPomPropertiesPath() ); + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + URL pomPropertiesPath = contextClassLoader.getResource( ((ReleaseIdImpl)releaseId).getPomPropertiesPath() ); + if (pomPropertiesPath != null) { + ReleaseId pomReleaseId = ReleaseIdImpl.fromPropertiesStream( + contextClassLoader.getResourceAsStream(((ReleaseIdImpl) releaseId).getPomPropertiesPath()), + pomPropertiesPath.getPath()); + + if (pomReleaseId.equals(releaseId)) { + String path = pomPropertiesPath.getPath(); + String pathToJar = path.substring(0, path.indexOf(".jar!") + 4); + + URL pathToKmodule; + try { + pathToKmodule = new URL( + pomPropertiesPath.getProtocol(), + pomPropertiesPath.getHost(), + pomPropertiesPath.getPort(), + pathToJar + "!/META-INF/kmodule.xml"); + } catch (MalformedURLException e) { + throw new RuntimeException("Unable to reconstruct path to kmodule"); + } + + log.info("Adding KieModule from resource: " + pathToJar); + return ClasspathKieProject.fetchKModule(pathToKmodule); + } + } return null; } diff --git a/drools-compiler/src/main/java/org/drools/compiler/kproject/ReleaseIdImpl.java b/drools-compiler/src/main/java/org/drools/compiler/kproject/ReleaseIdImpl.java index 2d45b76..2af8f25 100644 --- a/drools-compiler/src/main/java/org/drools/compiler/kproject/ReleaseIdImpl.java +++ b/drools-compiler/src/main/java/org/drools/compiler/kproject/ReleaseIdImpl.java @@ -17,6 +17,7 @@ package org.drools.compiler.kproject; import java.io.Externalizable; import java.io.IOException; +import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.StringReader; @@ -101,23 +102,34 @@ public class ReleaseIdImpl implements ReleaseId, Externalizable { return version.endsWith("-SNAPSHOT"); } - public static ReleaseId fromPropertiesString(String string) { + public static ReleaseId fromPropertiesString(String path) { Properties props = new Properties(); - ReleaseId releaseId = null; try { - props.load(new StringReader(string)); - String groupId = props.getProperty("groupId"); - String artifactId = props.getProperty("artifactId"); - String version = props.getProperty("version"); - if (StringUtils.isEmpty(groupId) || StringUtils.isEmpty(artifactId) || StringUtils.isEmpty(version)) { - throw new RuntimeException("pom.properties exists but ReleaseId content is malformed\n" + string); - } - releaseId = new ReleaseIdImpl(groupId, artifactId, version); + props.load(new StringReader(path)); + return getReleaseIdFromProperties(props, path); } catch (IOException e) { - throw new RuntimeException("pom.properties was malformed\n" + string, e); + throw new RuntimeException("pom.properties was malformed\n" + path, e); } + } + + public static ReleaseId fromPropertiesStream(InputStream stream, String path) { + Properties props = new Properties(); + try { + props.load(stream); + return getReleaseIdFromProperties(props, path); + } catch (IOException e) { + throw new RuntimeException("pom.properties was malformed\n" + path, e); + } + } - return releaseId; + private static ReleaseId getReleaseIdFromProperties(Properties props, String path) { + String groupId = props.getProperty("groupId"); + String artifactId = props.getProperty("artifactId"); + String version = props.getProperty("version"); + if (StringUtils.isEmpty(groupId) || StringUtils.isEmpty(artifactId) || StringUtils.isEmpty(version)) { + throw new RuntimeException("pom.properties exists but ReleaseId content is malformed\n" + path); + } + return new ReleaseIdImpl(groupId, artifactId, version); } @Override diff --git a/drools-compiler/src/test/java/org/drools/compiler/kie/builder/impl/KieRepositoryImplTest.java b/drools-compiler/src/test/java/org/drools/compiler/kie/builder/impl/KieRepositoryImplTest.java new file mode 100644 index 0000000..30d44a0 --- /dev/null +++ b/drools-compiler/src/test/java/org/drools/compiler/kie/builder/impl/KieRepositoryImplTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * 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.drools.compiler.kie.builder.impl; + +import org.drools.compiler.kproject.ReleaseIdImpl; +import org.junit.Assert; +import org.junit.Test; +import org.kie.api.builder.KieModule; +import org.kie.api.builder.ReleaseId; + +public class KieRepositoryImplTest { + @Test + public void testExistingReleaseIsCorrectlyReadFromClasspath() { + KieRepositoryImpl kieRepository = new KieRepositoryImpl(); + ReleaseId releaseId = new ReleaseIdImpl("org.group", "artifact", "1.0"); + KieModule kieModule = kieRepository.getKieModule(releaseId); + Assert.assertNotNull(kieModule); + } + + @Test + public void testReleaseIsNotFoundInClasspath() { + KieRepositoryImpl kieRepository = new KieRepositoryImpl(); + ReleaseId releaseId = new ReleaseIdImpl("org.group", "artifact", "1.1"); + KieModule kieModule = kieRepository.getKieModule(releaseId); + Assert.assertNull(kieModule); + } + +} \ No newline at end of file diff --git a/drools-compiler/src/test/resources/artifact-1.0.jar b/drools-compiler/src/test/resources/artifact-1.0.jar new file mode 100644 index 0000000000000000000000000000000000000000..9739295db237903da3658963b26025ea5033ba49 GIT binary patch literal 2490 zcmWIWW@h1H0D;WVU{5duN^k;cU)K;vT~9wZ{Q#gc5ugAE!wjIZNZl8w(-;{T*q9j@ z)R2{BXQt|=q!#4lSLUYXl>nK=$wiq3CHX~q6}dUBLEip{4S4pw4-eV9T(ZD1IQ$5s zK!%Nq!=qWI%RWzY-*~z7@%722IU>F8OJv@>PgF0f+qOw9lF6{j+^>IA;;aVky>8*X zm!B{9E}W$%Gv)HB3iih(ymRJ>Hpe*U{<+n}?ZiC2rS!v;H4^SI=MJpu)DL}OldgD0 zS@ZU`v(JLhGXq$cY26Zig{qgK92;eGBG-_71<`|`-~!w)Cc?3`;;HkD zqY9ns>&@3c6qwJrs8FJ7#Yr<(jZ-KxQlnW6+_JXlv;JNo$R-K$R?z5J&)o&82wlITYtNn!OJ%>og1i#FV;+5Xat z@$F^Dk4}G@LXY`Rz1hBYdFug&urrUMX2=QN5jwd$Z^6f)^*Jx^YKQTbT5mirw8zwA z0c$1m-ZfttuUu)+=uP_P#B=M%be@(|52~MUT`bZlb>eA!kE34wogGOZ7!fg59Ov#? z8#~G00~kYkJB*#mO`vKF@UkQp6UL`F*)o|p=n0io5;q*fNJ$o zJBN)?Kr6F>7?gD2X(=0OH84qRK6&n}zvfxb&=n`np48qJdP+l6bHks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