/** * Returns true if any of the condition sets match an inventoried Resource. * * @param context * @return */ public boolean isActive(GlobalActivationContext context) { ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); Subject subject = context.getSubject(); for (ResourceConditionSet rcs : this.resourceConditionSets) { ResourceCriteria criteria = new ResourceCriteria(); criteria.addFilterPluginName(rcs.getPluginName()); criteria.addFilterResourceTypeName(rcs.getResourceTypeName()); Set<Permission> requiredPermissions = rcs.getPermissions(); if (!((null == requiredPermissions) || requiredPermissions.isEmpty())) { Permission[] arr = requiredPermissions.toArray(new Permission[requiredPermissions.size()]); criteria.addRequiredPermissions(arr); } PageList<Resource> resources = resourceManager.findResourcesByCriteria(context.getSubject(), criteria); if (!((null == resources) || resources.isEmpty())) { return ActivatorHelper.areTraitsSatisfied( subject, rcs.getTraitMatchers(), resources, false); } } return false; }
private void initChildren() { ResourceCriteria criteria = new ResourceCriteria(); criteria.addFilterParentResourceId(resourceId); PageList<Resource> childResources = remoteClient .getResourceManager() .findResourcesByCriteria(remoteClient.getSubject(), criteria); for (Resource child : childResources) { this.children.add(proxyFactory.getResource(child.getId())); } }
private void fetchFavorites( Set<Integer> resourceIds, final Set<Integer> groupIds, final AsyncCallback<Favorites> callback) { ResourceCriteria criteria = new ResourceCriteria(); criteria.addFilterIds(resourceIds.toArray(new Integer[resourceIds.size()])); GWTServiceLookup.getResourceService() .findResourcesByCriteria( criteria, new AsyncCallback<PageList<Resource>>() { public void onFailure(Throwable caught) { callback.onFailure(caught); } public void onSuccess(final PageList<Resource> resources) { if (groupIds.isEmpty()) { callback.onSuccess( new Favorites(resources, new PageList<ResourceGroupComposite>())); return; } ResourceGroupCriteria criteria = new ResourceGroupCriteria(); criteria.addFilterIds(groupIds.toArray(new Integer[groupIds.size()])); GWTServiceLookup.getResourceGroupService() .findResourceGroupCompositesByCriteria( criteria, new AsyncCallback<PageList<ResourceGroupComposite>>() { public void onFailure(Throwable caught) { callback.onFailure(caught); } public void onSuccess(PageList<ResourceGroupComposite> groups) { callback.onSuccess(new Favorites(resources, groups)); } }); } }); }
private void refreshSingletons( final Resource parentResource, final AsyncCallback<PageList<Resource>> callback) { singletonChildren = new ArrayList<Resource>(); // initialize to non-null Integer[] singletonChildTypes = getSingletonChildTypes(parentResource.getResourceType()); if (canCreate && singletonChildTypes.length > 0 && (hasCreatableTypes || hasImportableTypes)) { ResourceCriteria criteria = new ResourceCriteria(); criteria.addFilterParentResourceId(parentResource.getId()); criteria.addFilterResourceTypeIds(singletonChildTypes); GWTServiceLookup.getResourceService() .findResourcesByCriteria( criteria, new AsyncCallback<PageList<Resource>>() { @Override public void onSuccess(PageList<Resource> result) { singletonChildren = result; if (callback != null) { callback.onSuccess(result); } } @Override public void onFailure(Throwable caught) { Log.error("Failed to load child resources for [" + parentResource + "]", caught); if (callback != null) { callback.onFailure(caught); } } }); } else { if (callback != null) { callback.onSuccess(new PageList<Resource>()); } } }
private Resource getPlatform(String hostname) { ResourceCriteria criteria = new ResourceCriteria(); criteria.setFiltersOptional(true); criteria.addFilterResourceKey(hostname); criteria.addFilterName(hostname); criteria.addFilterResourceCategories(ResourceCategory.PLATFORM); criteria.fetchResourceType(true); criteria.fetchExplicitGroups(true); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); PageList<Resource> resources = resourceManager.findResourcesByCriteria(overlord, criteria); if (resources.isEmpty()) { String msg = "Could not find platform with hostname " + hostname + ". The value that you specify for the " + "host argument should match either a platform's resource name and/or its resource key."; throw new ResourceNotFoundException(msg); } return resources.get(0); }
@Override public List<Resource> getResourcesAsList(Integer... resourceIds) { long start = System.currentTimeMillis(); ResourceCriteria criteria = new ResourceCriteria(); // get all of the resources for the supplied ids criteria.addFilterIds(resourceIds); // filter out any that are not actually in inventory criteria.addFilterInventoryStatuses( new ArrayList<InventoryStatus>(InventoryStatus.getInInventorySet())); // get all of them, don't limit to default paging criteria.clearPaging(); criteria.fetchResourceType(true); criteria.fetchPluginConfiguration(true); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); Subject overlord = LookupUtil.getSubjectManager().getOverlord(); List<Resource> result = resourceManager.findResourcesByCriteria(overlord, criteria); if (log.isDebugEnabled()) { log.debug( "Performance: get ResourcesAsList [" + resourceIds + "], timing (" + (System.currentTimeMillis() - start) + ")ms"); } // Now do some clean out of stuff the agent does not need // Perhaps we should limit the query above to only return relevant stuff for (Resource resource : result) { cleanoutResource(resource); } return result; }
@Test(enabled = ENABLE_TESTS) public void testDriftDef() throws Exception { Configuration config = new Configuration(); DriftDefinition driftDefPojo = new DriftDefinition(config); driftDefPojo.setName("testDriftDef"); driftDefPojo.setInterval(60L); driftDefPojo.setBasedir(new BaseDirectory(BaseDirValueContext.fileSystem, "foo/bar")); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); ResourceCriteria c = new ResourceCriteria(); c.addFilterId(newResource.getId()); c.fetchDriftDefinitions(true); List<Resource> resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); Set<DriftDefinition> driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); DriftDefinition driftDef = null; for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getName(), driftDef.getName()); assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); driftDefPojo.setInterval(120L); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); driftDef = null; for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertEquals(driftDefPojo.getName(), driftDef.getName()); assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(120L, driftDef.getInterval()); driftDefPojo.setName("testDriftDef-2"); driftDefPojo.setInterval(30L); driftDefPojo.setBasedir(new BaseDirectory(BaseDirValueContext.fileSystem, "foo/baz")); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(4, driftDefs.size()); for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if ("testDriftDef".equals(driftDef.getName())) { assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals("foo/bar", driftDef.getBasedir().getValueName()); assertEquals(BaseDirValueContext.fileSystem, driftDef.getBasedir().getValueContext()); assertEquals(120L, driftDef.getInterval()); } else if ("testDriftDef-2".equals(driftDef.getName())) { assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); } else if (!"test-1".equals(driftDef.getName()) && !"test-2".equals(driftDef.getName())) { fail("Unexpected drift def name: " + driftDef.getName()); } } driftManager.deleteDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), "testDriftDef"); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getName(), driftDef.getName()); assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); }