GCP Java - 1.0

Extension ID

com.castsoftware.java.gcp

What’s new

See GCP Java - 1.0 - Release Notes .

Supported services and frameworks

Framework →
Services ↓
SDK 1.x SDK 2.x
Bigtable
Cloud Storage
PubSub N/A

Objects

Icon Description

Java GCP Bigtable Table

Java GCP Unknown Bigtable Table

Java GCP Cloud Storage Bucket

Java GCP Unknown Cloud Storage Bucket

Java GCP Pub/Sub Publisher

Java GCP Unknown Pub/Sub Publisher

Java GCP Pub/Sub Subscription

Java GCP Pub/Sub Receiver

Java GCP Unknown Pub/Sub Receiver

In what situation should you install this extension?

This extension should be used for analyzing java source code using any of the supported GCP services. 

Support for SDK

Support for Cloud Bigtable

Supported Cloud Bigtable APIs for SDK v2

For the following methods of the clients com.google.cloud.bigtable.data.v2.BigtableDataClient and com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient, we create links to the Table:

Link Type Client Methods
useInsertLink com.google.cloud.bigtable.data.v2.BigtableDataClient bulkMutateRows
bulkMutateRowsAsync
bulkMutationCallable
mutateRow
mutateRowAsync
mutateRowCallable
checkAndMutateRow
checkAndMutateRowAsync
checkAndMutateRowCallable
newBulkMutationBatcher
useUpdateLink com.google.cloud.bigtable.data.v2.BigtableDataClient readModifyWriteRow
readModifyWriteRowAsync
readModifyWriteRowCallable
bulkMutateRows
bulkMutateRowsAsync
bulkMutationCallable
mutateRow
mutateRowAsync
mutateRowCallable
checkAndMutateRow
checkAndMutateRowAsync
checkAndMutateRowCallable
newBulkMutationBatcher
useSelectLink com.google.cloud.bigtable.data.v2.BigtableDataClient readRow
readRowAsync
readRowCallable
readRows
readRowsAsync
readRowsCallable
sampleRowKeys
sampleRowKeysAsync
sampleRowKeysCallable
newBulkReadRowsBatcher
useDeleteLink com.google.cloud.bigtable.data.v2.BigtableDataClient bulkMutateRows
bulkMutateRowsAsync
bulkMutationCallable
mutateRow
mutateRowAsync
mutateRowCallable
checkAndMutateRow
checkAndMutateRowAsync
checkAndMutateRowCallable
newBulkMutationBatcher
useDeleteLink com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient dropAllRows
dropAllRowsAsync
dropRowRange
dropRowRangeAsync

What results you can expect?

When the name of a given referred table in the code is not resolved, an unknown table object will be created with name Unknown. There can be a maximum of one unknown table per project.

The analysis of the following source code will create a Java Call to GCP Bigtable object named mobile-time-series with a useUpdateLink from the WriteIncrement to that object:

package com.example.bigtable;

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
import com.google.cloud.bigtable.data.v2.models.Row;
import java.nio.charset.Charset;

public class WriteIncrement {
  private static final String COLUMN_FAMILY_NAME = "stats_summary";

  public static void writeIncrement(String projectId, String instanceId, String tableId) {
    String projectId = "my-project-id";
    String instanceId = "my-instance-id";
    String tableId = "mobile-time-series";

    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        
      String rowkey = "phone#4c410523#20190501";
      ReadModifyWriteRow mutation =
          ReadModifyWriteRow.create(tableId, rowkey)
              .increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
      Row success = dataClient.readModifyWriteRow(mutation);

      System.out.printf(
          "Successfully updated row %s", success.getKey().toString(Charset.defaultCharset()));
    } catch (Exception e) {
      System.out.println("Error during WriteIncrement: \n" + e.toString());
    }
  }
}

Support for Cloud Storage

Supported Cloud Storage APIs for SDK v2

For the following methods of the client  com.google.cloud.storage, we create links to the Bucket:

Link Type Methods
useUpdateLink Blob.writer
Storage.createFrom
Storage.copy
Storage.writer
useSelectLink Blob.copyTo
Blob.downloadTo
Blob.reader
Blob.getContent
Storage.downloadTo
Storage.reader
Storage.copy
Storage.readAllBytes
useInsertLink Blob.copyTo
Blob.writer
Storage.createFrom
Storage.copy
Storage.writer
Storage.create
useDeleteLink Storage.delete
Blob.delete

What results you can expect?

When the name of a given referred bucket in the code is not resolved, an unknown bucket object will be created with name Unknown. There can be a maximum of one unknown bucket per project.

The analysis of the following source code will create a Java GCP Cloud Storage Bucket object named my-delete-bucket-o1 with a useDeleteLink.

package com.example.storage.object;

import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.LinkedList;
import java.util.List;

public class DeleteOldVersionOfObject {

public static void deleteOverloadedOne(String projectId, String bucketName, String objectName, long generationToDelete){
    bucketName = "my-delete-bucket-o1";
    String blobName1 = "my-blob-name1";
    String blobName2 = "my-blob-name2";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId firstBlob = BlobId.of(bucketName, blobName1);
    BlobId secondBlob = BlobId.of(bucketName, blobName2);
    storage.delete(firstBlob, secondBlob);
  }
}

Support for PubSub

Supported PubSub APIs for SDK v1

The following APIs are supported:

Object Created Methods
Publisher

com.google.cloud.pubsub.v1.Publisher.publish

com.google.cloud.pubsub.v1.TopicAdminClient.publish

Subscription

com.google.cloud.pubsub.v1.SubscriptionAdminClient.createSubscription

Receiver

com.google.cloud.pubsub.v1.SubscriptionAdminClient.pull

com.google.cloud.pubsub.v1.Subscriber.startAsync

com.google.api.gax.rpc.UnaryCallable.call

com.google.api.gax.rpc.UnaryCallable.futureCall

  • For the publish method a Java GCP Pub/Sub Publisher object is created. Its name is of topic name. 
  • For the create subscription method a Java GCP Pub/Sub Subscription object is created. Its name is of subscription name and will have topics as its properties.
  • For the startAsync or pull method a Java GCP Pub/Sub Receiver object is created. Its name is of the subscription.
  • The com.castsoftware.wbslinker will create a callLink between the Publisher, Subscription and Reciever.
  • A publisher object is linked to subscription object when the topic name matches, and the subscription object is linked to receiver object when the subscription name matches. 

What results you can expect?

When the name of a given referred publisher/subscriber in the code is not resolved, an unknown publisher/receiver object will be created with name Unknown.

The analysis of the following source code will create a Java GCP Pub/Sub Publisher object with callLink, a Java GCP Pub/Sub Subscription object and a Java GCP Pub/Sub Receiver object with callLink. 

Publisher example:

Publishing a message

import com.google.api.core.ApiFuture;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class PublisherExample {
  public static void main(String... args) throws Exception {
    String projectId = "your-project-id";
    String topicId = "topic_id_01";
    publisherExample(projectId, topicId);
  }
  public static void publisherExample(String projectId, String topicId)
      throws IOException, ExecutionException, InterruptedException {
    TopicName topicName = TopicName.of(projectId, topicId);

    Publisher publisher = null;
    try {
      publisher = Publisher.newBuilder(topicName).build();
      String message = "Hello World!";
      ByteString data = ByteString.copyFromUtf8(message);
      PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
      ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
    } finally {
      if (publisher != null) {
        publisher.shutdown();
        publisher.awaitTermination(1, TimeUnit.MINUTES);
      }
    }
  }
}

Subscription example:

Create a subscription to a given topic

import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.PushConfig;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.TopicName;
import java.io.IOException;

public class CreatePullSubscriptionExample {
  public static void main(String... args) throws Exception {
    String projectId = "your-project-id";
    String subscriptionId = "subscription_id_01";
    String topicId = "topic_id_01";
    createPullSubscriptionExample(projectId, subscriptionId, topicId);
  }
  public static void createPullSubscriptionExample(
      String projectId, String subscriptionId, String topicId) throws IOException {
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
      TopicName topicName = TopicName.of(projectId, topicId);
      SubscriptionName subscriptionName = SubscriptionName.of(projectId, subscriptionId);
      Subscription subscription =
          subscriptionAdminClient.createSubscription(
              subscriptionName, topicName, PushConfig.getDefaultInstance(), 10);
      System.out.println("Created pull subscription: " + subscription.getName());
    }
  }
}

Receiver example:

Receiving a message

import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class SubscribeAsyncExample {
  public static void main(String... args) throws Exception {
    String projectId = "your-project-id";
    String subscriptionId = "subscription_id_01";
    subscribeAsyncExample(projectId, subscriptionId);
  }
  public static void subscribeAsyncExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);
    MessageReceiver receiver =
        (PubsubMessage message, AckReplyConsumer consumer) -> {
          consumer.ack();
        };
    Subscriber subscriber = null;
    try {
      subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
      subscriber.startAsync().awaitRunning();
      subscriber.awaitTerminated(30, TimeUnit.SECONDS);
    } catch (TimeoutException timeoutException) {
      subscriber.stopAsync();
    }
  }
}

Data sensitivity

This extension is capable of setting a property on GCP Cloud Storage Bucket objects for the following:

  • custom sensitivity
  • GDPR
  • PCI-DSS

See Data Sensitivity  for more information.

Known limitations

  • LinkType is not evaluated for mutateRow calls, all linktypes are created by default.