Search in sources :

Example 6 with ServiceLifecycleException

use of org.apache.knox.gateway.services.ServiceLifecycleException in project knox by apache.

the class DefaultTopologyService method init.

@Override
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
    String gatewayConfDir = config.getGatewayConfDir();
    if (gatewayConfDir != null) {
        System.setProperty(ServiceDiscovery.CONFIG_DIR_PROPERTY, gatewayConfDir);
    }
    try {
        listeners = new HashSet<>();
        topologies = new HashMap<>();
        topologiesDirectory = calculateAbsoluteTopologiesDir(config);
        File configDirectory = calculateAbsoluteConfigDir(config);
        descriptorsDirectory = new File(configDirectory, "descriptors");
        sharedProvidersDirectory = new File(configDirectory, "shared-providers");
        // Add support for conf/topologies
        initListener(topologiesDirectory, this, this);
        // Add support for conf/descriptors
        descriptorsMonitor = new DescriptorsMonitor(config, topologiesDirectory, aliasService);
        initListener(descriptorsDirectory, descriptorsMonitor, descriptorsMonitor);
        log.monitoringDescriptorChangesInDirectory(descriptorsDirectory.getAbsolutePath());
        // Add support for conf/shared-providers
        SharedProviderConfigMonitor spm = new SharedProviderConfigMonitor(descriptorsMonitor, descriptorsDirectory);
        initListener(sharedProvidersDirectory, spm, spm);
        log.monitoringProviderConfigChangesInDirectory(sharedProvidersDirectory.getAbsolutePath());
        // For all the descriptors currently in the descriptors dir at start-up time, determine if topology regeneration
        // is required.
        // This happens prior to the start-up loading of the topologies.
        String[] descriptorFilenames = descriptorsDirectory.list();
        if (descriptorFilenames != null) {
            for (String descriptorFilename : descriptorFilenames) {
                if (DescriptorsMonitor.isDescriptorFile(descriptorFilename)) {
                    String topologyName = FilenameUtils.getBaseName(descriptorFilename);
                    File existingDescriptorFile = getExistingFile(descriptorsDirectory, topologyName);
                    // If there isn't a corresponding topology file, or if the descriptor has been modified since the
                    // corresponding topology file was generated, then trigger generation of one
                    File matchingTopologyFile = getExistingFile(topologiesDirectory, topologyName);
                    if (matchingTopologyFile == null || matchingTopologyFile.lastModified() < existingDescriptorFile.lastModified()) {
                        descriptorsMonitor.onFileChange(existingDescriptorFile);
                    } else {
                        // If regeneration is NOT required, then we at least need to report the provider configuration
                        // reference relationship (KNOX-1144)
                        String normalizedDescriptorPath = FilenameUtils.normalize(existingDescriptorFile.getAbsolutePath());
                        // Parse the descriptor to determine the provider config reference
                        SimpleDescriptor sd = SimpleDescriptorFactory.parse(normalizedDescriptorPath);
                        if (sd != null) {
                            File referencedProviderConfig = getExistingFile(sharedProvidersDirectory, FilenameUtils.getBaseName(sd.getProviderConfig()));
                            if (referencedProviderConfig != null) {
                                List<String> references = descriptorsMonitor.getReferencingDescriptors(referencedProviderConfig.getAbsolutePath());
                                if (!references.contains(normalizedDescriptorPath)) {
                                    references.add(normalizedDescriptorPath);
                                }
                            }
                        }
                    }
                }
            }
        }
        // Initialize the remote configuration monitor, if it has been configured
        remoteMonitor = RemoteConfigurationMonitorFactory.get(config);
    } catch (IOException | SAXException io) {
        throw new ServiceLifecycleException(io.getMessage());
    }
}
Also used : ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException) SimpleDescriptor(org.apache.knox.gateway.topology.simple.SimpleDescriptor) SAXException(org.xml.sax.SAXException) File(java.io.File)

Example 7 with ServiceLifecycleException

use of org.apache.knox.gateway.services.ServiceLifecycleException in project knox by apache.

the class AmbariServiceDefinitionTest method startGatewayServer.

public static void startGatewayServer() throws Exception {
    services = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        services.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    topos = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    gateway = GatewayServer.startGateway(config, services);
    MatcherAssert.assertThat("Failed to start gateway.", gateway, notNullValue());
    gatewayPort = gateway.getAddresses()[0].getPort();
    gatewayUrl = "http://localhost:" + gatewayPort + "/" + config.getGatewayPath();
    String topologyPath = "/test-topology";
    clusterPath = "/" + config.getGatewayPath() + topologyPath;
    clusterUrl = gatewayUrl + topologyPath;
    LOG.info("Gateway port = " + gateway.getAddresses()[0].getPort());
    params = new Properties();
    params.put("AMBARI_URL", "http://localhost:" + mockAmbari.getPort());
    velocity = new VelocityEngine();
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
    velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocity.init();
    context = new VelocityContext();
    context.put("cluster_url", clusterUrl);
    context.put("cluster_path", clusterPath);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Properties(java.util.Properties)

Example 8 with ServiceLifecycleException

use of org.apache.knox.gateway.services.ServiceLifecycleException in project knox by apache.

the class GatewayAdminTopologyFuncTest method setupGateway.

public static void setupGateway(GatewayTestConfig testConfig) throws Exception {
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    config = testConfig;
    testConfig.setGatewayHomeDir(gatewayDir.getAbsolutePath());
    File topoDir = new File(testConfig.getGatewayTopologyDir());
    topoDir.mkdirs();
    File deployDir = new File(testConfig.getGatewayDeploymentDir());
    deployDir.mkdirs();
    File providerConfigDir = new File(testConfig.getGatewayConfDir(), "shared-providers");
    providerConfigDir.mkdirs();
    File descriptorsDir = new File(testConfig.getGatewayConfDir(), "descriptors");
    descriptorsDir.mkdirs();
    File descriptor = new File(topoDir, "admin.xml");
    FileOutputStream stream = new FileOutputStream(descriptor);
    createKnoxTopology().toStream(stream);
    stream.close();
    File descriptor2 = new File(topoDir, "test-cluster.xml");
    FileOutputStream stream2 = new FileOutputStream(descriptor2);
    createNormalTopology().toStream(stream2);
    stream.close();
    DefaultGatewayServices srvcs = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        srvcs.init(testConfig, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    gateway = GatewayServer.startGateway(testConfig, srvcs);
    MatcherAssert.assertThat("Failed to start gateway.", gateway, notNullValue());
    LOG.info("Gateway port = " + gateway.getAddresses()[0].getPort());
    gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath();
    clusterUrl = gatewayUrl + "/admin";
}
Also used : HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File)

Example 9 with ServiceLifecycleException

use of org.apache.knox.gateway.services.ServiceLifecycleException in project knox by apache.

the class GatewayAppFuncTest method startGatewayServer.

public static void startGatewayServer() throws Exception {
    services = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        services.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    topos = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    gateway = GatewayServer.startGateway(config, services);
    MatcherAssert.assertThat("Failed to start gateway.", gateway, notNullValue());
    gatewayPort = gateway.getAddresses()[0].getPort();
    gatewayUrl = "http://localhost:" + gatewayPort + "/" + config.getGatewayPath();
    clusterUrl = gatewayUrl + "/test-topology";
    LOG.info("Gateway port = " + gateway.getAddresses()[0].getPort());
    params = new Properties();
    params.put("LDAP_URL", driver.getLdapUrl());
    params.put("WEBHDFS_URL", "http://localhost:" + mockWebHdfs.getPort());
}
Also used : HashMap(java.util.HashMap) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) Properties(java.util.Properties)

Example 10 with ServiceLifecycleException

use of org.apache.knox.gateway.services.ServiceLifecycleException in project knox by apache.

the class GatewayCorrelationIdTest method setupGateway.

public static void setupGateway() throws Exception {
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    GatewayTestConfig testConfig = new GatewayTestConfig();
    config = testConfig;
    testConfig.setGatewayHomeDir(gatewayDir.getAbsolutePath());
    File topoDir = new File(testConfig.getGatewayTopologyDir());
    topoDir.mkdirs();
    File deployDir = new File(testConfig.getGatewayDeploymentDir());
    deployDir.mkdirs();
    File descriptor = new File(topoDir, "test-cluster.xml");
    FileOutputStream stream = new FileOutputStream(descriptor);
    createTopology().toStream(stream);
    stream.close();
    DefaultGatewayServices srvcs = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        srvcs.init(testConfig, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    gateway = GatewayServer.startGateway(testConfig, srvcs);
    MatcherAssert.assertThat("Failed to start gateway.", gateway, notNullValue());
    LOG.info("Gateway port = " + gateway.getAddresses()[0].getPort());
    gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath();
    clusterUrl = gatewayUrl + "/test-cluster";
}
Also used : FileOutputStream(java.io.FileOutputStream) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) File(java.io.File)

Aggregations

ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)40 File (java.io.File)31 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)30 HashMap (java.util.HashMap)29 FileOutputStream (java.io.FileOutputStream)14 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)13 Test (org.junit.Test)9 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 Topology (org.apache.knox.gateway.topology.Topology)8 Service (org.apache.knox.gateway.topology.Service)7 Param (org.apache.knox.gateway.topology.Param)6 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)6 Document (org.w3c.dom.Document)6 URL (java.net.URL)5 Provider (org.apache.knox.gateway.topology.Provider)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Properties (java.util.Properties)4 GatewayServices (org.apache.knox.gateway.services.GatewayServices)4 AliasService (org.apache.knox.gateway.services.security.AliasService)4 IOException (java.io.IOException)3