Jython Scripting Language


Hi folks :)
how was going for you ? hope it's fine ;) ... for me, i'm really in a big dung those days :p and in this post i'm not really going to speak about that crappy shit but actually about Jython Scripting language under the Websphere Application server :)
Lets take a deep look into this J2EE application server, and view the command and tools given by IBM and developped in Jython to do the tricky work of server administration.

There are numerous alternative languages implemented for the Java VM. The following features help to separate Jython from the rest:
- Dynamic compilation to Java bytecodes - leads to highest possible performance without sacrificing interactivity.
- Ability to extend existing Java classes in Jython - allows effective use of abstract classes.
- Optional static compilation - allows creation of applets, servlets, beans, ...
- Bean Properties - make use of Java packages much easier.
- Python Language - combines remarkable power with very clear syntax. It also supports a full object-oriented programming model which makes it a natural fit for Java's OO design.

you can read further about jython here and here :) okey, let's talk about serious things now :
The wsadmin tool uses Jython V2.1 and can be divided up into four based groups or objects if you like :) AdminApp, AdminConfig, AdminControl, AdminTask, and Help

lets talk about each one in detail :)
+ The First one, AdminApp is a set of commands used to install, modify, and administer applications. The AdminApp object interacts with the WebSphere Application Server management and configuration services to make application inquiries and changes. This interaction includes installing and uninstalling applications, listing modules, exporting, and so on.
+ The second one is AdminConfig, and it's a set of commands used to invoke configuration commands and to create or change elements of the WebSphere AS configuration.
+ The AdminControl is used to invoke operational commands that manage objects for the application server.
Many of the AdminControl commands have multiple signatures so that they can either invoke in a raw mode using parameters that are specified by Java Management Extensions (JMX), or by using strings for parameters. In addition to operational commands, the AdminControl object supports some utility commands for tracing, reconnecting with a server, and converting data types.
+ AdminTask is used to run administrative commands with the wsadmin tool. Administrative commands are discovered dynamically when you start the wsadmin tool. The administrative commands that are available for you to use, and what you can do with them, depends on the edition of WebSphere Application Server that you have.

Now let's take a look at few examples that might prove useful.

the example above is simply getting all the servers in a node :) GO

import sys n1 = sys.argv[0] plist = "[-serverType APPLICATION_SERVER -nodeName " + n1 + "]" print plist slist = AdminTask.listServers(plist).split("\n") for s in slist: print s continue
another example for getting all the app in the server :

applist = AdminApp.list().split("\n") for a in applist: print a continue
this one is complicated and will change the heap size of the jvm on the server :

import sys # ./wsadmin.sh -lang jython -javaoption -Dscript.encoding=IBM-1047\ # -user userid -password password -f /scriptingpath/heapadjust.py servername initheap maxheap # def printHelp(): print "This script sets the jvm init and max heap sizes for a server" print "Format is heapadjust.py servername initheapsize maxheapsize" # if(len(sys.argv) > 2): # get server name, initialheapsize, and maxheapsize from the command line s1 = sys.argv[0] ih = sys.argv[1] mh = sys.argv[2] else: printHelp() sys.exit() ihc = 'initialHeapSize' mhc = 'maximumHeapSize' # # get the id of the server # srv = "/Server:" + s1 + "/" srvid = AdminConfig.getid(srv) s1pdefs = AdminConfig.list('ProcessDef', srvid).split("\n") for pd in s1pdefs: if AdminConfig.showAttribute(pd, 'processType') == 'Servant': jvms = AdminConfig.list('JavaVirtualMachine', pd).split("\n") continue # we now have the correct jvm for jvm in jvms: AdminConfig.modify(jvm, [[ihc, ih]]) AdminConfig.modify(jvm, [[mhc, mh]]) continue # # Save the Config # AdminConfig.save() # # Synchronize with the nodes... # nl = AdminConfig.list('Node').split("\n") print "Beginning node synchronization" for n in nl: nn = AdminConfig.showAttribute(n, 'name') objn = "type=NodeSync,node=" + nn + ",*" Syncl = AdminControl.completeObjectName(objn) if Syncl != "": AdminControl.invoke(Syncl, 'sync') print "Done with node " + nn else: print "Skipping node " + nn continue # All Done.

0 comments: