Search in sources :

Example 6 with XWikiStubContextProvider

use of com.xpn.xwiki.util.XWikiStubContextProvider in project xwiki-platform by xwiki.

the class XWikiStubContextInitializerTest method testWithAndWithoutXWikiContext.

public void testWithAndWithoutXWikiContext() throws Exception {
    XWikiContext xcontext = new XWikiContext();
    xcontext.put("key", "value");
    this.mockXWiki = mock(XWiki.class);
    this.mockXWiki.stubs().method("prepareResources");
    xcontext.setWiki((XWiki) this.mockXWiki.proxy());
    ExecutionContext context = new ExecutionContext();
    xcontext.declareInExecutionContext(context);
    XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
    stubContextProvider.initialize(xcontext);
    final ExecutionContext daemonContext = new ExecutionContext();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                executionContextManager.initialize(daemonContext);
            } catch (ExecutionContextException e) {
                fail("Failed to initialize execution context: " + e.getStackTrace());
            }
        }
    });
    thread.start();
    thread.join();
    XWikiContext daemonXcontext = (XWikiContext) daemonContext.getProperty("xwikicontext");
    assertNotNull(daemonXcontext);
    assertNotSame(xcontext, daemonXcontext);
    assertEquals("value", daemonXcontext.get("key"));
    assertNotNull(daemonXcontext.getWiki());
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 7 with XWikiStubContextProvider

use of com.xpn.xwiki.util.XWikiStubContextProvider 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 XWikiStubContextProvider

use of com.xpn.xwiki.util.XWikiStubContextProvider in project xwiki-platform by xwiki.

the class XWiki method getMainXWiki.

/**
 * @param wait true if the method should way for {@link XWiki} instance to be initialized
 * @param context see {@link XWikiContext}
 */
public static XWiki getMainXWiki(boolean wait, XWikiContext context) throws XWikiException {
    String xwikiname = DEFAULT_MAIN_WIKI;
    context.setMainXWiki(xwikiname);
    XWiki xwiki;
    try {
        XWikiEngineContext econtext = context.getEngineContext();
        xwiki = (XWiki) econtext.getAttribute(xwikiname);
        if (xwiki == null) {
            // Start XWiki initialization
            synchronized (XWiki.class) {
                xwiki = (XWiki) econtext.getAttribute(xwikiname);
                if (xwiki == null && job == null) {
                    job = Utils.getComponent((Type) Job.class, XWikiInitializerJob.JOBTYPE);
                    if (job.getStatus() == null) {
                        // "Pre-initialize" XWikiStubContextProvider so that XWiki initializer can find one
                        Utils.<XWikiStubContextProvider>getComponent(XWikiStubContextProvider.class).initialize(context);
                        job.startAsync();
                    }
                }
            }
            // Wait until XWiki is initialized
            if (wait) {
                job.join();
                xwiki = (XWiki) econtext.getAttribute(xwikiname);
            }
        }
        context.setWiki(xwiki);
        return xwiki;
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_INIT_FAILED, "Could not initialize main XWiki instance", e);
    }
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) EntityType(org.xwiki.model.EntityType) ResourceType(org.xwiki.resource.ResourceType) Type(java.lang.reflect.Type) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiEngineContext(com.xpn.xwiki.web.XWikiEngineContext) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException)

Aggregations

XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)8 XWikiContext (com.xpn.xwiki.XWikiContext)5 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)3 XWiki (com.xpn.xwiki.XWiki)3 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 File (java.io.File)3 Before (org.junit.Before)3 ComponentManager (org.xwiki.component.manager.ComponentManager)3 Execution (org.xwiki.context.Execution)3 ServletEnvironment (org.xwiki.environment.internal.ServletEnvironment)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)2 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)2 Date (java.util.Date)2 ServletContext (javax.servlet.ServletContext)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1