Пример #1
0
 public static void convertConfigurationToManagedProperties(
     Map<String, ManagedProperty> managedProperties,
     Configuration configuration,
     ResourceType resourceType,
     Map<String, PropertySimple> customProps) {
   ConfigurationDefinition configDefinition = resourceType.getResourceConfigurationDefinition();
   Set<String> missingManagedPropertyNames = new HashSet<String>();
   for (Property property : configuration.getProperties()) {
     String propertyName = property.getName();
     ManagedProperty managedProperty = managedProperties.get(propertyName);
     PropertyDefinition propertyDefinition = configDefinition.get(propertyName);
     if (managedProperty == null) {
       // NOTE: We expect the Profile Service to always return templates that contain *all*
       // ManagedProperties
       //       that are defined for the ComponentType, so this is considered an error. We could
       // build a
       //       ManagedProperty from scratch based on only a PropertyDefinition anyway, since a
       // propDef could
       //       map to multiple different types of MetaValues (e.g. a PropertyList could
       // potentially map to
       //       either an ArrayValue or a CollectionValue).
       missingManagedPropertyNames.add(propertyName);
     } else {
       populateManagedPropertyFromProperty(
           property, propertyDefinition, managedProperty, customProps.get(propertyName));
     }
     if (!missingManagedPropertyNames.isEmpty())
       throw new IllegalStateException(
           "***** The following properties are defined in this plugin's "
               + "descriptor but have no corresponding ManagedProperties: "
               + missingManagedPropertyNames);
   }
   return;
 }
  private void assertVersion2(ResourceType resourceType) {
    PropertyDefinition prop;
    Set<String> seen =
        new HashSet<String>(2); // we use to this remember names of the things that we've seen

    assert resourceType.getChildSubCategories().size() == 1;
    assert resourceType.getChildSubCategories().get(0).getName().equals(SUBCAT);
    assert resourceType.getChildSubCategories().get(0).getDisplayName().equals(SUBCAT_DISPLAYNAME);

    seen.clear();
    ConfigurationDefinition pcDef = resourceType.getPluginConfigurationDefinition();
    assert pcDef.getGroupDefinitions().size() == 2;
    for (PropertyGroupDefinition group : pcDef.getGroupDefinitions()) {
      seen.add(group.getName());
      if (group.getName().equals(CHANGED_PC_GROUP)) {
        assert group.isDefaultHidden() == CHANGED_PC_GROUP_HIDDEN;
      } else if (group.getName().equals(NEW_PC_GROUP)) {
        assert group.isDefaultHidden() == NEW_PC_GROUP_HIDDEN;
      } else {
        assert false : "Unexpected group [" + group.getName() + "]:" + group;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    prop = pcDef.get(CHANGED_PC_PROP);
    assert prop != null;
    assert prop.getName().equals(CHANGED_PC_PROP);
    assert prop.isRequired() == CHANGED_PC_PROP_REQUIRED;
    assert prop.getPropertyGroupDefinition().getName().equals(CHANGED_PC_GROUP);
    prop = pcDef.get(NEW_PC_PROP);
    assert prop != null;
    assert prop.getName().equals(NEW_PC_PROP);
    assert prop.isRequired() == NEW_PC_PROP_REQUIRED;
    assert prop.getPropertyGroupDefinition().getName().equals(NEW_PC_GROUP);

    seen.clear();
    assert resourceType.getProcessScans().size() == 2;
    for (ProcessScan processScan : resourceType.getProcessScans()) {
      seen.add(processScan.getName());
      if (processScan.getName().equals(CHANGED_PROCESS_SCAN_NAME)) {
        assert processScan.getQuery().equals(CHANGED_PROCESS_SCAN_QUERY);
      } else if (processScan.getName().equals(NEW_PROCESS_SCAN_NAME)) {
        assert processScan.getQuery().equals(NEW_PROCESS_SCAN_QUERY);
      } else {
        assert false : "Unexpected process scan[" + processScan.getName() + "]:" + processScan;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    seen.clear();
    assert resourceType.getOperationDefinitions().size() == 2;
    for (OperationDefinition op : resourceType.getOperationDefinitions()) {
      seen.add(op.getName());
      if (op.getName().equals(CHANGED_OP_NAME)) {
        assert op.getTimeout().intValue() == CHANGED_OP_TIMEOUT;
        assert op.getDescription().equals(CHANGED_OP_DESC);
      } else if (op.getName().equals(NEW_OP_NAME)) {
        assert op.getTimeout().intValue() == NEW_OP_TIMEOUT;
        assert op.getDescription().equals(NEW_OP_DESC);
      } else {
        assert false : "Unexpected operation [" + op.getName() + "]:" + op;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    seen.clear();
    assert resourceType.getMetricDefinitions().size() == 3; // include built-in Availability metric
    for (MeasurementDefinition metric : resourceType.getMetricDefinitions()) {
      if (metric.getName().equals(MeasurementDefinition.AVAILABILITY_NAME)) {
        // expected, ignore
        continue;
      }

      seen.add(metric.getName());
      if (metric.getName().equals(CHANGED_METRIC_PROP)) {
        // even though our _v2 plugin set this to something different, our upgrade doesn't change it
        // because
        // we don't want to overwrite changes a user possibly made to the defaut interval (aka
        // metric template)
        assert metric.getDefaultInterval() == METRIC_DEFAULT_INTERVAL;
      } else if (metric.getName().equals(NEW_METRIC_PROP)) {
        assert metric.getDefaultInterval() == NEW_METRIC_DEFAULT_INTERVAL;
      } else {
        assert false : "Unexpected metric [" + metric.getName() + "]:" + metric;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    seen.clear();
    assert resourceType.getEventDefinitions().size() == 2;
    for (EventDefinition event : resourceType.getEventDefinitions()) {
      seen.add(event.getName());
      if (event.getName().equals(CHANGED_EVENT_NAME)) {
        assert event.getDescription().equals(CHANGED_EVENT_DESC);
      } else if (event.getName().equals(NEW_EVENT_NAME)) {
        assert event.getDescription().equals(NEW_EVENT_DESC);
      } else {
        assert false : "Unexpected event [" + event.getName() + "]:" + event;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    assert resourceType.getResourceConfigurationDefinition().getGroupDefinitions().size() == 0;
    prop = resourceType.getResourceConfigurationDefinition().get(CHANGED_RC_PROP);
    assert prop != null;
    assert prop.getName().equals(CHANGED_RC_PROP);
    assert prop.isRequired() == CHANGED_RC_PROP_REQUIRED;
    prop = resourceType.getResourceConfigurationDefinition().get(NEW_RC_PROP);
    assert prop != null;
    assert prop.getName().equals(NEW_RC_PROP);
    assert prop.isRequired() == NEW_RC_PROP_REQUIRED;

    seen.clear();
    assert resourceType.getDriftDefinitionTemplates().size() == 2;
    for (DriftDefinitionTemplate drift : resourceType.getDriftDefinitionTemplates()) {
      DriftDefinition def = drift.getTemplateDefinition();
      seen.add(def.getName());
      if (def.getName().equals(CHANGED_DRIFT_DEF_NAME)) {
        BaseDirectory driftBasedir = def.getBasedir();
        assert driftBasedir.getValueContext().equals(CHANGED_DRIFT_DEF_BASEDIR_CONTEXT);
        assert driftBasedir.getValueName().equals(CHANGED_DRIFT_DEF_BASEDIR_VALUE);
      } else if (def.getName().equals(NEW_DRIFT_DEF_NAME)) {
        BaseDirectory driftBasedir = def.getBasedir();
        assert driftBasedir.getValueContext().equals(NEW_DRIFT_DEF_BASEDIR_CONTEXT);
        assert driftBasedir.getValueName().equals(NEW_DRIFT_DEF_BASEDIR_VALUE);
      } else {
        assert false : "Unexpected drift def [" + def.getName() + "]:" + def;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }

    seen.clear();
    ResourceTypeBundleConfiguration bundle = resourceType.getResourceTypeBundleConfiguration();
    assert bundle.getBundleDestinationBaseDirectories().size() == 2;
    for (BundleDestinationBaseDirectory bundleBasedir :
        bundle.getBundleDestinationBaseDirectories()) {
      seen.add(bundleBasedir.getName());
      if (bundleBasedir.getName().equals(CHANGED_BUNDLE_TARGET_NAME)) {
        assert bundleBasedir.getValueContext().equals(CHANGED_BUNDLE_BASEDIR_CONTEXT);
        assert bundleBasedir.getValueName().equals(CHANGED_BUNDLE_BASEDIR_VALUE);
      } else if (bundleBasedir.getName().equals(NEW_BUNDLE_TARGET_NAME)) {
        assert bundleBasedir.getValueContext().equals(NEW_BUNDLE_BASEDIR_CONTEXT);
        assert bundleBasedir.getValueName().equals(NEW_BUNDLE_BASEDIR_VALUE);
      } else {
        assert false
            : "Unexpected bundle basedir [" + bundleBasedir.getName() + "]:" + bundleBasedir;
      }
    }
    if (seen.size() != 2) {
      assert false : "did not see what we expected to see: " + seen;
    }
  }
  private void assertVersion1(ResourceType resourceType) {
    PropertyGroupDefinition group;
    PropertyDefinition prop;
    ProcessScan processScan;
    OperationDefinition op;
    MeasurementDefinition metric;
    EventDefinition event;
    DriftDefinitionTemplate drift;
    BaseDirectory driftBasedir;
    ResourceTypeBundleConfiguration bundle;
    BundleDestinationBaseDirectory bundleBasedir;

    assert resourceType.getChildSubCategories().size() == 1;
    assert resourceType.getChildSubCategories().get(0).getName().equals(SUBCAT);
    assert resourceType.getChildSubCategories().get(0).getDisplayName().equals(SUBCAT_DISPLAYNAME);

    assert resourceType.getPluginConfigurationDefinition().getGroupDefinitions().size() == 1;
    group = resourceType.getPluginConfigurationDefinition().getGroupDefinitions().get(0);
    assert group.getName().equals(PC_GROUP);
    assert group.isDefaultHidden() == PC_GROUP_HIDDEN;
    prop = resourceType.getPluginConfigurationDefinition().get(PC_PROP);
    assert prop != null;
    assert prop.getName().equals(PC_PROP);
    assert prop.isRequired() == PC_PROP_REQUIRED;
    assert prop.getPropertyGroupDefinition().getName().equals(PC_GROUP);

    assert resourceType.getProcessScans().size() == 1;
    processScan = resourceType.getProcessScans().iterator().next();
    assert processScan.getName().equals(PROCESS_SCAN_NAME);
    assert processScan.getQuery().equals(PROCESS_SCAN_QUERY);

    assert resourceType.getOperationDefinitions().size() == 1;
    op = resourceType.getOperationDefinitions().iterator().next();
    assert op.getName().equals(OP_NAME);
    assert op.getTimeout().intValue() == OP_TIMEOUT;
    assert op.getDescription().equals(OP_DESC);

    assert resourceType.getMetricDefinitions().size() == 2; // include built-in Availability metric
    metric = resourceType.getMetricDefinitions().iterator().next();
    assert metric.getName().equals(METRIC_PROP);
    assert metric.getDefaultInterval() == METRIC_DEFAULT_INTERVAL;

    assert resourceType.getEventDefinitions().size() == 1;
    event = resourceType.getEventDefinitions().iterator().next();
    assert event.getName().equals(EVENT_NAME);
    assert event.getDescription().equals(EVENT_DESC);

    assert resourceType.getResourceConfigurationDefinition().getGroupDefinitions().size() == 0;
    prop = resourceType.getResourceConfigurationDefinition().get(RC_PROP);
    assert prop != null;
    assert prop.getName().equals(RC_PROP);
    assert prop.isRequired() == RC_PROP_REQUIRED;

    assert resourceType.getDriftDefinitionTemplates().size() == 1;
    drift = resourceType.getDriftDefinitionTemplates().iterator().next();
    assert drift.getTemplateDefinition().getName().equals(DRIFT_DEF_NAME);
    driftBasedir = drift.getTemplateDefinition().getBasedir();
    assert driftBasedir.getValueContext().equals(DRIFT_DEF_BASEDIR_CONTEXT);
    assert driftBasedir.getValueName().equals(DRIFT_DEF_BASEDIR_VALUE);

    bundle = resourceType.getResourceTypeBundleConfiguration();
    assert bundle.getBundleDestinationBaseDirectories().size() == 1;
    bundleBasedir = bundle.getBundleDestinationBaseDirectories().iterator().next();
    assert bundleBasedir.getName().equals(BUNDLE_TARGET_NAME);
    assert bundleBasedir.getValueContext().equals(BUNDLE_BASEDIR_CONTEXT);
    assert bundleBasedir.getValueName().equals(BUNDLE_BASEDIR_VALUE);
  }
Пример #4
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Test
  public void loadResourceConfigurationWithType() throws Exception {
    // tell the method story as it happens: mock or create dependencies and configure
    // those dependencies to get the method under test to completion.
    ResourceContext mockResourceContext = mock(ResourceContext.class);

    ResourceType mockResourceType = mock(ResourceType.class);
    when(mockResourceContext.getResourceType()).thenReturn(mockResourceType);

    ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class);
    when(mockResourceType.getResourceConfigurationDefinition())
        .thenReturn(mockConfigurationDefinition);

    ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class);
    when(mockConfigurationDefinition.getDefaultTemplate()).thenReturn(mockConfigurationTemplate);

    Configuration mockConfiguration = mock(Configuration.class);
    when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration);

    Property mockProperty = mock(Property.class);
    when(mockConfiguration.get(eq("__type"))).thenReturn(mockProperty);

    Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class);
    when(mockConfigurationDefinition.getPropertyDefinitions()).thenReturn(mockMap);

    ConfigurationLoadDelegate mockConfigurationLoadDelegate = mock(ConfigurationLoadDelegate.class);
    PowerMockito.whenNew(ConfigurationLoadDelegate.class)
        .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class)
        .withArguments(
            any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class))
        .thenReturn(mockConfigurationLoadDelegate);

    when(mockConfigurationLoadDelegate.loadResourceConfiguration()).thenReturn(mockConfiguration);

    PropertySimple pathPropertySimple = new PropertySimple("path", "abc=def,xyz=test1");
    when(mockConfiguration.get(eq("path"))).thenReturn(pathPropertySimple);
    when(mockResourceContext.getPluginConfiguration()).thenReturn(mockConfiguration);

    ASConnection mockASConnection = mock(ASConnection.class);

    PropertySimple typePropertySimple = new PropertySimple("__type", "xyz");
    PowerMockito.whenNew(PropertySimple.class)
        .withParameterTypes(String.class, Object.class)
        .withArguments(eq("__type"), eq("xyz"))
        .thenReturn(typePropertySimple);

    // create object to test and inject required dependencies
    TemplatedComponent objectUnderTest = new TemplatedComponent();

    objectUnderTest.context = mockResourceContext;
    objectUnderTest.testConnection = mockASConnection;

    // run code under test
    Configuration result = objectUnderTest.loadResourceConfiguration();

    // verify the results (Assert and mock verification)
    Assert.assertEquals(result, mockConfiguration);

    verify(mockMap, times(1)).remove(eq("__type"));
    verify(mockConfiguration, times(1)).get(eq("__type"));
    verify(mockConfiguration, never()).get(eq("__name"));
    verify(mockConfiguration, times(1)).get(eq("path"));
    verify(mockConfiguration, times(1)).put(eq(typePropertySimple));

    PowerMockito.verifyNew(PropertySimple.class).withArguments(eq("__type"), eq("xyz"));
    PowerMockito.verifyNew(ConfigurationLoadDelegate.class)
        .withArguments(
            any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class));
  }
Пример #5
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Test
  public void updateResourceConfigurationWithName() throws Exception {
    // tell the method story as it happens: mock or create dependencies and configure
    // those dependencies to get the method under test to completion.
    ResourceContext mockResourceContext = mock(ResourceContext.class);

    ResourceType mockResourceType = mock(ResourceType.class);
    when(mockResourceContext.getResourceType()).thenReturn(mockResourceType);

    ConfigurationDefinition mockConfigurationDefinition = mock(ConfigurationDefinition.class);
    when(mockResourceType.getResourceConfigurationDefinition())
        .thenReturn(mockConfigurationDefinition);

    ConfigurationDefinition mockConfigurationDefinitionCopy = mock(ConfigurationDefinition.class);
    when(mockConfigurationDefinition.copy()).thenReturn(mockConfigurationDefinitionCopy);

    ConfigurationTemplate mockConfigurationTemplate = mock(ConfigurationTemplate.class);
    when(mockConfigurationDefinitionCopy.getDefaultTemplate())
        .thenReturn(mockConfigurationTemplate);

    Configuration mockConfiguration = mock(Configuration.class);
    when(mockConfigurationTemplate.getConfiguration()).thenReturn(mockConfiguration);

    Property mockProperty = mock(Property.class);
    when(mockConfiguration.get(eq("__type"))).thenReturn(null);
    when(mockConfiguration.get(eq("__name"))).thenReturn(mockProperty);

    Map<String, PropertyDefinition> mockMap = (Map<String, PropertyDefinition>) mock(Map.class);
    when(mockConfigurationDefinitionCopy.getPropertyDefinitions()).thenReturn(mockMap);

    ConfigurationUpdateReport mockReport = mock(ConfigurationUpdateReport.class);
    when(mockReport.getConfiguration()).thenReturn(mockConfiguration);

    ConfigurationWriteDelegate mockConfigurationWriteDelegate =
        mock(ConfigurationWriteDelegate.class);
    PowerMockito.whenNew(ConfigurationWriteDelegate.class)
        .withParameterTypes(ConfigurationDefinition.class, ASConnection.class, Address.class)
        .withArguments(
            any(ConfigurationDefinition.class), any(ASConnection.class), any(Address.class))
        .thenReturn(mockConfigurationWriteDelegate);

    ASConnection mockASConnection = mock(ASConnection.class);
    when(mockASConnection.execute(any(ReadResource.class))).thenReturn(new Result());

    // create object to test and inject required dependencies
    TemplatedComponent objectUnderTest = new TemplatedComponent();

    objectUnderTest.context = mockResourceContext;
    objectUnderTest.testConnection = mockASConnection;

    // run code under test
    objectUnderTest.updateResourceConfiguration(mockReport);

    // verify the results (Assert and mock verification)
    verify(mockMap, times(1)).remove(eq("__name"));
    verify(mockConfiguration, times(1)).get(eq("__type"));
    verify(mockConfiguration, times(1)).get(eq("__name"));
    verify(mockConfiguration, times(1)).remove(eq("__name"));

    PowerMockito.verifyNew(ConfigurationWriteDelegate.class)
        .withArguments(
            any(ConfigurationDefinition.class), eq(mockASConnection), any(Address.class));
  }
Пример #6
0
 public static Configuration convertManagedObjectToConfiguration(
     Map<String, ManagedProperty> managedProperties,
     Map<String, PropertySimple> customProps,
     ResourceType resourceType) {
   Configuration config = new Configuration();
   ConfigurationDefinition configDef = resourceType.getResourceConfigurationDefinition();
   Map<String, PropertyDefinition> propDefs = configDef.getPropertyDefinitions();
   Set<String> propNames = managedProperties.keySet();
   for (String propName : propNames) {
     PropertyDefinition propertyDefinition = propDefs.get(propName);
     ManagedProperty managedProperty = managedProperties.get(propName);
     if (propertyDefinition == null) {
       if (!managedProperty.hasViewUse(ViewUse.STATISTIC))
         LOG.debug(
             resourceType
                 + " does not define a property corresponding to ManagedProperty '"
                 + propName
                 + "'.");
       continue;
     }
     if (managedProperty == null) {
       // This should never happen, but don't let it blow us up.
       LOG.error(
           "ManagedProperty '" + propName + "' has a null value in the ManagedProperties Map.");
       continue;
     }
     MetaValue metaValue = managedProperty.getValue();
     if (managedProperty.isRemoved() || metaValue == null) {
       // Don't even add a Property to the Configuration if the ManagedProperty is flagged as
       // removed or has a
       // null value.
       continue;
     }
     PropertySimple customProp = customProps.get(propName);
     PropertyAdapter propertyAdapter = PropertyAdapterFactory.getCustomPropertyAdapter(customProp);
     if (propertyAdapter == null) {
       propertyAdapter = PropertyAdapterFactory.getPropertyAdapter(metaValue);
     }
     if (propertyAdapter == null) {
       LOG.error(
           "Unable to find a PropertyAdapter for ManagedProperty '"
               + propName
               + "' with MetaType ["
               + metaValue.getMetaType()
               + "] for ResourceType '"
               + resourceType.getName()
               + "'.");
       continue;
     }
     Property property;
     try {
       property = propertyAdapter.convertToProperty(metaValue, propertyDefinition);
     } catch (RuntimeException e) {
       throw new RuntimeException(
           "Failed to convert managed property "
               + managedProperty
               + " to RHQ property of type "
               + propertyDefinition
               + ".",
           e);
     }
     config.put(property);
   }
   return config;
 }
Пример #7
0
  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();
          }
        }
      }
    }
  }
Пример #8
0
  @Override
  public void updateResourceConfiguration(
      final ConfigurationUpdateReport configurationUpdateReport) {
    ResourceType resourceType = context.getResourceType();
    ConfigurationDefinition configDef = resourceType.getResourceConfigurationDefinition();
    Configuration newConfig = configurationUpdateReport.getConfiguration();

    // remove special property being possibly sent from server in case EAP requires reload/restart
    newConfig.remove("__OOB");

    // 1. First of all, read the current configuration
    ConfigurationLoadDelegate readDelegate =
        new ConfigurationLoadDelegate(configDef, getASConnection(), address, includeRuntime);
    Configuration currentConfig;
    try {
      currentConfig = readDelegate.loadResourceConfiguration();
    } catch (Exception e) {
      getLog().error("Could not read current configuration before update", e);
      configurationUpdateReport.setStatus(FAILURE);
      configurationUpdateReport.setErrorMessage(
          "Could not read current configuration before update: " + ThrowableUtil.getRootMessage(e));
      return;
    }

    // 2. We will wrap all property-simple and connection properties changes in a composite
    // operation
    CompositeOperation updateOperation = new CompositeOperation();

    // 3. Capture property-simple changes
    Map<String, PropertySimple> newConfigSimpleProperties = newConfig.getSimpleProperties();
    Map<String, PropertySimple> currentConfigSimpleProperties = currentConfig.getSimpleProperties();

    Set<String> allSimplePropertyNames =
        new HashSet<String>(
            newConfigSimpleProperties.size() + currentConfigSimpleProperties.size());
    allSimplePropertyNames.addAll(newConfigSimpleProperties.keySet());
    allSimplePropertyNames.addAll(currentConfigSimpleProperties.keySet());
    // Read-only
    allSimplePropertyNames.remove(ENABLED_ATTRIBUTE);

    for (String simplePropertyName : allSimplePropertyNames) {
      PropertySimple newConfigPropertySimple = newConfigSimpleProperties.get(simplePropertyName);
      String newConfigPropertySimpleValue =
          newConfigPropertySimple == null ? null : newConfigPropertySimple.getStringValue();
      PropertySimple currentConfigPropertySimple =
          currentConfigSimpleProperties.get(simplePropertyName);
      String currentConfigPropertySimpleValue =
          currentConfigPropertySimple == null ? null : currentConfigPropertySimple.getStringValue();
      boolean canUnset = !UNSET_FORBIDDEN_ATTRIBUTES.contains(simplePropertyName);

      if (newConfigPropertySimpleValue == null) {
        if (currentConfigPropertySimpleValue != null) {
          String val;
          if (canUnset) {
            val = null;
          } else {
            val = configDef.getPropertyDefinitionSimple(simplePropertyName).getDefaultValue();
          }
          updateOperation.addStep(new WriteAttribute(getAddress(), simplePropertyName, val));
        }
      } else if (!newConfigPropertySimpleValue.equals(currentConfigPropertySimpleValue)) {
        updateOperation.addStep(
            new WriteAttribute(getAddress(), simplePropertyName, newConfigPropertySimpleValue));
      }
    }

    // 4. Capture connection property changes
    String connPropAttributeNameOnServer, connPropPluginConfigPropertyName, keyName;
    if (isXADatasourceResource(resourceType)) {
      connPropAttributeNameOnServer = "xa-datasource-properties";
      connPropPluginConfigPropertyName = XA_DATASOURCE_PROPERTIES_ATTRIBUTE;
      keyName = "key";
    } else {
      connPropAttributeNameOnServer = "connection-properties";
      connPropPluginConfigPropertyName = CONNECTION_PROPERTIES_ATTRIBUTE;
      keyName = "pname";
    }

    Map<String, String> newConfigConnectionProperties =
        getConnectionPropertiesAsMap(newConfig.getList(connPropPluginConfigPropertyName), keyName);
    Map<String, String> currentConfigConnectionProperties =
        getConnectionPropertiesAsMap(
            currentConfig.getList(connPropPluginConfigPropertyName), keyName);
    Set<String> allConnectionPropertyNames =
        new HashSet<String>(
            newConfigConnectionProperties.size() + currentConfigConnectionProperties.size());
    allConnectionPropertyNames.addAll(newConfigConnectionProperties.keySet());
    allConnectionPropertyNames.addAll(currentConfigConnectionProperties.keySet());

    for (String connectionPropertyName : allConnectionPropertyNames) {
      Address propertyAddress = new Address(getAddress());
      propertyAddress.add(connPropAttributeNameOnServer, connectionPropertyName);

      String newConfigConnectionPropertyValue =
          newConfigConnectionProperties.get(connectionPropertyName);
      String currentConfigConnectionPropertyValue =
          currentConfigConnectionProperties.get(connectionPropertyName);

      if (newConfigConnectionPropertyValue == null) {
        updateOperation.addStep(new Operation("remove", propertyAddress));
      } else if (currentConfigConnectionPropertyValue == null) {
        Operation addOperation = new Operation("add", propertyAddress);
        addOperation.addAdditionalProperty("value", newConfigConnectionPropertyValue);
        updateOperation.addStep(addOperation);
      } else if (!newConfigConnectionPropertyValue.equals(currentConfigConnectionPropertyValue)) {
        updateOperation.addStep(new Operation("remove", propertyAddress));
        Operation addOperation = new Operation("add", propertyAddress);
        addOperation.addAdditionalProperty("value", newConfigConnectionPropertyValue);
        updateOperation.addStep(addOperation);
      }
    }

    // 5. Update config if needed
    if (updateOperation.numberOfSteps() > 0) {
      Result res = getASConnection().execute(updateOperation);
      if (res.isSuccess()) {
        configurationUpdateReport.setStatus(SUCCESS);
      } else {
        configurationUpdateReport.setStatus(FAILURE);
        configurationUpdateReport.setErrorMessage(res.getFailureDescription());
      }
    } else {
      configurationUpdateReport.setStatus(NOCHANGE);
    }
  }