use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ApplicationClient method listAppVersions.
/**
* Lists all applications currently deployed, optionally filtering to only include applications that use one of
* the specified artifact names and the specified artifact version.
*
* @param namespace the namespace to list application versions from
* @param appName the application name to list versions for
* @return list of {@link String} application versions.
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public List<String> listAppVersions(NamespaceId namespace, String appName) throws IOException, UnauthenticatedException, UnauthorizedException, ApplicationNotFoundException {
String path = String.format("apps/%s/versions", appName);
URL url = config.resolveNamespacedURLV3(namespace, path);
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ApplicationNotFoundException(namespace.app(appName));
}
return ObjectResponse.fromJsonBody(response, new TypeToken<List<String>>() {
}).getResponseObject();
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class DatumWriterGenerator method generateConstructor.
/**
* Generates the constructor. The constructor generated has signature {@code (Schema, FieldAccessorFactory)}.
*/
private void generateConstructor() {
Method constructor = getMethod(void.class, "<init>", Schema.class, FieldAccessorFactory.class);
// Constructor(Schema schema, FieldAccessorFactory accessorFactory)
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, classWriter);
// super(); // Calling Object constructor
mg.loadThis();
mg.invokeConstructor(Type.getType(Object.class), getMethod(void.class, "<init>"));
// if (!SCHEMA_HASH.equals(schema.getSchemaHash().toString())) { throw IllegalArgumentException }
mg.getStatic(classType, "SCHEMA_HASH", Type.getType(String.class));
mg.loadArg(0);
mg.invokeVirtual(Type.getType(Schema.class), getMethod(SchemaHash.class, "getSchemaHash"));
mg.invokeVirtual(Type.getType(SchemaHash.class), getMethod(String.class, "toString"));
mg.invokeVirtual(Type.getType(String.class), getMethod(boolean.class, "equals", Object.class));
Label hashEquals = mg.newLabel();
mg.ifZCmp(GeneratorAdapter.NE, hashEquals);
mg.throwException(Type.getType(IllegalArgumentException.class), "Schema not match.");
mg.mark(hashEquals);
// this.schema = schema;
mg.loadThis();
mg.loadArg(0);
mg.putField(classType, "schema", Type.getType(Schema.class));
// For each record field that needs an accessor, get the accessor and store it in field.
for (Map.Entry<TypeToken<?>, String> entry : fieldAccessorRequests.entries()) {
String fieldAccessorName = getFieldAccessorName(entry.getKey(), entry.getValue());
classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, fieldAccessorName, Type.getDescriptor(FieldAccessor.class), null, null);
// this.fieldAccessorName
// = accessorFactory.getFieldAccessor(TypeToken.of(Class.forName("className")), "fieldName");
mg.loadThis();
mg.loadArg(1);
mg.push(entry.getKey().getRawType().getName());
mg.invokeStatic(Type.getType(Class.class), getMethod(Class.class, "forName", String.class));
mg.invokeStatic(Type.getType(TypeToken.class), getMethod(TypeToken.class, "of", Class.class));
mg.push(entry.getValue());
mg.invokeInterface(Type.getType(FieldAccessorFactory.class), getMethod(FieldAccessor.class, "getFieldAccessor", TypeToken.class, String.class));
mg.putField(classType, fieldAccessorName, Type.getType(FieldAccessor.class));
}
mg.returnValue();
mg.endMethod();
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class StandardObjectInspectorsTest method testCollectionObjectInspector.
@Test
public void testCollectionObjectInspector() throws Throwable {
// Test with sets
ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<Set<String>>() {
}.getType());
Assert.assertTrue(oi instanceof StandardListObjectInspector);
StandardListObjectInspector loi = (StandardListObjectInspector) oi;
Set<String> set = Sets.newHashSet("foo", "bar", "foobar");
List<?> inspectedSet = loi.getList(set);
Assert.assertTrue(inspectedSet.contains("foo"));
Assert.assertTrue(inspectedSet.contains("bar"));
Assert.assertTrue(inspectedSet.contains("foobar"));
// Test with queues
oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<Queue<String>>() {
}.getType());
Assert.assertTrue(oi instanceof StandardListObjectInspector);
loi = (StandardListObjectInspector) oi;
Queue<String> queue = new LinkedList<>();
queue.add("foo");
queue.add("bar");
List<?> inspectedQueue = loi.getList(set);
Assert.assertEquals("bar", inspectedQueue.get(0));
Assert.assertEquals("foo", inspectedQueue.get(1));
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class ServiceSpecificationCodec method decodeOldSpec.
private ServiceSpecification decodeOldSpec(JsonObject json) {
String className = json.get("classname").getAsString();
TwillSpecification twillSpec = twillSpecificationAdapter.fromJson(json.get("spec").getAsString()).getTwillSpecification();
Map<String, HttpServiceHandlerSpecification> handlers = Maps.newHashMap();
RuntimeSpecification handlerSpec = twillSpec.getRunnables().get(twillSpec.getName());
Map<String, String> configs = handlerSpec.getRunnableSpecification().getConfigs();
// Get the class names of all handlers. It is stored in the handler runnable spec configs
List<String> handlerClasses = GSON.fromJson(configs.get("service.runnable.handlers"), new TypeToken<List<String>>() {
}.getType());
List<JsonObject> handlerSpecs = GSON.fromJson(configs.get("service.runnable.handler.spec"), new TypeToken<List<JsonObject>>() {
}.getType());
for (int i = 0; i < handlerClasses.size(); i++) {
String handlerClass = handlerClasses.get(i);
JsonObject spec = handlerSpecs.get(i);
Map<String, String> properties = GSON.fromJson(spec.get("properties"), new TypeToken<Map<String, String>>() {
}.getType());
// Reconstruct the HttpServiceSpecification. However there is no way to determine the datasets or endpoints
// as it is not recorded in old spec. It's ok since the spec is only used to load data from MDS during redeploy.
handlers.put(spec.get("name").getAsString(), new HttpServiceHandlerSpecification(handlerClass, spec.get("name").getAsString(), spec.get("description").getAsString(), properties, ImmutableSet.<String>of(), ImmutableList.<ServiceHttpEndpoint>of()));
}
ResourceSpecification resourceSpec = handlerSpec.getResourceSpecification();
return new ServiceSpecification(className, twillSpec.getName(), twillSpec.getName(), handlers, new Resources(resourceSpec.getMemorySize(), resourceSpec.getVirtualCores()), resourceSpec.getInstances());
}
use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class FlowletProgramRunner method outputEmitterFactory.
private OutputEmitterFactory outputEmitterFactory(final BasicFlowletContext flowletContext, final String flowletName, final QueueClientFactory queueClientFactory, final ImmutableList.Builder<ProducerSupplier> producerBuilder, final Table<Node, String, Set<QueueSpecification>> queueSpecs) {
return new OutputEmitterFactory() {
@Override
public <T> OutputEmitter<T> create(String outputName, TypeToken<T> type) {
try {
// first iterate over all queue specifications to find the queue name and all consumer flowlet ids
QueueName queueName = null;
List<String> consumerFlowlets = Lists.newLinkedList();
Node flowlet = Node.flowlet(flowletName);
Schema schema = schemaGenerator.generate(type.getType());
for (Map.Entry<String, Set<QueueSpecification>> entry : queueSpecs.row(flowlet).entrySet()) {
for (QueueSpecification queueSpec : entry.getValue()) {
if (queueSpec.getQueueName().getSimpleName().equals(outputName) && queueSpec.getOutputSchema().equals(schema)) {
queueName = queueSpec.getQueueName();
consumerFlowlets.add(entry.getKey());
break;
}
}
}
if (queueName == null) {
throw new IllegalArgumentException(String.format("No queue specification found for %s, %s", flowletName, type));
}
// create a metric collector for this queue, and also one for each consumer flowlet
final MetricsContext metrics = flowletContext.getProgramMetrics().childContext(Constants.Metrics.Tag.FLOWLET_QUEUE, outputName);
final MetricsContext producerMetrics = metrics.childContext(Constants.Metrics.Tag.PRODUCER, flowletContext.getFlowletId());
final Iterable<MetricsContext> consumerMetrics = Iterables.transform(consumerFlowlets, new Function<String, MetricsContext>() {
@Override
public MetricsContext apply(String consumer) {
return producerMetrics.childContext(Constants.Metrics.Tag.CONSUMER, consumer);
}
});
// create a queue metrics emitter that emit to all of the above collectors
ProducerSupplier producerSupplier = new ProducerSupplier(queueName, queueClientFactory, new QueueMetrics() {
@Override
public void emitEnqueue(int count) {
metrics.increment("process.events.out", count);
for (MetricsContext collector : consumerMetrics) {
collector.increment("queue.pending", count);
}
}
@Override
public void emitEnqueueBytes(int bytes) {
// no-op
}
});
producerBuilder.add(producerSupplier);
return new DatumOutputEmitter<>(producerSupplier, schema, datumWriterFactory.create(type, schema));
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
};
}
Aggregations