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) { JobContext 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; }
@Override public JobContext call() throws Exception { try { JobSynchronizationManager.register(jobExecution); JobContext context = JobSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { JobSynchronizationManager.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<String>() { @Override public String getObject() throws BeansException { return null; } }); assertEquals("bar", value); assertTrue(context.hasAttribute("foo")); }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { JobContext 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) { JobContext 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) { JobContext 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; }