WLST Obi White Paper v1
WLST Obi White Paper v1
WLST Obi White Paper v1
Introduction 1
Interactive mode 3
cmo variable 5
print 6
for loop 7
if then statement 7
Exception handling 7
Recursive programming 7
Using libraries 9
Style Guide 10
Conclusion 11
“WLST is based on Jython programming language with WebLogic WLST libraries. The execution can happen in the
following three modes: scripting, interactive, and embedded.”
“OEPE provides tooling for WLST that enable editing, executing, debugging, WebLogic MBean access and
navigation, as well as a built-in help for WLST commands.”
This white paper will guide you setting up a development environment, get you started with basic script
writing, and point you to useful documentation. We will focus on the interactive and script mode, for
embedded mode, please refer to the documentation for examples about instantiating the WLST
interpreter in your Java code and using it to run WLST commands and scripts
» Assumption: you have a Weblogic server running either locally or on a remote server, know the url
and have administrative access to that server.
Download the appropriate version for your OS in 'Oracle Enterprise Pack for Eclipse'.
There is an easy way and a slightly more difficult one. The easy way is to use a bundle installer, but not all versions
have one. The bundle installer will get you started with everything you need including Weblogic server. The
installation is an easy Next>Next>...>Finish.
The other way is slightly more complex, you will need to install Weblogic yourself and some install steps are less
easy (step 3, which won’t be possible until you create a target runtime, see the eclipse doc here:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jst.j2ee.doc.user%2Ftopics%2Ftjtargetserver.html).
Tip: unless you have special needs and require a precise version, use a bundle. If there is none for the latest
version, you will find bundles on the ‘Other versions’ page here:
http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/oepe-archive-1716547.html
e.g. File>New>Project... > JavaEE>Utility Project > Next > [give the project a name] > Finish
Tip: If you installed without using a bundle, you will need to select a Weblogic server target runtime (or create a new)
using the ‘Runtime’ tab on the right. Only compatible runtimes will be listed, others will not appear or will be grayed
out if you check “show all runtimes”, so you need to make sure the version you select (drop down) for your WLST
support matches the available Weblogic Server version to be able to select a runtime.
You can manage runtimes here: Window > Preferences > Server > Runtime environments
This step will add to the project the WLST interpreter and a directory for your scripts.
Alternatively you can [right click on the wlst directory in your project] > New > Oracle Weblogic Script > [give it a
name, and choose a template]
Templates are optional but useful to get started, and can be selected using the browse button on the right of the
template text box in the “New Weblogic WLST script” window. Try them all.
5. change the connection details in the sample script to point to your weblogic server details:
e.g.
username = 'me'
password = 'mypassword'
url='t3://myserver.com:7001'
http://docs.oracle.com > Fusion Middleware > Weblogic server > view Library > Reference > WLST Command
Reference for WebLogic Server
http://docs.oracle.com > Fusion Middleware > Weblogic server > view Library > Books > Understanding the
WebLogic Scripting Tool
e.g. http://docs.oracle.com/middleware/1212/wls/WLSTG/index.html
http://www.jython.org
Remember that Jython is an implementation of Python, most of the Python doc applies.
7. run
(CTRL+F11) or Run > Run or click the run icon
Interactive mode
Interactive mode is an easy way to experiment and try WLST commands.
You can start interactive mode from your operating system as follows:
» Linux
cd <ORACLE_HOME>/oracle_common/common/bin
./wlst.sh
(replace <ORACLE_HOME> with the path you chose when installing Weblogic. You can also type locate
wlst.sh to find the command location)
» Windows:
Start menu > Oracle WebLogic > WebLogic Server > Tools > WebLogic Scripting Tool
You can also use the interactive mode from within your OEPE environment:
» select the Console tab, and click on the WLST drop down on the top right of the console window
» select a server, then select the WLST command
You can also record scripts from the Weblogic Administration Console. Detailed information about recording a
WLST script from the console can be found in “Oracle Fusion Middleware Oracle WebLogic Server Administration
Console Online Help”, e.g.
http://docs.oracle.com/middleware/1213/wls/WLACH/taskhelp/console/RecordWLSTScripts.html
You can also test your scripts directly from the Eclipse interface:
» (CTRL+F11) or Run > Run or click the run icon > select WLST run
The hierarchy nodes can be browsed through Eclipse interface in the server tab (click on the tab, then on a server
name, and start the server if needed using the green triangle on the top right of the server tab).
Command Output
ls() lists current path properties and objects. Read the full reference documentation for the ls() Information function,
e.g. http://docs.oracle.com/middleware/1213/wls/WLSTC/reference.htm#WLSTC391
‘If you are using WLST to change the configuration of a security MBean, you must access the edit hierarchy and
start an edit session. For example, if you change the value of the LockoutThreshold attribute in
UserLockoutManagerMBean, you must be in the edit hierarchy.
If you invoke security provider operations to add, modify, or remove data in a security provider data store, WLST
does not allow you to be in the edit hierarchy. Instead, invoke these commands from the serverConfig or
domainConfig hierarchy. For example, you cannot invoke the createUser operation in an AuthenticatorMBean
MBean from the edit hierarchy. WLST enforces this restriction to prevent the possibility of incompatible changes. For
example, an edit session could contain an unactivated change that removes a security feature and will invalidate
modifications to the provider's data.’
‘WLST first connects to a WebLogic Server instance at the root of the server's configuration MBeans, a single
hierarchy whose root is DomainMBean.’
The table below provides some WLST commands needed to change hierarchy root:
Command Effect
serverRuntime() Location changed to serverRuntime tree. This is a read-only tree with ServerRuntimeMBean as the root.
domainRuntime() Location changed to domainRuntime tree. This is a read-only tree with DomainRuntimeMBean as the root.
serverConfig() Location changed to serverConfig tree. This is a read-only tree with DomainMBean as the root.
Edit() Location changed to edit tree. This is a writable tree with DomainMBean as the root. To make changes you will
need to start an edit session via startEdit().
domainCustom() Navigates to the root of domain custom MBeans that are registered in the Domain Runtime MBeanServer. WLST
navigates, interrogates, and edits domain custom MBeans; however, domain custom MBeans cannot use the cmo
variable because a stub is not available.
The domainCustom command is available when WLST is connected to an Administration Server instance.
Sample use is documented in http://docs.oracle.com > Fusion Middleware > Weblogic server > view Library >
Books > Development > Developing Custom Management Utilities Using JMX for Oracle WebLogic Server
e.g. http://docs.oracle.com/middleware/1213/wls/JMXCU/understandwls.htm#JMXCU239
cmo variable
The cmo (Current Management Object) variable is provided by WLST, and points to the current mBean you
navigated to. You can use the set and get methods to interact with the mBean properties, see sample code below.
Other useful variables are provided. All variables created by WLST are listed in the reference documentation for
WLST, e.g. http://docs.oracle.com/middleware/1213/wls/WLSTC/reference.htm#i1003217
Sample commands:
When starting WLST, Offline is the default mode. Online is started when using the connect() command successfully,
e.g. connect (‘me’, 'mypassword', 't3://myserver.com:7001')
Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and
stops. The only delimiter is a colon (:) and the indentation of the code itself. Be very careful with your code
indentation.
Use # before comments. Within the Eclipse IDE, the <Ctrl><Shift>/ will comment out the selected text, or un-
comment it.
print
print <anything>
myVar = 'world'
print 'Hello', myVar
if then statement
This is a frequently used statement in any code. Remember the semicolons and be careful with the indentation.
if weather == 'fair':
print ‘go for a walk”
else:
print ‘stay home’
print ‘this will always print, whatever the weather value’
Exception handling
Jython provides exception handling techniques that will allow your scripts to handle errors gracefully. The Jython
documentation about exception handling is here:
http://www.jython.org/jythonbook/en/1.0/ExceptionHandlingDebug.html
Recursive programming
The following function is recursively listing the MBeans hierarchy and properties. It is initially called from main.
def ListBranchNodesAndProperties(path):
if __name__== "main":
print 'starting the script ....'
username = 'me'
password = 'mypassword'
url='t3://localhost:7001'
connect(username,password,url)
domainConfig()
cd('Log')
print pwd()
ListBranchNodesAndProperties('')
print 'Done!'
connect(username,password,url)
edit()
cd('Servers')
ServerBean = lookup('myserver')
oldValue = ServerBean.getStuckThreadMaxTime()
newValue = 800
print 'StuckThreadMaxTime',oldValue
startEdit()
Using libraries
Importing external libraries (often called modules in python) in full or in part is straightforward. Jython also allows
easy reuse of Java libraries.
http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html
Also do not miss to read this other chapter about Jython and Java integration:
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
» Importing the OS module which provides a portable way of using operating system dependent functionality:
Import os
Style Guide
If the python code you write is for distribution (most often the case than not), you will want to make sure your syntax
is clean and readable by others, and follows Python style guide, aka PEP8 http://legacy.python.org/dev/peps/pep-
0008/.
OEPE actually includes PyDev (http://pydev.org/), which among many other useful features will help you by
checking your code against PEP8 rules, and can also auto correct some of it:
» Activating PEP8 checking in Eclipse: Window > Preferences > PyDev > Editor > Code Analysis > PEP8.py >
Warning
Warning signs will appear in Eclipse on the left of your code lines with the precise description of the style
infringement. Eventually, use [right click anywhere in your code] > PyDev > Code Analysis.
» Window > Preferences > PyDev > Editor > Code Style > Code Formatter > [check ‘Auto-format editor contents
before saving?’]
This will tidy up some of it each time you save your code. If you want to go further and have all the style fixes done
for you, check out this link: http://stackoverflow.com/questions/11046001/is-there-any-way-to-fix-pep-8-issues-with-
pydev
This will allow using WLST directly from Jython. All WLST commands will be available from your Jython program.
See http://docs.oracle.com/middleware/1212/wls/WLSTG/using_wlst.htm#i1093409.
Trying to embrace the full capabilities of this association can be daunting, and mastering the finest Python intricacies
may seem overwhelming, but the language allows a very progressive learning curve, and once the hierarchical
structure exposed by WLST is understood and some basic Python programming mastered, you will be set for quick
wins. You are not alone, and the web is full of code snippets, tutorials, and other documentation to support you
during that journey.
CONNECT W ITH US
blogs.oracle.com/oracle Copyright © 2014, Oracle and/or its affiliates. All rights reserved. This document is provided for information purposes only, and the
contents hereof are subject to change without notice. This document is not warranted to be error-free, nor subject to any other
facebook.com/oracle warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or
fitness for a particular purpose. We specifically disclaim any liability with respect to this document, and no contractual obligations are
formed either directly or indirectly by this document. This document may not be reproduced or transmitted in any form or by any
twitter.com/oracle means, electronic or mechanical, for any purpose, without our prior written permission.
oracle.com Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and
are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are
trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group. 1014