Creating Lucee PageContext from Java class

Does anyone have any example of creating a Lucee PageContext object from a Java class running inside the JVM, but not under a Lucee thread?

I’m trying to get Quartz Scheduler working and need to get a PageContext object, preferably to a specific app, but I could probably work with just a server context.

Thanks!

If you don’t really care what’s in the pc, you can just ask Lucee to get the current thread’s context and it will create an empty one for you. I always had no end of trouble creating my own contexts even though Micha always said it would work.

PageContext pc = CFMLEngineFactory.getInstance().getThreadPageContext();

Now, if you need to actually run CFML code inside a thread and you need it to have

  • application settings
  • CF Mappings
  • datasources from Application.cfc
  • Any sort of form/url/request scopes still populated from an original HTTP request
  • etc

then you’ll probably need to clone a “real” pc from that app and re-use it. Though what you really probably need is what’s in this ticket-- which I’ve been asking for, for years.

https://luceeserver.atlassian.net/browse/LDEV-3960

That’s what I had been trying to do, but it returns a null PageContext.

That ticket is certainly what I’m looking for. I did start browsing through the CLI classes, but it looked like there was nothing I could use verbatim, so I was hoping someone just had something already.

It would definitely be nice to have a PageContextBuilder that could be used to get a PageContext back.

Ahh dang, looking at the code, that makes sense. Perhaps try this method in the CFMLEngine

public PageContext createPageContext(File contextRoot, String host, String scriptName, String queryString, Cookie[] cookies, Map<String, Object> headers,
		Map<String, String> parameters, Map<String, Object> attributes, OutputStream os, long timeout, boolean register)

I tried that from the CreationImpl (I think that’s the class name, it’s definitely an instance of a Creation implementation ) and was having the same problem.

I’m wonder if something broke in recent builds, because I tested my code at one time and it worked, but perhaps something was screwed up in my testing. The code does work fine as long as it’s running within the context of a web request, but just breaks when in a background thread.

So I ultimately ended up following the basic pattern used here to access my application from threads running outside of Lucee, but in the same JVM:

I simplified things a bit. I just have two objects:

  • An java object which takes a PageContext object, then extracts the following and stores the information in a property:
    • The servlet config
    • The servlet context
    • The web config
    • The application context
    • The root directory
  • This object can then create a new page context based on the original page context.
  • A singleton which stores a reference to the above object via a map. I can then look up the page context to use from the singleton.

This seems to be working as expected.

1 Like