Android Open Source - android-gcm-demo Api Key Initializer






From Project

Back to project page android-gcm-demo.

License

The source code is released under:

Apache License

If you think the Android project android-gcm-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Google Inc.//w  w w . j  av  a 2 s .com
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */
package com.google.android.gcm.demo.server;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.logging.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Context initializer that loads the API key from the App Engine datastore.
 */
public class ApiKeyInitializer implements ServletContextListener {

  static final String ATTRIBUTE_ACCESS_KEY = "apiKey";

  private static final String ENTITY_KIND = "Settings";
  private static final String ENTITY_KEY = "MyKey";
  private static final String ACCESS_KEY_FIELD = "ApiKey";

  private final Logger logger = Logger.getLogger(getClass().getName());

  public void contextInitialized(ServletContextEvent event) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key key = KeyFactory.createKey(ENTITY_KIND, ENTITY_KEY);

    Entity entity;
    try {
      entity = datastore.get(key);
    } catch (EntityNotFoundException e) {
      entity = new Entity(key);
      // NOTE: it's not possible to change entities in the local server, so
      // it will be necessary to hardcode the API key below if you are running
      // it locally.
      entity.setProperty(ACCESS_KEY_FIELD,
          "replace_this_text_by_your_Simple_API_Access_key");
      datastore.put(entity);
      logger.severe("Created fake key. Please go to App Engine admin "
          + "console, change its value to your API Key (the entity "
          + "type is '" + ENTITY_KIND + "' and its field to be changed is '"
          + ACCESS_KEY_FIELD + "'), then restart the server!");
    }
    String accessKey = (String) entity.getProperty(ACCESS_KEY_FIELD);
    event.getServletContext().setAttribute(ATTRIBUTE_ACCESS_KEY, accessKey);
  }

  public void contextDestroyed(ServletContextEvent event) {
  }

}




Java Source Code List

com.google.android.gcm.GCMBaseIntentService.java
com.google.android.gcm.GCMBroadcastReceiver.java
com.google.android.gcm.GCMConstants.java
com.google.android.gcm.GCMLogger.java
com.google.android.gcm.GCMRegistrar.java
com.google.android.gcm.demo.app.CommonUtilities.java
com.google.android.gcm.demo.app.DemoActivity.java
com.google.android.gcm.demo.app.GCMIntentService.java
com.google.android.gcm.demo.app.ServerUtilities.java
com.google.android.gcm.demo.server.ApiKeyInitializer.java
com.google.android.gcm.demo.server.ApiKeyInitializer.java
com.google.android.gcm.demo.server.BaseServlet.java
com.google.android.gcm.demo.server.BaseServlet.java
com.google.android.gcm.demo.server.Datastore.java
com.google.android.gcm.demo.server.Datastore.java
com.google.android.gcm.demo.server.HomeServlet.java
com.google.android.gcm.demo.server.HomeServlet.java
com.google.android.gcm.demo.server.RegisterServlet.java
com.google.android.gcm.demo.server.RegisterServlet.java
com.google.android.gcm.demo.server.SendAllMessagesServlet.java
com.google.android.gcm.demo.server.SendAllMessagesServlet.java
com.google.android.gcm.demo.server.SendMessageServlet.java
com.google.android.gcm.demo.server.UnregisterServlet.java
com.google.android.gcm.demo.server.UnregisterServlet.java
com.google.android.gcm.server.Constants.java
com.google.android.gcm.server.InvalidRequestException.java
com.google.android.gcm.server.Message.java
com.google.android.gcm.server.MulticastResult.java
com.google.android.gcm.server.Result.java
com.google.android.gcm.server.Sender.java