Search in sources :

Example 6 with ServletEnvironment

use of org.xwiki.environment.internal.ServletEnvironment in project xwiki-platform by xwiki.

the class ServletContainerPingDataProvider method provideData.

@Override
public Map<String, Object> provideData() {
    Map<String, Object> jsonMap = new HashMap<>();
    if (this.environment instanceof ServletEnvironment) {
        ServletEnvironment servletEnvironment = (ServletEnvironment) this.environment;
        try {
            ServletContext servletContext = servletEnvironment.getServletContext();
            // Format of getServerInfo() is "name/version (text)" where " (text)" is optional.
            String serverInfo = servletContext.getServerInfo();
            jsonMap.put(PROPERTY_SERVLET_CONTAINER_NAME, StringUtils.trim(StringUtils.substringBefore(serverInfo, SERVLET_INFO_VERSION_SEPARATOR)));
            jsonMap.put(PROPERTY_SERVLET_CONTAINER_VERSION, StringUtils.trim(StringUtils.substringBefore(StringUtils.substringAfter(serverInfo, SERVLET_INFO_VERSION_SEPARATOR), SERVLET_INFO_OPTIONALSEPARATOR)));
        } catch (Throwable e) {
            // Ignore, we just don't save that information...
            // However we log a warning since it's a problem that needs to be seen and looked at.
            this.logger.warn("Failed to compute Servlet container information. " + "This information has not been added to the Active Installs ping data. Reason [{}]", ExceptionUtils.getRootCauseMessage(e));
        }
    }
    return jsonMap;
}
Also used : HashMap(java.util.HashMap) ServletContext(javax.servlet.ServletContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment)

Example 7 with ServletEnvironment

use of org.xwiki.environment.internal.ServletEnvironment in project xwiki-platform by xwiki.

the class AbstractBridgedComponentTestCase method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    // Statically store the component manager in {@link Utils} to be able to access it without
    // the context.
    Utils.setComponentManager(getComponentManager());
    this.context = new XWikiContext();
    this.context.setWikiId("xwiki");
    this.context.setMainXWiki("xwiki");
    // We need to initialize the Component Manager so that the components can be looked up
    getContext().put(ComponentManager.class.getName(), getComponentManager());
    // Bridge with old XWiki Context, required for old code.
    Execution execution = getComponentManager().getInstance(Execution.class);
    execution.getContext().setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.context);
    XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
    stubContextProvider.initialize(this.context);
    // Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
    // correctly with a Servlet Context.
    ServletEnvironment environment = (ServletEnvironment) getComponentManager().getInstance(Environment.class);
    final ServletContext mockServletContext = getMockery().mock(ServletContext.class);
    environment.setServletContext(mockServletContext);
    getMockery().checking(new Expectations() {

        {
            allowing(mockServletContext).getResourceAsStream("/WEB-INF/cache/infinispan/config.xml");
            will(returnValue(null));
            allowing(mockServletContext).getResourceAsStream("/WEB-INF/xwiki.cfg");
            will(returnValue(null));
            allowing(mockServletContext).getAttribute("javax.servlet.context.tempdir");
            will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
        }
    });
    final CoreConfiguration mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockCoreConfiguration).getDefaultDocumentSyntax();
            will(returnValue(Syntax.XWIKI_1_0));
        }
    });
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) Expectations(org.jmock.Expectations) Execution(org.xwiki.context.Execution) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) Environment(org.xwiki.environment.Environment) ServletContext(javax.servlet.ServletContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) File(java.io.File) Before(org.junit.Before)

Example 8 with ServletEnvironment

use of org.xwiki.environment.internal.ServletEnvironment in project xwiki-platform by xwiki.

the class XWikiServletContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    // Initializes the Embeddable Component Manager
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    // Initialize all the components. Note that this can fail with a Runtime Exception. This is done voluntarily so
    // that the XWiki webapp will not be available if one component fails to load. It's better to fail-fast.
    ecm.initialize(this.getClass().getClassLoader());
    this.componentManager = ecm;
    // This is a temporary bridge to allow non XWiki components to lookup XWiki components.
    // We're putting the XWiki Component Manager instance in the Servlet Context so that it's
    // available in the XWikiAction class which in turn puts it into the XWikiContext instance.
    // Class that need to lookup then just need to get it from the XWikiContext instance.
    // This is of course not necessary for XWiki components since they just need to implement
    // the Composable interface to get access to the Component Manager or better they simply
    // need to declare their components requirements using the @Inject annotation of the xwiki
    // component manager together with a private class member, for automatic injection by the CM on init.
    servletContextEvent.getServletContext().setAttribute(org.xwiki.component.manager.ComponentManager.class.getName(), this.componentManager);
    // Use a Component Event Manager that stacks Component instance creation events till we tell it to flush them.
    // The reason is that the Observation Manager used to send the events but we need the Application Context to
    // be set up before we start sending events since there can be Observation Listener components that require
    // the Application Context (this is the case for example for the Office Importer Lifecycle Listener).
    StackingComponentEventManager eventManager = new StackingComponentEventManager();
    this.componentManager.setComponentEventManager(eventManager);
    // Initialize the Environment
    try {
        ServletEnvironment servletEnvironment = this.componentManager.getInstance(Environment.class);
        servletEnvironment.setServletContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Servlet Environment", e);
    }
    // below in an Event Listener and move it to the legacy module.
    try {
        ServletContainerInitializer containerInitializer = this.componentManager.getInstance(ServletContainerInitializer.class);
        containerInitializer.initializeApplicationContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Application Context", e);
    }
    // Send an Observation event to signal the XWiki application is started. This allows components who need to do
    // something on startup to do it.
    ObservationManager observationManager;
    try {
        observationManager = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to find the Observation Manager component", e);
    }
    // Now that the Application Context is set up, send the Component instance creation events we had stacked up.
    eventManager.setObservationManager(observationManager);
    eventManager.shouldStack(false);
    eventManager.flushEvents();
    // Make sure installed extensions are initialized before sending ApplicationStartedEvent
    try {
        this.componentManager.getInstance(ExtensionInitializer.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize installed extensions", e);
    }
    // Indicate to the various components that XWiki is ready
    observationManager.notify(new ApplicationStartedEvent(), this);
}
Also used : EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ApplicationStartedEvent(org.xwiki.observation.event.ApplicationStartedEvent) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) StackingComponentEventManager(org.xwiki.component.internal.StackingComponentEventManager) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment)

Example 9 with ServletEnvironment

use of org.xwiki.environment.internal.ServletEnvironment in project xwiki-platform by xwiki.

the class ExtendedURLURLNormalizerTest method normalizeWhenNoConfigurationPropertyButEnvironment.

@Test
public void normalizeWhenNoConfigurationPropertyButEnvironment() throws Exception {
    ServletContext sc = mock(ServletContext.class);
    ServletEnvironment environment = mock(ServletEnvironment.class);
    this.mocker.registerComponent(Environment.class, environment);
    when(environment.getServletContext()).thenReturn(sc);
    when(sc.getContextPath()).thenReturn("/xwiki");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
    assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
}
Also used : ServletContext(javax.servlet.ServletContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Example 10 with ServletEnvironment

use of org.xwiki.environment.internal.ServletEnvironment in project xwiki-platform by xwiki.

the class ExtendedURLURLNormalizerTest method normalizeWhenNoConfigurationPropertyButEnvironmentWithRootContext.

@Test
public void normalizeWhenNoConfigurationPropertyButEnvironmentWithRootContext() throws Exception {
    ServletContext sc = mock(ServletContext.class);
    ServletEnvironment environment = mock(ServletEnvironment.class);
    this.mocker.registerComponent(Environment.class, environment);
    when(environment.getServletContext()).thenReturn(sc);
    when(sc.getContextPath()).thenReturn("");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"));
    assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize());
}
Also used : ServletContext(javax.servlet.ServletContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Aggregations

ServletEnvironment (org.xwiki.environment.internal.ServletEnvironment)11 ServletContext (javax.servlet.ServletContext)8 Environment (org.xwiki.environment.Environment)5 File (java.io.File)4 Test (org.junit.Test)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)3 ComponentManager (org.xwiki.component.manager.ComponentManager)3 Execution (org.xwiki.context.Execution)3 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)2 Date (java.util.Date)2 Expectations (org.jmock.Expectations)2 Before (org.junit.Before)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 ExtendedURL (org.xwiki.url.ExtendedURL)2 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiCfgConfigurationSource (com.xpn.xwiki.internal.XWikiCfgConfigurationSource)1