public void checkConfigurations(Resource resource, boolean checkChildren) {
    ResourceContainer resourceContainer = this.inventoryManager.getResourceContainer(resource);
    ConfigurationFacet resourceComponent = null;
    ResourceType resourceType = resource.getResourceType();

    if (resourceContainer != null
        && resourceContainer.getAvailability() != null
        && resourceContainer.getAvailability().getAvailabilityType() == AvailabilityType.UP) {

      try {
        resourceComponent =
            resourceContainer.createResourceComponentProxy(
                ConfigurationFacet.class,
                FacetLockType.NONE,
                CONFIGURATION_CHECK_TIMEOUT,
                true,
                false);
      } catch (PluginContainerException e) {
        // Expecting when the resource does not support configuration management
      }

      if (resourceComponent != null) {
        // Only report availability for committed resources; don't bother with new, ignored or
        // deleted resources.
        if (resource.getInventoryStatus() == InventoryStatus.COMMITTED
            && resourceType.getResourceConfigurationDefinition() != null) {

          if (log.isErrorEnabled())
            log.debug("Checking for updated resource configuration on: " + resource);

          ConfigurationUpdateRequest request =
              new ConfigurationUpdateRequest(
                  0, resource.getResourceConfiguration(), resource.getId());

          try {

            Configuration liveConfiguration = resourceComponent.loadResourceConfiguration();

            if (liveConfiguration != null) {
              ConfigurationDefinition configurationDefinition =
                  resourceType.getResourceConfigurationDefinition();

              // Normalize and validate the config.
              ConfigurationUtility.normalizeConfiguration(
                  liveConfiguration, configurationDefinition);
              List<String> errorMessages =
                  ConfigurationUtility.validateConfiguration(
                      liveConfiguration, configurationDefinition);
              for (String errorMessage : errorMessages) {
                log.warn(
                    "Plugin Error: Invalid "
                        + resourceType.getName()
                        + " resource configuration returned by "
                        + resourceType.getPlugin()
                        + " plugin - "
                        + errorMessage);
              }

              Configuration original = resource.getResourceConfiguration();
              if (!liveConfiguration.equals(original)) {
                log.info("New configuration version detected on resource: " + resource);
                this.configurationServerService.persistUpdatedResourceConfiguration(
                    resource.getId(), liveConfiguration);
                resource.setResourceConfiguration(liveConfiguration);
              }
            }
          } catch (Throwable t) {
            log.warn("Unable to check for updated configuration", t);
          }
        }
      }

      if (checkChildren) {
        // Avoid concurrent mod exceptions during potentially long duration issues
        Set<Resource> childSet = new HashSet<Resource>(resource.getChildResources());
        for (Resource child : childSet) {
          try {
            checkConfigurations(child, true);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
Esempio n. 2
0
  /**
   * Given a deployment, this examines the destination and the resource to determine where exactly
   * the bundle distribution should be written.
   *
   * @param bundleResourceDeployment describes where the bundle should be or is deployed
   * @return absolute directory location where the bundle should be deployed
   */
  private File getAbsoluteDestinationDir(BundleResourceDeployment bundleResourceDeployment) {
    BundleDestination dest = bundleResourceDeployment.getBundleDeployment().getDestination();
    String destBaseDirName = dest.getDestinationBaseDirectoryName();
    String relativeDeployDir = dest.getDeployDir();

    // paranoia, if no deploy dir is given, as assume it will be directly under the base location
    if (relativeDeployDir == null || relativeDeployDir.trim().length() == 0) {
      relativeDeployDir = File.separator;
    }

    // get the resource entity stored in our local inventory
    InventoryManager im = getInventoryManager();
    Resource resource = bundleResourceDeployment.getResource();
    ResourceContainer container = im.getResourceContainer(resource);
    resource = container.getResource();

    // find out the type of base location that is specified by the bundle destination
    BundleDestinationBaseDirectory bundleDestBaseDir = null;
    ResourceTypeBundleConfiguration rtbc =
        resource.getResourceType().getResourceTypeBundleConfiguration();
    if (rtbc == null) {
      throw new IllegalArgumentException(
          "The resource type doesn't support bundle deployments: " + resource);
    }
    for (BundleDestinationBaseDirectory bdbd : rtbc.getBundleDestinationBaseDirectories()) {
      if (bdbd.getName().equals(destBaseDirName)) {
        bundleDestBaseDir = bdbd;
        break;
      }
    }
    if (bundleDestBaseDir == null) {
      throw new IllegalArgumentException(
          "The resource type doesn't support bundle destination base location named ["
              + destBaseDirName
              + "]");
    }

    // based on the type of destination base location, determine the root base directory
    String destBaseDirValueName =
        bundleDestBaseDir.getValueName(); // the name we look up in the given context
    String baseLocation;
    switch (bundleDestBaseDir.getValueContext()) {
      case fileSystem:
        {
          if (!new File(relativeDeployDir).isAbsolute()) {
            // the deploy dir is not absolute; since we need to pin it to something, we assume the
            // top root directory
            // unless the descriptor told us to go somewhere else differently
            baseLocation = destBaseDirValueName; // ultimately this came from the plugin descriptor
            if (baseLocation == null || baseLocation.trim().length() == 0) {
              baseLocation =
                  File
                      .separator; // paranoia, if the plugin descriptor didn't specify, assume the
                                  // top root directory
            }
          } else {
            baseLocation = null; // so the relativeDeployDir is processed as an absolute dir
          }
          break;
        }
      case pluginConfiguration:
        {
          baseLocation =
              resource.getPluginConfiguration().getSimpleValue(destBaseDirValueName, null);
          if (baseLocation == null) {
            throw new IllegalArgumentException(
                "Cannot determine the bundle base deployment location - "
                    + "there is no plugin configuration setting for ["
                    + destBaseDirValueName
                    + "]");
          }
          break;
        }
      case resourceConfiguration:
        {
          baseLocation =
              resource.getResourceConfiguration().getSimpleValue(destBaseDirValueName, null);
          if (baseLocation == null) {
            throw new IllegalArgumentException(
                "Cannot determine the bundle base deployment location - "
                    + "there is no resource configuration setting for ["
                    + destBaseDirValueName
                    + "]");
          }
          break;
        }
      case measurementTrait:
        {
          baseLocation = getMeasurementManager().getTraitValue(container, destBaseDirValueName);
          if (baseLocation == null) {
            throw new IllegalArgumentException(
                "Cannot obtain trait ["
                    + destBaseDirName
                    + "] for resource ["
                    + resource.getName()
                    + "]");
          }
          break;
        }
      default:
        {
          throw new IllegalArgumentException(
              "Unknown bundle destination location context: " + bundleDestBaseDir);
        }
    }

    File destDir = new File(baseLocation, relativeDeployDir);

    if (!destDir.isAbsolute()) {
      throw new IllegalArgumentException(
          "The base location path specified by ["
              + destBaseDirValueName
              + "] in the context ["
              + bundleDestBaseDir.getValueContext()
              + "] along with the destination directory of ["
              + relativeDeployDir
              + "] did not resolve to an absolute path ["
              + destDir.getPath()
              + "] so there is no way to know where to put the bundle.");
    }

    return destDir;
  }