Beruflich Dokumente
Kultur Dokumente
Table of Contents
1. Introduction................................................................................................................. 3
1.1.
Overview............................................................................................................. 3
2. Before You Begin ........................................................................................................ 4
3. Linux and UNIX Installations ................................................................................... 4
3.1.
Installing JLI on the Application Server............................................................. 4
3.1.1.
Installing Jython.......................................................................................... 4
3.1.2.
Installing JLI Files ...................................................................................... 5
3.2.
Using the JLI....................................................................................................... 5
4. Windows Installations ................................................................................................ 5
4.1.
Installing JLI on a Windows Application Server................................................ 5
4.1.1.
Installing Jython.......................................................................................... 5
4.1.2.
Installing JLI Files ...................................................................................... 6
4.2.
Using the JLI....................................................................................................... 6
5. Instantiating and Executing the JLI ......................................................................... 7
6. Basic Jython Programming Principles...................................................................... 7
6.1.
Importing Native/Custom Modules .................................................................... 7
6.1.1.
Import Search Path...................................................................................... 8
6.2.
Suites................................................................................................................... 9
6.3.
Documenting....................................................................................................... 9
7. Examining Common JLI or Jython Examples....................................................... 10
7.1.
Setting Server Properties................................................................................... 10
7.1.1.
update_server_status.jli............................................................................. 10
7.1.2.
populate_server_hardware_properties.jli.................................................. 12
7.2.
Java via Jython and the execv module ........................................................... 15
7.2.1.
execv.py .................................................................................................... 15
8. Debugging Jython ..................................................................................................... 16
8.1.
pdb..................................................................................................................... 16
2 of 16
2005 BladeLogic, Inc. All rights reserved.
1. Introduction
1.1. Overview
The scope of this document concerns the installation of Jython to leverage the
BladeLogic JLI as well as examples of common uses of the JLI. Jython, the
open-source Java based version of Python, offers a native scripting interface with
Java classes. Leveraging this ability, the JLI acts as a Jython bridge to the
BLCLI. Therefore, by instantiating a single JLI connection to the BLCLI, a script
maintains BLCLI context from one command to another, requiring a single
occurrence of the Java Virtual Machine as well as a single authentication with the
BladeLogic Application Server.
The document below discusses the installation of Jython with instructions to
configure Jython on a machine to utilize the JLI modules. This document focuses
on installation and configuration of the JLI on the BladeLogic Application Server,
though the same configuration principles may be applied to installations on
workstation machines. This document further discusses common uses of the JLI
and examines specific examples of Jython code using the JLI. While this
document shall, from time to time, explain details of the Jython language, one
should not expect a full course in Jython within this document. While little
literature exists on Jython specifically, one wishing to learn more about Jython
should seek material focusing on Python. Specically, for a decent first or
refresher course in Python, one may want to download and read a free e-book,
Dive Into Python, available for download in multiple formats or for purchase in
physical form at http://www.diveintopython.org.
*One should take special note of the execv.py example in Section 7.2 document.
This Jython module offers an excellent example of how to utilize Jythons native
interface to Java classes. The execv.py module creates a way to execute OS level
system calls via Jython and retrieving results and result codes by utilizing native
Java classes.
3 of 16
2005 BladeLogic, Inc. All rights reserved.
4 of 16
2005 BladeLogic, Inc. All rights reserved.
4. Windows Installations
4.1. Installing JLI on a Windows Application Server
4.1.1. Installing Jython
- Copy jython-XX.class to c:\tmp
- Change to the c:\tmp directory
- Run command: java jython-XX
- Choose the Jython Install Directory: e.g.: c:\bladelogic\jython
- JLI directory, with Jython/BLCLI modules: c:\bladelogic\jli
- $HOME for user LocalSystem: c:\
- .jython file created in $HOME
c:\.jython
Python.path = C:\\bladelogic\\jli
5 of 16
2005 BladeLogic, Inc. All rights reserved.
- Create c:\WINDOWS\system32\jython.bat
c:\WINDOWS\system32\jython.bat
@echo off
set JAVA_HOME=" c:\Progra~1\BladeLogic\OM\jre\bin\java"
set STDLIB=C:\Progra~1\BladeLogic\OM\br\stdlib
set BRDIR=C:\Progra~1\BladeLogic\OM\br
set JYTHON=C:\bladelogic\jython
set JREDIR=C:\Program Files\BladeLogic\OM\jre\lib
SET CLASSPATH=%JYTHON%\jython.jar;%JREDIR%\rt.jar;%STDLIB%;
%STDLIB%\log4j-1.2.4.jar;
%STDLIB%\activation.jar;%STDLIB%\jaxp.jar;%STDLIB%\mailapi.jar;
%STDLIB%\parser.jar;%STDLIB%\mysql.jar;%STDLIB%\xerces.jar;
%STDLIB%\xml-writer.jar;%STDLIB%\oracle.jar;%STDLIB%\msbase.jar;
%STDLIB%\mssqlserver.jar;%STDLIB%\msutil.jar;%STDLIB%\smtp.jar;
%BRDIR%;%BRDIR%\bladelogic.jar;%STDLIB%\looks-1.1.2.jar;
C:\Progra~1\BladeLogic\OM\bin;%STDLIB%\rbachelp.jar
set ARGS=
:loop
if [%1] == [] goto end
set ARGS=%ARGS% %1
shift
goto loop
:end
java -Dpython.home=%JYTHON% -classpath %CLASSPATH%
org.python.util.jython %ARGS%
6 of 16
2005 BladeLogic, Inc. All rights reserved.
#!/usr/bin/env jython
from bladelogic.cli import CLI
jli = CLI()
jli.initialize()
cmd = [NameSpace,blcliCmd,Arg1,,ArgN]
returnObject = jli.run(cmd)
# returnObject has various methods and properties.
# The following if statement demonstrates the important three.
if(returnObject.success()):
print returnObject.returnValue
else:
print returnObject.getError()
*see Section 6 for more information about the following analysis
7 of 16
2005 BladeLogic, Inc. All rights reserved.
On import, an external file is compiled and processed, executing any code not in a
function. Scripts contain import statements on the first lines of a script. Import
statements take the following forms:
import bladelogic.cli as blcli
from bladelogic.cli import CLI
Exhibit 5.1
The first line in Exhibit 5.1 imports the bladelogic.cli module, naming the
imported object blcli. The second line does not import the entire
bladelogic.cli module. Instead, the second import statement imports only the
CLI class from within the bladelogic.cli module. If an external module
contains more than one class, function, variable or other such Jython object, the
from <module> import <object>, does not load the entire module, but rather
only the specific external object.
Using the second line from Exhibit 5.1, one may now access (instantiate) the CLI
class directly as a global variable within the script.
8 of 16
2005 BladeLogic, Inc. All rights reserved.
6.2. Suites
Anyone accustomed to other high-level programming or scripting languages such
as C/C++, Java, or PERL may find the lack of function and class delimiters
somewhat disconcerting. Rather than the normal squiggly brackets used to
indicate the beginning and end of a function or class, if or for statement, Jython
breaks its control structures into suites. A suite consists of a series of lines with
similar levels of indentation. See Exhibit 5.2 for more information.
def toggle(y):
if(y==0):
y = 1
elif(y==1):
y = 0
else:
y = -1
return y
if(x==1):
x = toggle(x)
print Hello World!
print x
Exhibit 5.2
The first suite in Exhibit 5.2 defines a function named toggle that takes an
argument y. Notice the indented if, elif (else if), and else control structures.
The statements within the toggle function indent 1 level, and the statements
within each control structure indent yet another level. The final line in the
function, the return statement, lies outside of the ifelse control structure and
therefore has the same level (1) of indentation.
Each of the if statements in the toggle function has 1 line within its suite. The
if(x==1) statement in Exhibit 5.2 demonstrates a multi-line suite with no other
indentations, unlike the multi-line, multi-suite toggle function.
Notice the : at the end of the first line of the if suite and toggle function
definition. This colon indicates the beginning of a suite. The Jython interpreter
will throw an error during compilation if it finds the next line not indented one
more than the beginning of the suite. Notice the lack of a syntactical indication of
the end of a suite. A suite ends when the compiler finds a line of the same
indentation level as the beginning line of the suite.
While the reader may find the concept of a suite somewhat non-intuitive, over
time the reader may discover that the suite methodology employed by Jython
remains logical and lends to more readable code.
6.3. Documenting
Jython allows for both single line code documentation and multiple line code
documentation. Single line documentation has no restrictions and one may place
the comments wherever one pleases by simple prefixing the comment with a #.
This # comments out the all text to the right of it, allowing one to temporarily
remove the execution of a line of code or to make a note to explain the purpose of
a particular line of code.
9 of 16
2005 BladeLogic, Inc. All rights reserved.
Multiple line documentation, aside from its obvious benefits, offers a special
functionality as well. Multiple line documentation, when used as the initial lines
of a function, class, or module, become available at runtime as
<functionName>.__doc__. One uses triple quotation marks to denote multiple
line documentation. One may use multiple line documentation anywhere, though
one restriction exists. Multiple line documentation must retain the same level of
indentation as the suite it documents.
function foo(bar):
This function prints the value of bar.
This is a test.
print bar
Exhibit 6.3
Importing a library with the function foo in Exhibit 6.3 allows one to access the
documentation via <module name>.foo.__doc__.
import sys
import os
import string as s
import re
from bladelogic.cli import CLI
jli = CLI()
jli.initialize()
CR=os.linesep
serverList = jli.run(['Server','listAllServers']).returnValue
serverList = s.rstrip(serverList)
serverArray = re.split('\n',serverList)
for serverName in serverArray:
cmd = []
NameSpace = Utility
blcliCmd = updateServersStatus
cmd.append(NameSpace)
cmd.append(blcliCmd)
cmd.append(serverName)
cmd.append(1)
cmd.append(2000)
cmd.append(true)
try:
update = jli.run(cmd)
assert update.success(), update.getError()
print 'Success for: '+serverName
except AssertionError,e:
print e
print 'All available servers updated.'
sys.exit(0)
11 of 16
2005 BladeLogic, Inc. All rights reserved.
7.1.2. populate_server_hardware_properties.jli
This script (Lines 47-51) reduces the time to update server properties by
leveraging the JLIs direct interface to the BLCLI, taking advantage of BLCLI
context. For more information on BLCLI context see the Best Practice for
Using the BLCLI [Internal] document in the BladeLogic Knowledge Base.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import string as s
import re
from execv import execv as e
from bladelogic.cli import CLI
jli = CLI()
jli.initialize()
serverFile = open(sys.argv[1],'r')
allServers = serverFile.readlines()
allServers.sort()
serverFile.close()
def getPropertyKey(propertyName):
propertyKey = ''
cmd = ['Property','propertyNameToDBKey']
cmd.append(propertyName)
propertyObject = jli.run(cmd)
if(propertyObject.success()):
propertyKey = propertyObject.returnValue
return str(propertyKey)
def setPropertyValue(propertyKey,propertyValue):
cmd = ['Server','putPropertyValue']
cmd.append(propertyKey)
cmd.append(str(propertyValue))
jli.run(cmd)
print 'Getting property keys.'
cpuNumKey = getPropertyKey('CPU_NUMBER')
cpuSpeedKey = getPropertyKey('CPU_SPEED')
memoryKey = getPropertyKey('MEMORY')
print 'Property keys retrieved. Processing server data.'
for server in allServers:
server = s.rstrip(server)
print 'Processing properties for '+server
noverOut = e('nover -c -H '+server).out
noverSubbed = re.sub('"','',noverOut)
noverStripped = s.rstrip(noverSubbed)
nover = re.split(',',noverSubbed)
if len(nover) > 1:
cpuNum = str(nover[3])
cpuSpeed = str(nover[4])
memory = str(nover[6])
try:
jli.run(['Server','findByName',server])
setPropertyValue(cpuNumKey,cpuNum)
setPropertyValue(cpuSpeedKey,cpuSpeed)
setPropertyValue(memoryKey,memory)
jli.run(['Server','save'])
except:
print 'ERROR:::Failure setting properties. '
continue
else:
print 'Finished for '+server+'.'
else:
print 'ERROR:::Timed out connecting to '+server+'.'
print 'Server property update completed.'
sys.exit(0)
Line 1: Import sys, the Jython module with method to handling system
operation.
12 of 16
2005 BladeLogic, Inc. All rights reserved.
14 of 16
2005 BladeLogic, Inc. All rights reserved.
Line 1: Import runtime class Runtime from java.lang, a native Java package.
Line 2: Import runtime class BufferedInputStream from java.io, a native
Java package.
Line 4: Define a Jython class named execv.
Lines 5 - 8: Set return values to None, the Jython Null value.
Line 10: When defining a class, the __init__ function acts as the class
initialization (or main) function. This function takes the assigns the passed
system call to the cmd variable. This function gets called when instantiating the
class. All class methods require self as their first argument.
Line 11: Execute a system call through the Java Runtime class, exec method.
Assign process to variable p.
Line 12: Call waitFor() method of process p. This method waits for the
process to finish, returning the executed commands return code to the exitVal
property of the execv class.
Line 13: This Java code gets the input stream, capturing STDOUT for the executed
command and buffers it to the variable stream.
Line 14: Initializes the variable e as a Jython array.
Line 15: If return code of executed command does not equal 0, the command
failed. Otherwise, execution jumps to Line 18.
Line 16: If the executed command failed, sets isErr property of execv property
to 1.
Line 17: Sets stream variable equal to error stream, capturing STDERR.
Line 18: Reads the first character of stream.
15 of 16
2005 BladeLogic, Inc. All rights reserved.
Line 19: Begins a while loop until the value of r equals -1, building stream
output into the array e.
Line 20: Appends the value of r to the array e.
Line 21: Reads the next character of stream. The read() function returns -1
at the end of the stream.
Line 22: Joins the array e into a single string, assigning joined string to
retVal.
Line 23: If the isErr property, set on Line 16, of execv equals 1, otherwise
continue to the else statement on Line 25. Sets the relevant property of execv
to the retVal value.
Line 24: Sets the err property of execv to the value of retVal.
Line 26: Sets the out property of execv to the value of retVal.
Usage
from execv import execv as e
ls = e(ls la)
if(not ls.isErr):
print ls.out
else:
print ls.err
8. Debugging Jython
8.1. pdb
The Jython debugger (the p in pdb stands for Python, as Jython is just a Java
port of Jython) allows the line by line debugging of Jython scripts. To execute a
script in debug mode, one must import the pdb module and insert the
set_trace() function call near the beginning of the script or where one
suspects problems:
import pdb
pdb.set_trace()
Execute the script from the command line. The script will pause, expecting input,
when it reaches the pdb.set_trace() line in the scripts execution. One may use
the following commands to navigate the debugger.
16 of 16
2005 BladeLogic, Inc. All rights reserved.