Date Created: Fri 30-Nov-2007

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/



JACL Script to create Virtual Hosts in Websphere Application Server


Often in a complex deployment you may wish to automatically create virtual hosts in WebSphere. This script will work on a single WebSphere Application server or a complex multi node cluster using WebSphere Network Deployment.

The JACL script is run by using the follwing command

UNIX <baseinstal>/bin/wsadmin.sh -f create_virtual_hosts.jacl
WINDOWS <baseinstal>\bin\wsadmin.bat -f create_virtual_hosts.jacl

create_virtual_hosts.jaclvirtualhosts.props

#
# JACL Script - Create Virtual Hosts
#
# Author: Steve Robinson
# Date Created: 30 November 2007
# Arguments: props file
# Decription: This script creates virtual hosts as defined by a props file.
# Version: 1.1
# History:
# 29-November-2007 - Created.
# 30-November-2007 - Added Comments
#
#####################################################################
# Procedure: Load properties file.
#####################################################################
proc loadProperties {propFileName} {
java::import java.io.FileInputStream
java::import java.util.Properties

set fileprop [java::new Properties]
set fileStream [java::new FileInputStream $propFileName]

$fileprop load $fileStream
return $fileprop
}

######################################################################
# Procedure: List installed virtual hosts.
######################################################################
proc showList { a } {

puts "\nList installed virtual hosts"
puts "\-----------------------------\n"

foreach e $a {
regexp {(.*)(\(cells.*)} $e 1 2 3
puts [ format "%-5s %-50s" " " $2 ]
lappend returnList $2
}

puts \n

return $returnList
}

#######################################################################
# Delete virtual host
#
# List virtual hosts check if target virtual host already exists.
# If so delete it and recreate
#
#######################################################################

proc deleteVirtualHost { a } {
global AdminConfig
set vHostId [ $AdminConfig getid /VirtualHost:$a/ ]
if { [string length $vHostId] == 0 } {
#puts "************************************"
#puts " ($a) $vHostId host does not exist!"
#puts "************************************\n"
} else {
puts "************************************"
puts "Deleting (a$) $vHostId "
puts "************************************\n"
catch { $AdminConfig remove $vHostId } r
puts $r
}
}


####################################################################
# Create Virtual Host.
####################################################################
proc createVirtualHost { vNewHostName hostAliases } {

######### DEBUG
#puts "\t>> Entering createVirtualHost \n"
######### DEBUG


######### DEBUG
#puts "vNewHostName =$vNewHostName \n"
######### DEBUG

global AdminConfig

set cell_Id [$AdminConfig list Cell]
######### DEBUG
#puts "\t>> Cell name = $cell_Id\n"
######### DEBUG
set vHosts [$AdminConfig list VirtualHost $cell_Id]


puts "************************************"
puts "Creating Virtual Host: $vNewHostName"
puts "************************************\n"

foreach i $hostAliases {

set hostname [ list hostname [ lindex [split $i : ] 0 ] ]
set port [ list port [ lindex [split $i : ] 1 ] ]

set hostAlias [ list $hostname $port ]

lappend hostAliasList $hostAlias

}

set aliases [ list aliases $hostAliasList ]
set name [ list name $vNewHostName ]

set attrList [ list $name $aliases ]

######### DEBUG
#puts "attrList=$attrList"
######### DEBUG

$AdminConfig create VirtualHost $cell_Id $attrList

######### DEBUG
#puts "\t>> Leaving createVirtualHost \n"
######### DEBUG

}

#######################################################################
# Begin: Main Routine.
#######################################################################
#set debug true
set props [loadProperties virtualhosts.props]

#list all virtual servers currently on the machine
set vHosts [ $AdminConfig list VirtualHost ]
showList $vHosts


# loop through all vitual servers
# we determine the end of the list by using
# the host & port attributes. If we retrieve a
# host_alias_list or port_list attribute and find it to be blank
# then we have reached the end of the list

set nodeName [ exec hostname ]
set nodeId [ $AdminConfig getid /Node:$nodeName/ ]

for {set indx 0} {[string length [set virtualhost [$props getProperty virtual_host_name_$indx]]] != 0} {incr indx} {

set vhName [$props getProperty virtual_host_name_$indx]

set host_port_list [$props getProperty host_port_list_$indx]

deleteVirtualHost [$props getProperty virtual_host_name_$indx]

####################################################################
# Install virtual host
####################################################################
if { [ catch { createVirtualHost $vhName $host_port_list} r ] == 0 } {
puts "************************************"
puts "$vhName installed successfully"
puts "************************************\n"

puts "************************************"
puts "Saving changes"
puts "************************************\n"

$AdminConfig save

} else {
puts "\n$vhName failed to install\n"
puts $r
puts "************************************\n"
}

}

#list all virtual servers currently on the machine
set vHosts [ $AdminConfig list VirtualHost ]
showList $vHosts

#####################################################################
# End: Main Routine.
#####################################################################

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • My Blog
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories