from java.util import *
import java.lang.System
from javax.management import *
import javax.management.Attribute
import com.bea.wli.dev2dev.manifestutils as mu
import com.bea.xml.XmlException
import com.bea.x2003.x03.wlw.config
import sys
import getopt
import wlst100 as wlst
# from weblogic.management.scripting.utils import wlst
import weblogic as wb
import java.util.StringTokenizer
# constants
cgJMSServer = "cgJMSServer"
cgJMSSystemResource = "conversational-jms"
cgSubDeploymentName = "cgJMSServer"
clusterJMS = "clusterJMS"
global adminSvrName

            

def createJMSQueue_single( queue, errorQueue ):
    if (errorQueue != None):
	wlst.cd("/")
	wlst.cd("/JMSSystemResources/" + cgJMSSystemResource + "/JMSResource/" + cgJMSSystemResource )
	errorQueueBean = wlst.getTarget( "JMSQueue/" + errorQueue )
	if (errorQueueBean != None):
		print "queue ", errorQueue, " already exists"
	else:
		errorQueueBean = wlst.create( errorQueue, "Queue")
		queueBean.setJNDIName(errorQueue)
		queueBean.setSubDeploymentName(cgSubDeploymentName)

    if (queue != None):
        wlst.cd("/")
        wlst.cd("/JMSSystemResources/" + cgJMSSystemResource + "/JMSResource/" + cgJMSSystemResource )
        queueBean = wlst.getTarget( "JMSQueue/" + queue )
        if (queueBean != None):
                print "queue " , queue , " already exists"
        else:
                queueBean = wlst.create( queue, "Queue" )
		queueBean.setJNDIName(queue)
		queueBean.setSubDeploymentName(cgSubDeploymentName)
                wlst.cd( "Queues/" + queue+ "/DeliveryFailureParams/"+ queue )
                if (errorQueue != None):
                    wlst.set("ErrorDestination", errorQueueBean )


def createClusterJMSModule(clName):
    testQ = wlst.getMBean('/JMSSystemResources/'+clusterJMS)
    clusterMBean = wlst.getMBean('/Clusters/'+clName)
    if testQ == None:
        print 'creating '+clusterJMS+' jms system module'
        wlst.cd('/')
        testQ = wlst.create(clusterJMS, 'JMSSystemResource')
	print 'created clusterjms'
        # wlst.cd('JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'NO_NAME_0')
	print 'assigning clusterjms to cluster'
	testQ.addTarget(clusterMBean)
	print 'creating subdeployment'
	
        



def createUniformDistQueue(queueName, errorQueueName):
    testQ = wlst.getMBean('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/' + queueName)

    if testQ == None:
	wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS)
	wlst.cmo.createUniformDistributedQueue(queueName)
	wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/' + queueName)
	wlst.set('JNDIName',queueName)
	wlst.set('LoadBalancingPolicy','Round-Robin')
	wlst.set('DefaultTargetingEnabled','true')
	wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS)

	if errorQueueName == None:
		print 'not adding any error queue for '+queueName
	else:
		print 'Adding error queue '+errorQueueName+' for '+queueName
		wlst.cmo.createUniformDistributedQueue(errorQueueName)
		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/'+ errorQueueName)
		wlst.set('JNDIName',errorQueueName)
		wlst.set('LoadBalancingPolicy','Round-Robin')
		wlst.set('DefaultTargetingEnabled','true')

		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/'+ errorQueueName + '/DeliveryFailureParams/' + errorQueueName)
		wlst.set('RedeliveryLimit','0')
		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/'+ errorQueueName + '/DeliveryParamsOverrides/' + errorQueueName)
		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/'+ errorQueueName +'/DeliveryFailureParams/' + errorQueueName)
		wlst.cmo.unSet('ErrorDestination')

		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/' + queueName + '/DeliveryFailureParams/' + queueName)
		wlst.set('RedeliveryLimit', redeliverylimit)
		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/' + queueName + '/DeliveryParamsOverrides/' + queueName)
		wlst.set('RedeliveryDelay','5000')
		wlst.cd('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/'+ queueName + '/DeliveryFailureParams/' + queueName)
		wlst.cmo.setErrorDestination(getMBean('/JMSSystemResources/'+clusterJMS+'/JMSResource/'+clusterJMS+'/UniformDistributedQueues/' + errorQueueName))



# main
def main():
	jarfile = None
	global name
	global password
	global url
	global domaindir
	global clName
	global qCount, dCount
	global adminSvrName
	try:
		opts, args = getopt.getopt( sys.argv[1:], "j:n:p:u:d:e:c:k:", "" )
	except getopt.GetoptError:
		print "invalid option"
		sys.exit(2)
		
	for o, a in opts:
		if o == "-n":
			name = a
		if o == "-p":
			password = a
		if o == "-u":
			url = a
        if o == "-d":
            domaindir = a
        if o == "-c":
            clName = a
                        
	if ((name == None) or (password == None) or (url == None) or (clName == None)):
		print "Bad or Missing parameters"
		sys.exit(2)

	print 'Connecting .... to server..'
	wlst.connect( name, password, url )
	print "connected to wlserver, clname is " + clName


	
	wlst.edit()
	wlst.startEdit()
	try:
		if (clName == 'single'):
			print('creating queue for single server')
			wlst.cd("/")
			mgdSvrs = wlst.get("Servers")
			print "Server == ",mgdSvrs[0]
			createJMSQueue_single( 'CCOutboundQueue', None);
		else:
			print('creating queue for cluster')
			createClusterJMSModule(clName)
			createUniformDistQueue( 'CCOutboundQueue', None);
		wlst.save()
		print 'ccoutbound queue created and saved'
		wlst.activate()
		print 'queue activated'
	except java.lang.Exception, ex:
		print 'exception while creating queue '
		ex.printStackTrace()
		wlst.dumpStack()
		wlst.cancelEdit('y')

          
        wlst.saveDomain()
        wlst.disconnect()
# invoke main
if __name__ == "__main__":
    connected="false"
    main()
	
	
