com.cinchapi.concourse.server.plugin.ConcourseRuntime Maven / Gradle / Ivy
/*
* Copyright (c) 2013-2017 Cinchapi 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 com.cinchapi.concourse.server.plugin;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.LogManager;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.cinchapi.concourse.lang.Criteria;
import com.cinchapi.concourse.lang.Language;
import com.cinchapi.concourse.server.plugin.data.ObjectResultDataset;
import com.cinchapi.concourse.server.plugin.data.TObjectResultDataset;
import com.cinchapi.concourse.server.plugin.io.PluginSerializer;
import com.cinchapi.concourse.thrift.ComplexTObject;
import com.cinchapi.concourse.util.ConcurrentMaps;
import com.cinchapi.concourse.util.Convert;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
/**
* Every Plugin application has a single instance of the
* {@link ConcourseRuntime} class that allows the Plugin to interact with the
* local Concourse Server node from which the Plugin was launched.
*
*
* The current runtime can be obtained using provided the {@link #getRuntime()}
* method. For convenience, each {@link Plugin} provides access to the runtime
* via the {@link Plugin#runtime} variable which is available to all
* implementing applications.
*
*
* @author Jeff Nelson
*/
public class ConcourseRuntime extends StatefulConcourseService {
static {
// turn off logging from java.util.logging
LogManager.getLogManager().reset();
}
/**
* Responsible for taking arbitrary objects and turning them into binary so
* they can be sent across the wire.
*/
private static PluginSerializer serializer = new PluginSerializer();
/**
* Return the runtime instance associated with the current plugin.
*
* @return the {@link ConcourseRuntime} instance associated with the current
* plugin
*/
@NoGuice
public static ConcourseRuntime getRuntime() {
return INSTANCE;
}
/**
* Return a {@link ConcourseRuntime} that is configured to intercept method
* calls and proxy them through the {@link #invokeServer(String, Object...)}
* method.
*
* @return a {@link ConcourseRuntime} instance
*/
@NoGuice
private static ConcourseRuntime init() {
Injector injector = Guice.createInjector(new ServerInvokerModule());
ConcourseRuntime runtime = injector.getInstance(ConcourseRuntime.class);
return runtime;
}
/**
* Invoke {@code method} with {@code args} on the local Concourse Server
* instance that is associated with this {@link ConcourseRuntime runtime}.
*
* @param method the name of the method to invoke
* @param args the args to pass to the method
* @return the result of the method invocation
*/
@NoGuice
@SuppressWarnings("unchecked")
private static T invokeServer(String method, Object... args) {
try {
ConcourseRuntimeAuthorized thread = (ConcourseRuntimeAuthorized) Thread
.currentThread();
List targs = Lists
.newArrayListWithCapacity(args.length);
Collection valueTransform = VALUE_TRANSFORM.get(method);
Collection criteriaTransform = CRITERIA_TRANSFORM
.get(method);
for (int i = 0; i < args.length; ++i) {
// Must go through each parameters and transform generic value
// objects into TObjects and all Criteria into TCriteria.
Object arg = args[i];
if(valueTransform.contains(i)) {
if(arg instanceof List) {
arg = Convert.javaListToThrift((List © 2015 - 2025 Weber Informatics LLC | Privacy Policy