Showing posts with label Jython. Show all posts
Showing posts with label Jython. Show all posts

Wednesday, May 26, 2010

Application Status Checking with WLST

def appStatus(server_name):
       try:
           print '---------------------- Application status---------------------'
           mBeans=adminHome.getMBeansByType("ApplicationRuntime")
           for bean in mBeans:
               if server_name != bean.getObjectName().getLocation():
                   continue
               components= bean.lookupComponents()
               for componentRTList in components:
                   #ejbRTList=componentRTList.getMBeansByType("EJBComponentRuntime")
                   app = componentRTList.getParent().getName()
                   istate=componentRTList.getDeploymentState()
                   if istate == 0:
                       istate='UNPREPARED'
                   if istate == 1:
                       istate='PREPARED'
                   if istate == 2:
                       istate='ACTIVE'
                   if istate == 3:
                       istate='NEW'

                   print "%65s %65s %7s" % (str(componentRTList.getName()), app, istate)

       except:
           print "This Server has no Applications"

Saturday, May 08, 2010

Monday, December 07, 2009

Apache POI and Jython

import sys

def setClassPath():
libDir = "E:/apps/jython2.5.1/com/"
classPaths = ["poi-3.5-FINAL-20090928.jar","commons-logging-1.1.jar","log4j-1.2.13.jar","poi-scratchpad-3.5-FINAL-20090928.jar"]
for classPath in classPaths:
sys.path.append(libDir + classPath)


setClassPath()

from org.apache.poi.hssf.usermodel import *
from java.io import FileInputStream

file = "users.xls"

fis = FileInputStream(file)
wb = HSSFWorkbook(fis)
sheet = wb.getSheetAt(0)

# get No. of rows
rows = sheet.getPhysicalNumberOfRows()
# print wb, sheet, rows

cols = 0 # No. of columns
tmp = 0

# This trick ensures that we get the data properly even if it
# doesn.t start from first few rows
for i in range(0, 10,1):
row = sheet.getRow(i)
if(row != None):
tmp = sheet.getRow(i).getPhysicalNumberOfCells()
if tmp > cols:
cols = tmp
# print cols

for r in range(0, rows, 1):
row = sheet.getRow(r)
# print r
if(row != None):
for c in range(0, cols, 1):
cell = row.getCell(c)
if cell != None:
# print cell
pass


print sheet.getRow(5).getCell(3)

#wb.close()
fis.close()

Setting Java Classpath in Jython

import sys

def setClassPath():
libDir = "E:/apps/jython2.5.1/com/"
classPaths = ["poi-3.5-FINAL-20090928.jar","commons-logging-1.1.jar","log4j-1.2.13.jar","poi-scratchpad-3.5-FINAL-20090928.jar"]
for classPath in classPaths:
sys.path.append(libDir + classPath)

setClassPath()

from org.apache.poi.hssf.usermodel import *
from java.io import FileInputStream

Monday, November 02, 2009

Great Jython tutorial

http://onjava.com/pub/a/onjava/2002/03/27/jython.html

Swing with Jython - I

from javax.swing import *


#from java.awt.event import *

from java.awt import *

from java.lang import *



win = JFrame("This is a frame")

win.bounds = 100,100,100,100

win.defaultCloseOperation = 3 #EXIT_ON_CLOSE



menuBar = JMenuBar()

fileMenu = JMenu("File")

menuBar.add(fileMenu);



newMenuItem = JMenuItem("New")

newMenuItem.actionPerformed=lambda event : System.out.println("Clicked on New Menu Item")

fileMenu.add(newMenuItem)



openMenuItem = JMenuItem("Open")

openMenuItem.actionPerformed=lambda event : System.out.println("Clicked on Open Menu Item")

fileMenu.add(openMenuItem)



saveMenuItem = JMenuItem("Save")

saveMenuItem.actionPerformed=lambda event : System.out.println("Clicked on Save Menu Item")

fileMenu.add(saveMenuItem)

saveMenuItem.enabled = 0 #false



win.JMenuBar = menuBar



contentPane = win.contentPane

contentPane.layout = GridLayout()



buttonOne = JButton("Button One")

buttonOne.actionPerformed=lambda event : System.out.println("Clicked on Button One")

contentPane.add(buttonOne)



buttonTwo = JButton("Button Two")

buttonTwo.actionPerformed=lambda event : System.out.println("Clicked on Button Two")

contentPane.add(buttonTwo)



buttonThree = JButton("Button Three")

buttonThree.actionPerformed=lambda event : System.out.println("Clicked on Button Three")

contentPane.add(buttonThree)



win.pack()

win.show()

Sunday, August 30, 2009

Metaclass in Python

Here is a way to do this and subclass it.

class CustomMetaclass(type):
    def __init__(cls, name, bases, dct):
    print "Creating class %s using CustomMetaclass" % name
    super(CustomMetaclass, cls).__init__(name, bases, dct)


class BaseClass(object):
    __metaclass__ = CustomMetaclass

class Subclass1(BaseClass):
    pass

And now, an example that actually means something, this will automatically make the variables in the list "attributes" set on the class, and set to None.
def init_attributes(name, bases, dict):
    if 'attributes' in dict:
        for attr in dict['attributes']:
            dict[attr] = None
    return type(name, bases, dict)


class Initialised(object):
    __metaclass__ = init_attributes
    attributes = ['foo', 'bar', 'baz']


print 'foo =>', Initialised.foo
# output=>
foo => None


Here is an even more concrete example, showing how you can subclass 'type' to make a metaclass that performs an action when the class is created. This is quite tricky:

class MetaSingleton(type):
    instance = None
    def __call__(cls, *args, **kw):
        if cls.instance is None:
            cls.instance = super(MetaSingleton, cls).__call__(*args, **kw)
        return cls.instance

class Foo(object):
    __metaclass__ == MetaSingleton

a = Foo()
b = Foo()
assert a is b

Monday, July 27, 2009

Grinder Load Testing Framework

1) To start the agent issue below command
java -cp "E:\apps\grinder-3.2\grinder-3.2\lib\grinder.jar" net.grinder.Grinder

2) To start the console
java -cp "E:\apps\grinder-3.2\grinder-3.2\lib\grinder.jar" net.grinder.Console

You have to set classpath for grinder.jar and jython.jar before this.

This assumes grinder is installed at E:\apps\grinder-3.2

Use TCPProxy to create a macro of the activity of testing

Here is a sample grinder.properties file

grinder.logDirectory=log
grinder.threads=5
grinder.processes=1
grinder.runs=0
grinder.script=mailcom.py

Saturday, July 25, 2009

Swing Frame using Jython

from javax.swing import JButton, JFrame

class MyFrame(JFrame):
def __init__(self):
JFrame.__init__(self, "Hello Jython")
button = JButton("Hello", actionPerformed=self.hello)
self.add(button)

self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
self.setSize(300, 300)
self.show()

def hello(self, event):
print "Hello, world!"

if __name__=="__main__":
MyFrame()

Swing using Jython

from java import awt
from pawt import swing

labels = ['7', '8', '9', '+',
'4', '5', '6', '-',
'1', '2', '3', '*',
'0', '.', '=', '/' ]

keys = swing.JPanel(awt.GridLayout(4, 4))
display = swing.JTextField()

def push(event): # Callback for regular keys
display.replaceSelection(event.actionCommand)

def enter(event): # Callback for '=' key
display.text = str(eval(display.text))
display.selectAll()

for label in labels:
key = swing.JButton(label)
if label == '=':
key.actionPerformed = enter
else:
key.actionPerformed = push
keys.add(key)

panel = swing.JPanel(awt.BorderLayout())
panel.add("North", display)
panel.add("Center", keys)
swing.test(panel)

Swing using Jython

from java import awt
from pawt import swing

labels = ['7', '8', '9', '+',
'4', '5', '6', '-',
'1', '2', '3', '*',
'0', '.', '=', '/' ]

keys = swing.JPanel(awt.GridLayout(4, 4))
display = swing.JTextField()

def push(event): # Callback for regular keys
display.replaceSelection(event.actionCommand)

def enter(event): # Callback for '=' key
display.text = str(eval(display.text))
display.selectAll()

for label in labels:
key = swing.JButton(label)
if label == '=':
key.actionPerformed = enter
else:
key.actionPerformed = push
keys.add(key)

panel = swing.JPanel(awt.BorderLayout())
panel.add("North", display)
panel.add("Center", keys)
swing.test(panel)