How to use WLST as a Jython 2.7 module

11
Share this on .. Tweet about this on TwitterShare on LinkedIn28Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

WebLogic Scripting Tool (WLST) in WebLogic Server 12.1.3 uses Jython version 2.2.1 (based on Python 2.2.1). This can be an important limitation when using WLST. Many modules are not available for 2.2.1 or are difficult to install. See here for an example. WLST however can be used as a module in Jython 2.7. This allows you to use all kinds of nice Jython 2.7 goodness while still having all the great WLST functionality available.

To just name some nice Jython 2.7 features:

  • pip and easy_install can be used to easily add new modules
  • useful new API’s are available such as xml.etree.ElementTree to allow XML processing, the multiprocessing module to use multiple threads and the argparse module to make parsing of script arguments easy.

In this article I’ll describe how you can use WLST as a Jython 2.7 module in order to allow you to combine the best of both worlds in your scripts.

Ready Jython

First you need to install Jython. You can obtain Jython from: http://www.jython.org/.

Obtain the classpath

In order for WLST as a module to function correctly, it needs its dependencies. Those dependencies are generated by several scripts such as:

  • <WLS_HOME>/wlserver/server/bin/setWLSEnv.sh
  • <WLS_HOME>/oracle_common/common/bin/wlst.sh
  • <WLS_HOME>/osb/tools/configjar/wlst.sh
  • <WLS_HOME>/soa/common/bin/wlst.sh
  • <WLS_HOME>/wlserver/common/bin/wlst.sh

It can be a challenge to abstract the logic used to obtain a complete classpath from those scripts. Why make it difficult for yourself? Just ask WLST:

<WLS_HOME>/soa/common/bin/wlst.sh

This will tell you the classpath for WLST (which in this case includes the SOA Suite WLST commands). Even though this is usually a long list, this is not enough! You also need wlfullclient.jar (see here on how to create it). Also apparently there are some JAR’s which are used but not in the default WLST classpath such as several <WLS_HOME>/oracle_common/modules/com.oracle.cie.* files. Just add <WLS_HOME>/oracle_common/modules/* to the classpath to fix issues like:

java.lang.RuntimeException: java.lang.RuntimeException: Could not find the OffLine WLST class
weblogic.management.scripting.utils.WLSTUtil.getOfflineWLSTScriptPathInternal

You can remove overlapping classpath entries. Since <WLS_HOME>/oracle_common/modules/* is in the classpath, you don’t need to mention individual modules anymore.

Obtain the module path

Jython needs a module path in order to find the modules used by WLST which are hidden in several JAR files. Again, simply ask WLST for it. Start

<WLS_HOME>/soa/common/bin/wlst.sh

And issue the command:

print sys.path

It will give you something like

['.', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/modules/features/weblogic.server.merged.jar/Lib', '__classpath__', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/server/lib/weblogic.jar', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/common/wlst/modules/jython-modules.jar/Lib', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/common/wlst', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/common/wlst/lib', '/home/maarten/Oracle/Middleware1213/Oracle_Home/wlserver/common/wlst/modules', '/home/maarten/Oracle/Middleware1213/Oracle_Home/oracle_common/common/wlst', '/home/maarten/Oracle/Middleware1213/Oracle_Home/oracle_common/common/wlst/lib', '/home/maarten/Oracle/Middleware1213/Oracle_Home/oracle_common/common/wlst/modules', '/home/maarten/Oracle/Middleware1213/Oracle_Home/oracle_common/common/script_handlers', '/home/maarten/Oracle/Middleware1213/Oracle_Home/soa/common/script_handlers', '/home/maarten/Oracle/Middleware1213/Oracle_Home/soa/common/wlst', '/home/maarten/Oracle/Middleware1213/Oracle_Home/soa/common/wlst/lib', '/home/maarten/Oracle/Middleware1213/Oracle_Home/soa/common/wlst/modules']

Interesting to see where Oracle has hidden all those modules. You can add them to the Jython module path by setting the PYTHONPATH variable.

Create a Jython start script

The easiest way to make sure your classpath and Python module path are set prior to executing a script is to create a Jython start script (similar to wlst.sh). My start script looked like:

startjython.sh

export WL_HOME=/home/maarten/Oracle/Middleware1213/Oracle_Home

export CLASSPATH=$WL_HOME/oracle_common/soa/modules/oracle.soa.mgmt_11.1.1/soa-infra-mgmt.jar:$WL_HOME/oracle_common/soa/modules/commons-cli-1.1.jar:$WL_HOME/soa/soa/modules/oracle.soa.mgmt_11.1.1/soa-infra-mgmt.jar:$WL_HOME/soa/soa/modules/commons-cli-1.1.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/fabric-runtime.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/soa-infra-tools.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/tracking-core.jar:$WL_HOME/soa/soa/modules/oracle.soa.workflow_11.1.1/bpm-services.jar:$WL_HOME/soa/soa/modules/chemistry-opencmis-client/chemistry-opencmis-client.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/testfwk-xbeans.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/oracle-soa-client-api.jar:$WL_HOME/soa/soa/modules/oracle.bpm.alm.script-legacy.jar:$WL_HOME/soa/soa/modules/oracle.bpm.bac.script.jar:$WL_HOME/oracle_common/modules/com.oracle.webservices.fmw.wsclient-rt-impl_12.1.3.jar:$WL_HOME/oracle_common/modules/com.oracle.classloader.pcl_12.1.3.jar:$WL_HOME/oracle_common/modules/org.apache.commons.logging_1.0.4.jar:$WL_HOME/oracle_common/modules/org.apache.commons.beanutils_1.6.jar:$WL_HOME/oracle_common/modules/oracle.ucp_12.1.0.jar:$WL_HOME/soa/soa/modules/oracle.rules_11.1.1/rulesdk2.jar:$WL_HOME/soa/soa/modules/oracle.rules_11.1.1/rl.jar:$WL_HOME/oracle_common/modules/oracle.adf.model_12.1.3/adfm.jar:$WL_HOME/oracle_common/modules/oracle.jdbc_12.1.0/ojdbc6dms.jar:$WL_HOME/oracle_common/modules/oracle.xdk_12.1.3/xmlparserv2.jar:$WL_HOME/oracle_common/modules/*:$WL_HOME/jdeveloper/wlserver/lib/wlfullclient.jar:$WL_HOME/oracle_common/soa/modules/oracle.soa.mgmt_11.1.1/soa-infra-mgmt.jar:$WL_HOME/oracle_common/soa/modules/commons-cli-1.1.jar:$WL_HOME/soa/soa/modules/oracle.soa.mgmt_11.1.1/soa-infra-mgmt.jar:$WL_HOME/soa/soa/modules/commons-cli-1.1.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/fabric-runtime.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/soa-infra-tools.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/tracking-core.jar:$WL_HOME/soa/soa/modules/oracle.soa.workflow_11.1.1/bpm-services.jar:$WL_HOME/soa/soa/modules/chemistry-opencmis-client/chemistry-opencmis-client.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/testfwk-xbeans.jar:$WL_HOME/soa/soa/modules/oracle.soa.fabric_11.1.1/oracle-soa-client-api.jar:$WL_HOME/soa/soa/modules/oracle.bpm.alm.script-legacy.jar:$WL_HOME/soa/soa/modules/oracle.bpm.bac.script.jar:$WL_HOME/oracle_common/modules/com.oracle.webservices.fmw.wsclient-rt-impl_12.1.3.jar:$WL_HOME/oracle_common/modules/com.oracle.classloader.pcl_12.1.3.jar:$WL_HOME/oracle_common/modules/org.apache.commons.logging_1.0.4.jar:$WL_HOME/oracle_common/modules/org.apache.commons.beanutils_1.6.jar:$WL_HOME/oracle_common/modules/oracle.ucp_12.1.0.jar:$WL_HOME/soa/soa/modules/oracle.rules_11.1.1/rulesdk2.jar:$WL_HOME/soa/soa/modules/oracle.rules_11.1.1/rl.jar:$WL_HOME/oracle_common/modules/oracle.adf.model_12.1.3/adfm.jar:$WL_HOME/oracle_common/modules/oracle.jdbc_12.1.0/ojdbc6dms.jar:$WL_HOME/oracle_common/modules/oracle.xdk_12.1.3/xmlparserv2.jar

export PYTHONPATH=.:$WL_HOME/wlserver/modules/features/weblogic.server.merged.jar/Lib:$WL_HOME/wlserver/server/lib/weblogic.jar:$WL_HOME/wlserver/common/wlst/modules/jython-modules.jar/Lib:$WL_HOME/wlserver/common/wlst:$WL_HOME/wlserver/common/wlst/lib:$WL_HOME/wlserver/common/wlst/modules:$WL_HOME/oracle_common/common/wlst:$WL_HOME/oracle_common/common/wlst/lib:$WL_HOME/oracle_common/common/wlst/modules:$WL_HOME/oracle_common/common/script_handlers:$WL_HOME/soa/common/script_handlers:$WL_HOME/soa/common/wlst:$WL_HOME/soa/common/wlst/lib:$WL_HOME/soa/common/wlst/modules

/home/maarten/jython2.7.0/bin/jython "$@"
exit $?

You can of course see that the PYTHONPATH is created from some search and replace actions on the output of sys.path executed with WLST. I removed [,] and ‘. Next I replaced , by : and removed the extra spaces after the :. Also I replaced my WL_HOME with a variable just to make the script look nice and more reusable. For a Windows script, the search and replace commands are slightly different such as ; as path separator and set instead of export.

You can use the start script in the same way as the wlst start script. You only have to mind that using WLST as a module requires some minor changes to WLST scripts. See below.

Ready the WLST module

In order to use WLST as a module in Jython 2.7 you need to generate a wl.py file. This is described here. Actually starting wlst.sh and executing: writeIniFile(“wl.py”) is enough.

When using the module though, the following exception is raised:

Traceback (most recent call last):
File "sample.py", line 8, in <module>;
import wl
File "/home/maarten/tmp/wl.py", line 13, in <module>;
origPrompt = sys.ps1
AttributeError: 'reflected field public org.python.core.PyObject o' object has no attribute 'ps1'

WLST apparently has some shell specific prompt handling code. Easy to get rid of this exception though by replacing the following line in wl.py

origPrompt = sys.ps1

With

origPrompt = ">>>"

This origPrompt looks pretty much like my default prompt and I didn’t encounter any errors after setting it like this.

Seeing it work

My directory contains the following script: wl.py. Generated as explained above with origPrompt replaced.

Next my listserver.py script:

import wl

wl.connect("weblogic","Welcome01", "t3://localhost:7101")
mbServers= wl.getMBean("Servers")
servers= mbServers.getServers()
print( "Array of servers: " )
print( servers )
for server in servers :
    print( "Server Name: " + server.getName() )
    print( "Done." )

Because I’m using the WebLogic module, you need to do wl.connect instead of connect and similar for other calls from the wl module. Otherwise you will get exceptions like:

Traceback (most recent call last):
File "listserver.py", line 9, in <module>
connect("weblogic","Welcome01", "t3://localhost:7101")
NameError: name 'connect' is not defined

The output when using my startjython.sh script as explained above:

startjython.sh listserver.py

Connecting to t3://localhost:7101 with userid weblogic ...
Successfully connected to Admin Server "DefaultServer" that belongs to domain "DefaultDomain".

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Array of servers:
array(weblogic.management.configuration.ServerMBean, [[MBeanServerInvocationHandler]com.bea:Name=DefaultServer,Type=Server])
Server Name: DefaultServer

Done.

Installing the logging module becomes

jython2.7.0/bin/pip install logging
Downloading/unpacking logging
Downloading logging-0.4.9.6.tar.gz (96kB): 96kB downloaded
Running setup.py (path:/tmp/pip_build_maarten/logging/setup.py) egg_info for package logging

Installing collected packages: logging
Running setup.py install for logging

Successfully installed logging
Cleaning up...

And of course using the logger also works.

import logging
logging.basicConfig()
log = logging.getLogger("MyFirstLogger")
log.setLevel(logging.DEBUG)
log.info("That does work =:-)")

Output:

INFO:MyFirstLogger:That does work =:-)
Share this on .. Tweet about this on TwitterShare on LinkedIn28Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

About Author

Maarten is a Senior Oracle Integration Consultant with focus on Oracle Fusion Middleware, Java and Continuous Integration / Continuous Delivery. In 2015 he was nominated ACE Associate. Over the past 10 years he has worked for numerous customers in the Netherlands where he has implemented integrations and streamlined software delivery processes. Maarten is passionate about his job and likes to share his knowledge through publications, frequent blogging and presentations.

11 Comments

  1. Oleksiy Lesyshyn on

    I have this issue:

    C:\proj>jython wl.py
    Traceback (most recent call last):
    File “wl.py”, line 16, in
    theInterpreter = WLSTUtil.ensureInterpreter();
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:92)
    at weblogic.management.scripting.utils.WLSTUtil.ensureInterpreter(WLSTUtil.java:257)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

    java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: weblogic/kernel/Kernel

    my classpath begins with this:

    set CLASSPATH= %WL_HOME%/wlservre/server/lib/wlfullclient.jar; %WL_HOME%/wlservre/server/lib/cryptoj.jar; %WL_HOME%/oracle_common/modules/*; etc etc etc etc……

    Any idea of what it can be?

  2. Hi Marteen

    I was able to get weblogic running as a Jython Module (with the help of David’s hack)
    Thank you for this great post!

    However, when I try to run wl.displayMetricTables(‘Oracle_BI*’,’dms_cProcessInfo’)
    I get
    Attribute error: ‘module’ object has no attribute ‘displayMetricTables’

    If I run this directly through wlst, it works

    Any pointers?

    Thanks
    P

  3. This post is useful but I face to same problem as David:

    theInterpreter = WLSTUtil.ensureInterpreter();
    at weblogic.management.scripting.utils.WLSTInterpreter.addModulesFromManifest(WLSTInterpreter.java:472)
    at weblogic.management.scripting.utils.WLSTInterpreter.addModulesFromManifest(WLSTInterpreter.java:465)
    at weblogic.management.scripting.utils.WLSTInterpreter.addWLSModules(WLSTInterpreter.java:431)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:219)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:121)
    at weblogic.management.scripting.utils.WLSTUtil.ensureInterpreter(WLSTUtil.java:198)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

    java.lang.NoSuchFieldError: java.lang.NoSuchFieldError: packageManager

    • Hi

      Since I cannot reproduce the issue, it is difficult to help you solve it. I do notice the JDK used in David’s message is an OpenJDK 8 version. You should try to use an Oracle JDK 7 version. Hope this helps. If not, please let me know.

      With kind regards,
      Maarten

    • Solved. wlfullclient.jar (and its dependency cryptoj.jar) needs to be set in the front of the classpath otherwise Jython uses weblogic.jar which causes this issue.

      Hope this helps.

      Bests
      Denis Maggiorotto

  4. Hello Maarten,

    This is a very useful post.

    I’m trying to follow these steps to use jython 2.7 on WebLogic 12c (not a SOA installation).

    Some minor fixes:

    It’s not clear how you obtain the classpath for the WSLT.
    I’ve used:

    System.getProperty("java.class.path")

    from within WLST to do so.

    For the prompt modification I included a space ( ‘>>> ‘) so the look and feel is the same as the original python/jython prompt

    origPrompt = ">>> "

    A problem I found when trying to import wl.py:
    ,code>
    Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
    [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.8.0_45
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import wl
    Hello there.
    This is indented.
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/orafmw/jython/Lib/wl.py”, line 15, in
    theInterpreter = WLSTUtil.ensureInterpreter();
    at weblogic.management.scripting.utils.WLSTInterpreter.addModulesFromManifest(WLSTInterpreter.java:472)
    at weblogic.management.scripting.utils.WLSTInterpreter.addModulesFromManifest(WLSTInterpreter.java:465)
    at weblogic.management.scripting.utils.WLSTInterpreter.addWLSModules(WLSTInterpreter.java:431)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:219)
    at weblogic.management.scripting.utils.WLSTInterpreter.(WLSTInterpreter.java:121)
    at weblogic.management.scripting.utils.WLSTUtil.ensureInterpreter(WLSTUtil.java:198)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

    java.lang.NoSuchFieldError: java.lang.NoSuchFieldError: packageManager

    The NoSuchFieldError possibly points to either overlapping classes at runtime or classes compiled with different libraries (I have manually removed any .class from the jython directory, but that didn’t help).

    As a workaround, I’m ignoring the error and obtaining a new interpreter object in wl.py:

    try:
    theInterpreter = WLSTUtil.ensureInterpreter()
    except:
    pass

    theInterpreter = WLSTUtil.ensureInterpreter()

    This is obviously an ugly hack, but I haven’t succeedded at finding the root cause.
    Any hints?

    Thanks!

Leave a Reply