|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Management
Strategies for WebLogic Domain Configuration
Scripting Options, part 2
By: Prakash Malani
Digg This!
In my previous article (WLDJ, Vol. 3, issue 8), I gave you a detailed overview of the different strategies available for domain creation and configuration and evaluated manual and templating options. In this article, I employ tools like WLShell, WebLogic Scripting Tool, Silent Scripts, and Ant for domain configuration. These tools leverage simple, high-level scripting languages. Note: This article relies heavily on the common steps such as Domain Creation, Database Configuration, and Verifying the Domain Configu-ration described in part 1. Scripting LanguageThere are many questions regarding the structure and semantics of the scripting language. How do you write comments? What, if any, is the line separator? What data-types does the language support? Does the language have variables and assignments? How do you perform query and navigation? How do you invoke behavior (i.e., methods)? Is there support for branching and looping? Fortunately, many scripting alternatives exist for domain configuration. Let's look at some of them.WLShell Installation of WLShell is easy. Download the appropriate installer and execute it. Follow the installer steps and complete the installation. WLShell scripting language supports single-line comments. The comment character is a hash-mark (#). Everything from # is commented out. There is no special end-of-line character. The echo command is either print or echo. WLShell uses the familiar concept of directories and files for navigating the WebLogic Server instance where the MBeans correspond to directories and the attributes of MBeans correspond to files. Hence, navigation happens with recognizable commands like cd and ls. The command to create an MBean is md and the command to delete an object is rd. For example, in order to create a connection pool named semJDBCConnectionPool, execute the following command: md /JDBCConnectionPool/semJDBCConnectionPool What if the name contains a forward slash? The forward slash is escaped by using an additional forward slash. For example, to create a data source named jdbc/semJDBCDataSource, execute the following command: md /JDBCTxDataSource/jdbc//semJDBCDataSource Setting the variable attribute of an object is done with the set command. To set a variable of the boolean type, invoke set variable-name true-or-false. For example, to set StdoutDebugEnabled variable to true, invoke the following command: set StdoutDebugEnabled true To set a variable of integer type, invoke set variable-name integer-value. For example, to set StdoutSeverityLevel to 64, invoke the following command: set StdoutSeverityLevel 64 To set a variable of string type, invoke set variable-name "string-value." For example, to set URL to jdbc:pointbase:server://localhost:9093/workshop, invoke the following command: set URL "jdbc:pointbase:server://localhost:9093/workshop" Setting variables of primitive types is easy, but how do you set a variable of java.util.Properties type? To set a variable of java.util.Properties type, use the special syntax. Invoke set variable-name (java.util.Properties) "name=value pairs separated by semicolon." For example, to set the properties for the connection pool, use the following: set Properties (java.util.Properties) So far, I have examined how to set variables of types boolean, int, String, and Properties. How do you set a variable of another MBean type? Just invoke set variable-name path-to-the-mbean. For example, to set the connection pool property of the JMS JDBC store, invoke the following command: set ConnectionPool /JDBCConnectionPool/semJDBCConnectionPool By executing the ls command, WLShell not only lists attributes that can be get or set, but also operations that can be invoked. How do you execute an operation? Using a special command called invoke. The syntax for the invoke command is: invoke method-name parameter-list-separated-by-space. For example, deploy the connection pool to the server, using the following command: invoke addTarget /Server/$server In the above example, the method name is addTarget. The method takes one parameter, the MBean of the server. Deploying an application with WLShell is involved. Get a reference to /Deployer-Runtime/DeployerRuntime instance and create an instance to hold the deployment data. Associate the data with the server, and activate the application. WLShell can be used in the script mode as well as in the interactive mode. In the interactive mode, a WLShell session is created and connected to a running WebLogic Server instance. Changes are made to the WebLogic Server instance by invoking commands in the WLShell session. To launch WLShell in interactive mode, execute the following command: %wlsh In the script mode, a script of WLShell commands is created and executed. The script can be created once and executed any number of times later to set up similar domains. The syntax to execute a script is wlsh -f script-file-name. To create the sample domain using script, execute the following command: %wlsh -f sem_domain_wlshell.txt In the above example, sem_domain_wlshell.txt file contains WLShell statements to configure the domain. Before executing the WLShell script, configure an empty WebLogic Workshop domain by following the steps in the Domain Creation section using SEMDomain-WLShell as the Configuration Name. Setup the database instance by following the steps in the Database Configuration section. In order to run WLShell, just verify that wlshell/bin directory is available in the PATH environment variable. Now, execute the script: wlsh -f sem_domain_wlshell.txt Verify that the configuration is correct by running the tests as described in the Verifying the Domain Configuration section. WLShell supports converting an existing config.xml file into WLShell script and commands via c2w command. WLShell supports working in off-line mode and connecting to config.xml instead of the running server. However, the implementation is not feature complete. For example, creation of a connection pool in the off-line mode results in a "feature not implemented" message. WebLogic Scripting Tool (WLST) Here are the installation instructions:
WLST can be used in script mode as well as in interactive mode. In the interactive mode, a WLST session is created in offline mode. In this mode, WLST is not connected to a running instance of WebLogic Server. Once WLST is connected to a running WebLogic Server instance changes are made to the WebLogic Server instance by invoking commands in WLST session. In the script mode, a script of WLST commands is created and executed. Just as in the interactive mode, the script contains Jython statements. The script file is a Jython file. The script can be created once and executed any number of times later to setup similar domains. To invoke WLST in interactive mode, invoke the following command: java weblogic.WLST To invoke WLST in script mode, pass in the name of the script. For example: java weblogic.WLST sem_domain_wlst.py Do not execute the above commands yet. At the end of this section, you will do additional setup and then execute the WLST script. The following are the quick and basic notes on Python (and Jython). Specifying an end of statement character (;) is optional unless multiple statements are specified on a single line. The comment character is hash mark (#). All the characters from # onwards on a single line are considered part of the comment. The echo command is print. Strings can be passed in either single quotes (') or double-quotes ("). WLST uses the familiar concept of directories and files for navigating the MBeans. Hence, navigation happens with recognizable commands like cd and ls. However, every command is a function. Therefore, the commands look like cd('Servers/cgServer') and ls(). WLST defines a very special variable named cmo for the current managed object. Initially, cmo is assigned to the root. The value of cmo changes as you navigate the MBeans. In file system parlance, the value of cmo is the current working directory. Execute ls('a') to find attributes whose value can be changed. Prefix the name of the attribute with the word set to change the value of the attribute. For example, to change the value of attribute StdoutSeverityLevel to 64, invoke the following method: setStdoutSeverityLevel(64). However, on what object should the setStdoutSeverityLevel(64) method be invoked? Of course, cmo: cmo.setStdoutSeverityLevel(64) The other option is to assign cmo to a variable. The variable can later be used at any time throughout the current session. For example: cgServer = cmo Suppose you want to set StdDebugEnabled value to true. However, neither Python nor Jython supports the boolean type. Fortunately, the integer equivalents, 1 for true and 0 for false, can be used. Therefore, set the value of StdDebugEnabled to true as follows: cgServer.setStdoutDebugEnabled(1) How do you create resources such as JDBC connections pools and JMS queues? Use WLST's built-in create() method. The create() method accepts two parameters. The first parameter is the name of the resource, whereas the second parameter is the type of resource. For example, to create a JDBC connection pool with semJDBCConnectionPool as its name, execute the following command: create('semJDBCConnectionPool','JDBCConnectionPool') Note: There is also a corresponding delete() method that works similarly. WLST provides a special method named makePropertiesObject() to create a properties object out of a string of name and value pairs separated via semicolon. Therefore, set the properties of a JDBC connection pool as follows: semCPProperties = Deploying the newly created resource, such as the connection pool, to the server is easy. Invoke the addTarget() method of the source as follows: semCP.addTarget(cgServer) Deploying an application is very easy with WLST using the deploy() method. The deploy() method takes three parameters. The first parameter is the name of the application. The second parameter is the path to the location of the EAR file. The third parameter is the target or the server name. The following is the example of the deploy() method: deploy('SEMApp','c:/Automate/SEMApp/SEMApp.ear','cgServer') Before executing the WLST script, configure an empty WebLogic Workshop domain by following the steps in the Domain Creation section using SEMDomain-WLST as the Configuration Name. Setup the database instance by following the steps in the Database Configuration section. Now, execute the script: java weblogic.WLST sem_domain_wlst.py Restart the WebLogic Server instance and verify that the configuration is correct by running the tests as described in the Verifying the Domain Configuration section. Is it possible to reverse engineer an existing domain into a WLST script? Absolutely. Using the configuretoscript() command, the contents of an existing domain are converted into a WLST script file. Use this feature to quickly incorporate WLST into your current process. Silent ScriptsA silent script consists of statements that are executed by the configuration wizard in silent mode. The silent script uses a very simple language. The statements are terminated by semicolons (;). The language supports Java-style comments. Double forward slash (//) is used to comment to the end of the line whereas /* ? */ is used to comment sections. The language has no concept of data-types; everything is a string enclosed in double quotes. For example, to set boolean or integer types just enclose the value in double quotes, as shown below:set cgServer.StdoutDebugEnabled "true"; The simple types can be converted to their string equivalents, but what about objects? In the case of objects the name of the object is used. For example, to set the error destination for a queue: set aq.ErrorDestination "SEMAppWeb.queue.AsyncDispatcher_error"; There is no support for branching and looping. There is no navigation. The structure of the silent script file is flat. The language does support variables. A variable can be assigned upon either creating a new object or finding a preexisting object. For example, a variable assignment on finding a preexisting object: find Server "cgServer" as cgServer; And, a variable assignment on creating a new object: create JDBCConnectionPool "semJDBCConnectionPool" as semCP; The script uses special syntax to deploy resources to their targets, i.e., calling the addTarget() method of an MBean. The special syntax is assign <resource type> "<resource name>" to Target "<server name>";. For example, to deploy semJDBCConnectionPool to cgServer use the following syntax: assign JDBCConnectionPool "semJDBCConnectionPool" to Target "cgServer"; The silent script enables domain creation very nicely. Therefore, you can either create the domain using the Configuration Wizard as described in the Domain Configuration section or use the silent script. I will illustrate domain creation with a silent script. The sem_domain_silent_create.txt script illustrates domain creation. The silent script reads Basic WebLogic Workshop Domain template and creates a domain. But, how do you invoke the script? The syntax to invoke the silent script is: > %BEA_HOME%/weblogic81/common/bin/config.cmd -mode=silent -silent_script=full path to script file Now, create the domain using the sem_domain_silent_create.txt script. The -log flag is optional, but highly recommended. When there are errors, the messages logged to the console are very terse, but the complete exception trace is logged to the log file. The log file is immensely important in debugging silent configuration scripts. Please note that in sem_domain_silent_create.txt file, if the WebLogic user is not assigned a password, the script will fail. Also, newly created users by default are assigned to group Administrators. Therefore, upon creating a user system, the user system belongs to group Administrators. If the script tries to write a domain to a location that already exists, the script fails. In order to replace a preexisting domain, set the OverwriteDomain variable to true. Next, set up the database as described in the Database Configuration section. Now, shut down the server if the server is running. Configure the server by executing the sem_domain_silent_config.txt script. Be aware that the order of setting driverName and URL is important. Otherwise, the creation of the connection pool fails! Note the update domain; command at the end of sem_domain_silent_config.txt script. This command is very important. Without it, changes made to the domain will not be committed, and the domain will not be changed! How do you deploy the application while configuring the domain using the silent script (Note: the server is not running)? Create a custom template that contains the application. The custom template is a JAR file that contains the EAR file, the relevant section of config.xml that will be merged into current config.xml, and some metadata in the template-info.xml file. In the silent script, add the template using the add Template ? command. Adding at least one template when updating the domain is necessary, otherwise the changes made to the domain are not persisted! A common technique, if you do not have your own template, is to use the DefaultWebApp template. Verify that the configuration is correct by running the tests described in the Verifying the Domain Configuration section. Special Ant TasksApache Ant is an invaluable tool for building and deploying applications. An Ant file is an XML file that contains many targets. The targets invoke tasks. WebLogic ships special tasks such as wlconfig and wldeploy. The wlconfig Ant task helps with domain configuration. The wldeploy Ant task helps with application deployment.WebLogic Server installation also includes Ant. Ant is automatically set up when setting up the environment by executing the setWLSEnv (bat or sh) script. The taskdefs for wlconfig and wldeploy are not required if this version of Ant is used. The language of the Ant file is XML. Therefore, language specifics like comments are XML comments. Other features, like the echo are Ant-specific. The wlconfig task contains nested elements such as query and create. The query element is used to find preexisting MBeans, whereas the create element is used to create new MBeans. Both query and create support get and set elements to get and set MBean attributes. The syntax for the set attribute is: <set attribute="MbeanAttributeName" value="NewValue"/>. All attribute values, regardless of type, are enclosed in double quotes and passed in as string. Consider setting the MBean attribute StdoutDebugEnabled value of type boolean to true. <set attribute="StdoutDebugEnabled" value="true"/> Similarly, consider setting the MBean attribute StdoutSeverityLevel value of type integer to 64. <set attribute="StdoutSeverityLevel" value="64"/> Once an object is queried, the object can optionally be set as an Ant property that is available throughout the file. For example, the server is queried and set into cgServer property. Later, in the build file, the cgServer property is utilized to target resources to the server. Note: The wlconfig does not currently support the invoke command. Therefore, for example, targets are set as attributes instead of invoking the addTarget operation. The wldeploy Ant task is used to deploy and undeploy the application. The wldeploy task takes actions such as deploy, undeploy, redeploy, cancel, start, stop, and distribute. In order to execute the included Ant file, first create the domain by following the steps described in the Domain Creation section, giving a meaningful name like SEMDomain-Ant as the Configuration Name. Note: Since the name of the domain is used in the Ant file, either use SEMDomain-ANT as the domain name or change the Ant file to match your domain name. Start the WebLogic server instance and use the steps described in the Database Configuration to configure the database. Launch a new command or shell window and execute the %WLS_HOME%/server/bin/setWLSEnv (cmd or sh) file to set up the environment. Execute the Ant using the following command: ant -v -f sem_domain_ant_config.xml Run the tests as described in Verifying the Domain Configuration section. The verbose (-v) option to the Ant command displays the MBean commands issued to the server. The information is very valuable in debugging and troubleshooting. An Ant file encourages very modular development of the automated script. Each individual resource is created and configured using a separate target. Using Ant dependencies, the all target calls the individual Ant targets and does the complete configuration. Ant encourages the creation of a clean target. The order of dependencies in the clean target is opposite that of the all target. Conclusion: Choices, Choices, and More ChoicesThis article described domain configuration using WLShell, WLST, Silent Scripts, and Ant tasks. These tools employ simple and powerful high-level scripting languages. Whereas WLShell uses a custom scripting language, WLST leverages Jython. Once a domain configuration script is created, setting up another identical domain is a simple matter of executing the script. Scripting domain configuration is the way to go for automating domain configuration.ReferencesBEA WEBLOGIC LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||