String comparision in Jython script class exceptions.NameError exceptions.NameError instance

I Started recently working on jython/python, and come across the following problems and here are the solutions for those
  • How to compare strings in the jython/python script
norm = "6.0.2.35"
if cmp(wsadminVers, norm) == -1:
     print "quote_required"
else:
    print "quote_not_required"
    quote = 0
    print quote
In the above code snippet cmp(wsadminVers, norm) will compare two string values in wsadminVers and norm. If wsadminVers less than norm it return -1. If both are equal it returns 0. If wsadminVers is greater than norm it return 1. So cmp method is very easy string comparison method in jython.
  • How to resolve this exceptions or errors
2010-02-25 03:14:40 CDSUpdateAPP.py checkWASVersion Entry ()
2010-02-25 03:14:40 CDSUpdateAPP.py main ERROR ('Update of application failed:', <class exceptions.NameError at 664938402>, <exceptions.NameError instance at 97256908>)
The above exception can be arise while running jythin scripts which has usage of undeclared variables. see example below.
 print super
The above is one of the code line in our jython script, in this case super is treated as variable and if it is not declared the above exceptions will results as super varibale is not initiated.
we can do some thing like
print "super" which will treat super as string and prints it on console.
P.S : Line spaces and alignment are very much important in python scripts 

WSVR0009E: Error occurred during startup Cannot get canonical host name for server

 [3/8/10 20:38:05:592 PST] 0000000a WsServerImpl  E   WSVR0009E: Error occurred during startup
META-INF/ws-server-components.xml
[3/8/10 20:38:05:602 PST] 0000000a WsServerImpl  E   WSVR0009E: Error occurred during startup
com.ibm.ws.exception.ConfigurationError: com.ibm.ws.exception.ConfigurationError: javax.naming.ConfigurationException: Cannot get canonical host name for server.
        at com.ibm.ws.runtime.WsServerImpl.bootServerContainer(WsServerImpl.java:184)
        at com.ibm.ws.runtime.WsServerImpl.start(WsServerImpl.java:139)


Caused by: javax.naming.ConfigurationException: Cannot get canonical host name for server.
        at com.ibm.ws.security.core.SecurityConfig.getHostName(SecurityConfig.java:2476)
        at com.ibm.ws.security.core.SecurityConfig.setValues(SecurityConfig.java:1041)
        at com.ibm.ws.security.core.distSecurityComponentImpl.initializeSecurityConfig(distSecurityComponentImpl.java:776)
        at com.ibm.ws.security.core.distSecurityComponentImpl.initialize(distSecurityComponentImpl.java:224)
        ... 27 more
Have you ever faced any such errors while starting the Websphere server ?

Resolving the problem
Ensure that the naming resolution works for the host name. In the example, the name lookup failed for host name.Run the following two commands and ensure that they return the correct information.

nslookup `hostname`
nslookup

If both are fine try to create entries in /etc/hosts file by resolving the IP of the host.
some thing like below
 #
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#
127.0.0.2       nc184120.tivlab.austin.ibm.com.tivlab.austin.ibm.com nc184120.tivlab.austin.ibm.com nc184120
9.48.184.120    nc184120 nc184120.tivlab.austin.ibm.com
Hope this will help solving ur problem

print the time,date and time zone in java program and change the time zone

Lets see how java uses the Timezone set on JVM. Lets print the current time and date of the existing time zone.
And then change the time zone to different time zone and observe the changes.
The below code will do the above purpose.
    public void testMain(Object[] args)
        {
            Date dt = new Date();
            System.out.println("Todays Date and time:"+dt);
            Calendar c = Calendar.getInstance();
            System.out.println("Current System timeZone : "+c.getTimeZone());
            System.out.println("current time : "+c.getTime());
            System.out.println("Current time in millis : "+c.getTimeInMillis());
            //Lets change the JVM time zone and see
            TimeZone cst = TimeZone.getTimeZone("CST");
            cst.setDefault(cst);
            System.out.println("new time zone: "+cst);
            Calendar curCal=Calendar.getInstance(cst);
            //System.out.println("New time Zone : "+curCal.getTimeZone());
            System.out.println("New time with : "+curCal.getTime());
            System.out.println("New Time in millis: " +curCal.getTimeInMillis());
            //Some other time operations      
            Timestamp timeNow = new Timestamp(curCal.getTimeInMillis());
            System.out.println("Time stamp type of timeNow= " + timeNow);
            sleep(10);
            //After some time
            c = Calendar.getInstance();
            System.out.println("Current timeZone : "+c.getTimeZone());
            System.out.println("current time : "+c.getTime());
            System.out.println("current time in millis : "+c.getTimeInMillis());
            //To pass the current local time format and display like that
            SimpleDateFormat DF = new SimpleDateFormat();
            System.out.println("Current sys time format : "+ DF.format(c.getTime()));
            SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
            System.out.println("Change the Current sys time format : "+ DF1.format(c.getTime()));
        }
Corresponding output is as follows :
    Todays Date and time:Thu Mar 11 12:21:59 IST 2010
    Current System timeZone : sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
    current time : Wed Mar 03 15:47:38 IST 2010
    Current time in millis : 1267611458390
    new time zone: sun.util.calendar.ZoneInfo[id="CST",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=CST,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
    New time with : Wed Mar 03 04:17:38 CST 2010
    New Time in millis: 1267611458406
    Time stamp type of timeNow= {SysUpTime = 146714 days 6:56:24} {Timestamp = Wed Mar 03 04:17:38 CST 2010}
    Current timeZone : sun.util.calendar.ZoneInfo[id="CST",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=CST,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
    current time : Wed Mar 03 04:17:48 CST 2010
    current time in millis : 1267611468406
    Current sys time format : 3/3/10 4:17 AM
    Change the Current sys time format : 2010-03-03 04.17.48
Explanation:
So by observing the output and code lines we can understand that the time changes according to Timezone used.

How to find Websphere Version in python/jython scripts WAS

How to find WebsphereVersion(Exact build number) in python/jython scripts ?

The following lines of code will give the exact WAS version along with build number when used in a jython or python script.

wsadminNode = AdminControl.getNode()
print wsadminNode
wsadminSvr = AdminControl.queryNames("node="+wsadminNode+",type=Server,*")
print wsadminSvr
#wsadminVers = AdminControl.getAttribute(wsadminSvr, "platformVersion")
# The above line also gives us the WAS version but it will not give the exact build number details.
wsadminSvrON = AdminControl.makeObjectName(wsadminSvr)
wsadminVers = wsadminSvrON.getKeyProperty('version')
print  wsadminSvr

Hope this helps in finding the WAS versions in a jython/python script.

The above when run with wsadmin.sh script some times it throws the following error.
AppSrv01\bin>wsadmin.bat -conntype NONE -javaoption -Xmx384m -f  C:/kb/1321CDSUpdateAPP.py -lang jython  -appServerName CDSServer  -logDir C:/kb -logFile test.log  -appFileName "C:\\kb\\test.txt"  -appName CDS -controlFile c:/kb/test.txt

2010-02-25 02:41:40 CDSUpdateAPP.py main ERROR ('Update of application failed:', , com.ibm.ws.scripting.ScriptingException: AdminControl service not available)

 The above error can be rectified by passing the hostname explicitly or else by changing the connection type to RMI and RMI port. When we run the above lines of code using connType none as below
./wsadmin.sh  -conntype NONE -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -lang jython  -appServerName CDSServer  -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt
It may through the error above we have seen
Solution for this will be
 -host with complete hostname or ipaddr is the alternate solution, since its a failure with Java
probably we can try  InetAddress.getAllByName(host)  in a standalone Java program and try to resolve it the same solution will apply to wsadmin as well.
Using Connection type RMI
./wsadmin.sh -conntype RMI -port 2809 -lang jython -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -appServerName CDSServer  -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt
Using Host name and removing connection type NONE
./wsadmin.sh  -host nc145002.tivlab.austin.ibm.com -lang jython -javaoption -Xmx384m -f /opt/builds/1321CDSUpdateAPP.py -appServerName CDSServer  -logDir /opt/builds -logFile test.log -appFileName /opt/builds/test.txt -appName CDS -controlFile /opt/IBM/WebSphere/AppServer/bin/testctrl.txt


P.S: To find the different connection types and ports used by the Websphere installation, try open the Administration console using the url http://:9060/ibm/console/ in browser.

tynt

Avatar Review (Analysis in view of The Ramayana)..click this ... Avatar Review
-Krishna babu G

 
Krishnas page - Blogged Visit blogadda.com to discover Indian blogs