use of org.xwiki.container.Container in project xwiki-platform by xwiki.
the class Util method getResourceAsStream.
/**
* Load resources from: 1. FileSystem 2. ServletContext 3. ClassPath in this order.
*
* @param resource resource path to load
* @return InputStream of resource or null if not found
*/
public static InputStream getResourceAsStream(String resource) {
File file = new File(resource);
try {
if (file.exists()) {
return new FileInputStream(file);
}
} catch (Exception e) {
// Probably running under -security, which prevents calling File.exists()
LOGGER.debug("Failed load resource [" + resource + "] using a file path");
}
try {
Container container = Utils.getComponent(Container.class);
InputStream res = container.getApplicationContext().getResourceAsStream(resource);
if (res != null) {
return res;
}
} catch (Exception e) {
LOGGER.debug("Failed to load resource [" + resource + "] using the application context");
}
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
}
use of org.xwiki.container.Container in project xwiki-platform by xwiki.
the class ResourceReferenceHandlerServlet method cleanupComponents.
private void cleanupComponents() throws ServletException {
Container container;
try {
container = this.rootComponentManager.getInstance(Container.class);
} catch (ComponentLookupException e) {
throw new ServletException("Failed to locate a Container component", e);
}
Execution execution;
try {
execution = this.rootComponentManager.getInstance(Execution.class);
} catch (ComponentLookupException e) {
throw new ServletException("Failed to locate a Execution component", e);
}
// We must ensure we clean the ThreadLocal variables located in the Container and Execution components as
// otherwise we will have a potential memory leak.
container.removeRequest();
container.removeResponse();
container.removeSession();
execution.removeContext();
}
use of org.xwiki.container.Container in project xwiki-platform by xwiki.
the class DefaultCSRFTokenTest method configure.
@Before
public void configure() throws Exception {
// set up mocked dependencies
// document access bridge
final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
final CopyStringMatcher returnValue = new CopyStringMatcher(resubmitUrl + "?", "");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNonNull(DocumentReference.class)), with("view"), with(returnValue), with(aNull(String.class)));
will(returnValue);
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNull(DocumentReference.class)), with("view"), with(aNull(String.class)), with(aNull(String.class)));
will(returnValue(mockDocumentUrl));
allowing(mockDocumentAccessBridge).getCurrentDocumentReference();
will(returnValue(null));
}
});
// configuration
final CSRFTokenConfiguration mockConfiguration = getComponentManager().getInstance(CSRFTokenConfiguration.class);
getMockery().checking(new Expectations() {
{
allowing(mockConfiguration).isEnabled();
will(returnValue(true));
}
});
// request
final HttpSession mockSession = getMockery().mock(HttpSession.class);
final HttpServletRequest httpRequest = getMockery().mock(HttpServletRequest.class);
final ServletRequest servletRequest = new ServletRequest(httpRequest);
getMockery().checking(new Expectations() {
{
allowing(httpRequest).getRequestURL();
will(returnValue(new StringBuffer(mockDocumentUrl)));
allowing(httpRequest).getRequestURI();
will(returnValue(mockDocumentUrl));
allowing(httpRequest).getParameterMap();
will(returnValue(new HashMap<String, String[]>()));
allowing(httpRequest).getSession();
will(returnValue(mockSession));
}
});
// session
getMockery().checking(new Expectations() {
{
allowing(mockSession).getAttribute(with(any(String.class)));
will(returnValue(new HashMap<String, Object>()));
}
});
// container
final Container mockContainer = getComponentManager().getInstance(Container.class);
getMockery().checking(new Expectations() {
{
allowing(mockContainer).getRequest();
will(returnValue(servletRequest));
}
});
// logging
getMockery().checking(new Expectations() {
{
// Ignore all calls to debug()
ignoring(any(Logger.class)).method("debug");
}
});
this.csrf = getComponentManager().getInstance(CSRFToken.class);
}
use of org.xwiki.container.Container in project xwiki-platform by xwiki.
the class WebJarsResourceReferenceHandlerTest method configure.
@Before
public void configure() throws Exception {
Container container = this.componentManager.getInstance(Container.class);
this.response = mock(ServletResponse.class);
ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
when(this.response.getOutputStream()).thenReturn(responseOutputStream);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
when(this.response.getHttpServletResponse()).thenReturn(httpResponse);
when(container.getResponse()).thenReturn(this.response);
this.request = mock(ServletRequest.class);
HttpServletRequest httpRequest = mock(HttpServletRequest.class);
when(this.request.getHttpServletRequest()).thenReturn(httpRequest);
when(container.getRequest()).thenReturn(this.request);
this.handler = this.componentManager.getComponentUnderTest();
this.classLoader = mock(NamespaceURLClassLoader.class);
ClassLoaderManager clm = this.componentManager.getInstance(ClassLoaderManager.class);
when(clm.getURLClassLoader("wiki:wiki", true)).thenReturn(this.classLoader);
}
use of org.xwiki.container.Container in project xwiki-platform by xwiki.
the class XWikiServletContextListener method contextDestroyed.
@Override
public void contextDestroyed(ServletContextEvent sce) {
SHUTDOWN_LOGGER.debug("Stopping XWiki...");
// It's possible that the Component Manager failed to initialize some of the required components.
if (this.componentManager != null) {
// to do something on stop to do it.
try {
ObservationManager observationManager = this.componentManager.getInstance(ObservationManager.class);
observationManager.notify(new ApplicationStoppedEvent(), this);
} catch (ComponentLookupException e) {
// Nothing to do here.
// TODO: Log a warning
}
// below in an Event Listener and move it to the legacy module.
try {
ApplicationContextListenerManager applicationContextListenerManager = this.componentManager.getInstance(ApplicationContextListenerManager.class);
Container container = this.componentManager.getInstance(Container.class);
applicationContextListenerManager.destroyApplicationContext(container.getApplicationContext());
} catch (ComponentLookupException ex) {
// Nothing to do here.
// TODO: Log a warning
}
// Make sure to dispose all components before leaving
this.componentManager.dispose();
}
SHUTDOWN_LOGGER.debug("XWiki has been stopped!");
}
Aggregations