Search in sources :

Example 6 with ConfigurationWriter

use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.

the class ContainerResource method convertToJson.

private CompletionStage<RestResponse> convertToJson(RestRequest restRequest) {
    NettyRestResponse.Builder responseBuilder = checkCacheManager(restRequest);
    if (responseBuilder.getHttpStatus() == NOT_FOUND)
        return completedFuture(responseBuilder.build());
    ContentSource contents = restRequest.contents();
    ConfigurationBuilderHolder builderHolder = parserRegistry.parse(new String(contents.rawContent(), UTF_8));
    Map.Entry<String, ConfigurationBuilder> entry = builderHolder.getNamedConfigurationBuilders().entrySet().iterator().next();
    StringBuilderWriter sw = new StringBuilderWriter();
    try (ConfigurationWriter w = ConfigurationWriter.to(sw).withType(APPLICATION_JSON).build()) {
        invocationHelper.getParserRegistry().serialize(w, entry.getKey(), entry.getValue().build());
    }
    responseBuilder.contentType(APPLICATION_JSON).entity(sw.toString());
    return completedFuture(responseBuilder.build());
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) StringBuilderWriter(org.infinispan.commons.io.StringBuilderWriter) ContentSource(org.infinispan.rest.framework.ContentSource) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) NettyRestResponse(org.infinispan.rest.NettyRestResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 7 with ConfigurationWriter

use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.

the class CounterConfigurationSerializer method serializeConfigurations.

/**
 * It serializes a {@link List} of {@link AbstractCounterConfiguration} to an {@link OutputStream}.
 * @param os the {@link OutputStream} to write to.
 * @param configs the {@link List} if {@link AbstractCounterConfiguration}.
 */
public void serializeConfigurations(OutputStream os, Collection<AbstractCounterConfiguration> configs) {
    BufferedOutputStream output = new BufferedOutputStream(os);
    ConfigurationWriter writer = ConfigurationWriter.to(output).build();
    writer.writeStartDocument();
    writer.writeStartMap(Element.COUNTERS);
    writeConfigurations(writer, configs);
    writer.writeEndMap();
    writer.writeEndDocument();
    Util.close(writer);
}
Also used : ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with ConfigurationWriter

use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.

the class OverlayLocalConfigurationStorage method persistConfigurations.

private void persistConfigurations(String prefix, File file, File lock, Set<String> configNames) {
    // Renaming is done using file locks, which are acquired by the entire JVM.
    // Use a regular lock as well, so another thread cannot lock the file at the same time
    // and cause an OverlappingFileLockException.
    persistenceLock.lock();
    try {
        GlobalConfiguration globalConfiguration = configurationManager.getGlobalConfiguration();
        File sharedDirectory = new File(globalConfiguration.globalState().sharedPersistentLocation());
        sharedDirectory.mkdirs();
        File temp = File.createTempFile(prefix, null, sharedDirectory);
        Map<String, Configuration> configurationMap = new HashMap<>();
        for (String config : configNames) {
            configurationMap.put(config, configurationManager.getConfiguration(config, true));
        }
        try (ConfigurationWriter writer = ConfigurationWriter.to(new FileOutputStream(temp)).clearTextSecrets(true).prettyPrint(true).build()) {
            parserRegistry.serialize(writer, null, configurationMap);
        }
        try {
            renameTempFile(temp, lock, file);
        } catch (Exception e) {
            throw CONFIG.cannotRenamePersistentFile(temp.getAbsolutePath(), file, e);
        }
    } catch (Exception e) {
        throw CONFIG.errorPersistingGlobalConfiguration(e);
    } finally {
        persistenceLock.unlock();
    }
}
Also used : GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) GlobalStateConfiguration(org.infinispan.configuration.global.GlobalStateConfiguration) Configuration(org.infinispan.configuration.cache.Configuration) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileOutputStream(java.io.FileOutputStream) Util.renameTempFile(org.infinispan.commons.util.Util.renameTempFile) File(java.io.File) CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 9 with ConfigurationWriter

use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.

the class ParserRegistry method serialize.

/**
 * Serializes a full configuration to an {@link OutputStream}
 *
 * @param os the output stream where the configuration should be serialized to
 * @param globalConfiguration the global configuration. Can be null
 * @param configurations a map of named configurations
 * @deprecated use {@link #serialize(ConfigurationWriter, GlobalConfiguration, Map)} instead
 */
@Deprecated
public void serialize(OutputStream os, GlobalConfiguration globalConfiguration, Map<String, Configuration> configurations) {
    ConfigurationWriter writer = ConfigurationWriter.to(os).build();
    serialize(writer, globalConfiguration, configurations);
    try {
        writer.close();
    } catch (Exception e) {
        throw new ConfigurationWriterException(e);
    }
}
Also used : ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) ConfigurationWriterException(org.infinispan.commons.configuration.io.ConfigurationWriterException) ConfigurationWriterException(org.infinispan.commons.configuration.io.ConfigurationWriterException) ConfigurationReaderException(org.infinispan.commons.configuration.io.ConfigurationReaderException) CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with ConfigurationWriter

use of org.infinispan.commons.configuration.io.ConfigurationWriter in project infinispan by infinispan.

the class SerializationUtils method toJson.

public static String toJson(RemoteStoreConfiguration configuration) {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    RemoteStoreConfigurationBuilder storeBuilder = builder.persistence().addStore(RemoteStoreConfigurationBuilder.class);
    storeBuilder.read(configuration);
    StringBuilderWriter sw = new StringBuilderWriter();
    try (ConfigurationWriter w = ConfigurationWriter.to(sw).withType(APPLICATION_JSON).build()) {
        parserRegistry.serialize(w, null, builder.build());
    }
    return Json.read(sw.toString()).at("local-cache").at("persistence").toString();
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) RemoteStoreConfigurationBuilder(org.infinispan.persistence.remote.configuration.RemoteStoreConfigurationBuilder) StringBuilderWriter(org.infinispan.commons.io.StringBuilderWriter) ConfigurationWriter(org.infinispan.commons.configuration.io.ConfigurationWriter) RemoteStoreConfigurationBuilder(org.infinispan.persistence.remote.configuration.RemoteStoreConfigurationBuilder)

Aggregations

ConfigurationWriter (org.infinispan.commons.configuration.io.ConfigurationWriter)16 StringBuilderWriter (org.infinispan.commons.io.StringBuilderWriter)9 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)9 Configuration (org.infinispan.configuration.cache.Configuration)8 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)8 NettyRestResponse (org.infinispan.rest.NettyRestResponse)6 Map (java.util.Map)5 ConfigurationBuilderHolder (org.infinispan.configuration.parsing.ConfigurationBuilderHolder)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 ParserRegistry (org.infinispan.configuration.parsing.ParserRegistry)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4 IOException (java.io.IOException)3 MediaType (org.infinispan.commons.dataconversion.MediaType)3 RemoteStoreConfiguration (org.infinispan.persistence.remote.configuration.RemoteStoreConfiguration)3 MediaTypeUtils.negotiateMediaType (org.infinispan.rest.resources.MediaTypeUtils.negotiateMediaType)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2