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

org.terracotta.management.l1bridge.AbstractRemoteAgentEndpointImpl Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * All content copyright (c) 2003-2012 Terracotta, Inc., except as may otherwise be noted in a separate copyright
 * notice. All rights reserved.
 */
package org.terracotta.management.l1bridge;

import org.terracotta.management.ServiceLocator;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Sample implementation of {@link RemoteAgentEndpoint} that perform method calls
 * on services registered in the {@link ServiceLocator}.
 *
 * @author Ludovic Orban
 */
public abstract class AbstractRemoteAgentEndpointImpl implements RemoteAgentEndpoint {

  public byte[] invoke(RemoteCallDescriptor remoteCallDescriptor) throws RemoteCallException {
    String serviceName = remoteCallDescriptor.getServiceName();
    try {
      Class serviceClass = Class.forName(serviceName);

      Object service = ServiceLocator.locate(serviceClass);
      if (service == null) {
        throw new RemoteCallException("No such service registered in ServiceLocator: " + serviceName);
      }

      Method method = service.getClass()
          .getMethod(remoteCallDescriptor.getMethodName(), remoteCallDescriptor.getParamClasses());

      Object returnValue = method.invoke(service, remoteCallDescriptor.getParams());
      return serialize(returnValue);
    } catch (ClassNotFoundException cnfe) {
      throw new RemoteCallException("Service class does not exist: " + serviceName, cnfe);
    } catch (NoSuchMethodException nsme) {
      throw new RemoteCallException("Service does not implement method " + fullMethodName(remoteCallDescriptor), nsme);
    } catch (IllegalAccessException iae) {
      throw new RemoteCallException("Error accessing method " + fullMethodName(remoteCallDescriptor), iae);
    } catch (InvocationTargetException ite) {
      throw new RemoteCallException("Error invoking remote method " + fullMethodName(remoteCallDescriptor), ite);
    } catch (IOException ioe) {
      throw new RemoteCallException("Error serializing return value of " + fullMethodName(remoteCallDescriptor), ioe);
    }
  }

  private static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    try {
      oos.writeObject(obj);
    } finally {
      oos.close();
    }
    return baos.toByteArray();
  }

  private static String fullMethodName(RemoteCallDescriptor remoteCallDescriptor) {
    return remoteCallDescriptor.getServiceName() + "." + remoteCallDescriptor.getMethodName() + "()";
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy