Android Open Source - kitty-compass Listable Memory Credential Store






From Project

Back to project page kitty-compass.

License

The source code is released under:

Apache License

If you think the Android project kitty-compass 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 (C) 2013 Google Inc.//www  . j a v a  2  s  .c o  m
 * 
 * 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.glassware;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.CredentialStore;


import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * A new credential store. It's exactly the same as
 * com.google.api.client.auth.oauth2.MemoryCredentialStore except it
 * has the added ability to list all of the users.
 *
 * @author Jenny Murphy - http://google.com/+JennyMurphy
 */
public class ListableMemoryCredentialStore implements CredentialStore {

  /**
   * Lock on access to the store.
   */
  private final Lock lock = new ReentrantLock();

  /**
   * Store of memory persisted credentials, indexed by userId.
   */
  private final Map<String, MemoryPersistedCredential> store =
      new HashMap<String, MemoryPersistedCredential>();

  public void store(String userId, Credential credential) {
    lock.lock();
    try {
      MemoryPersistedCredential item = store.get(userId);
      if (item == null) {
        item = new MemoryPersistedCredential();
        store.put(userId, item);
      }
      item.store(credential);
    } finally {
      lock.unlock();
    }
  }

  public void delete(String userId, Credential credential) {
    lock.lock();
    try {
      store.remove(userId);
    } finally {
      lock.unlock();
    }
  }

  public boolean load(String userId, Credential credential) {
    lock.lock();
    try {
      MemoryPersistedCredential item = store.get(userId);
      if (item != null) {
        item.load(credential);
      }
      return item != null;
    } finally {
      lock.unlock();
    }
  }

  public List<String> listAllUsers() {
    List<String> allUsers = new ArrayList<String>();
    // Is that a 47 character long generic for one line of behavior? Yes, yes it is.
    for (Iterator<Map.Entry<String, MemoryPersistedCredential>> iterator = store.entrySet()
        .iterator();
         iterator.hasNext(); ) {
      allUsers.add(iterator.next().getKey());
    }
    return allUsers;
  }

  class MemoryPersistedCredential {

    /**
     * Access token or {@code null} for none.
     */
    private String accessToken;

    /**
     * Refresh token {@code null} for none.
     */
    private String refreshToken;

    /**
     * Expiration time in milliseconds {@code null} for none.
     */
    private Long expirationTimeMillis;

    /**
     * Store information from the credential.
     *
     * @param credential credential whose {@link Credential#getAccessToken access token},
     *                   {@link Credential#getRefreshToken refresh token}, and
     *                   {@link Credential#getExpirationTimeMilliseconds expiration time} need to be stored
     */
    void store(Credential credential) {
      accessToken = credential.getAccessToken();
      refreshToken = credential.getRefreshToken();
      expirationTimeMillis = credential.getExpirationTimeMilliseconds();
    }

    /**
     * Load information into the credential.
     *
     * @param credential credential whose {@link Credential#setAccessToken access token},
     *                   {@link Credential#setRefreshToken refresh token}, and
     *                   {@link Credential#setExpirationTimeMilliseconds expiration time} need to be set if the
     *                   credential already exists in storage
     */
    void load(Credential credential) {
      credential.setAccessToken(accessToken);
      credential.setRefreshToken(refreshToken);
      credential.setExpirationTimeMilliseconds(expirationTimeMillis);
    }
  }
}




Java Source Code List

com.google.android.glass.sample.kittycompass.CompassMenuActivity.java
com.google.android.glass.sample.kittycompass.CompassRenderer.java
com.google.android.glass.sample.kittycompass.CompassService.java
com.google.android.glass.sample.kittycompass.CompassView.java
com.google.android.glass.sample.kittycompass.OrientationManager.java
com.google.android.glass.sample.kittycompass.StartCompassActivity.java
com.google.android.glass.sample.kittycompass.model.Landmarks.java
com.google.android.glass.sample.kittycompass.model.Place.java
com.google.android.glass.sample.kittycompass.util.MathUtils.java
com.google.glassware.AttachmentProxyServlet.java
com.google.glassware.AuthFilter.java
com.google.glassware.AuthServlet.java
com.google.glassware.AuthUtil.java
com.google.glassware.ListableMemoryCredentialStore.java
com.google.glassware.MainServlet.java
com.google.glassware.MirrorClient.java
com.google.glassware.NewUserBootstrapper.java
com.google.glassware.NotifyServlet.java
com.google.glassware.ReauthFilter.java
com.google.glassware.WebUtil.java
com.google.glassware.model.Landmarks.java
com.google.glassware.model.Place.java
com.google.glassware.util.MathUtils.java