diff --git a/README.md b/README.md index 17af646..4d9354a 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ private CxfAutoConfiguration cxfAutoConfiguration; * REST-Healthstatus-Endpoints, that check whether the soap-Services (dynamic?!) are available - using Swagger/Springfox * Support for WS-Security-Token (e.g. used in [BiPro] * Discribe already supported Client-Mode for JAX-WS-Services that are not hostet, but used +* Complete Automation of Endpoint Initialization: Auto-Detect javax.jws.WebService annotated, through to jaxws-maven-plugin generated Class Files representing your SEI and Auto-Initializing them, if found # Contribution diff --git a/pom.xml b/pom.xml index d64cf7d..f7cadbc 100644 --- a/pom.xml +++ b/pom.xml @@ -140,6 +140,14 @@ fluent-hc ${fluent-hc.version} + + + + io.github.lukehutch + fast-classpath-scanner + 2.0.6 + + org.springframework.boot diff --git a/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java b/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java new file mode 100644 index 0000000..baf3d98 --- /dev/null +++ b/src/main/java/de/codecentric/cxf/autodetection/WebServiceAutoDetector.java @@ -0,0 +1,50 @@ +package de.codecentric.cxf.autodetection; + +import de.codecentric.cxf.common.BootStarterCxfException; +import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; +import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult; + +import javax.xml.ws.Service; +import javax.xml.ws.WebServiceClient; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + + +public class WebServiceAutoDetector { + + public static Service searchWebServiceClient() throws BootStarterCxfException { + + try { + Class webServiceClientClass = scanForClassWithExtendJavaxXmlWsService(); + + Constructor constructor = webServiceClientClass.getConstructor(); + + Object webServiceClient = constructor.newInstance(); + + return (Service) webServiceClient; + + } catch (ClassNotFoundException | + NoSuchMethodException | + IllegalAccessException | + InstantiationException | + InvocationTargetException exception) { + throw new BootStarterCxfException("WebServiceClient Class not found", exception); + } + + } + + private static Class scanForClassWithExtendJavaxXmlWsService() throws ClassNotFoundException { + // see https://github.com/lukehutch/fast-classpath-scanner/wiki/1.-Usage#mechanism-2 + FastClasspathScanner fastClasspathScanner = new FastClasspathScanner(); + + ScanResult scanResult = fastClasspathScanner.scan(); + + List namesOfClassesWithAnnotation = scanResult.getNamesOfClassesWithAnnotation(WebServiceClient.class); + + String webServiceClientClassName = namesOfClassesWithAnnotation.get(0); + System.out.println("Class found: " + webServiceClientClassName); + + return Class.forName(webServiceClientClassName); + } +} diff --git a/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java b/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java new file mode 100644 index 0000000..1fd8e01 --- /dev/null +++ b/src/test/java/de/codecentric/cxf/autodetection/WebServiceAutoDetectorTest.java @@ -0,0 +1,41 @@ +package de.codecentric.cxf.autodetection; + +import de.codecentric.cxf.autodetection.WebServiceAutoDetector; +import de.codecentric.cxf.common.BootStarterCxfException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.xml.namespace.QName; +import javax.xml.ws.Service; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.mockito.Matchers.contains; + + +@RunWith(SpringRunner.class) +public class WebServiceAutoDetectorTest { + + @Test + public void isWebServiceSuccessfullyDetected() { + + + + assertThat(false, is(true)); + } + + @Test + public void isWebServiceClientSuccessfullyDetected() throws BootStarterCxfException { + + Service webServiceClient = WebServiceAutoDetector.searchWebServiceClient(); + + assertThat(webServiceClient, is(notNullValue())); + + QName serviceNameQName = webServiceClient.getServiceName(); + assertThat(serviceNameQName.getLocalPart(), is(equalTo("Weather"))); + } +}