Skip to content

Commit

Permalink
working Detection of Service Endpoint Interface class added
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashackt committed Nov 7, 2016
1 parent 8908f41 commit 9a04947
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import de.codecentric.cxf.common.BootStarterCxfException;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebService;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceClient;
import java.lang.reflect.Constructor;
Expand All @@ -13,38 +16,58 @@

public class WebServiceAutoDetector {

public static Service searchWebServiceClient() throws BootStarterCxfException {
private static final Logger LOG = LoggerFactory.getLogger(WebServiceAutoDetector.class);

public static Service searchAndInstantiateWebServiceClient() throws BootStarterCxfException {

Class webServiceClientClass = scanForClassWithExtendJavaxXmlWsService(WebServiceClient.class);
return instantiateFromClass(webServiceClientClass);
}

public static String searchServiceEndpointInterfaceName() throws BootStarterCxfException {

return scanForClassWithExtendJavaxXmlWsService(WebService.class).getSimpleName();
}


private static Class scanForClassWithExtendJavaxXmlWsService(Class annotationNameToSearchUsingClass) throws BootStarterCxfException {

try {
Class webServiceClientClass = scanForClassWithExtendJavaxXmlWsService();
// see https://github.com/lukehutch/fast-classpath-scanner/wiki/1.-Usage#mechanism-2
FastClasspathScanner fastClasspathScanner = new FastClasspathScanner();

Constructor<?> constructor = webServiceClientClass.getConstructor();
ScanResult scanResult = fastClasspathScanner.scan();

Object webServiceClient = constructor.newInstance();
List<String> namesOfClassesWithAnnotation = scanResult.getNamesOfClassesWithAnnotation(annotationNameToSearchUsingClass);

return (Service) webServiceClient;
String className = namesOfClassesWithAnnotation.get(0);

} catch (ClassNotFoundException |
NoSuchMethodException |
IllegalAccessException |
InstantiationException |
InvocationTargetException exception) {
LOG.debug("Class found: {}", className);

return Class.forName(className);

} catch (ClassNotFoundException 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();
private static <T> T instantiateFromClass(Class clazz) throws BootStarterCxfException {

ScanResult scanResult = fastClasspathScanner.scan();
try {
Constructor<?> constructor = clazz.getConstructor();
Object instance = constructor.newInstance();
return (T) instance;

List<String> namesOfClassesWithAnnotation = scanResult.getNamesOfClassesWithAnnotation(WebServiceClient.class);
} catch (NoSuchMethodException |
IllegalAccessException |
InstantiationException |
InvocationTargetException exception) {
throw new BootStarterCxfException("Class couldn´t be instantiated", exception);
}

String webServiceClientClassName = namesOfClassesWithAnnotation.get(0);
System.out.println("Class found: " + webServiceClientClassName);

return Class.forName(webServiceClientClassName);
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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;
Expand All @@ -14,24 +12,23 @@
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() {
public void isWebServiceSuccessfullyDetected() throws BootStarterCxfException {

String interfaceName = WebServiceAutoDetector.searchServiceEndpointInterfaceName();


assertThat(false, is(true));
assertThat(interfaceName, is(equalTo("WeatherService")));
}

@Test
public void isWebServiceClientSuccessfullyDetected() throws BootStarterCxfException {

Service webServiceClient = WebServiceAutoDetector.searchWebServiceClient();
Service webServiceClient = WebServiceAutoDetector.searchAndInstantiateWebServiceClient();

assertThat(webServiceClient, is(notNullValue()));

Expand Down

0 comments on commit 9a04947

Please sign in to comment.