private void setContextFromCollaborator() { if (context != null) { context.setAttribute("collaborator", collaborator.getName()); context.setAttribute("collaborator.class", collaborator.getClass().toString()); if (collaborator.getParent()!=null) { context.setAttribute("parent", collaborator.getParent().getName()); context.setAttribute("parent.class", collaborator.getParent().getClass().toString()); } } }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { if (logger.isDebugEnabled()) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); } scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
@Test public void testParentContextWithSameStepExecution() { context.setAttribute("foo", "bar"); StepContext other = StepSynchronizationManager.register(stepExecution); assertSame(other, context); }
@Override public StepContext call() throws Exception { try { StepSynchronizationManager.register(stepExecution); StepContext context = StepSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { StepSynchronizationManager.close(); } } });
@Test public void testRegisterDestructionCallback() { final List<String> list = new ArrayList<>(); context.setAttribute("foo", "bar"); scope.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); assertEquals(0, list.size()); // When the context is closed, provided the attribute exists the // callback is called... context.close(); assertEquals(1, list.size()); }
@Test public void testDestructionCallbackSunnyDay() throws Exception { context.setAttribute("foo", "FOO"); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); context.close(); assertEquals(1, list.size()); assertEquals("bar", list.get(0)); }
@Test public void testRemove() { context.setAttribute("foo", "bar"); scope.remove("foo"); assertFalse(context.hasAttribute("foo")); }
@Test public void testRegisterAnotherDestructionCallback() { final List<String> list = new ArrayList<>(); context.setAttribute("foo", "bar"); scope.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); scope.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); assertEquals(0, list.size()); // When the context is closed, provided the attribute exists the // callback is called... context.close(); assertEquals(2, list.size()); }
@Test public void testDestructionCallbackWithException() throws Exception { context.setAttribute("foo", "FOO"); context.setAttribute("bar", "BAR"); context.registerDestructionCallback("bar", new Runnable() { @Override
@Test public void testGetWithSomethingAlreadyThere() { context.setAttribute("foo", "bar"); Object value = scope.get("foo", new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return null; } }); assertEquals("bar", value); assertTrue(context.hasAttribute("foo")); }
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { Map<String, Object> context = chunkContext.getStepContext().getJobExecutionContext(); if(context.get("restart") != null) { contribution.setExitStatus(new ExitStatus("ES3")); } else { chunkContext.getStepContext().setAttribute("restart", true); contribution.setExitStatus(new ExitStatus("ES4")); } return RepeatStatus.FINISHED; } }
@Test public void testGetWithSomethingAlreadyInParentContext() { context.setAttribute("foo", "bar"); StepContext context = StepSynchronizationManager.register(new StepExecution("bar", new JobExecution(0L))); Object value = scope.get("foo", new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return "spam"; } }); assertEquals("spam", value); assertTrue(context.hasAttribute("foo")); StepSynchronizationManager.close(); assertEquals("bar", scope.get("foo", null)); }
@Override public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { // TODO Make shell configurable ProcessBuilder builder = new ProcessBuilder(command); builder.redirectErrorStream(true); Process process = builder.start(); // TODO Store process output in the step context StringBuilder out = new StringBuilder(); logger.info("Executing shell command: " + command); try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = reader.readLine ()) != null) { out.append(out); logger.info(" " + line); } } chunkContext.getStepContext().setAttribute("processOutput", out.toString()); int exitValue = process.exitValue(); if(exitValue != 0) { throw new RuntimeException(); } return RepeatStatus.FINISHED; } }
/** * @see Scope#get(String, ObjectFactory) */ public Object get(String name, ObjectFactory objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.name, name)); scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { if (logger.isDebugEnabled()) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); } scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { if (logger.isDebugEnabled()) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); } scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }