Solved

ServletContext.getResource is not returning a directory under WebLogic 9.2

Posted on 2011-03-16
1
1,153 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
countHi challenge 25 84
topping2 challenge 13 58
wordcount challenge 11 74
Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack 7.0.0.23 or Above. The following steps a…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now