Java Code Examples for org.osgi.service.cm.ConfigurationAdmin#listConfigurations()

The following examples show how to use org.osgi.service.cm.ConfigurationAdmin#listConfigurations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SystemMetatypeProvider.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
Set<String> getCMServicePIDs()
{
  final Set<String> pids = new HashSet<String>();
  final ConfigurationAdmin ca = cmTracker.getService();
  if (ca != null) {
    try {
      final Configuration[] configs = ca.listConfigurations("(service.pid=*)");
      for (int i = 0; configs != null && i < configs.length; i++) {
        if (configs[i].getFactoryPid() == null) {
          pids.add(configs[i].getPid());
        }
      }
    } catch (final Exception e) {
      log.error("Failed to get service pids", e);
    }
  }
  return pids;
}
 
Example 2
Source File: SystemMetatypeProvider.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
Set<String> getCMFactoryPIDs()
{
  final Set<String> pids = new HashSet<String>();
  final ConfigurationAdmin ca = cmTracker.getService();
  if (ca != null) {
    try {
      final Configuration[] configs = ca.listConfigurations("(service.pid=*)");
      for (int i = 0; configs != null && i < configs.length; i++) {
        if (configs[i].getFactoryPid() != null) {
          pids.add(configs[i].getFactoryPid());
        }
      }
    } catch (final Exception e) {
      log.error("Failed to get service pids", e);
    }
  }
  return pids;
}
 
Example 3
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Configuration[] getConfigurationsMatchingFilters(ConfigurationAdmin cm,
                                                         Filter[] filters)
    throws Exception
{
  final Configuration[] cs = cm.listConfigurations(null);
  if (cs == null || cs.length == 0) {
    return new Configuration[0];
  }
  if (filters == null || filters.length == 0) {
    return cs;
  }

  final List<Configuration> matching = new ArrayList<Configuration>();
  for (final Configuration cfg : cs) {
    for (final Filter filter : filters) {
      if (filter.match(cfg.getProperties())) {
        matching.add(cfg);
        break;
      }
    }
  }

  return matching.toArray(new Configuration[matching.size()]);
}
 
Example 4
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void deleteConfig(ConfigurationAdmin ca, String location) throws IOException, InvalidSyntaxException {
  Configuration [] cs = ca.listConfigurations("(" + ConfigurationAdmin.SERVICE_BUNDLELOCATION + "=" + location + ")");
  if (cs != null) {
    for (Configuration c : cs) {
      c.delete();
    }
  }
}
 
Example 5
Source File: OsgiLauncherImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private Configuration getConfiguration(ConfigurationAdmin configAdmin, String brooklynConfigPid) {
    String filter = '(' + Constants.SERVICE_PID + '=' + brooklynConfigPid + ')';
    Configuration[] configs;
    try {
        configs = configAdmin.listConfigurations(filter);
    } catch (InvalidSyntaxException | IOException e) {
        throw Exceptions.propagate(e);
    }
    if (configs != null && configs.length > 0) {
        return configs[0];
    } else {
        return null;
    }
}
 
Example 6
Source File: ListEndpointsCommand.java    From cxf with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: ManagedWorkQueueList.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Configuration findConfigForQueueName(AutomaticWorkQueueImpl queue,
                                             ConfigurationAdmin configurationAdmin) throws Exception {
    Configuration selectedConfig = null;
    String filter = "(service.factoryPid=" + ManagedWorkQueueList.FACTORY_PID + ")";
    Configuration[] configs = configurationAdmin.listConfigurations(filter);
    for (Configuration configuration : configs) {
        Dictionary<String, Object> props = configuration.getProperties();
        String name = (String)props.get(AutomaticWorkQueueImpl.PROPERTY_NAME);
        if (queue.getName().equals(name)) {
            selectedConfig = configuration;
        }
    }
    return selectedConfig;
}
 
Example 8
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void start(BundleContext bc)
{

  Activator.bc = bc;

  log = new LogRef(bc, true);

  serverFactory = new HttpServerFactory(bc, log);

  final Dictionary<String, String> parameters =
    new Hashtable<String, String>();
  parameters.put("service.pid", FACTORY_PID);
  configReg =
    bc.registerService(ManagedServiceFactory.class, serverFactory, parameters);

  ServiceReference<ConfigurationAdmin> adminRef = null;
  try {
    ConfigurationAdmin admin = null;
    Configuration[] configs = null;
    try {
      adminRef = bc.getServiceReference(ConfigurationAdmin.class);

      // Potential start order problem!
      if (adminRef != null) {
        admin = bc.getService(adminRef);
        final String filter =
          "(&(service.factoryPid=" + FACTORY_PID + ")"
              + "(|(service.bundleLocation=" + bc.getBundle().getLocation()
              + ")" + "(service.bundleLocation=NULL)"
              + "(!(service.bundleLocation=*))))";
        configs = admin.listConfigurations(filter);
      }
    } catch (final Exception e) {
      if (log.doDebug()) {
        log.debug("Exception when trying to get CM", e);
      }
    }
    if (admin == null) {
      if (log.doInfo()) {
        log.info("No CM present, using default configuration");
      }
      serverFactory.updated(HttpServerFactory.DEFAULT_PID,
                            HttpConfig.getDefaultConfig(bc));
    } else {
      if (configs == null || configs.length == 0) {
        if (log.doInfo()) {
          log.info("No configuration present, creating default configuration");
        }

        serverFactory.updated(HttpServerFactory.DEFAULT_PID,
                              HttpConfig.getDefaultConfig(bc));
      }
    }
  } catch (final ConfigurationException ce) {
    if (log.doError()) {
      log.error("Configuration error", ce);
    }

  } finally {
    if (adminRef != null) {
      bc.ungetService(adminRef);
    }
  }
  registerCommandGroup();

}
 
Example 9
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void start(BundleContext bc)
    throws Exception
{

  m_bc = bc;
  m_log = new LogRef(m_bc);

  m_factory = new SslServiceFactory(m_bc, m_log);

  final Dictionary<String, Object> parameters =
    new Hashtable<String, Object>();
  parameters.put("service.pid", SslServiceFactory.PID);
  m_reg =
    bc.registerService(ManagedServiceFactory.class, m_factory, parameters);

  ServiceReference<ConfigurationAdmin> adminRef = null;
  try {
    ConfigurationAdmin admin = null;
    Configuration[] configs = null;
    try {
      adminRef = bc.getServiceReference(ConfigurationAdmin.class);

      // Potential start order problem!
      if (adminRef != null) {
        admin = bc.getService(adminRef);
        final String filter =
          "(&(service.m_factoryPid=" + SslServiceFactory.PID + ")"
              + "(|(service.bundleLocation=" + bc.getBundle().getLocation()
              + ")" + "(service.bundleLocation=NULL)"
              + "(!(service.bundleLocation=*))))";
        configs = admin.listConfigurations(filter);
      }
    } catch (final Exception e) {
      if (m_log.doDebug()) {
        m_log.debug("Exception when trying to get CM", e);
      }
    }

    if (admin == null) {
      if (m_log.doInfo()) {
        m_log.info("No CM present, using default configuration");
      }

      m_factory.updated(SslServiceWrapper.DEFAULT_SERVICE_PID,
                        SslServiceWrapper.getDefaultConfig());
    } else {
      if (configs == null || configs.length == 0) {
        if (m_log.doInfo()) {
          m_log
              .info("No configuration present, creating default configuration");
        }

        m_factory.updated(SslServiceFactory.PID,
                          SslServiceWrapper.getDefaultConfig());
      }
    }

  } catch (final ConfigurationException ce) {
    m_log.error("Configuration error", ce);

  } finally {
    if (adminRef != null) {
      m_bc.ungetService(adminRef);
    }
  }
}
 
Example 10
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public int cmdList(Dictionary<?, ?> opts,
                   Reader in,
                   PrintWriter out,
                   Session session)
{
  int retcode = 1; // 1 initially not set to 0 until end of try block
  ConfigurationAdmin srvCA = null;
  try {
    srvCA = getCA();

    final String[] selection = (String[]) opts.get("selection");

    Configuration[] cs = null;
    if (selection == null) {
      cs = srvCA.listConfigurations(null);
    } else {
      cs = getConfigurations(out, session, srvCA, selection);
    }
    if (cs == null || cs.length == 0) {
      out.println("No configurations available");
    } else {
      sortConfigurationArray(cs);
      setSessionProperty(session, LISTED_CONFIGS, cs);
      final Map<String,Integer> pidToIndex = new HashMap<String, Integer>();
      for (int i = 0; i<cs.length; i++) {
        pidToIndex.put(cs[i].getPid(), new Integer(i));
      }
      setSessionProperty(session, LISTED_CONFIGS_PID_TO_INDEX, pidToIndex);
      out.println("Available configurations:");
      for (int i = 0; i < cs.length; ++i) {
        out.println("[" + i + "] " + cs[i].getPid());
      }
    }

    retcode = 0; // Success!
  } catch (final Exception e) {
    out.print("List failed. Details:");
    final String reason = e.getMessage();
    out.println(reason == null ? "<unknown>" : reason);

  } finally {
    if (srvCA != null) {
      bc.ungetService(refCA);
    }
  }
  return retcode;
}
 
Example 11
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public int cmdCreate(Dictionary<?, ?> opts,
                     Reader in,
                     PrintWriter out,
                     Session session)
{
  int retcode = 1; // 1 initially not set to 0 until end of try block
  setCurrent(session, null);
  setEditingDict(session);
  final String pid = (String) opts.get("pid");
  final boolean createFactoryConfiguration = opts.get("-f") != null;

  ConfigurationAdmin srvCA = null;
  Configuration templateCfg = null;
  try {
    srvCA = getCA();

    final String template = (String) opts.get("template");
    if (template != null) {
      final Configuration[] templates = getConfigurations(out, session, srvCA, template);
      if (templates == null || templates.length == 0) {
        throw new Exception("Template didn't match any configurations. "
                            + "Remove the template parameter or change "
                            + "your it to match exactly one "
                            +"configuration.");
      } else if (templates.length == 1) {
        templateCfg = templates[0];
        if (pid.equals(templateCfg.getPid())) {
          throw new Exception("template configuration has the same PID as "
                              +"the new one.");
        }
      } else {
        throw new Exception("Template matched " + templates.length
                            + " configurations. Refine your selection "
                            +"to match exactly one configuration.");
      }
    }

    Configuration cfg = null;
    if (createFactoryConfiguration) {
      cfg = srvCA.createFactoryConfiguration(pid, null);
    } else {
      final Configuration[] exisitingCfgs =
        srvCA
            .listConfigurations("(" + Constants.SERVICE_PID + "=" + pid + ")");
      if (exisitingCfgs != null && exisitingCfgs.length == 1) {
        throw new Exception("A configuration with PID '" + pid
                            + "' already exists.");
      }
      cfg = srvCA.getConfiguration(pid, null);
    }

    if (cfg == null) {
      throw new Exception("Failed creating configuration with PID: '" + pid +"'.");
    }
    if (templateCfg != null) {
      cfg.update(templateCfg.getProperties());
    }
    setCurrent(session, cfg);
    retcode = 0; // Success!
  } catch (final Exception e) {
    out.print("Create failed. Details:");
    final String reason = e.getMessage();
    out.println(reason == null ? "<unknown>" : reason);

  } finally {
    if (srvCA != null) {
      bc.ungetService(refCA);
    }
  }
  return retcode;
}