184 lines
5.4 KiB
Java
184 lines
5.4 KiB
Java
/*-
|
|
* ~~~~~~licensing~~~~~~
|
|
* base-support
|
|
* ==========
|
|
* Copyright (C) 2020 - 2021 EmDev LLC
|
|
* ==========
|
|
* 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.
|
|
* ~~~~~~/licensing~~~~~~
|
|
*/
|
|
package ru.entaxy.platform.base.support.osgi;
|
|
|
|
import org.osgi.framework.BundleContext;
|
|
import org.osgi.framework.Constants;
|
|
import org.osgi.framework.FrameworkUtil;
|
|
import org.osgi.framework.ServiceReference;
|
|
import org.osgi.util.tracker.ServiceTracker;
|
|
|
|
import ru.entaxy.platform.base.support.CommonUtils;
|
|
|
|
public class OSGIUtils {
|
|
|
|
protected static abstract class ServicesEntryPoint<T extends ServicesEntryPoint<T>> {
|
|
|
|
protected Class<?> serviceClass = null;
|
|
protected String serviceClassName = null;
|
|
|
|
protected String filter = null;
|
|
|
|
protected int wait = -1;
|
|
|
|
protected BundleContext bundleContext = FrameworkUtil.getBundle(OSGIUtils.class).getBundleContext();
|
|
|
|
public T ofClass(Class<?> clazz) {
|
|
this.serviceClass = clazz;
|
|
return this.getBuilder();
|
|
}
|
|
|
|
public T ofClass(String className) /* throws Exception */ {
|
|
this.serviceClassName = className;
|
|
// BundleWiring wiring = this.bundleContext.getBundle().adapt(BundleWiring.class);
|
|
// ClassLoader cl = wiring.getClassLoader();
|
|
// this.serviceClass = cl.loadClass(this.serviceClassName);
|
|
return this.getBuilder();
|
|
}
|
|
|
|
public T bundleContext(BundleContext context) {
|
|
if (context == null)
|
|
this.bundleContext = FrameworkUtil.getBundle(OSGIUtils.class).getBundleContext();
|
|
else
|
|
this.bundleContext = context;
|
|
return getBuilder();
|
|
}
|
|
|
|
protected abstract T getBuilder();
|
|
}
|
|
|
|
public static class ServicesFindHelper extends ServicesEntryPoint<ServicesFindHelper> {
|
|
|
|
@Override
|
|
protected ServicesFindHelper getBuilder() {
|
|
return this;
|
|
}
|
|
|
|
protected String getClassFilter() {
|
|
String className = (this.serviceClass != null)
|
|
?this.serviceClass.getName()
|
|
:this.serviceClassName;
|
|
return "(" +
|
|
(CommonUtils.isValid(className)
|
|
?(Constants.OBJECTCLASS + "=" + className)
|
|
:"")
|
|
+ ")";
|
|
}
|
|
|
|
public ServicesFindHelper filter(String filter) {
|
|
this.filter = filter;
|
|
return this;
|
|
}
|
|
|
|
public ServicesFindHelper waitService(int millis) {
|
|
this.wait = millis;
|
|
return this;
|
|
}
|
|
|
|
public <T> T get() throws Exception {
|
|
String classFilter = getClassFilter();
|
|
String finalFilter = "";
|
|
if (CommonUtils.isValid(classFilter))
|
|
if (CommonUtils.isValid(this.filter))
|
|
finalFilter = String.format("(&%s%s)", classFilter, this.filter);
|
|
else
|
|
finalFilter = "(" + classFilter + ")";
|
|
else
|
|
if (CommonUtils.isValid(this.filter))
|
|
finalFilter = "(" + this.filter + ")";
|
|
else
|
|
finalFilter = "";
|
|
if (wait > 0) {
|
|
// wait for service
|
|
ServiceTracker tracker;
|
|
if (CommonUtils.isValid(this.filter))
|
|
tracker = new ServiceTracker<>(this.bundleContext, this.bundleContext.createFilter(finalFilter), null);
|
|
else
|
|
tracker = new ServiceTracker<>(this.bundleContext, this.bundleContext.createFilter(classFilter), null);
|
|
tracker.open();
|
|
tracker.waitForService(this.wait);
|
|
ServiceReference<?> connectionManagerServiceReference =
|
|
tracker.getServiceReference();
|
|
tracker.close();
|
|
return (T)bundleContext.getService(connectionManagerServiceReference);
|
|
} else {
|
|
ServiceReference<?>[] serviceReferences;
|
|
ServiceReference<?> serviceReference;
|
|
if (this.serviceClass != null)
|
|
this.serviceClassName = this.serviceClass.getName();
|
|
if (!CommonUtils.isValid(this.serviceClassName))
|
|
return null;
|
|
if (CommonUtils.isValid(this.filter)) {
|
|
serviceReferences = this.bundleContext.getServiceReferences(serviceClassName, this.filter);
|
|
serviceReference = serviceReferences.length>0
|
|
?serviceReferences[0]
|
|
:null;
|
|
} else {
|
|
serviceReference = this.bundleContext.getServiceReference(serviceClassName);
|
|
}
|
|
if (serviceReference == null)
|
|
return null;
|
|
return (T) this.bundleContext.getService(serviceReference);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static class ServicesHelper extends ServicesEntryPoint<ServicesFindHelper> {
|
|
private ServicesFindHelper findHelper = new ServicesFindHelper();
|
|
|
|
@Override
|
|
public ServicesFindHelper ofClass(Class<?> clazz) {
|
|
return findHelper.ofClass(clazz);
|
|
}
|
|
|
|
@Override
|
|
public ServicesFindHelper ofClass(String className) {
|
|
return findHelper.ofClass(className);
|
|
}
|
|
|
|
@Override
|
|
protected ServicesFindHelper getBuilder() {
|
|
return findHelper;
|
|
}
|
|
|
|
}
|
|
|
|
static public <T> T getService(Class<T> serviceClass) {
|
|
BundleContext bundleContext = FrameworkUtil.getBundle(OSGIUtils.class).getBundleContext();
|
|
return getService(bundleContext, serviceClass);
|
|
}
|
|
|
|
|
|
static public <T> T getService(BundleContext bundleContext, Class<T> serviceClass) {
|
|
T result = null;
|
|
ServiceReference<T> ref = bundleContext.getServiceReference(serviceClass);
|
|
if (ref != null) {
|
|
result = bundleContext.getService(ref);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static public ServicesHelper services() {
|
|
return new ServicesHelper();
|
|
}
|
|
|
|
}
|