File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Probem in executing static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Probem in executing static method" Watch "Probem in executing static method" New topic

Probem in executing static method

sabyasachi Gupta

Joined: Jan 06, 2004
Posts: 1
I have a static method like this which does some string manupulation:
* This method returns a String contained within the first available
* set of opening and closing braces taking care of nested braces and quotes
* so given the String
* @ADD(@MUL(4,6), @DIV(8,2)) it will return @MUL(4,6), @DIV(8,2)
private static String getEnclosedString(String argExp) throws Exception {
int beginPos = -1;
int lastBracePos = -1;
int currentPos = -1;
int depth = 0;
boolean inQuotes=false;
boolean inBraces=false;
while (currentPos < argExp.length()-1) {
char currentChar = argExp.charAt(currentPos);
if ( currentChar=='\'' &&
currentPos<(argExp.length()-1) &&
) {
inQuotes = !inQuotes;
if ( currentChar=='(' && !inQuotes) {
Log.msg("DEPTH : "+depth);
if (beginPos == -1 ) beginPos = currentPos;
inBraces = true;
if ( currentChar==')' && !inQuotes) {
if (depth==0) {
lastBracePos = currentPos;
inBraces = false;
}//while more characters in the expression
//if (depth!=0) throw new Exception("Mismatched braces in expression ["+argExp+"]");
if (depth!=0) throw new Exception(hcySecGlobal.ERROR_MESSAGES[hcySecGlobal.ERR_MISMATCHED_BRACES] +argExp);

return argExp.substring(beginPos+1, lastBracePos);

Now this method works fine for all strings like @members('LEVEL') etc. But sometimes this method throws exception at the line throw new Exception(hcySecGlobal.ERROR_MESSAGES[hcySecGlobal.ERR_MISMATCHED_BRACES] +argExp);
Can anybody please explain why this is happening? This is a part of a application server code. A typical string is like @members('LEVEL'). If i make a stadalone program and execute the method this works fine. Even when this method is called in application server this works fine and then suddenly it begins to throw exception for strings which it has previously executed properly. The only option remains is to restart the java application server(This is our own socket server). Then again it starts working fine. can this be due to some memory related problem?
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
If the exception shows a string with properly matched braces...well, I have to admit I didn't see anything obvious that would cause depth to not be == 0 at the end of the method.
If the comments are to be believed, why not simply say:

I agree. Here's the link:
subject: Probem in executing static method