use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class LegacyDataMigrationAction method migrate.
@Override
public boolean migrate() throws IndyLifecycleException {
if (!(storeDataManager instanceof DataFileStoreDataManager)) {
logger.info("Store manager: {} is not based on DataFile's. Skipping migration.", storeDataManager.getClass().getName());
return false;
}
final DataFile basedir = dataFileManager.getDataFile(INDY_STORE);
final ChangeSummary summary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating legacy store definitions.");
if (!basedir.exists()) {
return false;
}
StoreType[] storeTypes = StoreType.values();
final String[] dirs = basedir.list();
if (dirs == null || dirs.length < 1) {
return false;
}
Map<String, String> migrationCandidates = new HashMap<>();
//noinspection ConstantConditions
Stream.of(storeTypes).forEach(type -> {
File[] files = basedir.getDetachedFile().toPath().resolve(type.singularEndpointName()).toFile().listFiles((dir, fname) -> fname.endsWith(".json"));
if (files != null) {
Stream.of(files).forEach((f) -> {
String src = Paths.get(type.singularEndpointName(), f.getName()).toString();
String target = Paths.get(MAVEN_PKG_KEY, type.singularEndpointName(), f.getName()).toString();
migrationCandidates.put(src, target);
});
}
});
boolean changed = false;
for (Map.Entry<String, String> entry : migrationCandidates.entrySet()) {
DataFile src = dataFileManager.getDataFile(INDY_STORE, entry.getKey());
DataFile target = dataFileManager.getDataFile(INDY_STORE, entry.getValue());
if (target.exists()) {
continue;
}
DataFile targetDir = target.getParent();
if (!targetDir.exists() && !targetDir.mkdirs()) {
throw new IndyLifecycleException("Cannot make directory: %s.", targetDir.getPath());
} else if (!targetDir.isDirectory()) {
throw new IndyLifecycleException("Not a directory: %s.", targetDir.getPath());
}
try {
logger.info("Migrating definition {}", src.getPath());
final String json = src.readString();
final String migrated = objectMapper.patchLegacyStoreJson(json);
target.writeString(migrated, summary);
changed = true;
} catch (final IOException e) {
throw new IndyLifecycleException("Failed to migrate artifact-store definition from: %s to: %s. Reason: %s", e, src, target, e.getMessage(), e);
}
}
if (changed) {
try {
storeDataManager.reload();
} catch (IndyDataException e) {
throw new IndyLifecycleException("Failed to reload migrated store definitions: %s", e, e.getMessage());
}
}
return changed;
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class DataFileStoreDataManagerTest method getDataFileReturnsRemoteRepoJsonFile.
@Test
public void getDataFileReturnsRemoteRepoJsonFile() throws Exception {
final String name = "foo";
final boolean success = mgr.storeArtifactStore(new RemoteRepository(MAVEN_PKG_KEY, name, "http://www.foo.com/"), new ChangeSummary("test-user", "init"), false, true, new EventMetadata());
assertThat(success, equalTo(true));
final DataFile dataFile = mgr.getDataFile(new StoreKey(StoreType.remote, name));
assertThat(dataFile.getDetachedFile().getAbsolutePath(), equalTo(new File(fileCfg.getDataBasedir(), "indy/remote/" + name + ".json").getAbsolutePath()));
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class LegacyDataMigrationActionTest method migrateGroupJSONWithMissingTypeAttribute.
@Test
public void migrateGroupJSONWithMissingTypeAttribute() throws Exception {
final DataFile dir = dfm.getDataFile(INDY_STORE, group.singularEndpointName());
dir.mkdirs();
DataFile in = dfm.getDataFile(INDY_STORE, group.singularEndpointName(), "test.json");
in.writeString("{\"name\": \"test\", \"packageType\": \"maven\", \"key\": \"maven:group:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test group creation"));
System.out.println("Wrote: " + in.getPath());
final boolean result = action.migrate();
assertThat(result, equalTo(true));
DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, group.singularEndpointName(), "test.json");
final String json = out.readString();
assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
assertThat(json.contains("\"key\" : \"maven:group:test\""), equalTo(true));
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class LegacyDataMigrationActionTest method dataFileMigrationForGroupJSONWithExistingTypeAttribute.
@Test
public void dataFileMigrationForGroupJSONWithExistingTypeAttribute() throws Exception {
final DataFile dir = dfm.getDataFile(INDY_STORE, group.singularEndpointName());
dir.mkdirs();
final DataFile file = dir.getChild("test.json");
file.writeString("{\"name\": \"test\", \"packageType\": \"maven\", \"type\" : \"group\", \"key\": \"maven:group:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test group creation"));
String json = file.readString();
assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
final boolean result = action.migrate();
assertThat(result, equalTo(true));
DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, group.singularEndpointName(), "test.json");
json = out.readString();
assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
assertThat(json.contains("\"key\" : \"maven:group:test\""), equalTo(true));
}
use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.
the class LegacyDataMigrationActionTest method dataFileMigrationForHostedRepoJSONWithExistingTypeAttribute.
@Test
public void dataFileMigrationForHostedRepoJSONWithExistingTypeAttribute() throws Exception {
final DataFile dir = dfm.getDataFile(INDY_STORE, hosted.singularEndpointName());
dir.mkdirs();
final DataFile file = dir.getChild("test.json");
file.writeString("{\"name\": \"test\", \"type\" : \"hosted\", \"packageType\": \"maven\", \"key\": \"maven:hosted:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test repo creation"));
String json = file.readString();
assertThat(json.contains("\"type\" : \"hosted\""), equalTo(true));
final boolean result = action.migrate();
assertThat(result, equalTo(true));
DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, hosted.singularEndpointName(), "test.json");
json = out.readString();
assertThat(json.contains("\"type\" : \"hosted\""), equalTo(true));
assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
assertThat(json.contains("\"key\" : \"maven:hosted:test\""), equalTo(true));
}
Aggregations