Skip navigation.
Downloads | Product Documentation | Support

Beehive Integration in WebLogic Server 9.1

# Python Scripts for Annotation Overrides

This appendix contains the complete code for the two Python scripts referred to in Annotation Overrides.

Note that customization is required before you can use these scripts. For example, you will have to set the username and password for the WebLogic Server domain you are using.

# driver.py

>     import sys
>     from jarray import zeros, array
>     from java.lang import System
>     from java.lang import String
>     from java.util import Random

>     if(len(sys.argv) != 4):
>         print "usage: scriptPath appPath planPath"
>         sys.exit()

>     sys.path.append(sys.argv[1])
>     import OverrideUtils

>     connect('weblogic','weblogic')
>     print 'Connected'

>     appPath  = sys.argv[2]
>     planPath = sys.argv[3]

>     plan = loadApplication(appPath, planPath, 'false')
>     print 'Got plan'

>     util = OverrideUtils.PlanUpdater(plan, appPath, planPath)

>     r = Random()

>     # Update a field's override value
>     moduleName='service/HelloService.war'
>     key=moduleName+'/service.HelloService/_jcxBean/value'
>     value='I hereby override this value to %d' % (r.nextInt())
>     util.applyOverride(moduleName, key, value)

>     # The array case
>     key=moduleName+'/service.HelloService/_jcxBean/arrayValue'
>     val1=str(r.nextInt())
>     val2=str(r.nextInt())
>     val3=str(r.nextInt())
>     val4=str(r.nextInt())
>     arrArg=array([val1,val2,val3,val4],String)
>     util.applyOverride(moduleName, key, arrArg)

>     # Update a class' override value
>     moduleName='OAMEar'
>     key=moduleName+'/controls.MyJcx/controls.MyAnnotation/value'
>     value2='Application scope set at %d!!' % (r.nextInt())
>     util.applyOverride(moduleName, key, value2)

>     util.flush()
>     progress=updateApplication('OAMEar', planPath, stageMode='STAGE', testMode='false')

>     f=open('override-value.txt', 'w')
>     f.writelines('override.value=' + value)
>     f.writelines('\n\n')
>     f.writelines('app.override.value=' + value2)
>     f.flush()
>     f.close()

# OverrideUtils.py

>     import sys

>     class PlanUpdater:

>         def __init__(self, plan, appPath, planPath):

>             self.MANIFEST="META-INF/annotation-manifest.xml"
>             self.plan = plan
>             self.updateCount = 0

>         ############################################################################
>         #
>         # Call this method repeatedly to override different values.
>         # In the end, call flush() to persist the changes
>         #
>         # There are 2 ways to construct the key here:
>         #
>         # 1. moduleName+"/"+className+"/"+annotationClassName+"/"+memberName
>         # 2. moduleName+"/"+className+"/"+fieldName+"/"+memberName
>         #
>         #
>         ############################################################################
>         def applyOverride(self, moduleName, key, value):

>             print '\n---------------------------------------------------------------\n'
>             if self.plan == None:
>                 print 'PlanUpdater was not initialized'
>                 return

>             if moduleName == None or key == None or value == None:
>                 print 'Invalid input parameter to overrideValue() - null parameters not allowed'
>                 return

>             moduleDescriptor =  self.plan.getModuleDescriptor(self.MANIFEST, moduleName)
>             if moduleDescriptor == None:
>                 print 'unable to obtain moduleDescriptor for ' + moduleName
>                 return

>             dConfigBean = self.plan.getDConfigBean(moduleDescriptor)
>             classes = dConfigBean.getAnnotatedClasses()
>             for clazz in classes:
>                 className = clazz.getAnnotatedClassName()
>                 annotations = clazz.getAnnotations()
>                 for annotation in annotations:
>                     annotationClassName = annotation.getAnnotationClassName()
>                     members = annotation.getMembers()
>                     for member in members:
>                         memberName = member.getMemberName()
>                         theKey = moduleName+"/"+className+"/"+annotationClassName+"/"+memberName
>                         ## FIXME: should probably only change if the values has changed
>                         if theKey == key:
>                             originalValue = member.getMemberValue()
>                             currentOverrideValue = member.getOverrideValue()
>                             overrideValue = value

>                             print '[    Updating Key]: '+ theKey
>                             if originalValue != None:
>                                 print '[  Original Value]: '+ originalValue
>                             if currentOverrideValue != None:
>                                 print '[Current Override]: '+ currentOverrideValue
>                             if overrideValue != None:
>                                 print '[    New Override]: '+ overrideValue
>                                 print '\n'
>                                 member.setOverrideValue(overrideValue)
>                                 self.updateCount += 1
>                             return

>                     arrayMembers = annotation.getArrayMembers()
>                     for arrayMember in arrayMembers:
>                         memberName = arrayMember.getMemberName()
>                         theKey = moduleName+"/"+className+"/"+annotationClassName+"/"+memberName
>                         if theKey == key:
>                             print 'About to override array member %s' % (memberName)
>                             original = arrayMember.getMemberValues()
>                             currOvrdValue = arrayMember.getOverrideValues()
>                             print 'There are %d items in the array' % (len(currOvrdValue))

>                             print '[    Updating Key]: '+ theKey
>                             if original != None and len(original) > 0:
>                                 print '[  Original Value]: '+self.arrayToString(original)
>                             if currOvrdValue != None and len(currOvrdValue) > 0:
>                                 print '[Current Override]: '+ self.arrayToString(currOvrdValue)
>                             if value != None and len(value) > 0:
>                                 print '[    New Override]: '+self.arrayToString(value)
>                                 print '\n'
>                                 arrayMember.setOverrideValues(value)
>                                 self.updateCount += 1
>                             return

>                 fields = clazz.getFields()
>                 for field in fields:
>                     fieldName = field.getFieldName()
>                     annotations = field.getAnnotations()
>                     for annotation in annotations:
>                         annotationClassName = annotation.getAnnotationClassName()
>                         members = annotation.getMembers()
>                         for member in members:
>                             memberName = member.getMemberName()
>                             theKey = moduleName+"/"+className+"/"+fieldName+"/"+memberName
>                             if theKey == key:
>                                 originalValue = member.getMemberValue()
>                                 currentOverrideValue = member.getOverrideValue()
>                                 overrideValue = value
>                                 print '[    Updating Key]: '+ theKey

>                                 if originalValue != None:
>                                     print '[  Original Value]: '+ originalValue
>                                 if currentOverrideValue != None:
>                                     print '[Current Override]: '+ currentOverrideValue
>                                 if overrideValue != None:
>                                     print '[    New Override]: '+ overrideValue
>                                     print '\n'
>                                     member.setOverrideValue(overrideValue)
>                                     self.updateCount += 1
>                                 return

>                         arrayMembers = annotation.getArrayMembers()
>                         if arrayMembers == None:
>                             print '====> No arrayMembers found'
>                         else:
>                             print '====>>> got arrayMembers, size = %d' % (len(arrayMembers))
>                         for arrayMember in arrayMembers:
>                             memberName = arrayMember.getMemberName()
>                             theKey = moduleName+"/"+className+"/"+fieldName+"/"+memberName
>                             if theKey == key:
>                                 print 'About to override array member %s' % (memberName)
>                                 original = arrayMember.getMemberValues()
>                                 currOvrdValue = arrayMember.getOverrideValues()

>                                 print '[    Updating Key]: '+ theKey
>                                 if original != None and len(original) > 0:
>                                     print '[  Original Value]: '+self.arrayToString(original)
>                                 if currOvrdValue != None and len(currOvrdValue) > 0:
>                                     print '[Current Override]: '+self.arrayToString(currOvrdValue)
>                                 if value != None and len(value) > 0:
>                                     print '[    New Override]: '+self.arrayToString(value)
>                                     print '\n'
>                                     arrayMember.setOverrideValues(value)
>                                     self.updateCount += 1
>                                 return

>         def arrayToString(self, values):
>             if values == None or len(values) == 0:
>                 return ''
>             else:
>                 result = '[ '
>                 for value in values:
>                     result = result + value + ' '
>                 result = result + ']'
>                 return result

>         ########################################################
>         #
>         # flush() - Persist the changes to file
>         #
>         #######################################################

>         def flush(self):

>             if self.updateCount > 0:
>                 print 'Saving plan. There were %d changes in this update.' % (self.updateCount)
>                 self.plan.save()
>             else:
>                 print 'There has been no change to override values. plan not saved.'