Solved

ServletContext.getResource is not returning a directory under WebLogic 9.2

Posted on 2011-03-16
1
1,169 Views
Last Modified: 2012-06-27
Under Tomcat, we initialize some values based on the contents of a directory contained in an unexploded WAR file.
To do this, the code gets a URL by issuing a servletContext.getResource("/myFolder") where myFolder is in the war file at the appropriate place.
With the URL, we do a "getContent", which returns an object with class "javax.naming.directory.DirContext", which can be used to get all the files in the directory.  All is fine in Tomcat.
When deployed under weblogic 9.2, the "getContent" returns a plain text object, which appears to be useless for our purposes.

Is there an appserver-independent way of getting a directory of files from an unexploded war file?

URL theFolderUrl = Z.context.getResource(tabCornerDir);
          if (theFolderUrl == null) return tabColorGifs;
          javax.naming.directory.DirContext jndd = (javax.naming.directory.DirContext)theFolderUrl.getContent();
//This last line produces a ClassCastException under weblogic
'weblogic.kernel.Default (self-tuning)' Exception = 
java.lang.ClassCastException: sun.net.www.content.text.PlainTextInputStream 
cannot be cast to javax.naming.directory.DirContext

Open in new window

0
Comment
Question by:RogueCar
1 Comment
 

Accepted Solution

by:
RogueCar earned 0 total points
ID: 35167227
Okay. With no answers forthcoming, I did some analysis and testing and discovered the following.
If the content type is plain text for a directory (as it is under weblogic), you can get the content of the url as an InputStream and read it.

I found that the InputStream returns a line (terminated by cr-lf) for each filename entry in the directory.

This turned out to be sufficient for my needs, so now the code parses the content in two ways:

1. as a directory under tomcat, and
2. as a plain text input stream under weblogic

I assume it is JRockit that is responsible for this rather odd content delivery, which could be considered a bug.

See the attached code snippet for details.
URLConnection uc = theFolderUrl.openConnection();
            Z.log.writeToLog(Z.log.DEBUG, "TD: resource type/length is:" + uc.getContentType() + "/" + uc.getContentLength());
            InputStream is = uc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            int fileCount = 0;
            while (true) {
              try {
                String fileName = br.readLine();
                fileCount++;
                //Z.log.writeToLog(Z.log.INFO, "TD: collected filename=" + fileName);
                if (fileName == null || fileCount > 500 ) break;
                theGifs.add(fileName);
              } catch(Exception e) {
                ErrorWriter.write(e, ErrorWriter.LOG);
                break;
              }
            }

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
Protect jar file - windows app 2 64
servlet example 11 49
tomcat administrtor 12 58
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question