Search in sources :

Example 1 with ServiceDeploymentContributor

use of org.apache.knox.gateway.deploy.ServiceDeploymentContributor in project knox by apache.

the class ServiceDefinitionsLoaderTest method testServiceDefinitionLoading.

@Test
public void testServiceDefinitionLoading() {
    URL url = ClassLoader.getSystemResource("services");
    Set<ServiceDeploymentContributor> contributors = ServiceDefinitionsLoader.loadServiceDefinitions(new File(url.getFile()));
    assertNotNull(contributors);
    assertEquals(2, contributors.size());
    for (ServiceDeploymentContributor contributor : contributors) {
        if (contributor.getName().equals("foo")) {
            Assert.assertEquals("1.0.0", contributor.getVersion().toString());
            assertEquals("FOO", contributor.getRole());
        } else if (contributor.getName().equals("bar")) {
            Assert.assertEquals("2.0.0", contributor.getVersion().toString());
            assertEquals("BAR", contributor.getRole());
        } else {
            fail("the loaded services don't match the test input");
        }
    }
}
Also used : ServiceDeploymentContributor(org.apache.knox.gateway.deploy.ServiceDeploymentContributor) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 2 with ServiceDeploymentContributor

use of org.apache.knox.gateway.deploy.ServiceDeploymentContributor in project knox by apache.

the class ServiceDefinitionsLoader method loadServiceDefinitions.

public static Set<ServiceDeploymentContributor> loadServiceDefinitions(File servicesDir) {
    Set<ServiceDeploymentContributor> contributors = new HashSet<>();
    if (servicesDir.exists() && servicesDir.isDirectory()) {
        try {
            JAXBContext context = JAXBContext.newInstance(ServiceDefinition.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            for (File file : getFileList(servicesDir)) {
                try {
                    FileInputStream inputStream = new FileInputStream(file);
                    ServiceDefinition definition = (ServiceDefinition) unmarshaller.unmarshal(inputStream);
                    // look for rewrite rules as a sibling (for now)
                    UrlRewriteRulesDescriptor rewriteRulesDescriptor = loadRewriteRules(file.getParentFile());
                    contributors.add(new ServiceDefinitionDeploymentContributor(definition, rewriteRulesDescriptor));
                    log.addedServiceDefinition(definition.getName(), definition.getRole(), definition.getVersion());
                } catch (FileNotFoundException e) {
                    log.failedToFindServiceDefinitionFile(file.getAbsolutePath(), e);
                }
            }
        } catch (JAXBException e) {
            log.failedToLoadServiceDefinition(SERVICE_FILE_NAME, e);
        }
    }
    return contributors;
}
Also used : ServiceDefinitionDeploymentContributor(org.apache.knox.gateway.deploy.impl.ServiceDefinitionDeploymentContributor) ServiceDeploymentContributor(org.apache.knox.gateway.deploy.ServiceDeploymentContributor) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)2 ServiceDeploymentContributor (org.apache.knox.gateway.deploy.ServiceDeploymentContributor)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 ServiceDefinitionDeploymentContributor (org.apache.knox.gateway.deploy.impl.ServiceDefinitionDeploymentContributor)1 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)1 ServiceDefinition (org.apache.knox.gateway.service.definition.ServiceDefinition)1 Test (org.junit.Test)1