[opencms-dev] Using OpenCms objects in external Java application?

Andreas Haumer andreas at xss.co.at
Fri Oct 6 18:21:32 CEST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Jonathan Woods schrieb:
> Andreas -
> 
> Yes, it's all possible.  You can access multiple instances of OpenCms in
> this way by ensuring that for each one you specify the location of the
> instance's WEB-INF folder.  The fact that you weren't doing that for your
> single instance may explain the NullPointerException - not sure.
> 

The NullPointerException is due to the fact that OpenCmsCore.m_securityManager
(a private variable in OpenCmsCore) does not get initialized properly when
calling OpenCms.initCmsObject()

IMHO this is a bug. OpenCms.initCmsObject() seems to be the only way for
an external application to get a CmsObject but it doesn't work as documented.

[...]
> 
> Take a look at the constructor CmsShell(String webInfPath, String
> servletMapping, String defaultWebAppName, String prompt, I_CmsShellCommands
> additionalShellCommands).  Forgetting about all the message bundle stuff,
> you should be able to crib from this code in your own Java class, starting
> with your own values for webInfPath, servletMapping and defaultWebAppName.
> 
I have looked into this and I see a lot of problems.

Before calling OpenCms.initCmsObject() I have to manually
initialize the OpenCmsCore object due to the problem
mentioned above (NullPointerException). In fact, the
CmsShell() constructor does this, too.

This is the basic sequence of commands as used in CmsShell():

// first initialize runlevel 1
OpenCmsCore m_opencms = OpenCmsCore.getInstance();

// set the path to the WEB-INF folder
m_opencms.getSystemInfo().init(webInfPath, servletMapping, null, defaultWebAppName);

// read the configuration properties
String propertyPath = m_opencms.getSystemInfo().getConfigurationFileRfsPath();
ExtendedProperties configuration = CmsPropertyUtils.loadProperties(propertyPath);

// upgrade to runlevel 2 and initialize all those "Manager" objects
m_opencms = m_opencms.upgradeRunlevel(configuration);

// create a context object with 'Guest' permissions
// this oject is the one we finally can work with!
CmsObject m_cms = m_opencms.initCmsObject(m_opencms.getDefaultUsers().getUserGuest());


So far, so good and it even looks quite straighforward.
But all methods I'd need to initialized the objects are
protected and so I can't use them directly in my code.
I can't subclass OpenCmsCore or OpenCms as they are final.

The only clean way I currently see is to subclass CmsShell, call
the base-class constructor and provide a public getter method
for the m_cms attribute like this:

public class MyCmsShell extends CmsShell
{
  public MyCmsShell (String webInfPath, String servletMapping, String webAppName)
  {
    super (webInfPath, servletMapping, webAppName, "", null);
  }

  public CmsObject getOpenCmsObject()
  {
    return m_cms;
  }
}

In my application I then could use it this way:

MyCmsShell shell = new MyCmsShell (webInfPath, null, null);
CmsObject cms = shell.getOpenCmsObject();
cms.loginUser(username, password);
...

A short tests suggests that this might work but it is still ugly,
as that way a lot of shell-specific functionality gets activated
in the base-class constructor which I never need (CmsShell
does not have a simple default constructor, you can only use
the full-blown constructor)

Has anyone actually successfully used OpenCms.initCmsObject() ?
Either I have missed some important functionality of the API
or it just doesn't work as documented.

Does anyone have a better way to use an OpenCms object
in a standalone Java application?

- - andreas

- --
Andreas Haumer                     | mailto:andreas at xss.co.at
*x Software + Systeme              | http://www.xss.co.at/
Karmarschgasse 51/2/20             | Tel: +43-1-6060114-0
A-1100 Vienna, Austria             | Fax: +43-1-6060114-71
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFJoKKxJmyeGcXPhERAjDbAJ9kQl2zB0XDike92f5zAswCsoFEcACffi7f
77Ev/8z36xKoCGS7O30AKgI=
=n++R
-----END PGP SIGNATURE-----



More information about the opencms-dev mailing list