Solved

ServletContext.getResource is not returning a directory under WebLogic 9.2

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

WHY MVC is the future technology... As many of you know, MVC is Model,View,Controller pattern. Model View Controller pattern was invented in a Smalltalk context at Xerox in late 1970s. Why is this buzz about MVC: --------------------------- …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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