Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2013 Google Inc. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
Client factory for Datastore.
 public class DatastoreFactory {
   private static final Logger logger = Logger.getLogger(DatastoreFactory.class.getName());

Singleton factory instance.
   private static final DatastoreFactory INSTANCE = new DatastoreFactory();
   private static final String VERSION = "v1beta1";
   // Lazy load this because we might be running inside App Engine and this
   // class isn't on the whitelist.
   private static ConsoleHandler methodHandler;
   public static DatastoreFactory get() {
     return ;
   // TODO(user): Support something other than console handler for when we're
   // running in App Engine
   private static synchronized StreamHandler getStreamHandler() {
     if ( == null) {
        = new ConsoleHandler();
       .setFormatter(new Formatter() {
         public String format(LogRecord record) {
           return record.getMessage() + "\n";
     return ;
   DatastoreFactory() { }
     if (options == null) {
       throw new IllegalArgumentException("options not set");
     HttpRequestFactory client = makeClient(options);
     return new RemoteRpc(client,
         buildUrl(options, System.getenv("DATASTORE_URL_INTERNAL_OVERRIDE")));

Provides access to a datastore using the provided options. Logs into the application using the credentials available via these options.

java.lang.IllegalArgumentException if the server or credentials weren't provided.
   public Datastore create(DatastoreOptions optionsthrows IllegalArgumentException {
     return new Datastore(newRemoteRpc(options));

Constructs a Google APIs HTTP client with the associated credentials.
     Credential credential = options.getCredential();
     if (credential == null) {
       .warning("Not using any credentials");
       return new NetHttpTransport().createRequestFactory();
     HttpTransport transport = credential.getTransport();
    return transport.createRequestFactory(credential);

Build a valid datastore URL.
  String buildUrl(DatastoreOptions optionsString overrideUrl) {
    try {
      if (options.getDataset() == null) {
        throw new IllegalArgumentException("datastore dataset not set in options");
      String url;
      if (overrideUrl != null) {
        url = String.format("%s/datasets/%s"overrideUrloptions.getDataset());
      } else {
        url = String.format("%s/datastore/%s/datasets/%s",
            options.getHost(), options.getDataset());
      return new URI(url).toString();
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e);

Starts logging datastore method calls to the console. (Useful within tests.)
  public static void logMethodCalls() {
    Logger logger = Logger.getLogger(Datastore.class.getName());
    if (!Arrays.asList(logger.getHandlers()).contains(getStreamHandler())) {
New to GrepCode? Check out our FAQ X