org.osgi.service.cm.Configuration类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(139)

本文整理了Java中org.osgi.service.cm.Configuration类的一些代码示例,展示了Configuration类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration类的具体详情如下:
包路径:org.osgi.service.cm.Configuration
类名称:Configuration

Configuration介绍

[英]The configuration information for a ManagedService or ManagedServiceFactory object. The Configuration Admin service uses this interface to represent the configuration information for a ManagedService or for a service instance of a ManagedServiceFactory.

A Configuration object contains a configuration dictionary and allows the properties to be updated via this object. Bundles wishing to receive configuration dictionaries do not need to use this class - they register a ManagedService or ManagedServiceFactory. Only administrative bundles, and bundles wishing to update their own configurations need to use this class.

The properties handled in this configuration have case insensitive String objects as keys. However, case must be preserved from the last set key/value.

A configuration can be bound to a specific bundle or to a region of bundles using the location. In its simplest form the location is the location of the target bundle that registered a Managed Service or a Managed Service Factory. However, if the location starts with ? then the location indicates multiple delivery. In such a case the configuration must be delivered to all targets. If security is on, the Configuration Permission can be used to restrict the targets that receive updates. The Configuration Admin must only update a target when the configuration location matches the location of the target's bundle or the target bundle has a Configuration Permission with the action ConfigurationPermission#TARGET and a name that matches the configuration location. The name in the permission may contain wildcards ( '*') to match the location using the same substring matching rules as Filter. Bundles can always create, manipulate, and be updated from configurations that have a location that matches their bundle location.

If a configuration's location is null, it is not yet bound to a location. It will become bound to the location of the first bundle that registers a ManagedService or ManagedServiceFactory object with the corresponding PID.

The same Configuration object is used for configuring both a Managed Service Factory and a Managed Service. When it is important to differentiate between these two the term "factory configuration" is used.
[中]ManagedService或ManagedServiceFactory对象的配置信息。Configuration Admin服务使用此接口表示ManagedService或ManagedServiceFactory的服务实例的配置信息。
配置对象包含配置字典,并允许通过该对象更新属性。希望接收配置字典的捆绑包不需要使用此类-它们注册ManagedService或ManagedServiceFactory。只有管理捆绑包和希望更新其自身配置的捆绑包需要使用此类。
此配置中处理的属性具有不区分大小写的字符串对象作为键。但是,必须从上次设置的键/值中保留大小写。
可以使用位置将配置绑定到特定捆绑包或捆绑包区域。在最简单的形式中,位置是注册托管服务或托管服务工厂的目标捆绑包的位置。但是,如果位置以开始?然后该位置指示多次交付。在这种情况下,必须将配置交付给所有目标。如果启用了安全性,则可以使用配置权限限制接收更新的目标。只有当配置位置与目标捆绑包的位置匹配,或者目标捆绑包的配置权限与action ConfigurationPermission#target以及与配置位置匹配的名称匹配时,配置管理员才能更新目标。权限中的名称可能包含通配符(“*”),以使用与筛选器相同的子字符串匹配规则匹配位置。捆绑包始终可以创建、操作和更新具有与其捆绑包位置匹配的位置的配置。
如果配置的位置为空,则它尚未绑定到位置。它将绑定到第一个捆绑包的位置,该捆绑包使用相应的PID注册ManagedService或ManagedServiceFactory对象。
同一配置对象用于配置托管服务工厂和托管服务。当需要区分这两种情况时,使用术语“工厂配置”。

代码示例

代码示例来源:origin: rhuss/jolokia

private String getConfigurationFromConfigAdmin(ConfigKey pkey) {
  ConfigurationAdmin configAdmin = (ConfigurationAdmin) configAdminTracker.getService();
  if (configAdmin == null) {
    return null;
  }
  try {
    Configuration config = configAdmin.getConfiguration(CONFIG_ADMIN_PID);
    if (config == null) {
      return null;
    }
    Dictionary<?, ?> props = config.getProperties();
    if (props == null) {
      return null;
    }
    return (String) props.get(CONFIG_PREFIX + "." + pkey.getKeyValue());
  } catch (IOException e) {
    return null;
  }
}

代码示例来源:origin: hawtio/hawtio

@Override
  public void configAdminUpdate(String pid, Map<String, String> data) {
    ServiceReference sref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (sref == null) {
      throw new IllegalStateException("The configuration admin service cannot be found.");
    }

    try {
      ConfigurationAdmin ca = (ConfigurationAdmin) bundleContext.getService(sref);
      if (ca == null) {
        throw new IllegalStateException("The configuration admin service cannot be found.");
      }
      Configuration config = ca.getConfiguration(pid, null);
      config.update(new Hashtable<String, String>(data));
    } catch (IOException ioe) {
      throw new RuntimeException(ioe);
    } finally {
      bundleContext.ungetService(sref);
    }
  }
}

代码示例来源:origin: org.apache.felix/org.apache.felix.ipojo

private Hashtable<String, Object> copyConfiguration(Configuration config) {
  Hashtable<String, Object> conf = new Hashtable<String, Object>();
  // Copy configuration
  Enumeration keys = config.getProperties().keys();
  while (keys.hasMoreElements()) {
    String key = (String) keys.nextElement();
    conf.put(key, config.getProperties().get(key));
  }
  return conf;
}

代码示例来源:origin: org.apache.felix.karaf.shell/org.apache.felix.karaf.shell.config

protected void doExecute(ConfigurationAdmin admin) throws Exception {
    Configuration[] configs = admin.listConfigurations(query);
    for (Configuration config : configs) {
      System.out.println("----------------------------------------------------------------");
      System.out.println("Pid:            " + config.getPid());
      if (config.getFactoryPid() != null) {
        System.out.println("FactoryPid:     " + config.getFactoryPid());
      }
      System.out.println("BundleLocation: " + config.getBundleLocation());
      if (config.getProperties() != null) {
        System.out.println("Properties:");
        Dictionary props = config.getProperties();
        for (Enumeration e = props.keys(); e.hasMoreElements();) {
          Object key = e.nextElement();
          System.out.println("   " + key + " = " + props.get(key));
        }
      }
    }
  }
}

代码示例来源:origin: apache/felix

public void deleteProperty(String key) throws Exception{
        Dictionary dic = configuration.getProperties();
        Enumeration keys = dic.keys();
        while (keys.hasMoreElements()) {
          String k = (String) keys.nextElement();
          if (k.equals(key)) {
            dic.remove(k);
              configuration.update(dic);
          }
        }
      }
}

代码示例来源:origin: org.talend.esb/sam-agent

/**
 * Check config.
 *
 * @param context the context
 * @return true, if successful
 * @throws Exception the exception
 */
private boolean checkConfig(BundleContext context) throws Exception {
  ServiceReference serviceRef = context.getServiceReference(ConfigurationAdmin.class.getName());
  ConfigurationAdmin cfgAdmin = (ConfigurationAdmin)context.getService(serviceRef); 
  Configuration config = cfgAdmin.getConfiguration("org.talend.esb.sam.agent");
  return "true".equalsIgnoreCase((String)config.getProperties().get("collector.lifecycleEvent"));
}

代码示例来源:origin: org.apache.karaf.cellar/org.apache.karaf.cellar.config

@Override
public void setExcludedProperties(String excludedProperties) throws Exception {
  Configuration nodeConfiguration = configurationAdmin.getConfiguration(Configurations.NODE, null);
  if (nodeConfiguration != null) {
    Dictionary properties = nodeConfiguration.getProperties();
    if (properties == null)
      properties = new Properties();
    properties.put("config.excluded.properties", excludedProperties);
    nodeConfiguration.update(properties);
  }
}

代码示例来源:origin: org.apache.aries.transaction/org.apache.aries.transaction.manager

private Dictionary<String, Object> getInitialConfig() {
  try {
    ServiceReference<ConfigurationAdmin> ref = bundleContext.getServiceReference(ConfigurationAdmin.class);
    if (ref != null) {
      ConfigurationAdmin configurationAdmin = bundleContext.getService(ref);
      if (configurationAdmin != null) {
        try {
          Configuration config = configurationAdmin.getConfiguration(PID);
          return config.getProperties();
        } finally {
          bundleContext.ungetService(ref);
        }
      }
    }
  } catch (Exception e) {
    // Ignore
  }
  return null;
}

代码示例来源:origin: apache/karaf

@Init
public void init() {
  registration = bundleContext.registerService(ConfigurationListener.class, this, null);
  Configuration[] configs;
  try {
    configs = admin.listConfigurations(null);
    if (configs == null) {
      return;
    }
  } catch (Exception e) {
    return;
  }
  Collection<String> pids = new ArrayList<>();
  for (Configuration config : configs) {
    pids.add(config.getPid());
  }
  delegate.getStrings().addAll(pids);
}

代码示例来源:origin: spring-projects/spring-roo

@Override
public void addRepository(String url) throws Exception {
 LOGGER.log(Level.INFO, "Adding repository " + url + "...");
 getRepositoryAdmin().addRepository(url);
 // Including repos into installed Repos list
 installedRepos.put(url, "");
 // Updating configuration service with installed repos
 config.update(installedRepos);
 LOGGER.log(Level.INFO, "Repository '" + url + "' added!");
}

代码示例来源:origin: apache/cxf

private String extractConfigProperty(ConfigurationAdmin configAdmin,
                   String pid, String propertyName) throws IOException,
  InvalidSyntaxException {
  String ret = null;
  Configuration[] configs = configAdmin.listConfigurations("(service.pid=" + pid + ")");
  if (configs != null && configs.length > 0) {
    Configuration configuration = configs[0];
    if (configuration != null) {
      ret = (String)configuration.getProperties().get(propertyName);
    }
  }
  return ret;
}

代码示例来源:origin: apache/jackrabbit-oak

private static Set<String> getConfigurationKeys(ConfigurationAdmin configurationAdmin)
    throws IOException {
  Set<String> keys = new HashSet<>();
  org.osgi.service.cm.Configuration c = configurationAdmin.getConfiguration(Configuration.PID);
  for (Object k : Collections.list(c.getProperties().keys())) {
    keys.add(k.toString());
  }
  return keys;
}

代码示例来源:origin: apache/karaf

@Override
public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException {
  List<Configuration> configs;
  if (filter == null) {
    configs = configurations;
  } else {
    configs = new ArrayList<>();
    Filter flt = context.createFilter(filter);
    for (Configuration config : configurations) {
      if (flt.match(config.getProperties())) {
        configs.add(config);
      }
    }
  }
  return configs.isEmpty() ? null : configs.toArray(new Configuration[configs.size()]);
}

代码示例来源:origin: apache/felix

private final Bundle getBoundBundle(Configuration config)
{
  if (null == config)
    return null;
  final String location = config.getBundleLocation();
  if (null == location)
    return null;
  final Bundle bundles[] = getBundleContext().getBundles();
  for (int i = 0; bundles != null && i < bundles.length; i++)
  {
    if (bundles[i].getLocation().equals(location))
      return bundles[i];
  }
  return null;
}

代码示例来源:origin: org.apache.karaf.cave.deployer/org.apache.karaf.cave.deployer.service

@Override
public void deleteConnection(String connection) throws Exception {
  Configuration configuration = configurationAdmin.getConfiguration(CONFIG_PID);
  Dictionary<String, Object> properties = configuration.getProperties();
  if (properties != null) {
    properties.remove(connection + ".jmx");
    properties.remove(connection + ".instance");
    properties.remove(connection + ".username");
    properties.remove(connection + ".password");
    configuration.update(properties);
  }
}

代码示例来源:origin: osgi/osgi.enroute.examples

/**
 * This code is the same as {@link #exampleSingleton()} but it creates a
 * Configuration Listener. A listener receives events from configuration
 * admin.
 */
@Tooltip("Create a a configuration for pid='listener', this activates the ConfigurationListenerExample component")
public void listener() throws IOException {
  Configuration configuration = cm.getConfiguration("listener", "?");
  if (configuration.getProperties() == null)
    configuration.update(new Hashtable<String, Object>());
}

代码示例来源:origin: spring-projects/spring-roo

protected void activate(final ComponentContext cContext) throws Exception {
 context = cContext.getBundleContext();
 repositories = new ArrayList<Repository>();
 installedSubsystems = new ArrayList<Subsystem>();
 config = getConfigurationAdmin().getConfiguration("installedRepositories");
 installedRepos = config.getProperties();
 if (installedRepos == null) {
  installedRepos = new Hashtable();
 }
 populateRepositories();
}

代码示例来源:origin: osgi/osgi.enroute.examples

/**
 * Remove a configuration.
 * 
 * @param pid
 *            the (instance) PID of a configuration
 */
public void removeConfiguration(String pid) throws IOException {
  cm.getConfiguration(pid, "?").delete();
}

代码示例来源:origin: osgi/osgi.enroute.examples

@Override
public void configurationEvent(ConfigurationEvent event) {
  try {
    ConfigurationEventProperties cep = new ConfigurationEventProperties();
    cep.factoryPid = event.getFactoryPid();
    cep.pid = event.getPid();
    if (ConfigurationEvent.CM_DELETED != event.getType()) {
      Configuration configuration = cm.getConfiguration(event
          .getPid());
      cep.location = configuration.getBundleLocation();
      Dictionary<String, Object> properties = configuration
          .getProperties();
      if (properties == null) {
        cep.properties = new HashMap<>();
      } else
        cep.properties = toMap(properties);
    }
    ea.postEvent(new Event(TOPIC, dtos.asMap(cep)));
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

private void addConfigurationEntry(TestConfig config, String key, Object value)
    throws IOException {
  config.put(key, value);
  org.osgi.service.cm.Configuration c = configAdmin.getConfiguration(config.servicePid);
  c.update(new Hashtable(config));
}

相关文章