configReader.read(artifactId.getNamespace(), configFile) : new ArtifactConfig(); validateParentSet(artifactId, artifactConfig.getParents()); validatePluginSet(artifactConfig.getPlugins()); systemArtifacts.put(artifactId, new SystemArtifactInfo(artifactId, jarFile, artifactConfig)); } catch (InvalidArtifactException e) { if (child.getConfig().hasParent(potentialParentId)) { childToParents.put(childId, potentialParentId); parentToChildren.put(potentialParentId, childId);
private void addSystemArtifact(SystemArtifactInfo systemArtifactInfo) throws Exception { String fileName = systemArtifactInfo.getArtifactFile().getName(); try { Id.Artifact artifactId = systemArtifactInfo.getArtifactId(); // if it's not a snapshot and it already exists, don't bother trying to add it since artifacts are immutable if (!artifactId.getVersion().isSnapshot()) { try { artifactStore.getArtifact(artifactId); LOG.info("Artifact {} already exists, will not try loading it again.", artifactId); return; } catch (ArtifactNotFoundException e) { // this is fine, means it doesn't exist yet and we should add it } } addArtifact(artifactId, systemArtifactInfo.getArtifactFile(), systemArtifactInfo.getConfig().getParents(), systemArtifactInfo.getConfig().getPlugins(), systemArtifactInfo.getConfig().getProperties()); LOG.info("Added system artifact {}.", artifactId); } catch (ArtifactAlreadyExistsException e) { // shouldn't happen... but if it does for some reason it's fine, it means it was added some other way already. } catch (ArtifactRangeNotFoundException e) { LOG.warn("Could not add system artifact '{}' because it extends artifacts that do not exist.", fileName, e); } catch (InvalidArtifactException e) { LOG.warn("Could not add system artifact '{}' because it is invalid.", fileName, e); } catch (UnauthorizedException e) { LOG.warn("Could not add system artifact '{}' because of an authorization error.", fileName, e); } }
); File pluginConfigFile = new File(systemArtifactsDir1, "APlugin-1.0.0.json"); ArtifactConfig pluginConfig1 = new ArtifactConfig( ImmutableSet.of(new ArtifactRange( NamespaceId.SYSTEM.getNamespace(), "PluginTest", new ArtifactVersion("0.9.0"), new ArtifactVersion("2.0.0"))), ); try (BufferedWriter writer = Files.newWriter(pluginConfigFile, Charsets.UTF_8)) { writer.write(pluginConfig1.toString()); ); pluginConfigFile = new File(systemArtifactsDir2, "BPlugin-1.0.0.json"); ArtifactConfig pluginConfig2 = new ArtifactConfig( ImmutableSet.of(new ArtifactRange( NamespaceId.SYSTEM.getNamespace(), "PluginTest", new ArtifactVersion("0.9.0"), new ArtifactVersion("2.0.0"))), ); try (BufferedWriter writer = Files.newWriter(pluginConfigFile, Charsets.UTF_8)) { writer.write(pluginConfig2.toString()); Assert.assertEquals(pluginConfig1.getProperties(), pluginArtifactDetail.getMeta().getProperties()); Assert.assertEquals(pluginConfig2.getProperties(), pluginArtifactDetail.getMeta().getProperties()); } finally { artifactRepository.clear(NamespaceId.SYSTEM);
@Test(expected = InvalidArtifactException.class) public void testInvalidParentNamespace() throws IOException, InvalidArtifactException { ArtifactConfig badConfig = new ArtifactConfig( ImmutableSet.of( new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "b", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")) ), ImmutableSet.<PluginClass>of(), ImmutableMap.<String, String>of()); File configFile = new File(tmpFolder.newFolder(), "r1-1.0.0.json"); try (BufferedWriter writer = Files.newWriter(configFile, Charsets.UTF_8)) { writer.write(badConfig.toString()); } configReader.read(Id.Namespace.SYSTEM, configFile); }
@Override public ArtifactConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Config file must be a JSON Object."); } JsonObject obj = json.getAsJsonObject(); // deserialize fields Set<ArtifactRange> parents = context.deserialize(obj.get("parents"), PARENTS_TYPE); parents = parents == null ? Collections.<ArtifactRange>emptySet() : parents; Set<PluginClass> plugins = context.deserialize(obj.get("plugins"), PLUGINS_TYPE); plugins = plugins == null ? Collections.<PluginClass>emptySet() : plugins; plugins.forEach(PluginClass::validate); Map<String, String> properties = context.deserialize(obj.get("properties"), PROPERTIES_TYPE); properties = properties == null ? Collections.<String, String>emptyMap() : properties; return new ArtifactConfig(parents, plugins, properties); } }
for (ArtifactRange parent : config.getParents()) { NamespaceId parentNamespace = new NamespaceId(parent.getNamespace()); if (!NamespaceId.SYSTEM.equals(parentNamespace) && !namespace.toEntityId().equals(parentNamespace)) {
@Test public void testRead() throws IOException, InvalidArtifactException { ArtifactConfig validConfig = new ArtifactConfig( ImmutableSet.of( new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), "a", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")), new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "b", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")) ), ImmutableSet.of( new PluginClass("type", "name", "desc", "classname", null, ImmutableMap.of( "x", new PluginPropertyField("x", "some field", "int", true, false), "y", new PluginPropertyField("y", "some other field", "string", false, false) )) ), ImmutableMap.of( "k1", "v1", "k2", "v2" ) ); File configFile = new File(tmpFolder.newFolder(), "r1-1.0.0.json"); try (BufferedWriter writer = Files.newWriter(configFile, Charsets.UTF_8)) { writer.write(validConfig.toString()); } Assert.assertEquals(validConfig, configReader.read(Id.Namespace.DEFAULT, configFile)); }
@Override public ArtifactConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Config file must be a JSON Object."); } JsonObject obj = json.getAsJsonObject(); // deserialize fields Set<ArtifactRange> parents = context.deserialize(obj.get("parents"), PARENTS_TYPE); parents = parents == null ? Collections.<ArtifactRange>emptySet() : parents; Set<PluginClass> plugins = context.deserialize(obj.get("plugins"), PLUGINS_TYPE); plugins = plugins == null ? Collections.<PluginClass>emptySet() : plugins; plugins.forEach(PluginClass::validate); Map<String, String> properties = context.deserialize(obj.get("properties"), PROPERTIES_TYPE); properties = properties == null ? Collections.<String, String>emptyMap() : properties; return new ArtifactConfig(parents, plugins, properties); } }
for (ArtifactRange parent : config.getParents()) { NamespaceId parentNamespace = new NamespaceId(parent.getNamespace()); if (!NamespaceId.SYSTEM.equals(parentNamespace) && !namespace.toEntityId().equals(parentNamespace)) {
private void addSystemArtifact(SystemArtifactInfo systemArtifactInfo) throws Exception { String fileName = systemArtifactInfo.getArtifactFile().getName(); try { Id.Artifact artifactId = systemArtifactInfo.getArtifactId(); // if it's not a snapshot and it already exists, don't bother trying to add it since artifacts are immutable if (!artifactId.getVersion().isSnapshot()) { try { artifactStore.getArtifact(artifactId); LOG.info("Artifact {} already exists, will not try loading it again.", artifactId); return; } catch (ArtifactNotFoundException e) { // this is fine, means it doesn't exist yet and we should add it } } addArtifact(artifactId, systemArtifactInfo.getArtifactFile(), systemArtifactInfo.getConfig().getParents(), systemArtifactInfo.getConfig().getPlugins(), systemArtifactInfo.getConfig().getProperties()); LOG.info("Added system artifact {}.", artifactId); } catch (ArtifactAlreadyExistsException e) { // shouldn't happen... but if it does for some reason it's fine, it means it was added some other way already. } catch (ArtifactRangeNotFoundException e) { LOG.warn("Could not add system artifact '{}' because it extends artifacts that do not exist.", fileName, e); } catch (InvalidArtifactException e) { LOG.warn("Could not add system artifact '{}' because it is invalid.", fileName, e); } catch (UnauthorizedException e) { LOG.warn("Could not add system artifact '{}' because of an authorization error.", fileName, e); } }
configReader.read(artifactId.getNamespace(), configFile) : new ArtifactConfig(); validateParentSet(artifactId, artifactConfig.getParents()); validatePluginSet(artifactConfig.getPlugins()); systemArtifacts.put(artifactId, new SystemArtifactInfo(artifactId, jarFile, artifactConfig)); } catch (InvalidArtifactException e) { if (child.getConfig().hasParent(potentialParentId)) { childToParents.put(childId, potentialParentId); parentToChildren.put(potentialParentId, childId);
artifactClient.add(namespace, artifactId.getEntityName(), () -> new FileInputStream(artifactFile), artifactId.getVersion(), artifactConfig.getParents(), artifactConfig.getPlugins()); Map<String, String> properties = artifactConfig.getProperties(); if (properties != null && !properties.isEmpty()) { artifactClient.writeProperties(artifactId, properties);
artifactClient.add(namespace, artifactId.getEntityName(), () -> new FileInputStream(artifactFile), artifactId.getVersion(), artifactConfig.getParents(), artifactConfig.getPlugins()); Map<String, String> properties = artifactConfig.getProperties(); if (properties != null && !properties.isEmpty()) { artifactClient.writeProperties(artifactId, properties);