Search This Blog

Sunday, November 18, 2012

Input and Output in WLST

In WLST we can have same kind of command line inputs and outputs as in Python and Jython languages. This can be useful when it is required to prepare a huge monitoring report using WLST or when you work for hundreds of servers in a domain such as in cloud where Middleware as a service (MAAS) and you need to find an automation task with WLST scripts.

Inputs in WLST

To read simple string of values we can use raw_input() built in function or sys.stdin.readline() method. For reading different data types such as number values we can use input() function, which internally calls raw_input() function and apply eval() to convert the input value to number values.
wls:/offline> x=raw_input('Enter a number:')
Enter a number:4009
wls:/offline> print x*2
40094009
wls:/offline> y=input('Enter a number: ')
Enter a number: 400
wls:/offline> print y*3
1200
Let’s experiment with Boolean type of variable in WLST
wls:/offline> b=input('Do you love WLST?')
Do you love WLST?True
wls:/offline> print b
1
wls:/offline> b=input('Do you love WLST?')
Do you love WLST?False
wls:/offline> print b
0
Here you enter as True or False but, the Boolean variables are stores on WLST SHELL as either zero or one. This is somewhat confusing you with regular Boolean types in other languages or UNIX it is opposite. One represents True value, zero represents False. So take care while you scripting for the Boolean type variables.
When you have the option to select the data in string format then go for raw_input() built-in function. This is much better way when situation demands your script to enter name of the WebLogic server instance or some questions to repeat the script task. On the other hand input() will be good option when you have to select multiple options for monitoring geographical sites to choose one of them with a numbered input.

Reading variable in WLST with stdin

We have one more option to read the values from the WLST command prompt that is using sys module. We can use sys.stdin.readline() method to read the input and assign it to a variable. This will fetches only text data in to the variable, so you use this when you want to input the strings.
wls:/offline> serv=sys.stdin.readline()
managed3_server
wls:/offline> serv
'managed3_server\n'
wls:/offline> print serv
managed3_server

Output in WLST

An output can be displayed using print command as we have seen many statements in WLST already. Although, we cover few basics of print statement, which accepts string as an argument.
  • Simple string to display
    print ‘Welcome to WLST!!!’
    
  • To display the string and variables combinations in Java Style concatenation that is using ‘+’ operator
    print  ‘Server Status ’+ stat
    
  • To display only WLST script variables then you can use the comma separated variables you can use the combination of string in the same line too
    print x, y
    
  • Now, we will see the good old C style formatting for the output, here we use % symbol followed by paranthesized list of variables that will be substituted in place of embedded conversion types (such as string - s, decimal - d, float - f) in the order.
    print ‘%14s %d %f’ %(server, freeJvm, throughput)
    
In WLST we can use the print statement for displaying the data from a variable or strings objects. We can display the combination of variables in the same line. This is possible with comma separation or simply we can use Java style of concatenation using plus symbol.

Obtain Environment variables in WLST

WLST supports the Jython capability that obtaining the environment variables from the System. To do this we need to import the os module (operating system) and use the environ dictionary variable.

wls:/offline> import os
wls:/offline> os.environ
{'WL_HOME': 'C:\\wls12c\\wlserver',  …}

You can pull out your required values of environment variable as you access the dictionary variable. Example you can retrieve WL_HOME or JAVA_HOME values as show below.

wls:/offline> os.environ["WL_HOME"]
'C:\\wls12c\\wlserver'

wls:/offline> os.environ["JAVA_HOME"]
'C:\\Java\\jdk1.6.0_34'

This WLST trick can be most often used; when you are configuring resources are creating WebLogic domains. Remember, This WLST trick will works on UNIX platform as well.



Popular Posts