use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class AutoProxAproxMigrationAction method migrate.
@Override
public boolean migrate() throws IndyLifecycleException {
final DataFile dataDir = ffManager.getDataFile(config.getBasedir());
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Scanning {} for autoprox rules...", dataDir);
int changed = 0;
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.trace("Checking for autoprox rule in: {}", pathname);
return pathname.getName().endsWith(".groovy");
});
logger.info("Migrating autoprox rules.");
for (final DataFile script : scripts) {
try {
String scriptContent = script.readString();
String migrated = scriptContent.replaceAll("A[Pp]rox", "Indy").replaceAll("aprox", "indy");
if (!migrated.equals(scriptContent)) {
logger.info("Migrating autoprox rule in: {}", script.getPath());
script.writeString(migrated, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating to Indy packages / naming"));
changed++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return changed != 0;
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class TemplatingEngine method getTemplate.
// TODO Cache these...though this will hurt hot-reloading. Perhaps a debug mode configuration?
private Template getTemplate(final String acceptHeader, final String templateKey) throws IndyGroovyException {
final String accept = (acceptHeader == null ? "" : acceptHeader.replace('/', '_') + "/");
try {
final String filename = accept + templateKey + ".groovy";
final DataFile templateFile = manager.getDataFile(TEMPLATES, filename);
logger.info("Looking for template: {} for ACCEPT header: {} in: {}", templateKey, acceptHeader, templateFile);
Template template;
if (templateFile.exists() && !templateFile.isDirectory()) {
template = engine.createTemplate(templateFile.readString());
} else {
final String urlpath = TEMPLATES + "/" + accept + templateKey + ".groovy";
logger.info("Looking for template: {} for ACCEPT header: {} in: {}", templateKey, acceptHeader, urlpath);
final URL u = Thread.currentThread().getContextClassLoader().getResource(urlpath);
template = u == null ? null : engine.createTemplate(u);
}
if (template == null) {
throw new IndyGroovyException("Failed to locate template: %s (with ACCEPT header: %s)", templateKey, acceptHeader);
}
return template;
} catch (final CompilationFailedException e) {
throw new IndyGroovyException("Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
} catch (final ClassNotFoundException e) {
throw new IndyGroovyException("Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
} catch (final IOException e) {
throw new IndyGroovyException("Failed to read template: %s. Reason: %s", e, templateKey, e.getMessage());
}
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class PromotionRuleAproxMigrationAction method migrate.
@Override
public boolean migrate() throws IndyLifecycleException {
DataFile dataDir = ffManager.getDataFile(config.getBasedir(), PromoteValidationsManager.RULES_DIR);
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Scanning {} for promotion validation rules...", dataDir);
int changed = 0;
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.trace("Checking for promote validation rule script in: {}", pathname);
return pathname.getName().endsWith(".groovy");
});
logger.info("Migrating promotion validation rules.");
for (final DataFile script : scripts) {
try {
String scriptContent = script.readString();
String migrated = scriptContent.replaceAll("A[Pp]rox", "Indy").replaceAll("aprox", "indy");
if (!migrated.equals(scriptContent)) {
logger.info("Migrating promotion validation rule in: {}", script.getPath());
script.writeString(migrated, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating to Indy packages / naming"));
changed++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return changed != 0;
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class PromoteValidationsManager method parseRules.
public synchronized void parseRules() throws PromotionValidationException {
if (!config.isEnabled()) {
this.enabled = false;
this.ruleMappings = Collections.emptyMap();
logger.info("Promotion is disabled.");
return;
}
final Map<String, ValidationRuleMapping> ruleMappings = new HashMap<>();
DataFile dataDir = ffManager.getDataFile(config.getBasedir(), RULES_DIR);
logger.info("Scanning {} for promotion validation rules...", dataDir);
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.info("Checking for promote validation rule script in: {}", pathname);
return pathname.getName().endsWith(".groovy");
});
if (scripts != null && scripts.length > 0) {
for (final DataFile script : scripts) {
logger.info("Reading promotion validation rule from: {}", script);
final ValidationRuleMapping rule = ruleParser.parseRule(script);
if (rule != null) {
ruleMappings.put(rule.getName(), rule);
}
}
} else {
logger.warn("No rule script file was defined for promotion: no rule script found in {} directory", RULES_DIR);
}
} else {
logger.warn("No rule script file was defined for promotion: {} directory not exists", RULES_DIR);
}
this.ruleMappings = ruleMappings;
Map<String, ValidationRuleSet> ruleSets = new HashMap<>();
dataDir = ffManager.getDataFile(config.getBasedir(), RULES_SETS_DIR);
logger.info("Scanning {} for promotion validation rule-set mappings...", dataDir);
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.info("Checking for promotion rule-set in: {}", pathname);
return pathname.getName().endsWith(".json");
});
if (scripts != null && scripts.length > 0) {
for (final DataFile script : scripts) {
logger.info("Reading promotion validation rule-set from: {}", script);
final ValidationRuleSet set = ruleParser.parseRuleSet(script);
if (set != null) {
ruleSets.put(script.getName(), set);
}
}
} else {
logger.warn("No rule-set json file was defined for promotion: no json file found in {} directory", RULES_SETS_DIR);
}
} else {
logger.warn("No rule-set json file was defined for promotion: {} directory not exists", RULES_SETS_DIR);
}
this.ruleSets = ruleSets;
this.enabled = true;
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class PromoteValidationsManagerTest method testRuleSetParseAndMatchOnStoreKey.
@Test
public void testRuleSetParseAndMatchOnStoreKey() throws Exception {
DataFile dataFile = fileManager.getDataFile("promote/rule-sets/test.json");
dataFile.writeString("{\"name\":\"test\",\"storeKeyPattern\":\".*\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "writing test data"));
promoteValidations = new PromoteValidationsManager(fileManager, config, parser);
ValidationRuleSet ruleSet = promoteValidations.getRuleSetMatching(new StoreKey(StoreType.hosted, "repo"));
assertThat(ruleSet, notNullValue());
assertThat(ruleSet.matchesKey("hosted:repo"), equalTo(true));
}
Aggregations