A Sample Python Script for a Tomcat Agent

The sample Python script can be used to stop a web application on a Tomcat agent.

Ensure that before running the sample script, you have followed the steps in Setting up Python Scripting. The sample script shown in the code defines two methods. The newTomcat() method accepts the name of the server and port number as parameters. It creates an instance of the tomcat agent, provided a server with the same name does not already exist. The stopWebApp() method accepts a server and the name of an application as parameters and stops an application running on the server.
import tibco.tea
tea = tibco.tea.EnterpriseAdministrator()

tomcat = tea.products['tomcat']
def newTomcat( name, port ):

# idempotency: don't create if a server with this name already exists

 if name not in tomcat.members.keys():
    tomcat.createserver( name, port )
    #tomcat.refresh_
    print( 'tomcat server '+ name +' created successfully');
    server = tomcat.members[ name ]
    server .start()
    return server
 else:
    print( 'tomcat server '+ name +' already exists');
    server = tomcat.members[name] 
    if server.status == 'RUNNING':
            print('tomcat server instance '+ name + ' is already running');
    else:            
            server.start()  
    return  server            
            
def stopWebapp( server, name ):
    webapp = server .members[ name ]
    # idempotency: don't generate an error if the app is already stopped
    if webapp.status == 'STOPPED':
        print( 'webapp ' + name + ' already stopped.' )
    else:
        webapp .stop()
        print( 'webapp ' + name + ' stopped.' )
    
    
tomcat1 = newTomcat( 'tomcat1', 8088 )
stopWebapp( tomcat1, 'docs' )
stopWebapp( tomcat1, 'examples' )
Assuming you have saved this file as TomcatManagement.py, you can run it by navigating to the location and running the command, TomcatManagement. This yields the following output:
C:\TEA>TomcatManagement.py
tomcat server tomcat1 created successfully
webapp docs stopped.
webapp examples stopped.
Related concepts