Advertisement
Guest User

Untitled

a guest
May 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function iterate(iterable, callback) {
  2.   var iterator = iterable.iterator();
  3.   while (iterator.hasNext()) {
  4.     callback(iterator.next());
  5.   }
  6. }
  7.  
  8. function filter(iterable, predicate) {
  9.   var matches = java.util.ArrayList();
  10.   iterate(iterable, function(obj) {
  11.     if (predicate(obj)) {
  12.       matches.add(obj);
  13.     }
  14.   });
  15.   return matches;
  16. }
  17.  
  18. function loadPluginConfigDef(resourceTypeName, pluginName) {
  19.   var resourceType = ResourceTypeManager.getResourceTypeByNameAndPlugin(resourceTypeName,
  20.       pluginName);
  21.  
  22.   if (resourceType == null) {
  23.     throw "Failed to find resource type '" + resourceTypeName + "'";
  24.   }
  25.  
  26.   configDef = ConfigurationManager.getPluginConfigurationDefinitionForResourceType(resourceType.id);
  27.  
  28.   if (configDef == null) {
  29.     throw "Failed to load plugin configuration for [resourceType=" + resourceTypeName +
  30.         ", plugin=" + pluginName + "]";
  31.   }
  32.  
  33.   return configDef;
  34. }
  35.  
  36. function findJBossServers() {
  37.   criteria = new ResourceCriteria();
  38.   criteria.addFilterResourceTypeName('JBossAS Server');
  39.   criteria.addFilterPluginName('JBossAS');
  40.   criteria.fetchPluginConfiguration(true);
  41.   criteria.strict = true;
  42.  
  43.   return ResourceManager.findResourcesByCriteria(criteria);
  44. }
  45.  
  46. function findAgents() {
  47.   criteria = new ResourceCriteria();
  48.   criteria.addFilterResourceTypeName('RHQ Agent');
  49.   criteria.addFilterPluginName('RHQAgent');
  50.   criteria.fetchPluginConfiguration(true);
  51.   criteria.strict = true;
  52.  
  53.   return ResourceManager.findResourcesByCriteria(criteria);
  54. }
  55.  
  56. function flattenProperties(configuration) {
  57.   var simpleProperties = java.util.ArrayList();
  58.  
  59.   iterate(configuration.map.values(), function(property) {
  60.     if (property instanceof PropertySimple) {
  61.       simpleProperties.add(property);
  62.     }
  63.     else if (property instanceof PropertyList) {
  64.       simpleProperties.addAll(flattenPropertyList(property));
  65.     }
  66.   });
  67.  
  68.   return simpleProperties;
  69. }
  70.  
  71. function flattenPropertyList(propertyList) {
  72.   var simpleProperties = java.util.ArrayList();
  73.  
  74.   iterate(propertyList.list, function(property) {
  75.     if (property instanceof PropertySimple) {
  76.       simpleProperty.add(property);
  77.     }
  78.     else if (property instanceof PropertyMap) {
  79.       simpleProperties.addAll(flattenPropertyMap(property));
  80.     }
  81.     else if (property instanceof PropertyList) {
  82.       simpleProperties.addAll(flattenPropertyList(property));
  83.     }
  84.   });
  85.  
  86.   return simpleProperties;
  87. }
  88.  
  89. function flattenPropertyMap(propertyMap) {
  90.   var simpleProperties = java.util.ArrayList();
  91.  
  92.   iterate(propertyMap.map.values(), function(property) {
  93.     if (property instanceof PropertySimple) {
  94.       simpleProperties.add(property);
  95.     }
  96.     else if (property instanceof PropertyMap) {
  97.       simpleProperties.addAll(flattenPropertyMap(property));
  98.     }
  99.     else if (property instanceof PropertyList) {
  100.       simpleProperties.addAll(flattPropertyList(property));
  101.     }
  102.   });
  103.  
  104.   return simpleProperties;
  105. }
  106.  
  107. function flattenPropertyDefs(configDef) {
  108.   var propertyDefs = java.util.HashMap();
  109.  
  110.   iterate(configDef.propertyDefinitions.values(), function(propertyDef) {
  111.     if (propertyDef instanceof PropertyDefinitionSimple) {
  112.       propertyDefs.put(propertyDef.name, propertyDef);
  113.     }
  114.     else if (propertyDef instanceof PropertyDefinitionMap) {
  115.       propertyDefs.putAll(flattenPropertyDefinitionMap(propertyDef));
  116.     }
  117.     else if (propertyDef instanceof PropertyDefinitionList) {
  118.       propertyDefs.putAll(flattenPropertyDefinitionList(propertyDef));
  119.     }
  120.   });
  121.  
  122.   return propertyDefs
  123. }
  124.  
  125. function flattenPropertyDefinitionMap(propertyDefMap) {
  126.   var propertyDefs = java.util.HashMap();
  127.  
  128.   iterate(propertyDefMap.propertyDefinitions.values(), function(propertyDef) {
  129.     if (propertyDef instanceof PropertyDefinitionSimple) {
  130.       propertyDefs.put(propertyDef.name, propertyDef);
  131.     }
  132.     else if (propertyDef instanceof PropertyDefinitionMap) {
  133.       propertyDefs.putAll(flattenPropertyDefinitionMap(propertyDef));
  134.     }
  135.     else if (propertyDef instanceof PropertyDefinitionList) {
  136.       propertyDefs.putAll(flattenPropertyDefinitionList(propertyDef));
  137.     }
  138.   });
  139.  
  140.   return propertyDefs;
  141. }
  142.  
  143. function flattenPropertyDefinitionList(propertyDefList) {
  144.   var propertyDefs = java.util.HashMap();
  145.  
  146.   if (propertyDefList.memberDefinition instanceof PropertyDefinitionSimple) {
  147.     propertyDefs.put(propertyDefList.memberDefinition.name);
  148.   }
  149.   else if (propertyDefList.memberDefinition instanceof PropertyDefinitionMap) {
  150.     propertyDefs.putAll(flattenPropertyDefinitionMap(propertyDefList.memberDefinition));
  151.   }
  152.   else if (propertyDefList.memberDefinition instanceof PropertyDefinitionList) {
  153.     propertyDefs.putAll(flattenPropertyDefinitionList(propertyDefList.memberDefinition));
  154.   }
  155.  
  156.   return propertyDefs;
  157. }
  158.  
  159. function isRequiredJBossSnapshotProperty(property) {
  160.   return jbossSnapshotPropertyDefs.containsKey(property.name);
  161. }
  162.  
  163. function isRequiredAgentSnapshotProperty(property) {
  164.   return agentSnapshotPropertyDefs.containsKey(property.name);
  165. }
  166.  
  167. function loadJBossSnapshotPropertyDefs() {
  168.   var snapshotPropertyDefs = java.util.HashMap();
  169.   var propertyDefs = flattenPropertyDefs(jbossPluginConfigDef);
  170.  
  171.   snapshotPropertyDefs.put('snapshotConfigEnabled', propertyDefs.get('snapshotConfigEnabled'));
  172.   snapshotPropertyDefs.put('snapshotConfigDirectory', propertyDefs.get('snapshotConfigDirectory'));
  173.   snapshotPropertyDefs.put('snapshotConfigRecursive', propertyDefs.get('snapshotConfigRecursive'));
  174.   snapshotPropertyDefs.put('snapshotLogEnabled', propertyDefs.get('snapshotLogEnabled'));
  175.   snapshotPropertyDefs.put('snapshotLogDirectory', propertyDefs.get('snapshotLogDirectory'));
  176.   snapshotPropertyDefs.put('snapshotLogRecursive', propertyDefs.get('snapshotLogRecursive'));
  177.   snapshotPropertyDefs.put('snapshotDataEnabled', propertyDefs.get('snapshotDataEnabled'));
  178.   snapshotPropertyDefs.put('snapshotDataDirectory', propertyDefs.get('snapshotDataDirectory'));
  179.   snapshotPropertyDefs.put('snapshotDataRecursive', propertyDefs.get('snapshotDataRecursive'));
  180.   snapshotPropertyDefs.put('snapshotAdditionalFilesEnabled', propertyDefs.get('snapshotAdditionalFilesEnabled'));
  181.   snapshotPropertyDefs.put('snapshotAdditionalFilesDirectory', propertyDefs.get('snapshotAdditionalFilesDirectory'));
  182.   snapshotPropertyDefs.put('snapshotAdditionalFilesRegex', propertyDefs.get('snapshotAdditionalFilesRegex'));
  183.   snapshotPropertyDefs.put('snapshotAdditionalFilesRecursive', propertyDefs.get('snapshotAdditionalFilesRecursive'));
  184.  
  185.   return snapshotPropertyDefs;
  186. }
  187.  
  188. function loadAgentSnapshotPropertyDefs() {
  189.   var snapshotPropertyDefs = new java.util.HashMap();
  190.   var propertyDefs = flattenPropertyDefs(agentPluginConfigDef);
  191.  
  192.   snapshotPropertyDefs.put('snapshotConfigEnabled', propertyDefs.get('snapshotConfigEnabled'));
  193.   snapshotPropertyDefs.put('snapshotLogEnabled', propertyDefs.get('snapshotLogEnabled'));
  194.   snapshotPropertyDefs.put('snapshotDataEnabled', propertyDefs.get('snapshotDataEnabled'));
  195.  
  196.   return snapshotPropertyDefs;
  197. }
  198.  
  199. function resetPluginConfigProps(resources, snapshotPropertyDefs) {
  200.   iterate(resources, function(resource) {
  201.     var simpleProperties = flattenProperties(resource.pluginConfiguration);
  202.     var snapshotProperties = filter(simpleProperties, function(property) {
  203.       return snapshotPropertyDefs.containsKey(property.name);
  204.     });
  205.  
  206.     iterate(snapshotProperties, function(property) {
  207.       var propertyDef = snapshotPropertyDefs.get(property.name);
  208.       if (propertyDef.defaultValue == null) {
  209.         println("Warning: default for " + propertyDef.name + " is null");
  210.       }
  211.       property.stringValue = propertyDef.defaultValue;
  212.     });
  213.  
  214.     ConfigurationManager.updatePluginConfiguration(resource.id, resource.pluginConfiguration);
  215.   });
  216. }
  217.  
  218. //////////////
  219. // main     //        
  220. //////////////
  221. jbossResourceTypeName = 'JBossAS Server';
  222. jbossPluginName = 'JBossAS';
  223.  
  224. agentResourceTypeName = 'RHQ Agent';
  225. agentPluginName = 'RHQAgent';
  226.  
  227. jbossPluginConfigDef = loadPluginConfigDef(jbossResourceTypeName, jbossPluginName);
  228. jbossSnapshotPropertyDefs = loadJBossSnapshotPropertyDefs();
  229.  
  230. agentPluginConfigDef = loadPluginConfigDef(agentResourceTypeName, agentPluginName);
  231. agentSnapshotPropertyDefs = loadAgentSnapshotPropertyDefs(agentPluginConfigDef);
  232.  
  233. resetPluginConfigProps(findJBossServers(), jbossSnapshotPropertyDefs);
  234. resetPluginConfigProps(findAgents(), agentSnapshotPropertyDefs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement