Search in sources :

Example 31 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class LegacyDataMigrationActionTest method dataFileMigrationForRemoteRepoJSONWithExistingTypeAttribute.

@Test
public void dataFileMigrationForRemoteRepoJSONWithExistingTypeAttribute() throws Exception {
    final DataFile dir = dfm.getDataFile(INDY_STORE, remote.singularEndpointName());
    dir.mkdirs();
    final DataFile file = dir.getChild("test.json");
    file.writeString("{\"name\": \"test\", \"type\" : \"remote\", \"packageType\": \"maven\", \"key\": \"maven:remote:test\", \"url\": \"http://www.google.com\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test repo creation"));
    final boolean result = action.migrate();
    assertThat(result, equalTo(true));
    DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, remote.singularEndpointName(), "test.json");
    String json = out.readString();
    assertThat(json.contains("\"type\" : \"remote\""), equalTo(true));
    assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
    assertThat(json.contains("\"key\" : \"maven:remote:test\""), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Example 32 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class LegacyDataMigrationActionTest method migrateRemoteRepoJSONWithMissingTypeAttribute.

@Test
public void migrateRemoteRepoJSONWithMissingTypeAttribute() throws Exception {
    final DataFile dir = dfm.getDataFile(INDY_STORE, remote.singularEndpointName());
    dir.mkdirs();
    final DataFile file = dir.getChild("test.json");
    file.writeString("{\"name\": \"test\", \"packageType\": \"maven\", \"key\": \"maven:remote:test\", \"url\": \"http://www.google.com\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test repo creation"));
    final boolean result = action.migrate();
    assertThat(result, equalTo(true));
    DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, remote.singularEndpointName(), "test.json");
    final String json = out.readString();
    assertThat(json.contains("\"type\" : \"remote\""), equalTo(true));
    assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
    assertThat(json.contains("\"key\" : \"maven:remote:test\""), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Example 33 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class ScriptEngine method parseStandardScriptInstance.

/**
     * Provide a standard set of places to store common types of scripts used throughout Indy, such as
     * {@link org.commonjava.indy.model.core.ArtifactStore} creators. This is better from a config maintenance
     * perspective than having these spread out across the whole data/ subdirectory structure, particularly since it's
     * likely if one script needs to be revised, they all will.
     *
     * If the script doesn't exist in the data/scripts directory structure, this method will search the classpath
     * for the same path (scripts/[type]/[name]).
     */
public <T> T parseStandardScriptInstance(final StandardScriptType scriptType, final String name, final Class<T> type, boolean processCdiInjections) throws IndyGroovyException {
    DataFile dataFile = dataFileManager.getDataFile(SCRIPTS_SUBDIR, scriptType.subdir(), name);
    String script = null;
    if (dataFile == null || !dataFile.exists() || dataFile.isDirectory()) {
        URL resource = Thread.currentThread().getContextClassLoader().getResource(Paths.get(SCRIPTS_SUBDIR, scriptType.subdir(), name).toString());
        if (resource == null) {
            throw new IndyGroovyException("Cannot read standard script from: %s/%s/%s", SCRIPTS_SUBDIR, scriptType.subdir(), name);
        } else {
            Logger logger = LoggerFactory.getLogger(getClass());
            logger.debug("Loading script: {}/{}/{} for class: {} from classpath resource: {}", SCRIPTS_SUBDIR, scriptType, name, type.getName(), resource);
            try (InputStream in = resource.openStream()) {
                script = IOUtils.toString(in);
            } catch (IOException e) {
                throw new IndyGroovyException("Cannot read standard script from classpath: %s/%s/%s. Reason: %s", e, SCRIPTS_SUBDIR, scriptType.subdir(), name, e.getMessage());
            }
        }
    } else {
        try {
            script = dataFile.readString();
        } catch (IOException e) {
            throw new IndyGroovyException("Failed to read standard script from: %s/%s. Reason: %s", e, scriptType.subdir(), name, e.getMessage());
        }
    }
    Object instance = null;
    try {
        final Class<?> clazz = groovyClassloader.parseClass(script);
        instance = clazz.newInstance();
        T result = type.cast(instance);
        return processCdiInjections ? inject(result) : result;
    } catch (final CompilationFailedException e) {
        throw new IndyGroovyException("Failed to compile groovy script: '%s'. Reason: %s", e, script, e.getMessage());
    } catch (final InstantiationException | IllegalAccessException e) {
        throw new IndyGroovyException("Cannot instantiate class parsed from script: '%s'. Reason: %s", e, script, e.getMessage());
    } catch (final ClassCastException e) {
        throw new IndyGroovyException("Script: '%s' instance: %s cannot be cast as: %s", e, script, instance, type.getName());
    }
}
Also used : InputStream(java.io.InputStream) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) Logger(org.slf4j.Logger) URL(java.net.URL) DataFile(org.commonjava.indy.subsys.datafile.DataFile)

Example 34 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class AutoProxCatalogManager method storeRule.

public synchronized RuleMapping storeRule(final String name, final String spec, final ChangeSummary changelog) throws AutoProxRuleException {
    if (!checkEnabled()) {
        throw new AutoProxRuleException("AutoProx is disabled");
    }
    final RuleMapping mapping = ruleParser.parseRule(spec, name);
    if (mapping == null) {
        throw new AutoProxRuleException("Cannot construct RuleMapping for: {} with spec:\n\n{}\n\n", name, spec);
    }
    final int idx = ruleMappings.indexOf(mapping);
    if (idx > -1) {
        final RuleMapping existing = ruleMappings.get(idx);
        if (mapping.getSpecification().equals(existing.getSpecification())) {
            return existing;
        }
        logger.info("Replacing rule: {} at index: {}. Spec was:\n\n{}\n\n", mapping, idx, spec);
        ruleMappings.set(idx, mapping);
    } else {
        logger.info("Appending rule: {}. Spec was:\n\n{}\n\n", mapping, spec);
        ruleMappings.add(mapping);
        Collections.sort(ruleMappings);
    }
    final DataFile dataDir = ffManager.getDataFile(apConfig.getBasedir());
    if (!dataDir.exists()) {
        dataDir.mkdirs();
    }
    final DataFile scriptFile = dataDir.getChild(name + ".groovy");
    try {
        scriptFile.writeString(spec, changelog);
    } catch (final IOException e) {
        throw new AutoProxRuleException("Failed to write rule: %s to: %s. Reason: %s", e, name, scriptFile, e.getMessage());
    }
    return mapping;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) IOException(java.io.IOException)

Example 35 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class AutoProxCatalogManager method parseRules.

public synchronized void parseRules() throws AutoProxRuleException {
    if (!checkEnabled()) {
        return;
    }
    final List<RuleMapping> ruleMappings = new ArrayList<RuleMapping>();
    final DataFile dataDir = ffManager.getDataFile(apConfig.getBasedir());
    logger.info("Scanning {} for autoprox rules...", dataDir);
    if (dataDir.exists()) {
        final DataFile[] scripts = dataDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(final File pathname) {
                logger.info("Checking for autoprox script in: {}", pathname);
                return pathname.getName().endsWith(".groovy");
            }
        });
        for (final DataFile script : scripts) {
            logger.info("Reading autoprox rule from: {}", script);
            final RuleMapping rule = ruleParser.parseRule(script);
            if (rule != null) {
                ruleMappings.add(rule);
            }
        }
    }
    this.ruleMappings = ruleMappings;
    this.enabled = true;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ArrayList(java.util.ArrayList) FileFilter(java.io.FileFilter) DataFile(org.commonjava.indy.subsys.datafile.DataFile) File(java.io.File)

Aggregations

DataFile (org.commonjava.indy.subsys.datafile.DataFile)36 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)21 IOException (java.io.IOException)16 Test (org.junit.Test)14 IndyDataException (org.commonjava.indy.data.IndyDataException)5 StoreKey (org.commonjava.indy.model.core.StoreKey)5 File (java.io.File)3 HashMap (java.util.HashMap)3 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)3 StoreType (org.commonjava.indy.model.core.StoreType)3 ValidationRuleMapping (org.commonjava.indy.promote.validate.model.ValidationRuleMapping)3 DataFileEvent (org.commonjava.indy.subsys.datafile.change.DataFileEvent)3 Logger (org.slf4j.Logger)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)2 ValidationRuleSet (org.commonjava.indy.promote.model.ValidationRuleSet)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 Template (groovy.text.Template)1