Tuesday, September 29, 2015

How to use WLST as a Jython 2.7 module

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. 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 =:-)  

No comments:

Post a Comment