use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.
the class AbstractUserAgentAnalyzerDirect method initializeMatchers.
public synchronized void initializeMatchers() {
if (matchersHaveBeenInitialized) {
return;
}
LOG.info("Initializing Analyzer data structures");
if (allMatchers.isEmpty()) {
throw new InvalidParserConfigurationException("No matchers were loaded at all.");
}
long start = System.nanoTime();
allMatchers.forEach(Matcher::initialize);
long stop = System.nanoTime();
matchersHaveBeenInitialized = true;
LOG.info("Built in {} msec : Hashmap {}, Ranges map:{}", (stop - start) / 1000000, informMatcherActions.size(), informMatcherActionRanges.size());
for (Matcher matcher : allMatchers) {
if (matcher.getActionsThatRequireInput() == 0) {
zeroInputMatchers.add(matcher);
}
}
// Reset all Matchers
for (Matcher matcher : allMatchers) {
matcher.reset();
}
touchedMatchers = new MatcherList(32);
}
use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.
the class ConfigLoader method loadYaml.
private synchronized void loadYaml(Yaml yaml, InputStream yamlStream, String filename) {
Node loadedYaml;
try {
loadedYaml = yaml.compose(new UnicodeReader(yamlStream));
} catch (Exception e) {
throw new InvalidParserConfigurationException("Parse error in the file " + filename + ": " + e.getMessage(), e);
}
if (loadedYaml == null) {
LOG.warn("The file {} is empty", filename);
return;
}
// Get and check top level config
requireNodeInstanceOf(MappingNode.class, loadedYaml, filename, "File must be a Map");
MappingNode rootNode = (MappingNode) loadedYaml;
NodeTuple configNodeTuple = null;
for (NodeTuple tuple : rootNode.getValue()) {
String name = getKeyAsString(tuple, filename);
if ("config".equals(name)) {
configNodeTuple = tuple;
break;
}
if ("version".equals(name)) {
// Check the version information from the Yaml files
assertSameVersion(tuple, filename);
return;
}
}
require(configNodeTuple != null, loadedYaml, filename, "The top level entry MUST be 'config'.");
SequenceNode configNode = getValueAsSequenceNode(configNodeTuple, filename);
List<Node> configList = configNode.getValue();
for (Node configEntry : configList) {
requireNodeInstanceOf(MappingNode.class, configEntry, filename, "The entry MUST be a mapping");
NodeTuple entry = getExactlyOneNodeTuple((MappingNode) configEntry, filename);
MappingNode actualEntry = getValueAsMappingNode(entry, filename);
String entryType = getKeyAsString(entry, filename);
switch(entryType) {
case "lookup":
loadYamlLookup(actualEntry, filename);
break;
case "set":
loadYamlLookupSets(actualEntry, filename);
break;
case "matcher":
loadYamlMatcher(actualEntry, filename);
break;
case "test":
if (keepTests) {
loadYamlTestcase(actualEntry, filename);
}
break;
default:
throw new InvalidParserConfigurationException("Yaml config.(" + filename + ":" + actualEntry.getStartMark().getLine() + "): " + "Found unexpected config entry: " + entryType + ", allowed are 'lookup', 'set', 'matcher' and 'test'");
}
}
}
use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.
the class ConfigLoader method loadResources.
// ------------------------------------------
public void loadResources(String resourceString, boolean showLoadMessages, boolean areOptional) {
long startFiles = System.nanoTime();
final boolean loadingDefaultResources = DEFAULT_RESOURCES.equals(resourceString);
Map<String, Resource> resources = findAllResources(resourceString, showLoadMessages, areOptional, loadingDefaultResources);
doingOnlyASingleTest = false;
int maxFilenameLength = 0;
// Just trying to open a stream for one of the resources is enough to see if we can continue.
if (!resources.isEmpty()) {
Resource resource = resources.entrySet().iterator().next().getValue();
try (InputStream ignored = resource.getInputStream()) {
// Just seeing if opening this stream triggers an error.
// Having a useless statement that references the 'ignored' to avoid checkstyle and compilation warnings.
LOG.debug("Opening the resource worked. {}", ignored);
} catch (IOException e) {
LOG.error("Cannot load the resources (usually classloading problem).");
LOG.error("- Resource : {}", resource);
LOG.error("- Filename : {}", resource.getFilename());
LOG.error("- Description: {}", resource.getDescription());
if (loadingDefaultResources) {
LOG.warn("Falling back to the built in list of resources");
resources.clear();
} else {
LOG.error("FATAL: Unable to load the specified resources for {}", resourceString);
throw new InvalidParserConfigurationException("Error reading resources (" + resourceString + "): " + e.getMessage(), e);
}
}
}
if (resources.isEmpty()) {
if (areOptional) {
LOG.warn("NO optional resources were loaded from expression: {}", resourceString);
} else {
LOG.error("NO config files were found matching this expression: {}", resourceString);
if (loadingDefaultResources) {
LOG.warn("Unable to load the default resources, usually caused by classloader problems.");
LOG.warn("Retrying with built in list.");
PackagedRules.getRuleFileNames().forEach(s -> loadResources(s, false, false));
} else {
LOG.warn("If you are using wildcards in your expression then try explicitly naming all yamls files explicitly.");
throw new InvalidParserConfigurationException("There were no resources found for the expression: " + resourceString);
}
}
return;
}
// We need to determine if we are trying to load the yaml files TWICE.
// This can happen if the library is loaded twice (perhaps even two different versions).
Set<String> resourceBasenames = resources.keySet().stream().map(k -> k.replaceAll("^.*/", "")).collect(Collectors.toSet());
Set<String> alreadyLoadedResourceBasenames = yamlRules.keySet().stream().map(k -> k.replaceAll("^.*/", "")).collect(Collectors.toSet());
alreadyLoadedResourceBasenames.retainAll(resourceBasenames);
if (!alreadyLoadedResourceBasenames.isEmpty()) {
LOG.error("Trying to load these {} resources for the second time: {}", alreadyLoadedResourceBasenames.size(), alreadyLoadedResourceBasenames);
throw new InvalidParserConfigurationException("Trying to load " + alreadyLoadedResourceBasenames.size() + " resources for the second time");
}
for (Map.Entry<String, Resource> resourceEntry : resources.entrySet()) {
try {
Resource resource = resourceEntry.getValue();
String filename = resource.getFilename();
if (filename != null) {
maxFilenameLength = Math.max(maxFilenameLength, filename.length());
String yamlString;
try (InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
yamlString = bufferedReader.lines().collect(Collectors.joining("\n"));
}
}
yamlRules.put(filename, yamlString);
}
} catch (IOException e) {
throw new InvalidParserConfigurationException("Error reading resources: " + e.getMessage(), e);
}
}
long stopFiles = System.nanoTime();
try (Formatter msg = new Formatter(Locale.ENGLISH)) {
msg.format("- Loaded %2d files in %4d ms using expression: %s", resources.size(), (stopFiles - startFiles) / 1000000, resourceString);
LOG.info("{}", msg);
}
}
use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.
the class TestErrorHandling method runTest.
private void runTest(String resourceString, Matcher<String> expectedMessage) {
InvalidParserConfigurationException exception = assertThrows(InvalidParserConfigurationException.class, () -> {
UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.newBuilder().dropDefaultResources().keepTests().addResources(resourceString).build();
assertTrue(uaa.runTests(false, false));
});
assertTrue(expectedMessage.matches(exception.getMessage()), "Bad message:" + exception.getMessage());
}
use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.
the class TestAnnotationSystemAnonymous method testMissingAnnotations.
// ----------------------------------------------------------------
@Test
void testMissingAnnotations() {
InvalidParserConfigurationException exception = assertThrows(InvalidParserConfigurationException.class, () -> record = new MyErrorMapper() {
// Called via the annotation
@SuppressWarnings("unused")
public void setWasNotAnnotated(TestRecord testRecord, String value) {
fail("May NEVER call this method");
}
}.enrich(record));
assertEquals("You MUST specify at least 1 field to extract.", exception.getMessage());
}
Aggregations