Search in sources :

Example 16 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ConfigurationChangeListener method addOrRemoveJarFromFilesystem.

// when a new jar is added, if it does not exist in the current locator, download it from
// another locator.
// when a jar is removed, if it exists in the current locator, remove it.
private void addOrRemoveJarFromFilesystem(EntryEvent<String, Configuration> event) {
    String group = event.getKey();
    Configuration newConfig = (Configuration) event.getNewValue();
    Configuration oldConfig = (Configuration) event.getOldValue();
    Set<String> newJars = newConfig.getJarNames();
    Set<String> oldJars = (oldConfig == null) ? new HashSet<>() : oldConfig.getJarNames();
    Set<String> jarsAdded = new HashSet<>(newJars);
    Set<String> jarsRemoved = new HashSet<>(oldJars);
    jarsAdded.removeAll(oldJars);
    jarsRemoved.removeAll(newJars);
    if (!jarsAdded.isEmpty() && !jarsRemoved.isEmpty()) {
        throw new IllegalStateException("We don't expect to have jars both added and removed in one event");
    }
    for (String jarAdded : jarsAdded) {
        if (!jarExistsInFilesystem(group, jarAdded)) {
            try {
                sharedConfig.downloadJarFromOtherLocators(group, jarAdded);
            } catch (Exception e) {
                logger.error("Unable to add jar: " + jarAdded, e);
            }
        }
    }
    for (String jarRemoved : jarsRemoved) {
        File jar = sharedConfig.getPathToJarOnThisLocator(group, jarRemoved).toFile();
        if (jar.exists()) {
            try {
                FileUtils.forceDelete(jar);
            } catch (IOException e) {
                logger.error("Exception occurred while attempting to delete a jar from the filesystem: {}", jarRemoved, e);
            }
        }
    }
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 17 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method getAllJarsFromThisLocator.

// used when creating cluster config response
public Map<String, byte[]> getAllJarsFromThisLocator(Set<String> groups) throws IOException {
    Map<String, byte[]> jarNamesToJarBytes = new HashMap<>();
    for (String group : groups) {
        Configuration groupConfig = getConfiguration(group);
        if (groupConfig == null) {
            break;
        }
        Set<String> jars = groupConfig.getJarNames();
        for (String jar : jars) {
            byte[] jarBytes = getJarBytesFromThisLocator(group, jar);
            jarNamesToJarBytes.put(jar, jarBytes);
        }
    }
    return jarNamesToJarBytes;
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) HashMap(java.util.HashMap)

Example 18 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method modifyXmlAndProperties.

/**
   * we don't need to trigger the change listener for this modification, so it's ok to operate on
   * the original configuration object
   */
public void modifyXmlAndProperties(Properties properties, XmlEntity xmlEntity, String[] groups) {
    lockSharedConfiguration();
    try {
        if (groups == null) {
            groups = new String[] { ClusterConfigurationService.CLUSTER_CONFIG };
        }
        Region<String, Configuration> configRegion = getConfigurationRegion();
        for (String group : groups) {
            Configuration configuration = configRegion.get(group);
            if (configuration == null) {
                configuration = new Configuration(group);
            }
            if (xmlEntity != null) {
                String xmlContent = configuration.getCacheXmlContent();
                if (xmlContent == null || xmlContent.isEmpty()) {
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    CacheXmlGenerator.generateDefault(pw);
                    xmlContent = sw.toString();
                }
                try {
                    Document doc = XmlUtils.createAndUpgradeDocumentFromXml(xmlContent);
                    // Modify the cache attributes
                    XmlUtils.modifyRootAttributes(doc, xmlEntity);
                    // Change the xml content of the configuration and put it the config region
                    configuration.setCacheXmlContent(XmlUtils.prettyXml(doc));
                } catch (Exception e) {
                    logger.error("error updating cluster configuration for group {}", group, e);
                }
            }
            if (properties != null) {
                configuration.getGemfireProperties().putAll(properties);
            }
            configRegion.put(group, configuration);
        }
    } finally {
        unlockSharedConfiguration();
    }
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) StringWriter(java.io.StringWriter) Document(org.w3c.dom.Document) TimeoutException(org.apache.geode.cache.TimeoutException) CancelException(org.apache.geode.CancelException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) IOException(java.io.IOException) LeaseExpiredException(org.apache.geode.distributed.LeaseExpiredException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) PrintWriter(java.io.PrintWriter)

Example 19 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method loadSharedConfigurationFromDisk.

/**
   * Loads the internal region with the configuration in the configDirPath
   */
public void loadSharedConfigurationFromDisk() throws SAXException, ParserConfigurationException, TransformerException, IOException {
    lockSharedConfiguration();
    File[] groupNames = new File(this.configDirPath).listFiles((FileFilter) DirectoryFileFilter.INSTANCE);
    try {
        Map<String, Configuration> sharedConfiguration = new HashMap<>();
        for (File groupName : groupNames) {
            Configuration configuration = readConfiguration(groupName);
            sharedConfiguration.put(groupName.getName(), configuration);
        }
        Region<String, Configuration> clusterRegion = getConfigurationRegion();
        clusterRegion.clear();
        clusterRegion.putAll(sharedConfiguration);
        // Overwrite the security settings using the locator's properties, ignoring whatever
        // in the import
        persistSecuritySettings(clusterRegion);
    } finally {
        unlockSharedConfiguration();
    }
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) HashMap(java.util.HashMap) File(java.io.File)

Example 20 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method getConfigurationRegion.

/**
   * Gets the region containing the shared configuration data. The region is created , if it does
   * not exist already. Note : this could block if this locator contains stale persistent
   * configuration data.
   * 
   * @return {@link Region} ConfigurationRegion, this should never be null
   */
private Region<String, Configuration> getConfigurationRegion() {
    Region<String, Configuration> configRegion = this.cache.getRegion(CONFIG_REGION_NAME);
    try {
        if (configRegion == null) {
            File diskDir = new File(this.configDiskDirPath);
            if (!diskDir.exists()) {
                if (!diskDir.mkdirs()) {
                    // TODO: throw caught by containing try statement
                    throw new IOException("Cannot create directory at " + this.configDiskDirPath);
                }
            }
            File[] diskDirs = { diskDir };
            this.cache.createDiskStoreFactory().setDiskDirs(diskDirs).setAutoCompact(true).setMaxOplogSize(10).create(CLUSTER_CONFIG_DISK_STORE_NAME);
            AttributesFactory<String, Configuration> regionAttrsFactory = new AttributesFactory<>();
            regionAttrsFactory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
            regionAttrsFactory.setCacheListener(new ConfigurationChangeListener(this));
            regionAttrsFactory.setDiskStoreName(CLUSTER_CONFIG_DISK_STORE_NAME);
            regionAttrsFactory.setScope(Scope.DISTRIBUTED_ACK);
            InternalRegionArguments internalArgs = new InternalRegionArguments();
            internalArgs.setIsUsedForMetaRegion(true);
            internalArgs.setMetaRegionWithTransactions(false);
            configRegion = this.cache.createVMRegion(CONFIG_REGION_NAME, regionAttrsFactory.create(), internalArgs);
        }
    } catch (CancelException e) {
        if (configRegion == null) {
            this.status.set(SharedConfigurationStatus.STOPPED);
        }
        // CONFIG: don't rethrow as Exception, keep it a subclass of CancelException
        throw e;
    } catch (Exception e) {
        if (configRegion == null) {
            this.status.set(SharedConfigurationStatus.STOPPED);
        }
        throw new RuntimeException("Error occurred while initializing cluster configuration", e);
    }
    return configRegion;
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) IOException(java.io.IOException) TimeoutException(org.apache.geode.cache.TimeoutException) CancelException(org.apache.geode.CancelException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) IOException(java.io.IOException) LeaseExpiredException(org.apache.geode.distributed.LeaseExpiredException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) AttributesFactory(org.apache.geode.cache.AttributesFactory) ConfigurationChangeListener(org.apache.geode.management.internal.configuration.callbacks.ConfigurationChangeListener) CancelException(org.apache.geode.CancelException) File(java.io.File)

Aggregations

Configuration (org.apache.geode.management.internal.configuration.domain.Configuration)25 IOException (java.io.IOException)14 File (java.io.File)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 TransformerException (javax.xml.transform.TransformerException)8 SAXException (org.xml.sax.SAXException)8 CancelException (org.apache.geode.CancelException)7 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)7 TimeoutException (org.apache.geode.cache.TimeoutException)7 LeaseExpiredException (org.apache.geode.distributed.LeaseExpiredException)7 Properties (java.util.Properties)5 ClusterConfigurationService (org.apache.geode.distributed.internal.ClusterConfigurationService)5 InternalLocator (org.apache.geode.distributed.internal.InternalLocator)4 ConfigurationResponse (org.apache.geode.management.internal.configuration.messages.ConfigurationResponse)4 Document (org.w3c.dom.Document)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2