Jython script to create domain and set database connection using WLST.

In one of my recent project I have to prepare a jython script to create a domain and database connection and up this database connection on admin server in Weblogic environment. I have gone through various Weblogic documentation and few other websites where I find some examples but no explanation was given about these example. Being a new jython user I found it very difficult to make changes in the code according to my requirement. Here I am trying to explain those things along with script, so that whenever somebody have to modifiy the script, he/she can do it with ease according to his/her requirement.

 

Here is the script.

 

myScript.py

(When you copy this script, please remove all lines which are in brackets in italic fonts and in the color you are reading now. These are explanation lines.)

 

#============================================================

# Open a domain template.

#============================================================

 

readTemplate(“C:/bea/weblogic92/common/templates/domains/wls.jar”)

 

(Here you can modify the template by extract wls.jar file on your local disk. Put all the changes which are specific to your project. You can put all library jar’s in modified domain which your application need and you want to deliver to client. Other changes like which JDK you want to use SUN or JRocket you can define here. Also you can set  JAVA_Opetions for your environment. Then again make a jar file with these changes with some other name like myProject.jar to use new template. When your script will use this customized template, it will set your environment on any machine. All you have to do is to deliver this customized template (myProject.jar) and your script (myScript.py).I will explain every thing about template customization in my next post on this blog.)

 

#============================================================# #Configure the Administration Server and SSL port.

# To enable access by both local and remote processes, you should not set the

# listen address for the server instance (that is, it should be left blank or not set).

# In this case, the server instance will determine the address of the machine and

# listen on it.

#===========================================================

cd(‘Servers/AdminServer’)

set(‘ListenAddress’,”)

set(‘ListenPort’, 7001)

(Leave above section as it is if you want your application to run on 7001. Or you can change port number according to your requirement.)

 

create(‘AdminServer’,’SSL’)

cd(‘SSL/AdminServer’)

set(‘Enabled’, ‘True’)

set(‘ListenPort’, 7002)

 

(Leave above section as it is.)

 

#===========================================================

# Define the user password for weblogic.

#===========================================================

cd(‘/’)

cd(‘Security/DOMAIN_NAME/User’)

delete(‘weblogic’,’User’)   

create(‘YOUR_USERNAME’,’User’)   

cd(‘YOUR_USERNAME’)   

set(‘Password’,’YOUR_PASSWORD’)   

set(‘IsDefaultAdmin’,1)

 

(In the above section, you have to change the following variables 1. DOMAIN_NAME = replace this with the name of domain. 2. YOUR_USERNAME = replace this with the name you want to use for login in WL console 3. YOUR_PASSWORD = replace this with the password you want to set for login in WL console. Left rest of the things as it is.) 

 

#===========================================================

# Create and configure a JDBC Data Source, and sets the JDBC user.

#===========================================================

 

cd(‘/’)

create(‘YOUR_DATABASE_NAME’, ‘JDBCSystemResource’)

cd(‘JDBCSystemResource/YOUR_DATABASE_NAME/JdbcResource/YOUR_DATABASE_NAME’)

create(‘YOUR_DATABASE_NAME’,’JDBCDriverParams’)

cd(‘JDBCDriverParams/NO_NAME_0′)

set(‘DriverName’,’ca.ingres.jdbc.IngresDriver’)

set(‘URL’,’jdbc:ingres://localhost:21071/YOUR_DATABASE_NAME’)

set(‘PasswordEncrypted’, ‘YOUR_DATABASE_PASSWORD’)

set(‘UseXADataSourceInterface’, ‘false’)

create(‘YOUR_DATABASE_NAME’,’Properties’)

cd(‘Properties/NO_NAME_0′)

create(‘YOUR_DATABASE_USER_NAME’, ‘Property’)

cd(‘Property/YOUR_DATABASE_USER_NAME’)

cmo.setValue(‘YOUR_DATABASE_PASSWORD’)

 

cd(‘/JDBCSystemResource/YOUR_DATABASE_NAME/JdbcResource/YOUR_DATABASE_NAME’)

create(‘YOUR_DATABASE_NAME’,’JDBCDataSourceParams’)

cd(‘JDBCDataSourceParams/NO_NAME_0′)

set(‘JNDIName’, java.lang.String(“jdbc/YOUR_DATABASE_NAME”))

 

cd(‘/JDBCSystemResource/YOUR_DATABASE_NAME/JdbcResource/YOUR_DATABASE_NAME’)

create(‘YOUR_DATABASE_NAME’,’JDBCConnectionPoolParams’)

cd(‘JDBCConnectionPoolParams/NO_NAME_0′)

set(‘TestTableName’,’SQL SELECT 1 FROM iitables’)

 

(In the above section, you have to change the following variables

1. YOUR_DATABASE_NAME = replace this with the name of your database.

2. YOUR_DATABASE_USER_NAME = replace this with the user name with which you access data base.

3. YOUR_DATABASE_PASSWORD = replace this with the password you want to set for login in WL console.

4) ca.ingres.jdbc.IngresDriver = replace this driver with the name of driver of database which you want to use.

5) jdbc:ingres://localhost:21071/YOUR_DATABASE_NAME  = replace this with the URL of your database.

The best way to get this URL and driver name is by first creating a dataresource manually from WL console and copy these fields from there. Left rest of the things as it is.)

 

#===========================================================

# Target resources to the servers.

#===========================================================

 

cd(‘/’)

assign(‘JDBCSystemResource’, ‘YOUR_DATABASE_NAME’, ‘Target’, ‘AdminServer’)

 

(In the above section, you have to change the following variables

1. YOUR_DATABASE_NAME = replace this with the name of your database. Left rest of the things as it is.)

 

#===========================================================

# Write the domain and close the domain template.

#===========================================================

 

setOption(‘OverwriteDomain’, ‘true’)

writeDomain(‘C:/bea/user_projects/domains/DOMAIN_NAME’)

closeTemplate()

 

(In the above section, you just have to give the path of domain where you want to create it. We are just assuming that your WL is installed at C:/ drive. Left rest of the things as it is.)

 

#===========================================================

# Exit WLST.

#===========================================================

 

exit()

 

 

If you have any further question or you need some more clarification on this script, you can write me back. Below are the links which help me in my course of action. You can refer to these links as they might prove handy in your requirement.

 

http://weblogicserver.blogspot.com/

http://e-docs.bea.com/wls/docs92/deployment/index.html

http://e-docs.bea.com/wls/docs92/config_scripting/using_WLST.html

 

Any suggestion or comment on this post is most welcome.

 

Regards,

–vikas

About these ads

5 Comments

Filed under WebLogic

5 responses to “Jython script to create domain and set database connection using WLST.

  1. Fil

    This script is straight copy from, the examples
    bundled with the weblogic distribution.

    • skybehindme

      Thanks for your comments Fil! Yes this is a copy from the example bundled with weblogic. But the reason behind putting this on my blog was that when I try to use that bundled scripts, it confuses me and my team a lot. It was not clear which is the field is the name of variable and which field is required for putting the value of that variable. It consume lot of time and efforts to sort it out and run it successfully. So I decide to put it on my blog so that if somebody need, they get an easier and clear version of script.
      My intention to publish it here was not to prove that its my unique or different work. It’s just putting things in more clear ways. My requirements were fulfilled by this, so in your case you might need some more things to do. But you can use it as base work.

      Thanks a lot for giving the feedback!

  2. lance

    Hi Vikas,

    Thanks a lot for your writing such a great blog, which helped me a lot when I wrote a script to create domain automatically. I googled a lot before I came here.

    • skybehindme

      Good to know that u get help from this. I see so many traffic on this blog for these issues but only few tells me if they r worth or not. Something I thought to remove them from blog as it seems to me as if no body get befitted from it. Thanks for writing your feedback. It can motivate me to kept it and add something new as well.
      Thanks,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s