private void validateTaskContext(TaskContext context) { if (context == null) { throw new IllegalArgumentException("Task context cannot be null"); } else if (context.getConfiguration() == null || context.getConfiguration().isEmpty()) { throw new IllegalArgumentException("Task context configuration cannot be null"); } else if (!context.getConfiguration().containsKey(TEMPLATE_PROFILE_PROPERTY_NAME)) { throw new IllegalArgumentException( "Task context configuration: Missing required property: " + TEMPLATE_PROFILE_PROPERTY_NAME); } }
private void manageProfile(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); ProfileData profileData = createProfileData(context); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; Version version = current.getVersion(); try { if (lock.acquire(60, TimeUnit.SECONDS)) { if (profileData.isEmpty()) { if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } return; } else if (!version.hasProfile(profileId)) { // Create the profile fabricService.get().getDataStore().createProfile(version.getId(), profileId); } Profile managedProfile = version.getProfile(profileId); // managedProfile.setConfigurations(profileData.getConfigs()); managedProfile.setFileConfigurations(profileData.getFiles()); current.addProfiles(managedProfile); } else { throw new TimeoutException("Timed out waiting for lock"); } } catch (Exception e) { LOGGER.error("Error managing work items.", e); } finally { releaseLock(); } }
@Override public void stop(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } }
/** * Creates a representation of the profile based on the assigned item for the specified * {@linkTaskContext}. * * @param context * @return */ private ProfileData createProfileData(TaskContext context) { ProfileData profileData = new ProfileData(); Set<WorkItem> workItems = assignedWorkItems.get(context); if (workItems.isEmpty()) { return profileData; } Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String templateProfileName = String.valueOf(context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME)); Profile templateProfile = version.getProfile(templateProfileName); Set<String> allFiles = templateProfile.getFileConfigurations().keySet(); Iterable<String> mvelFiles = Iterables.filter(allFiles, MvelPredicate.INSTANCE); Iterable<String> plainFiles = Iterables.filter(allFiles, Predicates.not(MvelPredicate.INSTANCE)); for (String mvelFile : mvelFiles) { Key key = new Key(templateProfile.getId(), mvelFile); synchronized (templates) { CompiledTemplate template = templates.get(key); if (template == null) { template = TemplateCompiler.compileTemplate( new String(templateProfile.getFileConfigurations().get(mvelFile)), parserContext); templates.put(key, template); } } } for (WorkItem workItem : workItems) { Map<String, WorkItem> data = new HashMap<String, WorkItem>(); data.put(WorkItem.ITEM, workItem); // Render templates for (String fileTemplate : mvelFiles) { String file = renderTemplateName(fileTemplate, workItem); Key key = new Key(templateProfile.getId(), fileTemplate); try { String renderedTemplate = TemplateRuntime.execute(templates.get(key), parserContext, data).toString(); updateProfileData(file, renderedTemplate, profileData); } catch (Exception ex) { LOGGER.warn("Failed to render {}. Ignoring.", fileTemplate); } } // Copy plain files. for (String file : plainFiles) { String content = new String(templateProfile.getFileConfigurations().get(file)); updateProfileData(file, content, profileData); } } return profileData; }