WebSphere Administrative Objects Not Accessible from Imported Jython Modules

In Websphere Jython (as of WAS6.1), the administrative objects are not automatically accessible from imported modules, unlike when you call them from your main script. It is also not possible to import them either since they are not defined in sys.modules.

“myModule.py”

print AdminConfig.types()

For example, the code above will break if this Jython module is imported from the main script or another module. If this is executed as the main script it would work right off the bat even without importing anything.

To fix this problem, you need to add the administrative objects to the “sys.modules” dictionary in the main script so that they will be visible to all imported modules. Once the administrative objects are available in “sys.modules”, your imported modules can then call the import command for those object to access them.

For example, in your main script, you need to construct the lines shown below before you can import any modules that will require access to the WebSphere administrative objects:

“myMain.py”

    import sys
    sys.modules['AdminConfig'] = AdminConfig
    sys.modules['AdminControl'] = AdminControl
    sys.modules['AdminApp'] = AdminApp
    sys.modules['AdminTask'] = AdminTask
    sys.modules['Help'] = Help

    # now you can import your module
    import myModule

Your imported modules can then call the import command to access these objects. The module below shows import commands for each WebSphere Administrative object.

“myModule.py”

    try:
        import AdminConfig
        import AdminControl
        import AdminApp
        import AdminTask
        import Help
    except:
        pass

    # this works now even if this module is imported
    print AdminConfig.types()

The try-except clause above is used to prevent the module from failing if you tried to run this as your main script. If you tried to run this in your main wsadmin script and the sys.modules were not set, this would simply ignore the exceptions and should be able to proceed to run any administrative commands directly.

I hope someday IBM will fix this problem by simply adding the administrative objects to the sys.modules by default so that we won’t need to do this ourselves.

Advertisements

5 thoughts on “WebSphere Administrative Objects Not Accessible from Imported Jython Modules

  1. myMain, and myModule are written “assuming” that all of the objects should be available. However, this is not always true.

    For example, should one start wsadmin either specifying, or defaulting the conntype, and should the target Application Server not be available, then the AdminTask object isn’t available.

    So, my “main” code includes:

    WSASobjects = { ‘AdminApp’ : AdminApp ,
    ‘AdminConfig’ : AdminConfig ,
    ‘AdminControl’ : AdminControl,
    ‘Help’ : Help
    }

    if ‘AdminTask’ in dir() :
    WSASobjects[ ‘AdminTask’ ] = AdminTask

    sys.modules.update( WSASobjects )

    and my “module” code uses something like:

    import AdminConfig, AdminControl, AdminApp, Help

    try:
    import AdminTask
    except:
    print ‘Required WebSphere Administrative object missing: AdminTask’
    sys.exit( -1 )

  2. Hi,

    Thank you for your comments.

    >myMain, and myModule are written “assuming” that all of the objects should be available. However, this is not always true. For example, should one start wsadmin either specifying, or defaulting the conntype, and should the target Application Server not be available, then the AdminTask object isn’t available.

    There is one case where if you specify -conntype NONE, Websphere will report that you are not connected to a server process, however, the AdminTask object will still be available. It will be in dir() like what you use in your code above to test it. It will be of limited capability like you won’t be able to execute runtime Mbeans operations. But you will still be able to perform some config operations like AdminTask.listNodes(), AdminTask.listServers(), AdminTask.listDatasources(), etc.

    – Alvin

  3. Hi Alvin,

    Thanks for sharing. I have been struggling with that for a while, and your solution is the most elegant I’ve seen (among the other two :-).

    Cheers,
    Alexei

  4. Thanks, that helped a lot when backporting WAS 7.0 wsadmin scripts. Do you think such an altered script will still work with newer versions of jython, like in WAS 7.0?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s