 
#
# heapSize.py
#
# by Arthur Kevin McGrath
#
# General Comments
#   NONE of the methods in this file that create things call AdminConfig.save()
#   Therefore, no changes are committed unless YOU explicitly commit them
#   Any methods that cause changes also call AdminConfig.queryChanges()
#   so that you at least know which files are going to change if you
#   commit the changes
import AdminConfig,AdminTask




#setServerHeaps()
#Set min and max heap size for all servers of a given type
#If no server type is specified, set min and max heap size
# for all servers in a cell.  In addition, you can enable or disable
# verbose garbage collection for either a class of servers or
# for all servers in a cell
#parameters:
#    minHeap - the minimum heap size you want (in MB).  This size MUST be smaller than max
#    maxHeap - the maximum heap size you want (in MB).  This size MUST be larger than min
#    serverClass - OPTIONAL PARAMETER - a String that names the kind of server you want to modify.  Must be a type returned by AdminConfig.types().  All servers of that type will be modified.  If no servers of that type exist in your cell, this routine silently returns without doing anything
#    verbose - OPTIONAL PARAMETER - set to 1 if you want to enable verbose garbage collection.  Set to zero if you want to disable verbose garbage collection.  The default is zero.
def setServerHeaps( minHeap, maxHeap, serverClass, verbose):
  "Set min and max heap size for all servers of a given type.  If no server type is specified, set min and max heap size for all servers in a cell.  In addition, you can enable or disable verbose garbage collection for either a class of servers or for all servers in a cell\nparameters:\n    minHeap - the minimum heap size you want (in MB).  This size MUST be smaller than max\n    maxHeap - the maximum heap size you want (in MB).  This size MUST be larger than min\n    serverClass - OPTIONAL PARAMETER - a String that names the kind of server you want to modify.  Must be a type returned by AdminConfig.types().  All servers of that type will be modified.  If no servers of that type exist in your cell, this routine silently returns without doing anything\n    verbose - OPTIONAL PARAMETER - set to 1 if you want to enable verbose garbage collection.  Set to zero if you want to disable verbose garbage collection.  The default is zero."
  raw = AdminConfig.list( serverClass )
  if len(raw) > 5:
      n = raw.splitlines()
      setHeap( n, minHeap, maxHeap, verbose )
  whatFilesChanged()
  return



# common routines
# The methods that follow are NEVER expected to be called
# by code outside of this file

# whatFilesChanged
# Reports the configuration files that will be changed
# as a result of any code in this file
# The changes will take effect when you call AdminConfig.save()
def whatFilesChanged():
  print "The following congfiguration files have changes pending:"
  print AdminConfig.queryChanges()
  return


#setHeap()
#Changes the minimum heap size and
#maximum heap size of an array
#of Java Virtual Machines
#If the array is zero length, this method silently returns
#This method depends on a method in parsing.py
#parameters:
#   array - an array of Java Virtual Machine 
#           configuration IDs as returned by
#           AdminConfig.list()
#   min - the desired minimum heap size
#   max - the desired maximum heap size
#   diagnostic - set to 1 to turn verbose garbage collection on
#           set to zero to turn verbose garbage collection off
def setHeap( array, min, max, diagnostic ):
  "setHeap()\nChanges the minimum heap size and maximum heap size\nof an array of Java Virtual Machines.  If the array is zero length, this method silently returns.  This method depends on a method in parsing.py\nparameters: (all parameters are mandatory)\n  array - an array of Java Virtual Machine\n           configuration IDs as returned by\n           AdminConfig.list()\n  min - the desired minimum heap size\n  max - the desired maximum heap size\n  diagnostic - set to 1 to turn verbose garbage collection on\n           set to zero to turn verbose garbage collection off"
  verboseOrNot = 'false'
  if diagnostic != 0:
    verboseOrNot = 'true'
  #Diagnostic print
  #print array
  for x in array:
    #Diagnostic print
    #print x
    s = parsing.findValueInConfigurationIDString( x, 'server' )
    n = parsing.findValueInConfigurationIDString( x, 'node' )
    print AdminTask.setJVMProperties( '[-serverName ' + s + ' -nodeName ' +  n  + ' -verboseModeGarbageCollection ' + verboseOrNot +  ' -maximumHeapSize ' + str( max ) + ' -initialHeapSize ' + str( min ) + ' ]' )



# findValueInConfigurationIDString
# parameters:
#   idString - the value returned by AdminControl.getConfigID
#                 or one value out of the array of values returned
#                 by AdminConfig.list
#   p - the property whose value we want to search for
#      known legal values are:
#           "name" for the name of this thing (if available - might be empty string)
#           "cell" for the cell name (note the singular)
#           "node" for the node name (note the singular)
#           "server" for the server name (note the singular)
#           "application" for the application EAR or WAR file name
# returns either the value or '"Not Found"
def findValueInConfigurationIDString( idString, p ):
  # initialize some variables
  value = "Not Found"
  namelessString = "Some bogus value"
  startOfConfigID = "(cells/"
  s = []
  #       DEBUG ONLY!!!!!
  #print "# # # inside findValueInConfigurationIDString()"
  #print "     idString- - >" + idString + "< - -"
  #print "                p- - >" + p + "< - -"
  # a configurationID has a whole bunch of junk that gets in the way of parsing
  # eliminate that junk
  # step 1: split the idString 
  #            separate the name and the configuration ID into separate strings
  #print "idString.find( startOfConfigID )",idString.find( startOfConfigID )
  namelessString = idString[ idString.find( startOfConfigID ) : ]
  name = idString[   : idString.find( startOfConfigID )  ]
  #       DEBUG ONLY!!!!!
  #print "                  name- - >" + name + "< - -"
  #print "namelessString- - >" + namelessString + "< - -"
  #       DEBUG ONLY!!!!!
  #print "\nidString[ ", idString.find( startOfConfigID ) , " : ] - ->" + namelessString + "<- -\n"
  #print "\nidString[ :",idString.find( startOfConfigID), " ] - ->" + name + "<- -"
  if cmp(p, "name") == 0:
    value = name
    return value
  else:
    #convert the type passed by our caller
    #into the form AdminConfig uses within a configID
    p = p.lower() + "s"
  # step 2: split the string at the '|' and use everything BEFORE '|'
  #             Remember to remove the leading "("
  s = namelessString.split( "|" )
  #       DEBUG ONLY!!!!!
  #print "s.split( '|' ) - -> first string - ->" + s[0] + "<- -\n\n"
  # step 3: everything else is delimited by '/' so tokenize
  #           and take the token AFTER the one that matches p
  s[0] = s[0].replace( "("," " )
  s[0] = s[0].strip()
  s = s[0].split( "/" )
  for i in range( len(s) ):
    #       DEBUG ONLY!!!!!
    #print "s[",i,"] - - >" + s[i] + "< - -"
    if cmp(p, s[i]) == 0:
      value = s[i+1]
      #       DEBUG ONLY!!!!!
      #print p,"matches",s[i]
      break
    else:
      #       DEBUG ONLY!!!!!
      #print p,"does NOT match",s[i]
      pass
  return value

# findValueInObjectNameString
# parameters:
#   nameString - the value returned by AdminControl.queryNames
#   p - the property whose value we want to search for
# returns either the value or '"Not Found"
def findValueInObjectNameString( nameString, p ):
    returnValue = "Not Found"
    p += "="
    nameValuePairs = nameString.split( ',' )
    if len(nameValuePairs) > 1:
        for x in nameValuePairs:
            if x.find( p ) > -1:
                parts = x.split( '=' )
                returnValue = parts[1]
    return returnValue



