use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class TMSAlertPublisher method getPluginClass.
private static PluginClass getPluginClass() {
Map<String, PluginPropertyField> properties = new HashMap<>();
properties.put("topic", new PluginPropertyField("topic", "", "string", true, false));
properties.put("topicNamespace", new PluginPropertyField("topicNamespace", "", "string", true, false));
return new PluginClass(AlertPublisher.PLUGIN_TYPE, NAME, "", TMSAlertPublisher.class.getName(), "conf", properties);
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactInspector method inspectConfigField.
/**
* Inspects the plugin config class and build up a map for {@link PluginPropertyField}.
*
* @param configType type of the config class
* @param result map for storing the result
* @throws UnsupportedTypeException if a field type in the config class is not supported
*/
private void inspectConfigField(TypeToken<?> configType, Map<String, PluginPropertyField> result) throws UnsupportedTypeException {
for (TypeToken<?> type : configType.getTypes().classes()) {
if (PluginConfig.class.equals(type.getRawType())) {
break;
}
for (Field field : type.getRawType().getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers) || field.isSynthetic()) {
continue;
}
PluginPropertyField property = createPluginProperty(field, type);
if (result.containsKey(property.getName())) {
throw new IllegalArgumentException("Plugin config with name " + property.getName() + " already defined in " + configType.getRawType());
}
result.put(property.getName(), property);
}
}
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactRepositoryTest method testAddSystemArtifacts.
@Test
public void testAddSystemArtifacts() throws Exception {
Id.Artifact systemAppArtifactId = Id.Artifact.from(Id.Namespace.SYSTEM, "PluginTest", "1.0.0");
File systemAppJar = createAppJar(PluginTestApp.class, new File(systemArtifactsDir1, "PluginTest-1.0.0.jar"), createManifest(ManifestFields.EXPORT_PACKAGE, PluginTestRunnable.class.getPackage().getName()));
// write plugins jar
Id.Artifact pluginArtifactId1 = Id.Artifact.from(Id.Namespace.SYSTEM, "APlugin", "1.0.0");
Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
File pluginJar1 = createPluginJar(TestPlugin.class, new File(systemArtifactsDir1, "APlugin-1.0.0.jar"), manifest);
// write plugins config file
Map<String, PluginPropertyField> emptyMap = Collections.emptyMap();
Set<PluginClass> manuallyAddedPlugins1 = ImmutableSet.of(new PluginClass("typeA", "manual1", "desc", "co.cask.classname", null, emptyMap), new PluginClass("typeB", "manual2", "desc", "co.cask.otherclassname", null, emptyMap));
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"))), // add a dummy plugin to test explicit addition of plugins through the config file
manuallyAddedPlugins1, ImmutableMap.of("k1", "v1", "k2", "v2"));
try (BufferedWriter writer = Files.newWriter(pluginConfigFile, Charsets.UTF_8)) {
writer.write(pluginConfig1.toString());
}
// write another plugins jar to a different directory, to test that plugins will get picked up from both directories
Id.Artifact pluginArtifactId2 = Id.Artifact.from(Id.Namespace.SYSTEM, "BPlugin", "1.0.0");
manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
File pluginJar2 = createPluginJar(TestPlugin.class, new File(systemArtifactsDir2, "BPlugin-1.0.0.jar"), manifest);
// write plugins config file
Set<PluginClass> manuallyAddedPlugins2 = ImmutableSet.of(new PluginClass("typeA", "manual1", "desc", "co.notcask.classname", null, emptyMap), new PluginClass("typeB", "manual2", "desc", "co.notcask.otherclassname", null, emptyMap));
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"))), manuallyAddedPlugins2, ImmutableMap.of("k3", "v3"));
try (BufferedWriter writer = Files.newWriter(pluginConfigFile, Charsets.UTF_8)) {
writer.write(pluginConfig2.toString());
}
artifactRepository.addSystemArtifacts();
Assert.assertTrue(systemAppJar.delete());
Assert.assertTrue(pluginJar1.delete());
Assert.assertTrue(pluginJar2.delete());
try {
// check app artifact added correctly
ArtifactDetail appArtifactDetail = artifactRepository.getArtifact(systemAppArtifactId);
Map<ArtifactDescriptor, Set<PluginClass>> plugins = artifactRepository.getPlugins(NamespaceId.DEFAULT, systemAppArtifactId);
Assert.assertEquals(2, plugins.size());
Set<PluginClass> pluginClasses = plugins.values().iterator().next();
Set<String> pluginNames = Sets.newHashSet();
for (PluginClass pluginClass : pluginClasses) {
pluginNames.add(pluginClass.getName());
}
Assert.assertEquals(Sets.newHashSet("manual1", "manual2", "TestPlugin", "TestPlugin2"), pluginNames);
Assert.assertEquals(systemAppArtifactId.getName(), appArtifactDetail.getDescriptor().getArtifactId().getName());
Assert.assertEquals(systemAppArtifactId.getVersion(), appArtifactDetail.getDescriptor().getArtifactId().getVersion());
// check plugin artifact added correctly
ArtifactDetail pluginArtifactDetail = artifactRepository.getArtifact(pluginArtifactId1);
Assert.assertEquals(pluginArtifactId1.getName(), pluginArtifactDetail.getDescriptor().getArtifactId().getName());
Assert.assertEquals(pluginArtifactId1.getVersion(), pluginArtifactDetail.getDescriptor().getArtifactId().getVersion());
// check manually added plugins are there
Assert.assertTrue(pluginArtifactDetail.getMeta().getClasses().getPlugins().containsAll(manuallyAddedPlugins1));
// check properties are there
Assert.assertEquals(pluginConfig1.getProperties(), pluginArtifactDetail.getMeta().getProperties());
// check other plugin artifact added correctly
pluginArtifactDetail = artifactRepository.getArtifact(pluginArtifactId2);
Assert.assertEquals(pluginArtifactId2.getName(), pluginArtifactDetail.getDescriptor().getArtifactId().getName());
Assert.assertEquals(pluginArtifactId2.getVersion(), pluginArtifactDetail.getDescriptor().getArtifactId().getVersion());
// check manually added plugins are there
Assert.assertTrue(pluginArtifactDetail.getMeta().getClasses().getPlugins().containsAll(manuallyAddedPlugins2));
// check properties are there
Assert.assertEquals(pluginConfig2.getProperties(), pluginArtifactDetail.getMeta().getProperties());
} finally {
artifactRepository.clear(NamespaceId.SYSTEM);
}
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactStoreTest method testPluginParentInclusiveExclusiveVersions.
@Test
public void testPluginParentInclusiveExclusiveVersions() throws Exception {
// write artifacts that extend:
// parent-[1.0.0,1.0.0] -- only visible by parent-1.0.0
Id.Artifact id1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.1");
Set<ArtifactRange> parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
List<PluginClass> plugins = ImmutableList.of(new PluginClass("typeA", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
writeArtifact(id1, meta, "some contents");
// parent-[2.0.0,2.0.1) -- only visible by parent-2.0.0
Id.Artifact id2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.2");
parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.1"), false));
plugins = ImmutableList.of(new PluginClass("typeA", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
writeArtifact(id2, meta, "some contents");
// parent-(3.0.0,3.0.1] -- only visible by parent-3.0.1
Id.Artifact id3 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.3");
parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("3.0.0"), false, new ArtifactVersion("3.0.1"), true));
plugins = ImmutableList.of(new PluginClass("typeA", "plugin3", "", "c.c.c.plugin3", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
writeArtifact(id3, meta, "some contents");
// parent-(4.0.0,4.0.2) -- only visible by parent-4.0.1
Id.Artifact id4 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.4");
parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("4.0.0"), false, new ArtifactVersion("4.0.2"), false));
plugins = ImmutableList.of(new PluginClass("typeA", "plugin4", "", "c.c.c.plugin4", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
writeArtifact(id4, meta, "some contents");
ArtifactMeta parentMeta = new ArtifactMeta(ArtifactClasses.builder().build());
// check parent-1.0.0 has plugin1 but parent-0.0.9 does not and 1.0.1 does not
Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "0.0.9");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.1");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
writeArtifact(parentId, parentMeta, "content");
Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
// check parent-2.0.0 has plugin2 but parent-1.9.9 does not and 2.0.1 does not
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.9.9");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.1");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0");
writeArtifact(parentId, parentMeta, "content");
Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
// check parent-3.0.1 has plugin3 but parent-3.0.0 does not and 3.0.2 does not
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.0");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.2");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.1");
writeArtifact(parentId, parentMeta, "content");
Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
// check parent-4.0.1 has plugin4 but parent-4.0.0 does not and 4.0.2 does not
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.0");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.2");
writeArtifact(parentId, parentMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.1");
writeArtifact(parentId, parentMeta, "content");
Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactStoreTest method testConcurrentWrite.
@Category(SlowTests.class)
@Test
public void testConcurrentWrite() throws Exception {
// start up a bunch of threads that will try and write the same artifact at the same time
// only one of them should be able to write it
int numThreads = 20;
final Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "abc", "1.0.0");
final List<String> successfulWriters = Collections.synchronizedList(Lists.<String>newArrayList());
// use a barrier so they all try and write at the same time
final CyclicBarrier barrier = new CyclicBarrier(numThreads);
final CountDownLatch latch = new CountDownLatch(numThreads);
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numThreads; i++) {
final String writer = String.valueOf(i);
executorService.execute(new Runnable() {
@Override
public void run() {
try {
barrier.await();
ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + writer, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build());
writeArtifact(artifactId, meta, writer);
successfulWriters.add(writer);
} catch (InterruptedException | BrokenBarrierException | IOException e) {
// something went wrong, fail the test
throw new RuntimeException(e);
} catch (ArtifactAlreadyExistsException | WriteConflictException e) {
// these are ok, all but one thread should see this
} finally {
latch.countDown();
}
}
});
}
// wait for all writers to finish
latch.await();
// only one writer should have been able to write
Assert.assertEquals(1, successfulWriters.size());
String successfulWriter = successfulWriters.get(0);
// check that the contents weren't mixed between writers
ArtifactDetail info = artifactStore.getArtifact(artifactId);
ArtifactMeta expectedMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + successfulWriter, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build());
assertEqual(artifactId, expectedMeta, successfulWriter, info);
}
Aggregations