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

The following examples show how to use org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration() . 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: 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;
}
 
Example 2
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test that SCR handles factory CM pids with target filters.
 * 
 */

public void runTest() {
  Bundle b1 = null;
  ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> cmt = null;
  try {
    b1 = Util.installBundle(bc, "componentC_test-1.0.0.jar");
    b1.start();
    final String b1loc = b1.getLocation();

    cmt = new ServiceTracker<ConfigurationAdmin,ConfigurationAdmin>(bc, ConfigurationAdmin.class.getName(), null);
    cmt.open();
    
    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.service.componentC_test.ComponentU");
    assertNull("Should not get serviceRef U", ref);

    ConfigurationAdmin ca = cmt.getService();
    Configuration c = ca.createFactoryConfiguration("componentC_test.U", b1loc);

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("vRef.target", "(vnum=v0)");
    c.update(props);
    
    Thread.sleep(SLEEP_TIME);

    ref = bc.getServiceReference("org.knopflerfish.service.componentC_test.ComponentU");
    assertNull("Should not get serviceRef U", ref);

    props.put("vRef.target", "(vnum=v1)");
    c.update(props);
    
    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> [] refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", null);
    assertTrue("Should get one serviceRef to ComponentU", refs != null && refs.length == 1);

    Configuration c2 = ca.createFactoryConfiguration("componentC_test.U", b1loc);
    props = new Hashtable<String, Object>();
    props.put("vRef.target", "(vnum=v2)");
    c2.update(props);
    
    Thread.sleep(SLEEP_TIME);

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", null);
    assertTrue("Should get two serviceRef to ComponentU", refs != null && refs.length == 2);

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", "(vRef.target=\\(vnum=v1\\))");
    assertTrue("Should get one serviceRef to ComponentU with ref v1", refs != null && refs.length == 1);
    org.knopflerfish.service.componentC_test.ComponentU u =
        (org.knopflerfish.service.componentC_test.ComponentU)bc.getService(refs[0]);
    assertEquals("Should get v1 version", "v1", u.getV());

    refs = bc.getServiceReferences("org.knopflerfish.service.componentC_test.ComponentU", "(vRef.target=\\(vnum=v2\\))");
    assertTrue("Should get one serviceRef to ComponentU with ref v2", refs != null && refs.length == 1);
    org.knopflerfish.service.componentC_test.ComponentU u2 =
        (org.knopflerfish.service.componentC_test.ComponentU)bc.getService(refs[0]);
    assertNotSame("Services should differ", u, u2);
    assertEquals("Should get v2 version", "v2", u2.getV());
  } catch (Exception e) {
    e.printStackTrace();
    fail("Test11: got unexpected exception " + e);
  } finally {
    if (b1 != null) {
      try {
        if (cmt != null) {
          deleteConfig(cmt.getService(), b1.getLocation());
          cmt.close();
        }
        b1.uninstall();
      } catch (Exception be) {
        be.printStackTrace();
        fail("Test11: got uninstall exception " + be);
      }
    }
  }
}
 
Example 3
Source File: DeployedCMData.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void installIfNeeded()
    throws BundleException
{
  final ConfigurationAdmin ca = DirDeployerImpl.caTracker.getService();
  if (pids == null && ca != null) {
    try {
      fileLastModified = file.lastModified();
      // List with all PIDs that is created from the current file.
      final List<String> pidList = new ArrayList<String>();

      // List with all PIDs for factory configurations that are defined in the
      // current file, used for duplicate detection.
      final List<String> pidListFactory = new ArrayList<String>();

      final Hashtable<String, Object>[] configs = loadCMDataFile(file);
      for (final Hashtable<String, Object> config : configs) {
        final String pid = (String) config.get(CMDataReader.SERVICE_PID);
        config.remove("service.bundleLocation");
        final String fpid = (String) config.get(CMDataReader.FACTORY_PID);

        Configuration cfg;
        if (fpid == null) {
          // Non-factory Configuration
          if (pidList.contains(pid)) {
            DirDeployerImpl.logErr("Skipping dupplicated configuration "
                                   + "with pid='" + pid + "' found in "
                                   + file, null);
            continue;
          }
          final File otherFile = installedCfgs.get(pid);
          if (!file.equals(otherFile)) {
            DirDeployerImpl.log("Overwriting configuration with pid='" + pid
                                + "' defined in '" + otherFile + "'.");
          }
          cfg = ca.getConfiguration(pid, null);
          // Make sure that an existing configuration is unbound from
          // location.
          if (cfg.getBundleLocation() != null) {
            cfg.setBundleLocation(null);
          }
        } else {
          // Factory configuration
          if (pidListFactory.contains(pid)) {
            DirDeployerImpl.logErr("Skipping non-unique factory "
                                   + "configuration with service.pid='" + pid
                                   + "' found in " + file, null);
            continue;
          }
          pidListFactory.add(pid);
          cfg = ca.createFactoryConfiguration(fpid, null);
          DirDeployerImpl.log("Created factory config with pid '"
                              + cfg.getPid()
                              + "' for file configuration with service.pid '"
                              + pid + "'.");
          filePidToCmPid.put(getFilePidForPid(pid), cfg.getPid());
        }

        cfg.update(config);
        pidList.add(cfg.getPid());
        installedCfgs.put(cfg.getPid(), file);
      }
      pids = pidList.toArray(new String[pidList.size()]);

      DirDeployerImpl.log("installed " + this);
    } catch (final Exception e) {
      DirDeployerImpl.log("Failed to install " + this + "; " + e, e);
    }
  } else {
    DirDeployerImpl.log("already installed " + this);
  }

  if (pids != null) {
    saveState();
  }
}
 
Example 4
Source File: DeployedCMData.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void installIfNeeded()
    throws BundleException
{
  final ConfigurationAdmin ca = DirDeployerImpl.caTracker.getService();
  if (pids == null && ca != null) {
    try {
      fileLastModified = file.lastModified();
      // List with all PIDs that is created from the current file.
      final List<String> pidList = new ArrayList<String>();

      // List with all PIDs for factory configurations that are defined in the
      // current file, used for duplicate detection.
      final List<String> pidListFactory = new ArrayList<String>();

      final Hashtable<String, Object>[] configs = loadCMDataFile(file);
      for (final Hashtable<String, Object> config : configs) {
        final String pid = (String) config.get(CMDataReader.SERVICE_PID);
        config.remove("service.bundleLocation");
        final String fpid = (String) config.get(CMDataReader.FACTORY_PID);

        Configuration cfg;
        if (fpid == null) {
          // Non-factory Configuration
          if (pidList.contains(pid)) {
            DirDeployerImpl.logErr("Skipping dupplicated configuration "
                                   + "with pid='" + pid + "' found in "
                                   + file, null);
            continue;
          }
          final File otherFile = installedCfgs.get(pid);
          if (!file.equals(otherFile)) {
            DirDeployerImpl.log("Overwriting configuration with pid='" + pid
                                + "' defined in '" + otherFile + "'.");
          }
          cfg = ca.getConfiguration(pid, null);
          // Make sure that an existing configuration is unbound from
          // location.
          if (cfg.getBundleLocation() != null) {
            cfg.setBundleLocation(null);
          }
        } else {
          // Factory configuration
          if (pidListFactory.contains(pid)) {
            DirDeployerImpl.logErr("Skipping non-unique factory "
                                   + "configuration with service.pid='" + pid
                                   + "' found in " + file, null);
            continue;
          }
          pidListFactory.add(pid);
          cfg = ca.createFactoryConfiguration(fpid, null);
          DirDeployerImpl.log("Created factory config with pid '"
                              + cfg.getPid()
                              + "' for file configuration with service.pid '"
                              + pid + "'.");
          filePidToCmPid.put(getFilePidForPid(pid), cfg.getPid());
        }

        cfg.update(config);
        pidList.add(cfg.getPid());
        installedCfgs.put(cfg.getPid(), file);
      }
      pids = pidList.toArray(new String[pidList.size()]);

      DirDeployerImpl.log("installed " + this);
    } catch (final Exception e) {
      DirDeployerImpl.log("Failed to install " + this + "; " + e, e);
    }
  } else {
    DirDeployerImpl.log("already installed " + this);
  }

  if (pids != null) {
    saveState();
  }
}