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

Shi Yusen shiys at langhua.cn
Sat Oct 7 21:37:41 CEST 2006


Interesting. Is your OpenCms still safe after this modification?

Regards,

Shi Yusen/Beijing Langhua Ltd.

在 2006-10-07六的 20:27 +0200,Andreas Haumer写道:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi!
> 
> Today I have implemented a solution which might not be
> perfect but seems to be a good compromise in the given
> situation.
> 
> My reasoning for this implementation was:
> 
> a) The CmsShell class contains all necessary functionality
>    and is public so I can subclass it and implement a
>    method which gives me access to the CmsObject I need.
>    Alas, it's constructor does a lot more than I want.
>    It's a working, but still ugly solution.
> 
> b) The most important classes are OpenCmsCore and OpenCms
>    in package org.opencms.main. These classes contain all
>    the methods needed to get access to the OpenCms VFS.
>    But all the important methods are protected and the
>    classes are declared final, so there is no way for my
>    application to use the classes or methods directly.
> 
> 
> Looking at a and b I came to the conclusion the best way
> was to implement a new class inside package org.opencms.main
> and use the protected methods I need there. Luckily, Java
> let's me implement a class for a package in a different
> filesystem namespace and still have the class considered
> part of the package. That way I don't have to touch the
> OpenCms sources and can implement my class in our own
> project source directory structure.
> 
> So I have implemented class org.opencms.main.CmsObjectFactory
> to give me access to an correctly initialized CmsObject
> object. I have the sources for this class attached to this
> mail, just in case someone else might find it useful.
> 
> With this new class, I can implement a resource upload function
> in my application as follows:
> 
>     private static void upload (String webappName,
>                                 String servletMapping,
>                                 String webInfPath,
>                                 String username,
>                                 String password,
>                                 String siteRoot,
>                                 String filename,
>                                 byte[] data, List properties)
>     throws Exception
>     {
>         CmsObjectFactory factory = new CmsObjectFactory();
> 
>         try {
>             LOG.info("get OpenCMS instance");
>             CmsObject cms = factory.getCmsObject(webInfPath, servletMapping, webappName);
> 
>             LOG.info("login to OpenCMS at \"" + webInfPath + "\" with username \"" + username + "\"");
>             cms.loginUser(username, password);
> 
>             // switch to the offline project
>             LOG.info("switch to offline project");
>             cms.getRequestContext().setCurrentProject(cms.readProject(OFFLINE_PROJECT_STR));
> 
>             // set site root if requested
>             if (siteRoot != null) {
>                 LOG.info("set site root to\"" + siteRoot + "\"");
>                 cms.getRequestContext().setSiteRoot(siteRoot);
>             }
> 
>             String foldername = cms.getRequestContext().getFolderUri();
>             if (!foldername.endsWith(DIRECTORY_DELIMITER_STR))
>                 foldername += DIRECTORY_DELIMITER_STR;
> 
>             if (filename.startsWith(DIRECTORY_DELIMITER_STR)) {
>                 filename = filename.substring(DIRECTORY_DELIMITER_STR.length());
>             }
> 
>             String resourcename = foldername + filename;
> 
>             LOG.info("upload resource \"" + resourcename + "\" into site \"" + cms.getRequestContext().getSiteRoot() + "\"");
> 
>             // delete old resource if it already exists
>             if (cms.existsResource(resourcename)) {
>                 LOG.info("resource \"" + resourcename + "\" exists, deleting it");
>                 cms.lockResource(resourcename);
>                 cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
>             }
> 
>             LOG.info("creating resource \"" + resourcename + "\"");
>             cms.createResource(resourcename, DEFAULT_RESOURCE_TYPE, data, properties);
>             LOG.info("unlocking resource \"" + resourcename + "\"");
>             cms.unlockResource(resourcename);
>             LOG.info("publishing resource \"" + resourcename + "\"");
>             cms.publishResource(resourcename);
> 
>             // go back to online project
>             LOG.info("switch back to online project");
>             cms.getRequestContext().setCurrentProject(cms.readProject(ONLINE_PROJECT_STR));
>         }
>         catch (Exception ex) {
>             LOG.error("resource upload failed: " + ex.getLocalizedMessage());
>             throw(ex);
>         }
>         finally {
>             // finally, release all OpenCms resources
>             LOG.info("shutdown OpenCMS instance");
>             factory.destroyCmsObject();
>         }
>     }
> 
> 
> 
> I have tested this in my application and it works quite fine.
> There is still room for improvement, but at least for now I'm
> happy.
> 
> I would be interested in any comments, especially from the
> OpenCms core developers. I think the functionality implemented
> in the CmsObjectFactory class is missing from the OpenCms API.
> Would you consider adding this class (or at least the functionality)
> to the OpenCms core sources?
> 
> Regards,
> 
> - - 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
> 
> iD8DBQFFJ/GnxJmyeGcXPhERAoFlAKCbWekQbxYyc/wZ3bB2wQKHIXahNACfcgU5
> MYObT84qXqHW+k3O7A/SuPg=
> =fyF1
> -----END PGP SIGNATURE-----
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev




More information about the opencms-dev mailing list