osgi中CM组件的使用

来源:互联网 发布:生孩子的手机游戏知乎 编辑:程序博客网 时间:2024/05/22 14:22

OSGi的CM就是Configuration Admin Service,是用于管理Bundle属性、并在

属性发生变更时通知相应的BUNDLE。这样,系统就能够动态的修改配置属性,

而不需要重启系统。

它的实现原理:当一个BUNDLE需要能够动态的改变它的属性值时,该需要向

OSGI CM注册该属性,注册时需要使用一个PID来标识这个配置属性。在OSGI

CM框架中,PID是用来唯一标识一个配置项属性集。在配置项的管理BUNDLE中

可以通过PID和BUNDLE的location来得到该配置的值,也可以更新该配置属性

值并通知该配置属性所对应的BUNDLE该配置属性已经改变了。
其中ConfigurationAdmin,ManagedService均为CM定义的规范的服务接口,该

BUNDLE向OSGI框架注册配置属性时需要继承ManagedService接口实现update方

法来改变该BUNDLE运行时配置属性。配置管理BUNDLE可以通过Configuration
Admin的getConfiguration方法得到BUNDLE的配置属性。以下是基于OSGI CM组件

实现配置属性的典型序列图:

   

以下为一个OSGI CM动态实现配置管理的源程序:

配置BUNDLE源程序:

package com.example.config.test_configure;

import java.util.Dictionary;
import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

public class Activator implements BundleActivator, ManagedService {
 
 
 // 需要配置的项, 这里假设为IP地址和端口
 
 private String ipAddr;
 
 private int port;
 
 /**
  * 配置注册服务
  */
 ServiceRegistration configReg;
 
 private static final String service_pid = "com.example.config.service.pid";
 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
  */
 public void start(BundleContext context) throws Exception
 {
  //注册该bundle所关心的配置对象
  Properties pro = new Properties();
 
  //添加配置对象PID
  pro.put(Constants.SERVICE_PID, service_pid);
 
  pro.put("ipaddr", "127.0.0.1");
  pro.put("port", String.valueOf(8080));
 
  configReg = context.registerService(ManagedService.class.getName(), this, pro);
  if (null == configReg)
  {
   System.out.println("Register the config failed!");
  }
  System.out.println("Register the config success!");
 }
 
 /**
  * default constructor
  */
 public Activator()
 {
  ipAddr = "";
  port = 0;
 }

 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
  */
 public void stop(BundleContext context) throws Exception
 {
  // 取消注册的配置服务
  if (configReg != null)
  {
   configReg.unregister();
  }
 }

 /* (non-Javadoc)
  * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
  * 配置项更新回调函数
  */
 public void updated(Dictionary properties) throws ConfigurationException {
  // TODO Auto-generated method stub
  System.out.println("Update the configuration!");
  if (properties == null)
  {
   System.out.println("the properies is null!");
   return;
  }
  System.out.println("ipaddr : " + properties.get("ipaddr"));
  System.out.println("port : " + properties.get("port"));
  this.ipAddr = properties.get("ipaddr").toString();
  this.port = Integer.valueOf(properties.get("port").toString());
 }

}


配置管理BUNDLE

package com.example.config.configuremanager;

import java.util.Properties;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.*;


public class Activator implements BundleActivator {
 
 /**
  * 注册配置项服务的PID
  */
 public static final String service_pid = "com.example.config.service.pid";

 /**
  * 注册配置服务的bundle名称
  */
 public static final String config_bundle_name = "com.example.config.test_configure";
 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
  */
 public void start(BundleContext context) throws Exception
 {
  // 得到配置管理服务
  ConfigurationAdmin configAdmin= null;
  configAdmin = (ConfigurationAdmin)context.getService(context.getServiceReference(ConfigurationAdmin.class.getName()));
  if (null == configAdmin)
  {
   System.out.println("Get configAdmin failed!");
   return;
  }
 
  Bundle []bundles = context.getBundles();
  for (Bundle bundle:bundles)
  {  
   if (bundle.getSymbolicName().indexOf(config_bundle_name) != -1)
   {
    // 得到配置对象
    Configuration config = configAdmin.getConfiguration(service_pid, bundle.getLocation());
   
    // 得到bundle初始化配置项值
    ServiceReference []refs = bundle.getRegisteredServices();
    for (ServiceReference ref : refs)
    {
     String ipAddr = ref.getProperty("ipaddr").toString();
     if (ipAddr != null)
     {
      System.out.println("ipAddr=" + ipAddr);
     }
    
     String portStr = ref.getProperty("port").toString();
     if (portStr != null)
     {
      System.out.println("port=" + portStr);
     }   
    }  
   
    // 改变配置项并通知相应的bundle去修改运行参数   
    Properties confProp = new Properties();
    confProp.put("ipaddr", "172.16.128.1");
    confProp.put("port", "5060");
    config.update(confProp);
   
   }
  }
 
 
 
 }

 /*
  * (non-Javadoc)
  * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
  */
 public void stop(BundleContext context) throws Exception {
 }

}

 

转自:http://itshare.17gigs.com


原创粉丝点击