Monday, December 1, 2008

Exception Details in JavaScript


function displayException(e,method)
{
if(method != null)
{
alert('Error occured in ' + method);
}
if(e instanceof EvalError)
{
alert('Eval function is used in an incorrect manner. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof RangeError)
{
alert('A numeric variable exceeds its allowed range. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof ReferenceError)
{
alert('An invalid reference is used. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof SyntaxError)
{
alert('Syntax error occured while parsing JavaScript code. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof TypeError)
{
alert('The type of a variable is not as expected. Error name: ' + e.name + '. Error message: ' + e.message);
}
else if(e instanceof URIError)
{
alert('encodeURI() or decodeURI() functions are used in an incorrect manner. Error name: ' + e.name + '. Error message: ' + e.message);
}
else
{
alert('An unspecified error occurred!. Error name: ' + e.name + '. Error message: ' + e.message);
}
}

Friday, November 28, 2008

Java code to execute a command etc

Java code to execute a command and enter input to that command and view output of that command.


import java.io.*;

public class CommandInput {
public static void main(String args[]) throws Exception{
String cmd = "java HelloWorld";
String passwd = "P@ssw0rd";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
(new ConsolePrint(p.getInputStream())).start();
(new ConsolePrint(p.getErrorStream())).start();
BufferedWriter commandWriter = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
commandWriter.write(passwd);
commandWriter.close();
}
}

class ConsolePrint extends Thread{
InputStream is ;
ConsolePrint(InputStream is){
this.is = is;
}
public void run(){
BufferedReader br = new BufferedReader (new InputStreamReader(is));
String line = "";
try{
while ((line=br.readLine())!=null) {
System.out.println(line);
}
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

Friday, October 24, 2008

DIV shrink-to-fit width

You need to give the DIV shrink-to-fit width, which you can get by
giving it display: inline-block, display: table, display: table-cell or
float: left. float: left is probably the most widely supported.

JSP KeepGenerated in Weblogic 9 and 10

By default Weblogic deletes the java files generated during the transation phase of a jsp. It just retains the .class files. These java files are sometimes very useful in debugging a jsp.

It can be achieved by modifying the weblogic.xml in the following way :

<jsp-descriptor>
<precompile>false</precompile>
<precompile-continue>false</precompile-continue>
<keepgenerated>true</keepgenerated>
<verbose>true</verbose>
<working-dir>c:/temp/bea</working-dir>
</jsp-descriptor>

If this does not work, then login to Weblogic Admin Console. Go to Deployments. Select your application. If it is an enterprise application you may have to choose the sub-module. Go to Configuration. Apply Lock and Edit. Check the Keepgenerated flag and save the changes.

You may have to restart the server !!!

Sunday, October 5, 2008

GDuration & Duration manipulation

There is a lot of difference between the Java Bean generated for xsd:duration using Apache xbean.jar and Weblogic weblogic ant target jwsc/wsdlc.

While the Apache translates it to org.apache.xmlbeans.GDuration, Weblogic translates it to java.lang.String. Since a String can be anything, it is recommended to use the toString() method in Java XML datatype API javax.xml.datatype.Duration to have consistent behaviour.

So, if there is a requirement to have both the xml beans, then create javax.xml.datatype.Duration using any of the methods present in javax.xml.datatype.DatatypeFactory and then convert it to the required formats.


Duration duration = DatatypeFactory.newInstance().newDuration(durationInMillis);
//for Apache
GDuration gDuration = new GDuration(1, duration.getYears(), duration.getMonths(), duration.getDays(),duration.getHours(),duration.getMinutes(),duration.getSeconds(), null);
//for Weblogic
String s = duration.toString();

Notice that the sign of the duration(positive or negative) is hard-coded as 1(positive). The getSign() method in javax.xml.datatype.Duration returns -1,0,+1. But the passing 0 to the GDuration(int,int,int,int,int,int,int,java.math.BigDecimal) constructor throws an IllegaArgumentException. Also, javax.xml.datatype.Duration does not have a method to return fraction part of the duration unlike the getFraction() method in org.apache.xmlbeans.GDuration.

To get the total number of milliseconds from javax.xml.datatype.Duration, we just need to call either getTimeInMillis(java.util.Calendar)
or getTimeInMillis(java.util.Date). However there is no equivalent API in org.apache.xmlbeans.GDuration.

To get the total number of milliseconds from org.apache.xmlbeans.GDuration, we need to do the following :


GDuration gDuration = new GDuration(durationString);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
GDate base = new GDate(cal);
GDate d = base.add(gDuration);
long durationInMillis = d.getDate().getTime();