[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ServletContext.getResource is not returning a directory under WebLogic 9.2

Posted on 2011-03-16
1
Medium Priority
?
1,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

650 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