All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsr107.tck.integration.CacheLoaderClient Maven / Gradle / Ivy

Go to download

Tests for JSR107 compliant caches. These are tests from the JSR107 TCK that were published as Open Source. The artifact may also contain additional tests and improvements. For compliance testing use the original TCK as published via jcp.org.

The newest version!
/**
 *  Copyright 2011-2013 Terracotta, Inc.
 *  Copyright 2011-2013 Oracle, Inc.
 *
 *  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 org.jsr107.tck.integration;

import org.jsr107.tck.support.CacheClient;
import org.jsr107.tck.support.Operation;
import org.jsr107.tck.support.Server;

import javax.cache.Cache;
import javax.cache.integration.CacheLoader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * A {@link CacheLoader} that delegates requests to a {@link CacheLoaderServer}.
 *
 * @param  the type of keys
 * @param  the type of values
 * @author Brian Oliver
 */
public class CacheLoaderClient extends CacheClient implements CacheLoader {

  private CacheLoaderServer loaderServer;

  /**
   * Constructs a {@link CacheLoaderClient}.
   *
   * @param address the {@link InetAddress} on which to connect to the {@link CacheLoaderServer}
   * @param port    the port to which to connect to the {@link CacheLoaderServer}
   */
  public CacheLoaderClient(InetAddress address, int port) {
    super(address, port);
  }

  @Override
  protected void checkDirectCallsPossible() {
    super.checkDirectCallsPossible();
    if (directServer != null) {
      loaderServer = (CacheLoaderServer) directServer;
    }
  }

  @Override
  public V load(final K key) {
    if (isDirectCallable()) {
      return loaderServer.getCacheLoader().load(key);
    }
    return getClient().invoke(new LoadOperation(key));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Map loadAll(Iterable keys) {
    if (isDirectCallable()) {
      return loaderServer.getCacheLoader().loadAll(keys);
    }
    return getClient().invoke(new LoadAllOperation(keys));
  }

  /**
   * The {@link LoadOperation} representing a {@link CacheLoader#load(Object)}
   * request.
   *
   * @param  the type of keys
   * @param  the type of values
   */
  private static class LoadOperation implements Operation {
    /**
     * The key to load.
     */
    private K key;

    /**
     * Constructs a {@link LoadOperation}.
     *
     * @param key the Key to load
     */
    public LoadOperation(K key) {
      this.key = key;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getType() {
      return "load";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public V onInvoke(ObjectInputStream ois,
                      ObjectOutputStream oos) throws IOException, ClassNotFoundException {
      oos.writeObject(key);

      Object o = ois.readObject();

      if (o instanceof RuntimeException) {
        throw (RuntimeException) o;
      } else {
        return (V) o;
      }
    }
  }

  /**
   * The {@link LoadAllOperation} representing a {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
   * request.
   *
   * @param  the type of keys
   * @param  the type of values
   */
  private static class LoadAllOperation implements Operation> {
    /**
     * The keys to load.
     */
    private Iterable keys;

    /**
     * Constructs a {@link LoadAllOperation}.
     *
     * @param keys the keys to load
     */
    public LoadAllOperation(Iterable keys) {
      this.keys = keys;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getType() {
      return "loadAll";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map onInvoke(ObjectInputStream ois, ObjectOutputStream oos)
        throws IOException, ClassNotFoundException, ExecutionException {

      //send the keys to load
      for(K key : keys) {
        oos.writeObject(key);
      }
      oos.writeObject(null);

      //read the resulting map
      HashMap map = new HashMap();

      Object result = ois.readObject();
      while (result != null && !(result instanceof Exception)) {
        K key = (K)result;
        V value = (V) ois.readObject();

        map.put(key, value);

        result = ois.readObject();
      }

      if (result instanceof RuntimeException) {
        throw (RuntimeException)result;
      } else {
        return map;
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy