More ways to check the existense of a resource with WLST

In one of my previous post, I discussed a couple of ways with which you can find out whether a particular configuration already exists before you create them. The methods are not just limited to the one on that post. So now I am here to discuss couple more way to do the same check. But just for completeness I shall also include the methods I discussed in my previous post. I am taking a sample use case to check whether a particular WebLogic Server instance exists in the domain or not. But you can extrapolate these technique for any other resource like JDBC Data Source, JMS Server, JMS Queue etc. Goal - Check whether a server with the name 'mymanagedserver' exists in the domain Method-1 Using "cd" A simple approach is to use the "cd" command to navigate into that configuration MBean. If the "cd" command throws an exception then you can safely assume that the resource/configuration doesn't exists and you can continue with your task.
...
mgdServerName = 'mymanagedserver'
### Checking for the server
try: 
    cd('/Servers/' + mgdServerName)
    print '===> Server \"' +mgdServerName+'\" already exists'
    print '===> No action was performed'
    exit()
except:
    pass
### Continue to create the server
...
Method-2 Catch "BeanAlreadyExistsException" Another approach is to catch the "BeanAlreadyExistsException" when creating the resource and safely exiting the script.
...
mgdServerName = 'mymanagedserver'
### Create the Managed Server
edit()
startEdit()
try:
    create(mgdServerName, resourceType)
    print '===> Created Managed Server - ' + mgdServerName
    pass 
except BeanAlreadyExistsException:
    print '===> Server \"' +mgdServerName+'\" already exists'
    print '===> No action was performed'
    cancelEdit('y')
    exit()
...
But with this approach you are starting an edit session and creating the server to find out it's existence. So you have to cancel the edit session once you find out that the resource that you are creating already exists if not you might get a warning message similar to the following: You have an edit session open and you will lose all outstanding changes and your edit session will be stopped if you exit. Are you sure you would like to exit? (y/n) So canceling the edit session with a response 'y' will make WLST not to prompt for the user response. Method-3 Using "getMBean" There is a WLST command called getMBean which will return the MBean object when you specify the appropriate path. If the instance is not found then it will return None. But getMBean command does not throw an exception when an instance is not found. So you have explicitly check result of the getMBean.
...
mgdServerName = 'mymanagedserver'
ref = getMBean('/Servers/' + mgdServerName)
if(ref != None):
	print 'Server ' +  servername + 'already exists'
	disconnect()
	exit()
else:
	pass
### Continue to create the server
...
Method-4 Using "ls" You can simply navigate to the resource type and execute ls. This will return the list of all the instances as a string when you assign the output to a variable. Then you can use the find method on that string variable to check whether your server exists. The find method returns a "0" on success or "-1" on failure.
...
mgdServerName = 'mymanagedserver'
cd('/Servers')
servers = ls()
if (servers.find(servername) != -1):
	print 'Server ' +  servername + ' already exists'
	disconnect()
	exit()
else:
	pass
### Continue to create the server
...
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

I shall use this space to share the information and thought about Oracle Fusion Middleware products

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today