/*
 * Copyright (c) 2015. hp.weber GmbH & Co secucard KG (www.secucard.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.secucard.connect.auth;

import com.secucard.connect.auth.model.Token;
import com.secucard.connect.client.DiskCache;

/**
 * Abstract implementation which just delegates the token persistence to a file based cache.
 * Accepts already existing tokens.
 */
public abstract class AbstractClientAuthDetails implements ClientAuthDetails {
  private DiskCache diskCache;

  /**
   * Create an instance.
   *
   * @param dir The cache directory to use. May be absolute (starting with "/" or "\") or relative.
   *            Creates the directory if necessary.
   */
  public AbstractClientAuthDetails(String dir) {
    this.diskCache = new DiskCache(dir);
  }

  public Token getCurrent() {
    return (Token) diskCache.get("token");
  }

  /**
   * Handles token changed event.
   * Override and call super() to place further notification hooks.
   */
  public void onTokenChanged(Token token) {
    diskCache.save("token", token);
  }

  /**
   * Clear cache.
   */
  public void clear() {
    diskCache.clear("token", null);
  }

  /**
   * Remove cache dir.
   */
  public void remove(){
    diskCache.destroy();
  }
}

@@ -699,16 +699,64 @@
 .Token;%0A
+import com.secucard.connect.client.DataStorage;%0A
 import c
@@ -835,13 +835,8 @@
 ich 
-just 
 dele
@@ -872,60 +872,84 @@
 o a 
-file based cache.%0A * Accepts already existing tokens
+instance of %7B@link DataStorage%7D.%0A * Default instance is of %7B@link DiskCache%7D
 .%0A *
@@ -1043,23 +1043,48 @@
 ate 
-DiskCache diskC
+final String id;%0A  private DataStorage c
 ache
@@ -1138,241 +1138,167 @@
 ram 
-dir The cache directory to use. May be absolute (starting with %22/%22 or %22%5C%22) or relative.%0A   *            Creates the directory if necessary.%0A   */%0A  public AbstractClientAuthDetails(String dir) %7B%0A    this.diskCache = new DiskCache(dir
+arg Any argument to pass to %7B@link #createCache(Object)%7D%0A   */%0A  public AbstractClientAuthDetails(Object arg) %7B%0A    cache = createCache(arg);%0A    id = tokenId(
 );%0A 
@@ -1350,21 +1350,17 @@
 (Token) 
-diskC
+c
 ache.get
@@ -1360,23 +1360,18 @@
 che.get(
-%22token%22
+id
 );%0A  %7D%0A%0A
@@ -1528,29 +1528,25 @@
 oken) %7B%0A    
-diskC
+c
 ache.save(%22t
@@ -1543,23 +1543,18 @@
 he.save(
-%22token%22
+id
 , token)
@@ -1577,21 +1577,63 @@
 * Clear 
-cache
+the token from cache. The cache itself may stay
 .%0A   */%0A
@@ -1656,29 +1656,25 @@
 ear() %7B%0A    
-diskC
+c
 ache.clear(%22
@@ -1676,15 +1676,10 @@
 ear(
-%22token%22
+id
 , nu
@@ -1710,17 +1710,64 @@
 ove 
-cache dir
+the whole cache by calling %7B@link DataStorage#destroy()%7D
 .%0A  
@@ -1796,32 +1796,769 @@
 ve()
+ 
 %7B%0A    
-diskCache.destroy(
+cache.destroy();%0A  %7D%0A%0A  /**%0A   * Returns the id to use as reference to token in cache. Called one time by constructor.%0A   * Default is %22token%22. Customize by overriding.%0A   */%0A  protected String tokenId() %7B%0A    return %22token%22;%0A  %7D%0A%0A  /**%0A   * Returns the cache instance to use to persist the token data. Called one time by constructor.%0A   * Default is %7B@link DiskCache%7D. Customize by overriding.%0A   *%0A   * @param arg A string denoting the cache directory to use. May be absolute (starting with %22/%22 or %22%5C%22) or relative.%0A   *            Creates the directory if necessary. If null is passed %22.sccauth%22 is used.%0A   */%0A  protected DataStorage createCache(Object arg) %7B%0A    if (arg == null) %7B%0A      arg = %22.sccauth%22;%0A    %7D%0A    return new DiskCache((String) arg
 );%0A 

Improve basic client auth detail impl.