io.fabric8.runtime.itests.support.ServiceProxy Maven / Gradle / Ivy
/**
* Copyright 2005-2014 Red Hat, Inc.
*
* Red Hat licenses this file to you 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 io.fabric8.runtime.itests.support;
import io.fabric8.api.DynamicReference;
import io.fabric8.api.jcip.ThreadSafe;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.TimeUnit;
import org.jboss.gravia.runtime.ModuleContext;
import org.jboss.gravia.runtime.ServiceReference;
import org.jboss.gravia.runtime.ServiceTracker;
@ThreadSafe
public final class ServiceProxy {
public static long DEFAULT_TIMEOUT = 30000L;
private final Class serviceClazz;
private final DelegatingInvocationHandler invocationHandler;
public static ServiceProxy createServiceProxy(ModuleContext context, Class serviceClazz) {
return new ServiceProxy(context, serviceClazz, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
}
public static ServiceProxy createServiceProxy(ModuleContext context, Class serviceClazz, long timeout, TimeUnit timeUnit) {
return new ServiceProxy(context, serviceClazz, timeout, timeUnit);
}
private ServiceProxy(ModuleContext moduleContext, Class serviceClazz, long timeout, TimeUnit timeUnit) {
this.invocationHandler = new DelegatingInvocationHandler(moduleContext, serviceClazz, timeout, timeUnit);
this.serviceClazz = serviceClazz;
}
@SuppressWarnings("unchecked")
public T getService() {
return (T) Proxy.newProxyInstance(serviceClazz.getClassLoader(), new Class[] { serviceClazz }, invocationHandler);
}
public void close() {
invocationHandler.close();
}
private static class DelegatingInvocationHandler implements InvocationHandler {
private final DynamicReference dynamicReference;
private final ServiceTracker tracker;
DelegatingInvocationHandler(ModuleContext context, Class type, long timeout, TimeUnit unit) {
dynamicReference = new DynamicReference(type.getSimpleName(), timeout, unit);
tracker = new ServiceTracker(context, type, null) {
@Override
public T addingService(ServiceReference reference) {
T service = super.addingService(reference);
dynamicReference.bind(service);
return service;
}
@Override
public void modifiedService(ServiceReference reference, T service) {
super.modifiedService(reference, service);
dynamicReference.bind(service);
}
@Override
public void removedService(ServiceReference reference, T service) {
super.removedService(reference, service);
dynamicReference.unbind(service);
}
};
tracker.open();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
T service = dynamicReference.get();
return method.invoke(service, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
void close() {
tracker.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy