private static Resource convertToPojoResource(Resource resource, boolean includeDescendants) { Resource pojoResource = new Resource(resource.getId()); pojoResource.setUuid(resource.getUuid()); pojoResource.setResourceKey(resource.getResourceKey()); pojoResource.setResourceType(resource.getResourceType()); pojoResource.setMtime(resource.getMtime()); pojoResource.setInventoryStatus(resource.getInventoryStatus()); Configuration pcCopy = resource.getPluginConfiguration(); if (pcCopy != null) { pcCopy = pcCopy.deepCopy(); } pojoResource.setPluginConfiguration(pcCopy); pojoResource.setName(resource.getName()); pojoResource.setDescription(resource.getDescription()); pojoResource.setLocation(resource.getLocation()); pojoResource.setVersion(resource.getVersion()); if (resource.getParentResource() != null) { pojoResource.setParentResource(convertToPojoResource(resource.getParentResource(), false)); } if (includeDescendants) { for (Resource childResource : resource.getChildResources()) { if (isVisibleInInventory(childResource)) { pojoResource.addChildResource(convertToPojoResource(childResource, true)); } } } return pojoResource; }
@Override public void resourceActivated(Resource resource) { // Schedule content discovery if resource is synchronized and COMMITED if (resource.getId() != 0 && resource.getInventoryStatus() == COMMITTED) { ContentManager.this.scheduleDiscoveries(resource); } }
/** * @see ResourceContext#ResourceContext(org.rhq.core.domain.resource.Resource, org.rhq.core.pluginapi.inventory.ResourceComponent, org.rhq.core.pluginapi.inventory.ResourceContext, org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent, org.rhq.core.system.SystemInfo, java.io.File, java.io.File, String, org.rhq.core.pluginapi.event.EventContext, org.rhq.core.pluginapi.operation.OperationContext, org.rhq.core.pluginapi.content.ContentContext, org.rhq.core.pluginapi.availability.AvailabilityContext, org.rhq.core.pluginapi.inventory.InventoryContext, org.rhq.core.pluginapi.inventory.PluginContainerDeployment) * * @since 4.0 */ public ResourceUpgradeContext(Resource resource, ResourceContext<?> parentResourceContext, T parentResourceComponent, ResourceDiscoveryComponent<T> resourceDiscoveryComponent, SystemInfo systemInfo, File temporaryDirectory, File baseDataDirectory, String pluginContainerName, EventContext eventContext, OperationContext operationContext, ContentContext contentContext, AvailabilityContext availabilityContext, InventoryContext inventoryContext, PluginContainerDeployment pluginContainerDeployment) { super(resource, parentResourceComponent, parentResourceContext, resourceDiscoveryComponent, systemInfo, temporaryDirectory, baseDataDirectory, pluginContainerName, eventContext, operationContext, contentContext, availabilityContext, inventoryContext, pluginContainerDeployment); this.resourceConfiguration = resource.getResourceConfiguration(); this.name = resource.getName(); this.description = resource.getDescription(); this.version = resource.getVersion(); }
private Resource cloneResourceWithoutChildren(Resource resourceFromServer) { // Use a CopyOnWriteArraySet for childResources to allow the field to be concurrently accessed safely // (i.e. to avoid ConcurrentModificationExceptions). Set<Resource> childResources = new CopyOnWriteArraySet<Resource>(); Resource resource = new Resource(childResources); resource.setId(resourceFromServer.getId()); resource.setUuid(resourceFromServer.getUuid()); resource.setResourceKey(resourceFromServer.getResourceKey()); resource.setResourceType(resourceFromServer.getResourceType()); resource.setMtime(resourceFromServer.getMtime()); resource.setInventoryStatus(resourceFromServer.getInventoryStatus()); resource.setPluginConfiguration(resourceFromServer.getPluginConfiguration()); resource.setVersion(resourceFromServer.getVersion()); resource.setName(resourceFromServer.getName()); compactResource(resource); return resource; }
private void compactResource(final Resource resource) { resource.setAncestry(null); resource.setAlertDefinitions(null); resource.setLocation(null); resource.setModifiedBy(null); resource.setOperationHistories(Collections.EMPTY_LIST); resource.setTags(Collections.EMPTY_SET); resource.setInstalledPackages(Collections.EMPTY_SET); resource.setInstalledPackageHistory(Collections.EMPTY_LIST); resource.setDescription(null); resource.setImplicitGroups(Collections.EMPTY_SET); resource.setExplicitGroups(Collections.EMPTY_SET); resource.setCreateChildResourceRequests(Collections.EMPTY_LIST); resource.setDeleteResourceRequests(Collections.EMPTY_LIST); resource.setAutoGroupBackingGroups(Collections.EMPTY_LIST); if (resource.getSchedules() != null) { // TODO used at all in the agent? if (resource.getSchedules().size() == 0) { resource.setSchedules(Collections.EMPTY_SET); if (resource.getVersion() != null) { resource.setVersion(resource.getVersion().intern()); if (resource.getName() != null) { resource.setName(resource.getName().intern()); Configuration pluginConfiguration = resource.getPluginConfiguration(); if (pluginConfiguration != null) { pluginConfiguration.cleanoutRawConfiguration(); Configuration resourceConfiguration = resource.getResourceConfiguration();
if (existingResource.getInventoryStatus() == InventoryStatus.UNINVENTORIED) { return; Resource existingParent = existingResource.getParentResource(); Resource updatedParent = updatedResource.getParentResource(); ResourceType existingResourceParentType = (existingParent != null) ? existingResource.getParentResource() .getResourceType() : null; ResourceType updatedResourceParentType = (updatedParent != null) ? updatedResource.getParentResource() .getResourceType() : null; Set<ResourceType> validParentTypes = existingResource.getResourceType().getParentResourceTypes(); if (existingResource.getParentResource() != null) { existingResource.getParentResource().removeChildResource(existingResource); updatedParent = entityManager.find(Resource.class, updatedParent.getId()); updatedParent.addChildResource(existingResource); } else { existingResource.setParentResource(Resource.ROOT); if (existingResource.getDescription() == null && updatedResource.getDescription() != null) { if (LOG.isDebugEnabled()) { LOG.debug("Setting description of existing resource with id " + existingResource.getId() + " to '" + updatedResource.getDescription() + "' (as reported by agent)..."); existingResource.setDescription(updatedResource.getDescription()); if ((existingResource.getResourceKey() != null) && !existingResource.getResourceKey().equals(updatedResource.getResourceKey())) {
ret.setResourceId(resource.getId()); StringBuilder logMessage = new StringBuilder("Resource [").append(resource.toString()).append( "] upgraded ["); if (needsUpgrade(resource.getResourceKey(), resourceKey)) { resource.setResourceKey(resourceKey); logMessage.append("resourceKey, "); ret.setUpgradedResourceKey(resource.getResourceKey()); if (isUpgradeAll && needsUpgrade(resource.getName(), name)) { resource.setName(name); logMessage.append("name, "); ret.setUpgradedResourceName(resource.getName()); if (isUpgradeAll && needsUpgrade(resource.getDescription(), description)) { resource.setDescription(description); logMessage.append("description, "); ret.setUpgradedResourceDescription(resource.getDescription()); if (needsUpgrade(resource.getVersion(), version)) { resource.setVersion(version); logMessage.append("version, "); ret.setUpgradedResourceVersion(resource.getVersion()); pluginConfig = pluginConfig.deepCopy(false); PluginConfigurationUpdate update = configurationManager.upgradePluginConfiguration( subjectManager.getOverlord(), resource.getId(), pluginConfig); ret.setUpgradedResourcePluginConfiguration(update.getResource().getPluginConfiguration());
resourceContainer = getResourceContainer(inventoryFile.getPlatform().getId(), inventoryFile .getResourceContainers()); disabledResource = pluginManager.getMetadataManager().getType(resource.getResourceType()) == null; exportWriter.printf("%s <id>%d</id>\n", indent, resource.getId()); exportWriter.printf("%s <key>%s</key>\n", indent, resource.getResourceKey()); exportWriter.printf("%s <name>%s</name>\n", indent, resource.getName()); exportWriter.printf("%s <version>%s</version>\n", indent, resource.getVersion()); exportWriter.printf("%s <uuid>%s</uuid>\n", indent, resource.getUuid()); exportWriter.printf("%s <mtime>%s</mtime>\n", indent, resource.getMtime()); exportWriter.printf("%s <mtime-date>%s</mtime-date>\n", indent, new Date(resource.getMtime())); exportWriter.printf("%s <description>%s</description>\n", indent, resource.getDescription()); exportWriter.printf("%s <inventory-status>%s</inventory-status>\n", indent, resource .getInventoryStatus()); exportWriter.printf("%s <type>%s</type>\n", indent, (resource.getResourceType() != null) ? resource .getResourceType().getName() : "null"); exportWriter.printf("%s <availability-type>%s</availability-type>\n", indent, availString); exportWriter.printf("%s <category>%s</category>\n", indent, (resource.getResourceType() != null) ? resource.getResourceType().getCategory() : "null"); exportWriter.printf("%s <container>\n", indent); exportWriter.printf("%s <availability>%s</availability>\n", indent, avail); children.addAll(resource.getChildResources()); for (Resource child : children) { ResourceContainer childContainer; childContainer = getResourceContainer(child.getId(), inventoryFile.getResourceContainers());
Integer parentId = (null != resource.getParentResource()) ? resource.getParentResource().getId() : null; resource.setParentResource(parentResource); parentMap.put(parentId, parentResource); ResourceType resourceType = resource.getResourceType(); resource.getResourceKey()); if (matchingHistory != null) { String userSuppliedResourceName = matchingHistory.getCreatedResourceName(); if (!isBlank(userSuppliedResourceName) && !userSuppliedResourceName.equals(resource.getName())) { resource.setName(userSuppliedResourceName); if ((resource.getVersion() != null) && (resource.getVersion().length() > 0)) { ProductVersion productVersion = productVersionManager .addProductVersion(resourceType, resource.getVersion()); resource.setProductVersion(productVersion); resource.setItime(System.currentTimeMillis()); resource.setModifiedBy(overlord.getName());
public void fillInFromResource(Resource resource) { setNewDescription(resource.getDescription()); setNewName(resource.getName()); setNewResourceKey(resource.getResourceKey()); setNewPluginConfiguration(resource.getPluginConfiguration()); setNewVersion(resource.getVersion()); }
private void cleanoutResource(Resource resource) { resource.setAncestry(null); resource.setAlertDefinitions(Collections.EMPTY_SET); resource.setLocation(null); resource.setDescription(null); resource.setAutoGroupBackingGroups(Collections.EMPTY_LIST); resource.setExplicitGroups(Collections.EMPTY_SET); resource.setCreateChildResourceRequests(Collections.EMPTY_LIST); resource.setDeleteResourceRequests(Collections.EMPTY_LIST); resource.setImplicitGroups(Collections.EMPTY_SET); resource.setInstalledPackageHistory(Collections.EMPTY_LIST); resource.setInstalledPackages(Collections.EMPTY_SET); resource.setPluginConfigurationUpdates(Collections.EMPTY_LIST); resource.setResourceConfigurationUpdates(Collections.EMPTY_LIST); if (resource.getPluginConfiguration()!=null) { resource.getPluginConfiguration().cleanoutRawConfiguration(); } }
/** * @see #constructResource(int, String, String, String, Integer, int, AvailabilityType) * * @param original the resource * @return the initialized resource flyweight */ public ResourceFlyweight constructResource(Resource original) { int id = original.getId(); String name = original.getName(); String uuid = original.getUuid(); String resourceKey = original.getResourceKey(); Resource parent = original.getParentResource(); ResourceType type = original.getResourceType(); ResourceAvailability avail = original.getCurrentAvailability(); return constructResource(id, name, uuid, resourceKey, parent != null ? parent.getId() : null, type.getId(), avail != null ? avail.getAvailabilityType() : AvailabilityType.UNKNOWN); }
public ResourceWithType fillRWT(Resource res, UriInfo uriInfo) { ResourceType resourceType = res.getResourceType(); ResourceWithType rwt = new ResourceWithType(res.getName(), res.getId()); rwt.setTypeName(resourceType.getName()); rwt.setTypeId(resourceType.getId()); rwt.setPluginName(resourceType.getPlugin()); rwt.setStatus(res.getInventoryStatus().name()); rwt.setLocation(res.getLocation()); rwt.setDescription(res.getDescription()); rwt.setAvailability(res.getCurrentAvailability().getAvailabilityType().toString()); Resource parent = res.getParentResource(); if (parent != null) { rwt.setParentId(parent.getId()); } else { rwt.setParentId(0); rwt.setAncestry(res.getAncestry()); uriBuilder.queryParam("resourceId", res.getId()); URI uri = uriBuilder.build(); Link link = new Link("operationDefinitions", uri.toString()); uriBuilder = uriInfo.getBaseUriBuilder(); uriBuilder.path("/resource/{id}/schedules"); uri = uriBuilder.build(res.getId()); link = new Link("schedules", uri.toString()); rwt.addLink(link); uriBuilder = uriInfo.getBaseUriBuilder(); uriBuilder.path("/resource/{id}/availability"); uri = uriBuilder.build(res.getId());
@Override public Resource updateResource(Subject user, Resource resource) { Resource persistedResource = entityManager.find(Resource.class, resource.getId()); if (persistedResource == null) { throw new ResourceNotFoundException(resource.getId()); } if (!authorizationManager.hasResourcePermission(user, Permission.MODIFY_RESOURCE, resource.getId())) { throw new PermissionException("You do not have permission to modify Resource with id " + resource.getId() + "."); } /*if (getResourceByParentAndKey(user, resource.getParentResource(), resource.getResourceKey()) != null) * { throw new ResourceAlreadyExistsException("Resource with key '" + resource.getName() + "' already * exists");}*/ // On name change make sure we update the ancestry as the name is part of the ancestry string if (!persistedResource.getName().equals(resource.getName())) { persistedResource.setName(resource.getName()); updateAncestry(persistedResource); } persistedResource.setLocation(resource.getLocation()); persistedResource.setDescription(resource.getDescription()); persistedResource.setAgentSynchronizationNeeded(); persistedResource.setModifiedBy(user.getName()); return entityManager.merge(persistedResource); }
+ resource.getResourceType()); mergeResourceResponse = new MergeResourceResponse(existingResource.getId(), existingResource.getMtime(), true); } else { Resource parentResource = this.resourceManager.getResourceById(creator, resource.getParentResource() .getId()); resource.setAgent(parentResource.getAgent()); resource.setModifiedBy(creator.getName()); resource.setInventoryStatus(InventoryStatus.COMMITTED); resource.setItime(System.currentTimeMillis()); try { this.resourceManager.createResource(creator, resource, parentResource.getId()); } catch (ResourceAlreadyExistsException e) { throw new IllegalStateException(e); mergeResourceResponse = new MergeResourceResponse(resource.getId(), resource.getCtime(), false);
Resource parentResource = this.getParentResource(); parentResource.getId() <= 0 || // parentResource.getResourceType() == null) { return null; ancestry.append(parentResource.getResourceType().getId()); ancestry.append(ANCESTRY_ENTRY_DELIM); ancestry.append(parentResource.getId()); ancestry.append(ANCESTRY_ENTRY_DELIM); ancestry.append(parentResource.getName()); String parentAncestry = parentResource.getAncestry(); if (null != parentAncestry) { ancestry.append(ANCESTRY_DELIM); this.setAncestry(ancestry.toString()); return this.getAncestry();
private Set<Resource> findDuplicitSiblingResources(Resource resource, ResourceUpgradeReport upgradeReport) { Resource parent = resource.getParentResource(); if (parent == null) { //there is only a single platform resource on an agent return Collections.emptySet(); } Set<Resource> ret = new HashSet<Resource>(); for (Resource sibling : parent.getChildResources()) { //we'd have a resource key conflict if there was a resource //of the same type under the same parent that would have the same //resource key. if (upgradeReport.getNewResourceKey() != null && sibling.getResourceType().equals(resource.getResourceType()) && !sibling.getUuid().equals(resource.getUuid())) { if (sibling.getResourceKey().equals(upgradeReport.getNewResourceKey())) { ret.add(sibling); } } } return ret; }
private boolean matches(Resource newResource, Resource existingResource) { try { return ((existingResource.getId() != 0) && (existingResource.getId() == newResource.getId())) || (existingResource.getUuid().equals(newResource.getUuid())) || (existingResource.getResourceType().equals(newResource.getResourceType()) && existingResource .getResourceKey().equals(newResource.getResourceKey())); } catch (RuntimeException e) { log.error("Runtime error while attempting to compare existing Resource " + existingResource + " to new Resource " + newResource); throw e; } }
String existingVersion = existingResource.getVersion(); String discoveredVersion = discoveredResource.getVersion(); boolean versionChanged = (existingVersion != null) ? !existingVersion.equals(discoveredVersion) : discoveredVersion != null && !discoveredVersion.isEmpty(); boolean versionUpdated = existingResource.getInventoryStatus() != InventoryStatus.COMMITTED || mergeExistingResourceVersionOnServer(existingResource, discoveredVersion); discoveredResource.getPluginConfiguration()); existingResource.setVersion(discoveredVersion); existingResource.setPluginConfiguration(mergedPluginConfiguration); log.info("Version of [" + existingResource + "] changed from [" + existingVersion + "] to [" + discoveredVersion + "]");
private void prefetchResource(Resource resource, boolean recursive) { if (resource == null) { return; // Nothing to do on invalid input } resource.getId(); resource.getPluginConfiguration().getNotes(); // Initialize the lazy plugin config // Init the lazy parent... // Don't fetch the parent's children, otherwise we'll end up in infinite recursion. prefetchResource(resource.getParentResource(), false); if (recursive) { // Recurse... for (Resource child : resource.getChildResources()) { prefetchResource(child, true); } } }