Scripting Oracle UCM With Jython

Sometimes when I'm working on a big-ish project, I need to quickly whip out a script to alter items in the content server. The old-school way to do this would be to use the IdcCommand application... other folks might prefer a Java application written with the J2EE connectors in the Content Integration Suite (CIS), or maybe even SOAP... but my preference would be to do it all in a scripting language. In particular, Jython.

Jython is a Java implementation of the Python programming language... which is my favorite language these days. Jython did stagnate for may years, stuck on Python 2.2, and more than a little buggy... but the project is alive and kicking and just released version Jython 2.5 beta 3, which I recommend you use. I'd wager that the Jython project was revived partly because of envy about the rise of Ruby and JRuby. Whatever the reason, I'm always happy to have new code to play with.

You can invoke any Java libraries in Jython, so naturally you could use SOAP or CIS to make administrative scripts. However, I think the majority of people would prefer a new-ish Java connector for Oracle UCM: the Remote IntraDoc Client (RIDC). In contrast with both CIS and SOAP, the RIDC connector is very lightweight, very fast, and very simple to use. There's no WSDL or J2EE bloat at all; RIDC is just a "Plain Old Java Object" wrapper around UCM web services... so it's very easy to embed in a Java application.

To get started, download the most recent version of the Content Integration Suite from Oracle. This ZIP file contains two folders: one for the new RIDC connector, and one for the standard CIS connector. I'd suggest you take a look at the "ridc-developer-guide.pdf" before you go any further. The samples and JavaDocs are also very useful, but you can peruse them later.

Next, download Jython 2.5b3, and run the installer.

Next, make a folder to contain your UCM Jython scripts. Copy the "jython" launcher file from its install directory to this directory. On Windows, this file is named "jython.bat". Also copy the RIDC library "oracle-ridc-client-10g.jar" to this folder.

Next, edit your copy of the Jython launcher file to make sure the Java classpath includes the RIDC library. You can set this near where they set JAVA_HOME at the top. On Windows, you would edit "jython.bat" and add this:

        set CLASSPATH=%CLASSPATH%;D:\FOOBAR\oracle-ridc-client-10g.jar

On Unix, your would edit the "jython" text file, and add something like this:

        CLASSPATH=$CLASSPATH:/FOOBAR/oracle-ridc-client-10g.jar

That's it! Now just run "jython" on the command line, and you'll get an interactive shell where you can load Java classes, and use them. Loading them is fairly similar to how you load libraries in Python. For example, the script below will load the RIDC libraries, connect to the content server, run a search, and dump out the results:

# import the Oracle UCM libraries into Python from oracle.stellent.ridc import IdcClientManager from oracle.stellent.ridc import IdcContext # create the manager and client objects manager = IdcClientManager() client = manager.createClient("idc://localhost:4444") userContext = IdcContext("sysadmin", "idc") # prepare to run a search binder = client.createBinder() binder.putLocal("IdcService", "GET_SEARCH_RESULTS") binder.putLocal("QueryText", "") binder.putLocal("ResultCount", "20") # get the response response = client.sendRequest(userContext, binder) responseBinder = response.getResponseAsBinder() # dump out the response data localData = responseBinder.getLocalData() print("LocalData:") for key in localData: print(key + " = " + localData.get(key)) for name in responseBinder.getResultSetNames(): print("\nResult Set '" + str(name) + "':") rset = responseBinder.getResultSet(name) fields = rset.getFields() rows = rset.getRows() for rowItem in rows: for fieldItem in fields: print ("\t" + str(fieldItem.getName()) + " = " + str(rowItem.get(fieldItem.getName()))) print("\t----------------")

Remember: whitespace is relevant in Python, so watch your indentations...

You can easily expand on this to create scripts to run archives, batch update metadata fields, resubmit items to the indexer, or run them through a converter to generate PDFs or HTML. Also, there are multiple ways you can set up the security if you don't want to send the password with every request, or if you want to use SSL instead of clear-text sockets. See the RIDC documentation for examples.

Enjoy!

Smoking!

Thanks!

Technical + Useful + Examples = Winning Post

You do not need to modify the jython/jython.bat file.

You do not need to change the jython/jython.bat file to refer to your project specific classpath.

Simply set the CLASSPATH variable before running jython, and jython will automatically recognise the contents of the CLASSPATH variable, e.g.

C:\>set CLASSPATH=D:\FOOBAR\oracle-ridc-client-10g.jar
C:\>jython
*sys-package-mgr*: processing new jar, 'D:\FOOBAR\oracle-ridc-client-10g.jar'
Jython 2.5b3 (Release_2_5beta3:6092, Mar 10 2009, 15:34:57)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.5.0_17
Type "help", "copyright", "credits" or "license" for more information.
>>>

As for the reason why the jython project has come back to life, it's because people have put effort into it, because it's a cool language. Envy has nothing to do with it.

In fact, the opposite is true: the jython project collaborates extensively with the jruby project, and the two projects share a lot of code.

Regards,

Alan.

Thanks, Alan!

You do not need to change the jython/jython.bat

When folks run lots of Java applications, they would probably prefer to not set a global "classpath" variable... that can lead to library version conflicts. I'm partial to portable executables that are scoped with a small number of libraries added. But yes, you can do it both ways.

As for the reason why the Jython project has come back to life, it's because people have put effort into it, because it's a cool language. Envy has nothing to do with it. In fact, the opposite is true: the jython project collaborates extensively with the jruby project, and the two projects share a lot of code.

Did you guys get a corporate sponsor yet? I know Google has some "Summer of Code" projects, but any steady cash?

Classpath

"Simply set the CLASSPATH variable before running jython, and jython will automatically recognise the contents of the CLASSPATH variable, e.g."

That is asking for trouble if you do much Java work at all. I agree with the author, keep environments clean and task specific.

With that aside, thanks for writing this article. I'm a Java programmer and recently found Jython in our build. I knew what it was, but that was about it. I've never used Python, let alone Jython, so I did a little investigation.

What I found was a nice simplistic scripting language with all of the capabilities I need. What I did not find was the answer to my very first question when I found it in our build, that being "What is it good for?". That question led me to this article and I now have a real world example of it's usefulness.

I'm still not sure if I'll use it much, but it's certainly worth keeping in my wizard bag.

Any chance for an update?

It's been about 3 years since the last post and since this article was published. Was Jython ever pursued beyond the simple script shown here? I wonder because I am now learning to use Jython with WCC/UCM and there is a remarkable lack of examples on how to do even simple things, like GET_FILE, using Jython and WCC on the internet. Was that a limitation that led to it not reaching its potential, I wonder?

Recent comments