private static FramePseudonymizer initMetalPseudonymizerWith(final XMLConfiguration config) throws InvalidKeyException, ClassNotFoundException { Processor processor = new Processor(); List<HierarchicalConfiguration<ImmutableNode>> fields = config.configurationsAt("fields.field"); for (HierarchicalConfiguration field : fields) { List<HierarchicalConfiguration> parameters = field.childConfigurationsAt("algorithm.params"); List<AlgorithmParameter> parameterList = new ArrayList<>(); for (HierarchicalConfiguration parameter : parameters) { parameterList.add(new AlgorithmParameter(parameter.getString("name"), parameter.getString("value"), Class.forName(parameter.getString("type")))); } // Add the transformers processor.addTransformer(Constraints.CONSTRAINT_MAP.get(field.getString("constraint")), field.getString("name"), getParseValueTransformer(ParseValueTransformerFactory.TransformerId.valueOf(field.getString("algorithm.name")), parameterList)); logger.info("Packets containing " + field.getString("name") + " will be pseudonymized based on this constraint: " + field.getString("constraint") + ", with this algorithm "+ field.getString("algorithm.name") ); } // TODO PEF-43: Move and change functionality. if (config.getBoolean("checksum_reset")) { processor.addTransformer(Constraints.IPV4_UDP_DNS, "udpchecksum",new IPv4UDPChecksumCalculator()) .addTransformer(Constraints.IPV6_UDP_DNS, "udpchecksum", new IPv6UDPChecksumCalculator()) .addTransformer(Constraints.IPV4_UDP_DNS, "headerchecksum", new IPv4ChecksumCalculator()) .addTransformer(Constraints.ICMP_DNS, "icmpchecksum", new IPv6UDPChecksumCalculator()); } return new FramePseudonymizer(processor); }
/** * Creates a node for the specified map. * * @param builder The node builder. * @param map The map. * @return The created node. */ private ImmutableNode mapToNode(final Builder builder, final Map<String, Object> map) { for (final Map.Entry<String, Object> entry : map.entrySet()) { final String key = entry.getKey(); final Object value = entry.getValue(); if (value instanceof List) { // For a list, add each list item as a child of this node. for (final Object item : (List)value) { addChildNode(builder, key, item); } } else { // Otherwise, add the value as a child of this node. addChildNode(builder, key, value); } } return builder.create(); }
protected ProjectConfig(HierarchicalConfiguration<ImmutableNode> config, @Nullable String metaborgVersion, @Nullable Collection<IExportConfig> sources, @Nullable Collection<LanguageIdentifier> compileDeps, @Nullable Collection<LanguageIdentifier> sourceDeps, @Nullable Collection<LanguageIdentifier> javaDeps) { this(config); if(metaborgVersion != null) { config.setProperty(PROP_METABORG_VERSION, metaborgVersion); } if(sources != null) { config.setProperty(PROP_SOURCES, sources); } if(compileDeps != null) { config.setProperty(PROP_COMPILE_DEPENDENCIES, compileDeps); } if(sourceDeps != null) { config.setProperty(PROP_SOURCE_DEPENDENCIES, sourceDeps); } if(javaDeps != null) { config.setProperty(PROP_JAVA_DEPENDENCIES, javaDeps); } }
/** * Gets the configuration from the given file. * * @param configFile * The configuration file with the configuration. * @return The configuration, or <code>null</code> when no configuration could be retrieved. */ public ConfigRequest<TConfig> getFromConfigFile(FileObject configFile, FileObject rootFolder) { final HierarchicalConfiguration<ImmutableNode> configuration; try { configuration = readConfig(configFile, rootFolder); } catch(ConfigurationException | IOException e) { // @formatter:off final IMessage message = MessageBuilder .create() .withSource(configFile) .withMessage("Unable to read configuration from " + configFile) .withException(e) .build(); // @formatter:on return new ConfigRequest<>(message); } if(configuration != null) { return toConfig(configuration, configFile); } else { return new ConfigRequest<>(); } }
protected LanguageSpecConfig(HierarchicalConfiguration<ImmutableNode> config, ProjectConfig projectConfig, @Nullable LanguageIdentifier id, @Nullable String name, @Nullable Boolean sdfEnabled, @Nullable Sdf2tableVersion sdf2tableVersion, @Nullable Boolean dataDependent, @Nullable String parseTable, @Nullable String completionsParseTable, @Nullable JSGLRVersion jsglrVersion, @Nullable Collection<LanguageContributionIdentifier> langContribs, @Nullable Collection<IGenerateConfig> generates, @Nullable Collection<IExportConfig> exports, @Nullable Collection<String> pardonedLanguages, @Nullable Boolean useBuildSystemSpec) { super(config, projectConfig, id, name, sdfEnabled, parseTable, completionsParseTable, sdf2tableVersion, dataDependent, jsglrVersion, langContribs, generates, exports); if(pardonedLanguages != null) { config.setProperty(PROP_PARDONED_LANGUAGES, pardonedLanguages); } if(useBuildSystemSpec != null) { config.setProperty(PROP_USE_BUILD_SYSTEM_SPEC, useBuildSystemSpec); } }
protected CommonsConfiguration[] createConfigurations(List<HierarchicalConfiguration<ImmutableNode>> subs) { CommonsConfiguration[] result = new CommonsConfiguration[subs.size()]; for (int i = 0; i < result.length; i++) { result[i] = new CommonsConfiguration((BaseHierarchicalConfiguration) subs.get(i)); } return result; }
private static void testPseudonymizationSettings(final XMLConfiguration config) throws ConfigurationException, IllegalArgumentException, ClassNotFoundException { List<HierarchicalConfiguration<ImmutableNode>> fields = config.configurationsAt("fields.field"); for (HierarchicalConfiguration field : fields) { // Test the name of the field. final String constraint = field.getString("constraint"); final String fieldName = field.getString("name"); final String algorithmName = field.getString("algorithm.name"); if ( (fieldName == null) || (constraint == null) || (algorithmName == null) ) throw new NoSuchElementException("Name of the field, constraint and algoritm needs to be set for each field."); // Test the constraint. if (!Constraints.CONSTRAINT_MAP.containsKey(field.getString("constraint"))) { throw new ConfigurationException(field.getString("constraint") + "should be defined in constraints list (Constraints.java)"); } // TODO PEF-78: Test if the fieldName matches with the context. // TODO PEF-79: No expression support. // Test if the algorithm is part of the configured algorithms. List<HierarchicalConfiguration> parameters = field.childConfigurationsAt("algorithm.params"); List<AlgorithmParameter> parameterList = new ArrayList<>(); //TODO PEF-81: Create here a specific parameter type. for (HierarchicalConfiguration parameter : parameters) { parameterList.add(new AlgorithmParameter(parameter.getString("name"), parameter.getString("value"), Class.forName(parameter.getString("type")))); } final StringBuilder buffer = new StringBuilder(); if (!testParseValueTransformerConfiguration(TransformerId.valueOf(algorithmName), parameterList, buffer)) { throw new ConfigurationException("Error in configuration for algorithm "+ algorithmName + " " + buffer.toString()); } } }
/** * Initializes a new instance of the {@link JacksonConfiguration} class. * * @param factory The Jackson factory to use. * @param config The configuration whose nodes to copy into this configuration. */ protected JacksonConfiguration(final JsonFactory factory, final HierarchicalConfiguration<ImmutableNode> config) { super(config); Preconditions.checkNotNull(factory); this.module = new SimpleModule(); this.mapper = new ObjectMapper(factory); this.mapper.registerModule(this.module); }
/** * Reads the configuration. * * @param reader The reader to read from. * @throws ConfigurationException * @throws IOException */ @Override public void read(final Reader reader) throws ConfigurationException, IOException { Preconditions.checkNotNull(reader); final HashMap<String, Object> settings = this.mapper.readValue(reader, HASH_MAP_TYPE_REFERENCE); final ImmutableNode rootNode = toNode(new Builder(), settings); this.getSubConfigurationParentModel().mergeRoot(rootNode, null, null, null, this); }
/** * Creates a node for the specified object. * * @param builder The node builder. * @param obj The object. * @return The created node. */ private ImmutableNode toNode(final Builder builder, final Object obj) { assert !(obj instanceof List); if (obj instanceof Map) { return mapToNode(builder, (Map<String, Object>) obj); } else { return valueToNode(builder, obj); } }
/** * Gets a serialization object from a node. * * @param node The node. * @return The object representing the node. */ private Object fromNode(final ImmutableNode node) { if (!node.getChildren().isEmpty()) { final Map<String, List<ImmutableNode>> children = getChildrenWithName(node); final HashMap<String, Object> map = new HashMap<>(); for (final Map.Entry<String, List<ImmutableNode>> entry : children.entrySet()) { assert !entry.getValue().isEmpty(); if (entry.getValue().size() == 1) { // Just one node. final ImmutableNode child = entry.getValue().get(0); final Object childValue = fromNode(child); map.put(entry.getKey(), childValue); } else { // Multiple nodes. final ArrayList<Object> list = new ArrayList<>(); for (final ImmutableNode child : entry.getValue()) { final Object childValue = fromNode(child); list.add(childValue); } map.put(entry.getKey(), list); } } return map; } else { return node.getValue(); } }
private Map<String, List<ImmutableNode>> getChildrenWithName(final ImmutableNode node) { final Map<String, List<ImmutableNode>> children = new HashMap<>(); for (final ImmutableNode child : node.getChildren()) { final String name = child.getNodeName(); if (!children.containsKey(name)) { children.put(name, new ArrayList<ImmutableNode>()); } children.get(name).add(child); } return children; }
@Test public void readConfiguration() throws IOException, ConfigurationException { // Arrange final T sut = create(); final String input = getExampleConfiguration(); // Act final StringReader reader = new StringReader(input); sut.read(reader); final HierarchicalConfiguration<ImmutableNode> objs = sut.configurationAt("listOfObjs(0)", true); // Assert assertThat(sut.getString("name"), is("testName")); assertThat(sut.getString("obj.name"), is("test")); assertThat(sut.getInt("obj.value"), is(1)); assertThat(sut.getString("listOfObjs(0).name"), is("testname")); assertThat(sut.getInt("listOfObjs(0).value"), is(4)); assertThat(sut.getString("listOfObjs(1).name"), is("other")); assertThat(sut.getInt("listOfObjs(1).value"), is(20)); assertThat(sut.getStringArray("listOfObjs.name"), is(new String[]{"testname", "other"})); assertThat(sut.getProperty("nullValue"), is(nullValue())); assertThat(sut.getProperty("emptyList"), is(nullValue())); assertThat(sut.getString("listOfComplexObjs(0).someObj.name"), is("a name")); assertThat(sut.getInt("listOfComplexObjs(0).someObj.value"), is(20)); assertThat(sut.getString("listOfComplexObjs(1).someObj.name"), is("another name")); assertThat(sut.getInt("listOfComplexObjs(1).someObj.value"), is(40)); }
@Override protected ConfigRequest<ISpoofaxLanguageSpecConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) { final SpoofaxProjectConfig projectConfig = new SpoofaxProjectConfig(config); final SpoofaxLanguageSpecConfig languageSpecConfig = new SpoofaxLanguageSpecConfig(config, projectConfig); final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile); final Collection<IMessage> messages = languageSpecConfig.validate(mb); return new ConfigRequest<ISpoofaxLanguageSpecConfig>(languageSpecConfig, messages); }
@Override protected HierarchicalConfiguration<ImmutableNode> fromConfig(ISpoofaxLanguageSpecConfig config) { if(!(config instanceof IConfig)) { configBuilder.reset(); configBuilder.copyFrom(config); config = configBuilder.build(null); } return ((IConfig) config).getConfig(); }
private LanguageSpecBuildPhase phase(HierarchicalConfiguration<ImmutableNode> config) { final String phaseStr = config.getString("phase"); try { return phaseStr != null ? LanguageSpecBuildPhase.valueOf(phaseStr) : defaultPhase; } catch(IllegalArgumentException e) { logger.warn("Language specification build phase with name {} does not exist, defaulting to {}", e, phaseStr, defaultPhase); return defaultPhase; } }
@Override protected ConfigRequest<IProjectConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) { final ProjectConfig projectConfig = new ProjectConfig(config); final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile); final Collection<IMessage> messages = projectConfig.validate(mb); return new ConfigRequest<IProjectConfig>(projectConfig, messages); }
@Override protected HierarchicalConfiguration<ImmutableNode> fromConfig(IProjectConfig config) { if(!(config instanceof IConfig)) { configBuilder.reset(); configBuilder.copyFrom(config); config = configBuilder.build((FileObject) null); } return ((IConfig) config).getConfig(); }
public ProjectConfig(HierarchicalConfiguration<ImmutableNode> config) { super(config); // Set metaborgVersion to default if it was not set in the config. if(!config.containsKey(PROP_METABORG_VERSION)) { config.setProperty(PROP_METABORG_VERSION, MetaborgConstants.METABORG_VERSION); } }
protected @Nullable HierarchicalConfiguration<ImmutableNode> configurationAt(String key, boolean supportUpdates) { try { return config.configurationAt(key, supportUpdates); } catch(ConfigurationRuntimeException ex) { return null; } }
/** * Writes the configuration for the given subject. * * @param rootDirectory * The root directory of the subject to set the configuration for. * @param config * The configuration, or <code>null</code> to remove an existing configuration. * @param access */ public void write(FileObject rootDirectory, TConfig config, @Nullable IFileAccess access) throws ConfigException { final FileObject configFile; try { configFile = getConfigFile(rootDirectory); } catch(FileSystemException e) { throw new ConfigException("Unable to locate configuration at root directory " + rootDirectory, e); } if(access != null) { access.write(configFile); } final HierarchicalConfiguration<ImmutableNode> configuration = fromConfig(config); writeConfig(configFile, configuration, rootDirectory); }
@Override protected ConfigRequest<ILanguageComponentConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) { final ProjectConfig projectConfig = new ProjectConfig(config); final LanguageComponentConfig languageComponentConfig = new LanguageComponentConfig(config, projectConfig); final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile); final Collection<IMessage> messages = languageComponentConfig.validate(mb); return new ConfigRequest<ILanguageComponentConfig>(languageComponentConfig, messages); }
@Override protected HierarchicalConfiguration<ImmutableNode> fromConfig(ILanguageComponentConfig config) { if(!(config instanceof IConfig)) { configBuilder.reset(); configBuilder.copyFrom(config); config = configBuilder.build((FileObject) null); } return ((IConfig) config).getConfig(); }
protected HierarchicalConfiguration<ImmutableNode> cloneConfiguration(IConfig config) { // Clone configuration. final IConfig iconfig = (IConfig) config; final HierarchicalConfiguration<ImmutableNode> apacheConfig = iconfig.getConfig(); final Configuration clonedConfig = ConfigurationUtils.cloneConfiguration(apacheConfig); @SuppressWarnings("unchecked") final HierarchicalConfiguration<ImmutableNode> clonedHierachicalConfig = (HierarchicalConfiguration<ImmutableNode>) clonedConfig; return clonedHierachicalConfig; }
@Override protected ConfigRequest<ILanguageSpecConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) { final ProjectConfig projectConfig = new ProjectConfig(config); final LanguageSpecConfig languageSpecConfig = new LanguageSpecConfig(config, projectConfig); final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile); final Collection<IMessage> messages = languageSpecConfig.validate(mb); return new ConfigRequest<>(languageSpecConfig, messages); }
@Override protected HierarchicalConfiguration<ImmutableNode> fromConfig(ILanguageSpecConfig config) { if(!(config instanceof IConfig)) { configBuilder.reset(); configBuilder.copyFrom(config); config = configBuilder.build(null); } return ((IConfig) config).getConfig(); }
protected SpoofaxProjectConfig(HierarchicalConfiguration<ImmutableNode> config, String metaborgVersion, Collection<IExportConfig> sources, Collection<LanguageIdentifier> compileDeps, Collection<LanguageIdentifier> sourceDeps, Collection<LanguageIdentifier> javaDeps, Boolean typesmart, NaBL2Config nabl2Config) { super(config, metaborgVersion, sources, compileDeps, sourceDeps, javaDeps); if(typesmart != null) { config.setProperty(PROP_STR_TYPESMART, typesmart); } if(nabl2Config != null) { Optional.ofNullable(configurationAt(PROP_NABL2, true)) .ifPresent(c -> NaBL2ConfigReaderWriter.write(nabl2Config, c)); } }
@Override protected ConfigRequest<ISpoofaxProjectConfig> toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) { final SpoofaxProjectConfig projectConfig = new SpoofaxProjectConfig(config); final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile); final Collection<IMessage> messages = projectConfig.validate(mb); return new ConfigRequest<>(projectConfig, messages); }
@Override protected HierarchicalConfiguration<ImmutableNode> fromConfig(ISpoofaxProjectConfig config) { if(!(config instanceof IConfig)) { configBuilder.reset(); configBuilder.copyFrom(config); config = configBuilder.build((FileObject) null); } return ((IConfig) config).getConfig(); }
public static void write(NaBL2Config nabl2Config, HierarchicalConfiguration<ImmutableNode> config) { if(nabl2Config.incremental()) { config.setProperty(PROP_INCREMENTAL, nabl2Config.incremental()); } if(!nabl2Config.debug().flags().isEmpty()) { config.setProperty(PROP_DEBUG, nabl2Config.debug().flags()); } }
public SpoofaxLanguageSpecConfig(HierarchicalConfiguration<ImmutableNode> config, SpoofaxProjectConfig projectConfig) { super(config, projectConfig); this.projectConfig = projectConfig; }
public ProjectConfig build(HierarchicalConfiguration<ImmutableNode> configuration) { return new ProjectConfig(configuration, metaborgVersion, sources, compileDeps, sourceDeps, javaDeps); }
@Override protected JacksonConfiguration createNew( @Nullable HierarchicalConfiguration<ImmutableNode> sourceConfiguration) { final JacksonConfiguration config = new YamlConfiguration(sourceConfiguration); config.setConversionHandler(new MetaborgConversionHandler()); return config; }
public AConfig(HierarchicalConfiguration<ImmutableNode> config) { this.config = config; }
@Override public HierarchicalConfiguration<ImmutableNode> getConfig() { return this.config; }
public LanguageComponentConfig(HierarchicalConfiguration<ImmutableNode> config, ProjectConfig projectConfig) { super(config); this.projectConfig = projectConfig; }
protected LanguageComponentConfig(HierarchicalConfiguration<ImmutableNode> config, ProjectConfig projectConfig, @Nullable LanguageIdentifier identifier, @Nullable String name, @Nullable Boolean sdfEnabled, @Nullable String parseTable, @Nullable String completionParseTable, @Nullable Sdf2tableVersion sdf2tableVersion, @Nullable Boolean dataDependent, @Nullable JSGLRVersion jsglrVersion, @Nullable Collection<LanguageContributionIdentifier> langContribs, @Nullable Collection<IGenerateConfig> generates, @Nullable Collection<IExportConfig> exports) { super(config); this.projectConfig = projectConfig; if(sdfEnabled != null) { config.setProperty(PROP_SDF_ENABLED, sdfEnabled); } if(sdf2tableVersion != null) { config.setProperty(PROP_SDF2TABLE_VERSION, sdf2tableVersion); } if(dataDependent != null) { config.setProperty(PROP_SDF_DATA_DEPENDENT, dataDependent); } if(parseTable != null) { config.setProperty(PROP_SDF_PARSE_TABLE, parseTable); } if(completionParseTable != null) { config.setProperty(PROP_SDF_COMPLETION_PARSE_TABLE, completionParseTable); } if(jsglrVersion != null) { config.setProperty(PROP_SDF_JSGLR_VERSION, jsglrVersion); } if(name != null) { config.setProperty(PROP_NAME, name); } if(identifier != null) { config.setProperty(PROP_IDENTIFIER, identifier); } if(langContribs != null) { config.setProperty(PROP_LANGUAGE_CONTRIBUTIONS, langContribs); } if(generates != null) { config.setProperty(PROP_GENERATES, generates); } if(exports != null) { config.setProperty(PROP_EXPORTS, exports); } }
@Override public HierarchicalConfiguration<ImmutableNode> getConfig() { return projectConfig.getConfig(); }
void setConfiguration(HierarchicalConfiguration<ImmutableNode> configuration) { this.configuration = configuration; }
public LanguageSpecConfig(HierarchicalConfiguration<ImmutableNode> config, ProjectConfig projectConfig) { super(config, projectConfig); }