OSGI里面用于操作配置文件(cfg)的接口有2个
org.osgi.service.cm.ManagedService 用于操作单个配置文件 用法见 OSGI CM之ManagedService的使用
org.osgi.service.cm.ManagedServiceFactory 用于操作一组相关的配置文件
org.osgi.service.cm.ManagedServiceFactory是一个接口,实现这个接口后,就可以创建一组相关的服务
下面,就举例说明它的使用方法,这里举例说明创建javax.sql.DataSource的例子
首先,加入依赖
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
一:写一个类DataSourceServiceFactory实现ManagedServiceFactory接口,用于创建javax.sql.DataSource服务
package com.lala.service;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
public class DataSourceServiceFactory implements ManagedServiceFactory {
private static final String KEY_ID = "DataSourceInstanceId";
private BundleContext bundleContext;
private Map<String, ServiceRegistration<DataSource>> dsMap = new ConcurrentHashMap<>();
public DataSourceServiceFactory(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
public String getName() {
return "DataSourceServiceFactory";
}
/**
* 当有新的配置文件产生时,就会触发
*/
public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {
if(properties == null || properties.isEmpty()) {
return;
}
System.out.println("updated pid : " + pid);
ServiceRegistration<DataSource> registration = null;
DataSource ds = null;
try
{
ds = createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
return;
}
Dictionary<String, String> props = new Hashtable<>();
props.put(KEY_ID, getStrVal(properties, "id", null));
registration = bundleContext.registerService(DataSource.class, ds, props);
dsMap.put(pid, registration);
}
/**
* 当文件被删除(移走)后,会触发
*/
public void deleted(String pid) {
System.out.println("deleted pid : " + pid);
ServiceRegistration<DataSource> registration = dsMap.get(pid);
if(registration != null)
{
registration.unregister();
dsMap.remove(pid);
}
}
private DataSource createDataSource(Dictionary<String, ?> properties) throws Exception {
BasicDataSource ds = new BasicDataSource();
ds.setUsername(getStrVal(properties, "username", null));
ds.setPassword(getStrVal(properties, "password", null));
ds.setDriverClassName(getStrVal(properties, "driverClassName", null));
ds.setUrl(getStrVal(properties, "url", null));
ds.setInitialSize(getIntVal(properties, "initialSize", 5));
ds.setMaxIdle(getIntVal(properties, "maxIdle", 5));
ds.setMaxTotal(getIntVal(properties, "maxTotal", 5));
ds.setMinIdle(getIntVal(properties, "minIdle", 5));
return ds;
}
private String getStrVal(Dictionary<String, ?> properties, String key, String defaultValue) {
Object value = properties.get(key);
if (value != null) {
return value.toString();
}
return defaultValue;
}
private Integer getIntVal(Dictionary<String, ?> properties, String key, Integer defaultValue) {
String value = getStrVal(properties, key, null);
if (value != null) {
return Integer.valueOf(value);
}
return defaultValue;
}
}
二:在BundleActivator里面注册上一步的ManagedServiceFactory服务
package com.lala.service;
import java.util.Dictionary;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedServiceFactory;
public class ManagedServiceFactoryActivator implements BundleActivator {
private static final String SERVICE_PID = "com.lala.service.datasource";
private ServiceRegistration<ManagedServiceFactory> registration = null;
public void start(BundleContext context) throws Exception {
Dictionary<String, String> props = new Hashtable<>();
props.put("service.pid", SERVICE_PID);
context.registerService(ManagedServiceFactory.class, new DataSourceServiceFactory(context), props);
}
public void stop(BundleContext context) throws Exception {
if(registration != null) {
registration.unregister();
}
}
}
注意这里的SERVICE_PID的值,下面要用到
三,在pom.xml里面加入
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>com.lala.service.ManagedServiceFactoryActivator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
四:打包好jar之后,就需要运行了,我这里的运行环境为apache-karaf-4.0.3
启动apache-karaf-4.0.3之后,需要先安装commons-dbcp2、commons-pool2(上面的创建javax.sql.DataSource,用到了dbcp,而dbcp又用到了pool)
install -s mvn:org.apache.commons/commons-pool2/2.4.2
install -s mvn:org.apache.commons/commons-dbcp2/2.1.1
OK,安装好之后,就把上面打包好的jar,扔到karaf的deploy目录中去
五:接下来,我们就需要用到上面的SERVICE_PID的值了,
我们在karaf的deploy或者etc下创建com.lala.service.datasource-xxx.cfg 其中,xxx是任意的字符串(最好不要是特殊字符)
比如说:com.lala.service.datasource-mysql01.cfg
内容如下
id=mysql01
username=
password=
driverClassName=
url=
initialSize=
maxIdle=
maxTotal=
minIdle=
以后,只要扔一个com.lala.service.datasource-xxx.cfg的文件到deploy目录中,就可以创建一个javax.sql.DataSource服务
删除对应的文件,相应的服务,也会被删除
六,服务的使用
(1)编程方式的引用
/**
* @param context
* @param id 表示配置文件里面的id值
* @throws Exception
*/
private DataSource getDataSource(BundleContext context, String id)throws Exception
{
String filter = "(DataSourceInstanceId="+id+")";
Collection<ServiceReference<DataSource>> colls = context.getServiceReferences(DataSource.class, filter);
if(colls != null && !colls.isEmpty())
{
ServiceReference<DataSource> ref = colls.iterator().next();
if(ref != null)
{
return context.getService(ref);
}
}
return null;
}
(2)blueprint.xml的引用
<reference interface="javax.sql.DataSource" filter="(DataSourceInstanceId=mysql01)"></reference>