Package org.butor.json.service

Source Code of org.butor.json.service.ServiceCallerFactory

/*******************************************************************************
* Copyright 2013 butor.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 org.butor.json.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Set;

import org.butor.json.JsonServiceRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.reflect.Reflection;

public class ServiceCallerFactory {
  protected Logger logger = LoggerFactory.getLogger(getClass());
  private ServiceCaller _sc;

  public ServiceCallerFactory(String namespace_, String url_) {
    this(namespace_, url_, -1, null);
  }
  public ServiceCallerFactory(String namespace_, String url_, int maxPayloadLengthToLog) {
    this(namespace_, url_, -1, null);
  }
  public ServiceCallerFactory(String namespace, String url,
      int maxPayloadLengthToLog, Set<String> servicesToNotLogArgs) {
    logger.info("Creating service caller: namespace={}, url={} ...", namespace, url);
    _sc = getServiceCaller(namespace, url, maxPayloadLengthToLog, servicesToNotLogArgs);
  }
  protected ServiceCaller getServiceCaller(String namespace, String url,
      int maxPayloadLengthToLog, Set<String> servicesToNotLogArgs) {
    DefaultServiceCaller dsc = new DefaultServiceCaller(namespace, url);
    dsc.setMaxPayloadLengthToLog(maxPayloadLengthToLog);
    dsc.setServicesToNotLogArgs(servicesToNotLogArgs);
    return dsc;
  }
  public Context getContext(
      final String userId_,
      final String sessionId_,
      final String lang_,
      final ResponseHandler<?> handler_) {

    final JsonServiceRequest jsr = _sc.createRequest(
        "", null, userId_, sessionId_);

    jsr.setLang(lang_);
    Context ctx = new Context() {
      @Override
      public ResponseHandler getResponseHandler() {
        return handler_;
      }
      @Override
      public JsonServiceRequest getRequest() {
        return jsr;
      }
    };
    return ctx;
  }
  public <T> T createServiceCaller(Class<T> interface_) {
    InvocationHandler ih = new InvocationHandler() {

      public Object invoke(Object proxy_, Method method_, Object[] args_)
          throws Throwable {

        Context ctx = (Context)args_[0];

        // remove context from args passed to service as json args
        Object[] serviceArgs = new Object[args_.length-1];
        for (int ii=1; ii<args_.length;ii++)
          serviceArgs[ii-1] = args_[ii];

        JsonServiceRequest jsr = (JsonServiceRequest)ctx.getRequest();
        jsr.setService(method_.getName());
        jsr.setServiceArgsJson(_sc.serialize(serviceArgs));

        _sc.call(jsr, ctx.getResponseHandler());

        return null;
      }
    };
    return Reflection.newProxy(interface_, ih);
  }
}
TOP

Related Classes of org.butor.json.service.ServiceCallerFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.