The following example presents an example of a Data Provider Adapter class that supports hypothetical WhatsIt objects. In the example, the class constructor method initializes the class member variables for the Data Service and registers a Resource Type Resolver. The example assumes that the Data Provider Adapter is registered implicitly by registering the service as an OSGi bundle. The Data Service and Resource Type Resolver Registry services are passed as arguments to the class constructor.

As a best practice, you can initialize the other services that your Data Provider Adapter requires in your Data Provider Adapter class constructor. These might include the Data Service, the Resource Type Resolver Registry if your adapter handles multiple custom object types, and the vSphere Object Reference Service if your adapter requires data from regular vSphere objects.

For more complete examples of Data Provider Adapters, see the sample extensions included in the SDK.

The getData() method is called by the Data Service when it receives a query for one of the objects or properties specified at registration. In the getData() method, your Data Provider Adapter must parse the query, compute the results, and return that result data as a Response object. For a more complete example, see the ChassisDataAdapter class in the SDK.

package com.MyAdapter.DataProvider;
 
import java.net.URI;
 
import com.vmware.vise.data.uri.ResourceTypeResolverRegistry;
import com.vmware.vise.data.query.DataProviderAdapter;
import com.vmware.vise.data.query.QuerySpec;
import com.vmware.vise.data.query.RequestSpec;
import com.vmware.vise.data.query.Response;
import com.vmware.vise.data.query.type;
 
@type("samples:WhatsIt") // type that the adapter supports
public class MyAdapter implements DataProviderAdapter {
 
  private final DataService _dataService;
 
  // Resource resolver, used to resolve the URIs of objects serviced by this adapter
  private static final ModelObjectUriResolver RESOURCE_RESOLVER = new ModelObjectUriResolver();
 
  // constructor method
  public MyAdapter( DataService dataService,
                    ResourceTypeResolverRegistry typeResolverRegistry )
  {
 
    if ( dataService == null || typeResolverRegistry == null ) {
      throw new IllegalArgumentException("MyAdapter constructor arguments must be non-null.");
    }
    _dataService = dataService;
    try {
      // Register the Resource Type resolver for multiple custom object types
      typeResolverRegistry.registerSchemeResolver( ModelObjectUriResolver.SCHEME,
                                                   RESOURCE_RESOLVER);
    } catch (UnsupportedOperationException e) {
      _logger.warn("ModelObjectUriResolver registration failed.", e);
    }
  }
 
  @Override
  // All query requests for the types supported by this adapter are routed here by the vSphere
  // Web Client Data Service; this method is the starting point for processing constraints,
  // discovering objects and properties, and returning results
  public Response getData(RequestSpec request) {
    QuerySpec[] querySpecs = request.querySpec;
    List<ResultSet> results = new ArrayList<ResultSet>(querySpecs.length);
    for (QuerySpec qs : querySpecs) {
      // Call your logic for query processing, constraint processing, object discovery:
      ResultSet rs = processQuery(qs);
      results.add(rs);
    }
    Response response = new Response();
    response.resultSet = results.toArray(new ResultSet[]{});
    return response;
  }
}