Solved

getting top level directory structure with applet

Posted on 2000-04-13
29
232 Views
Last Modified: 2013-11-23
Is it possible for a JAVA applet, embedded in an HTML page with security privileges granted, to identify the drive letters for each top level directory?
Does anyone have source code I could cut and paste?  I'm pretty new to JAVA programming.  I know how to request/grant I/O privileges already.
Thanks in advance!!
Tony
0
Comment
Question by:boatful
  • 9
  • 8
  • 6
  • +3
29 Comments
 

Expert Comment

by:Ash_
ID: 2713779
I think in JDK 1.2 and up use java.io.File.listRoots() , I am not sure.
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2714538
Hi
Use FileSystemView class and from that class use method
abstract  File[] getRoots()
This returns all root partitians on the system.

Rajiv
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2714546
That is correct, Ash. The static method listRoots() was introduced in Java 1.2. It will return an array of File objects identifying the drives of the system. The method does not throw security exceptions. If a security manager exists and the user is denied access to a root directory then it will not be included in the result.

However, before Java 1.2 (and still since), the javax.swing.filechooser.FileSystemView class provides similar behavior in its getRoots() method. It also will return an array of File objects. To invoke it, you need to get the singleton FileSystemView object first by calling FileSystemView.getFileSystemView(). This method does not declare that it throws any exceptions either.

Best regards,
Jim Cakalic
0
 
LVL 3

Accepted Solution

by:
terajiv earned 100 total points
ID: 2714574
Hi boatful,

Here is ur code...

import javax.swing.*;
import javax.swing.filechooser.*;

import java.io.File;
import java.util.Hashtable;

/**
 * A convenience implementation of the FileView interface that
 * manages name, icon, traversable, and file type information.
 *
 * This this implemention will work well with file systems that use
 * "dot" extensions to indicate file type. For example: "picture.gif"
 * as a gif image.
 *
 * If the java.io.File ever contains some of this information, such as
 * file type, icon, and hidden file inforation, this implementation may
 * become obsolete. At minimum, it should be rewritten at that time to
 * use any new type information provided by java.io.File
 *
 * Example:
 *    JFileChooser chooser = new JFileChooser();
 *    fileView = new ExampleFileView();
 *    fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
 *    fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
 *    chooser.setFileView(fileView);
 *
 * @version 1.7 08/26/98
 * @author Jeff Dinkins
 */
public class ExampleFileView extends FileView {
    private Hashtable icons = new Hashtable(5);
    private Hashtable fileDescriptions = new Hashtable(5);
    private Hashtable typeDescriptions = new Hashtable(5);

    /**
     * The name of the file.  Do nothing special here. Let
     * the system file view handle this.
     * @see #setName
     * @see FileView#getName
     */
    public String getName(File f) {
      return null;
    }

    /**
     * Adds a human readable description of the file.
     */
    public void putDescription(File f, String fileDescription) {
      fileDescriptions.put(fileDescription, f);
    }

    /**
     * A human readable description of the file.
     *
     * @see FileView#getDescription
     */
    public String getDescription(File f) {
      return (String) fileDescriptions.get(f);
    };

    /**
     * Adds a human readable type description for files. Based on "dot"
     * extension strings, e.g: ".gif". Case is ignored.
     */
    public void putTypeDescription(String extension, String typeDescription) {
      typeDescriptions.put(typeDescription, extension);
    }

    /**
     * Adds a human readable type description for files of the type of
     * the passed in file. Based on "dot" extension strings, e.g: ".gif".
     * Case is ignored.
     */
    public void putTypeDescription(File f, String typeDescription) {
      putTypeDescription(getExtension(f), typeDescription);
    }

    /**
     * A human readable description of the type of the file.
     *
     * @see FileView#getTypeDescription
     */
    public String getTypeDescription(File f) {
      return (String) typeDescriptions.get(getExtension(f));
    }

    /**
     * Conveinience method that returnsa the "dot" extension for the
     * given file.
     */
    public String getExtension(File f) {
      String name = f.getName();
      if(name != null) {
          int extensionIndex = name.lastIndexOf('.');
          if(extensionIndex < 0) {
            return null;
          }
          return name.substring(extensionIndex+1).toLowerCase();
      }
      return null;
    }

    /**
     * Adds an icon based on the file type "dot" extension
     * string, e.g: ".gif". Case is ignored.
     */
    public void putIcon(String extension, Icon icon) {
      icons.put(extension, icon);
    }

    /**
     * Icon that reperesents this file. Default implementation returns
     * null. You might want to override this to return something more
     * interesting.
     *
     * @see FileView#getIcon
     */
    public Icon getIcon(File f) {
      Icon icon = null;
      String extension = getExtension(f);
      if(extension != null) {
          icon = (Icon) icons.get(extension);
      }
      return icon;
    }

    /**
     * Whether the file is hidden or not. This implementation returns
     * true if the filename starts with a "."
     *
     * @see FileView#isHidden
     */
    public Boolean isHidden(File f) {
      String name = f.getName();
      if(name != null && !name.equals("") && name.charAt(0) == '.') {
          return Boolean.TRUE;
      } else {
          return Boolean.FALSE;
      }
    };

    /**
     * Whether the directory is traversable or not. Generic implementation
     * returns true for all directories.
     *
     * You might want to subtype ExampleFileView to do somethimg more interesting,
     * such as recognize compound documents directories; in such a case you might
     * return a special icon for the diretory that makes it look like a regular
     * document, and return false for isTraversable to not allow users to
     * descend into the directory.
     *
     * @see FileView#isTraversable
     */
    public Boolean isTraversable(File f) {
      if(f.isDirectory()) {
          return Boolean.TRUE;
      } else {
          return Boolean.FALSE;
      }
    };

}

I have checked it its running fine... on Win NT machine...

Rajiv
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2714620
Huh? How does ExampleFileView help in getting the root directories of the system?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2714628
Here is the code for Java 1.2 (and up):

    import java.io.File;
    ...
    File[] roots = File.listRoots();

With the swing release prior to Java 1.2

    // maybe com.sun.java.swing?
    import javax.swing.filechooser.FileSystemView;
    ...
    File[] roots = FileSystemView.getFileSystemView().getRoots();

Jim
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2714913
Copy&Paste


    public File[] getRoots() {
      Vector rootsVector = new Vector();

      // Create the A: drive whether it is mounted or not
      WindowsFloppy floppy = new WindowsFloppy();
      rootsVector.addElement(floppy);

      // Run through all possible mount points and check
      // for their existance.
      for (char c = 'C'; c <= 'Z'; c++) {
          char device[] = {c, ':', '\\'};
          String deviceName = new String(device);
          File deviceFile = new File(deviceName);
          if (deviceFile != null && deviceFile.exists()) {
            rootsVector.addElement(deviceFile);
          }
      }
      File[] roots = new File[rootsVector.size()];
      rootsVector.copyInto(roots);
      return roots;
    }




    /**
     * Fake the floppy drive. There is no way to know whether
     * it is mounted or not, and doing a file.isDirectory or
     * file.exists() causes Windows to pop up the "Insert Floppy"
     * dialog. We therefore assume that A: is the floppy drive,
     * and force it to always return true for isDirectory()
     */
    class WindowsFloppy extends File {
      public WindowsFloppy() {
          super("A" + ":" + "\\");
      }

      public boolean isDirectory() {
          return true;
      };
    }
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2718827
Hi All,

Just Run the programe given above u will find everything what u want...

Ur all doubts will be cleared...

Rajiv
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2719778
heyhey,
that's a neat way of doing it!
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2722563
well ... swing sources :)
0
 

Author Comment

by:boatful
ID: 2724886
Heyhey,
This seems much more straightforward than Terajiv's answer.  I have yet to compile/test it.  I have 1 question:

Do I have to worry about the CD-ROM drive as well as the floppy?  I don't want folks to mistakenly get the impression they can write to it.

Thanks everyone for your advise on this one!!

Tony
0
 

Author Comment

by:boatful
ID: 2726138
I am nearly there I think...

Here's the source:

import java.io.File;
import java.applet.Applet;
import java.util.Vector;

public class getroots extends Applet
{

public File[] getRoots() {
Vector rootsVector = new Vector();

// Create the A: drive whether it is mounted or not
WindowsFloppy floppy = new WindowsFloppy();
rootsVector.addElement(floppy);

// Run through all possible mount points and check
// for their existance.
for (char c = 'C'; c <= 'Z'; c++) {
    char device[] = {c, ':', '\\'};
    String deviceName = new String(device);
    File deviceFile = new File(deviceName);
    if (deviceFile != null && deviceFile.exists()) {
rootsVector.addElement(deviceFile);
    }
}
File[] roots = new File[rootsVector.size()];
rootsVector.copyInto(roots);
return roots;
    }

    class WindowsFloppy extends File {
public WindowsFloppy() {
    super("A" + ":" + "\\");
}

public boolean isDirectory() {
    return true;
};
    }

}

Here's the HTML source:

<html>
<head>
<title>Get all Roots</title>
<applet code="getroots.class" name="top_dirs" width=1 height=1></applet>
</head>
<body>
<a href="javascript:alert(top_dirs)">Get all Roots</a>
</body>
</html>

Here's the response:

getroots[panel16,0,0,1x1,layout=java.awt.FlowLayout]

But how do I call getroots.class in order to get back the driveletter array?????

Tony
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2729139
Hey My Program Gives everything u want... See u need Roots and u get Everything including Roots...
Infact this is the Best Example for Getting roots... I have taken this example from java's Examples...
0
 

Author Comment

by:boatful
ID: 2736271
Terajiv,
It looks like in your answer code I need to first provide a file name.  I don't see how this helps me determine the top level directory structure of the client's system.
Unless you can explain how I can call this applet and in return get the root structure (i.e. A:,C:,D:,S:,etc) I will need to reject your answer.
Tony
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Expert Comment

by:terajiv
ID: 2739420
Hi boatful,

See this Program gets all available roots. i.e. available drives. along with that it gets files also those having a single dot in between.
Now if ur using applets then u can put one button clicking which will invoke another applet or if ur using browser then either on same Browser or new Browser. This u can invoke in actionPerformed method.

Dont u think its easy? the code is availble here so use it. If u want more codes u can always use Java sample codes. those r really good...

I hope ur doubt is cleared...

Have A Nice Time

Rajiv
0
 

Author Comment

by:boatful
ID: 2739721
Rajiv,
I really need more direction than:
"This u can invoke in actionPerformed method".

For example, in an HTML form:
<FORM>
<INPUT TYPE="BUTTON" VALUE="Get Root Heirarchy" OnClick="???????????????">
</FORM>

Please fill in the ??????????????????

Thanks,
Tony
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2740059
Ok. I'm a little slow but I finally figured out whats going on here with ExampleFileView. If you add the following method to the code that terajiv posted you should be able to compile and run the application.

    public static void main(String[] args) {
        JFrame frame = new JFrame("File chooser");
        JFileChooser chooser = new JFileChooser();
        FileView fileView = new ExampleFileView();
        chooser.setFileView(fileView);
        frame.getContentPane().add(chooser);
        frame.pack();
        frame.show();
    }

It will display a JFileChooser. When you drop drown the combobox labeled "Look in:", it will present a number of options including what appear to be the valid drives on the system. My combobox has A:, C:, and D:. Your contents will vary.

Our contention, terajiv, is that this behavior of JFileChooser has little or nothing to do with the code that you posted. I can get exactly the same functionality by removing the two lines above that reference a fileview. When I run the program, it works exactly the same.

The reason for this is that FileView is intended to be a hook into JFileChooser to allow you to provide icons and type descriptions for files and extensions that override those provided by the current look and feel. By definition, JFileChooser still uses the default L&F FileView implementation to get information it needs whenever the user-provided FileView methods return null.

In summary, the behavior of determining the root drives of the system is not provided by your ExampleFileView class but by JFileChooser. Your class is a way of customizing how JFileChooser will present that information. If boatful wants to determine this information without using a JFileChooser, he cannot do so using your class.

Best regards,
Jim Cakalic
0
 

Author Comment

by:boatful
ID: 2740470
Hi:
Are you saying the code could be as simple as:

public static void main(String[] args) {
        JFrame frame = new JFrame("File chooser");
        JFileChooser chooser = new JFileChooser();
       
        frame.getContentPane().add(chooser);
        frame.pack();
        frame.show();
    }

It would be ideal if the class returned an array of drive letters, such as [a:,b:,d:) etc.

Boatful (Tony Shaw)
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2741134
No, Tony. What I am saying is that JFileChooser knows how to determine the file system roots. This is the gist of my initial post on this topic. JFileChooser uses a concrete implementation of  javax.swing.filechooser.FileSystemView to, in the words of the jdk documentation, 'intuit as much OS specific file system information as possible'.

The main method I posted was simply to demonstrate how ExampleFileView was to be used and show that JFileChooser's ability to obtain file system roots is independent of that class. It is not what you want if you need to determine file system roots independent of any mechanism for interacting with the user (like JFileChooser). You can use any of the other several methods that have been described:

1) The code as posted by heyhey.
2) java.io.File.listRoots() as suggested by Ash (Java 1.2).
3) javax.swing.filechooser.FileSystemView.getRoots() as suggested by terajiv (requires swing).

As to how these might be called from JavaScript, someone more familiar with programming applets will have to help your there.

Best regards,
Jim Cakalic
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2741493
Hi boatful,
I told u my friend that I have taken this Example from Documents...

Anyway... U can also go for FileDialog Class this u can use it...

Gives the similar results...

Rajiv
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2743177
well - do we really need Swing to get list of available drives ? :)

probably somebody have enough time to go through the Swing sources and extract the appropriate piece of code (I just made a quick Copy&Paste)
0
 

Author Comment

by:boatful
ID: 2743272
Thanks All!

Again, my feeble brain prefers the cleanest/simplest implementation, by HEYHEY.

I'd like to use it, but again, when I call the applet:

<applet code="getroots.class" name="top_getDirs_applet" width=1 height=1></applet>

from a web page in JavaScript:

alert(top_getDirs_applet);

the applet returns:

getroots[panel16,0,0,1x1,layout=java.awt.FlowLayout]

not the drive letters themselves.

What do I need to do differently??

Tony
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2743431
>> alert(top_getDirs_applet);

you need to call method on that applet and analyze the result - not access the applet (top_getDirs_applet) directly.
0
 

Author Comment

by:boatful
ID: 2745298
Oh, yeah!
Sorry I wasn't thinking.  I will try again!
In the example above, it would be something like:
alert(new String(top_getDirs_applet.getRoots));

Tony
0
 

Author Comment

by:boatful
ID: 2746871
This gives a JavaScript error too, when I try calling top_getDirs_applet.getRoots

I probably need to amend the applet to first establish I/O privileges through the security manager.  I know how to do this, but I left it out up til now.
Tony
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2798696
So, Tony, did you have an answer to the problem you posted?
0
 

Author Comment

by:boatful
ID: 2799285
Jim,
I never tried compiling the answer from terajiv, as it seems to reach far beyond my limited needs.
I compiled HEYHEY's suggestion (see my comment of 4/18), but still didn't ever get it to return the drive letter names (see my comment of 4/25). Since then I've been too busy to re-compile it in a form which first establishes I/O privileges.  I'll put it on top of my todo list and post another comment...
Tony
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2803307
hey boatful.... try my example Why dont u try...??? Try it and see the result...
Ur all Ques. will be solved in that...

Rajiv
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2803309
hey boatful.... try my example Why dont u try...??? Try it and see the result...
Ur all Ques. will be solved in that...

Rajiv
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

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

12 Experts available now in Live!

Get 1:1 Help Now