?
Solved

ServletContext.getResource is not returning a directory under WebLogic 9.2

Posted on 2011-03-16
1
Medium Priority
?
1,189 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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