home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / Security / loader / SampleLoader.java < prev   
Encoding:
Java Source  |  1998-03-05  |  3.1 KB  |  98 lines

  1. // SampleLoader.java
  2. //
  3. // (C)Copyright 1997-1998 Microsoft Corporation, All rights reserved.
  4. //
  5.  
  6. import com.ms.security.*;
  7. import com.ms.security.permissions.*;
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11.  
  12. public class SampleLoader extends SecurityClassLoader
  13. {
  14.     String dir;
  15.  
  16.     PermissionSet perms;
  17.  
  18.  
  19.     public SampleLoader (String dir, String permfilename) throws IOException
  20.     {
  21.         if (!dir.endsWith(File.separator))
  22.             dir = dir.concat(File.separator);
  23.         this.dir = dir;
  24.  
  25.         if (permfilename == null)
  26.         {
  27.             // Populate the defaults with what is normally accessible to applets, plus
  28.             // the ability to read files from the current directory.
  29.             PermissionDataSet pds = com.ms.security.management.ZonePermissions.createDefaultPermissionSet();
  30.             FileIOPermission fileperm = (FileIOPermission)pds.find(PermissionID.FILEIO);
  31.             if (fileperm == null)
  32.                 fileperm = new FileIOPermission();
  33.             fileperm.addReadableFiles(System.getProperty("user.dir")+File.separator+"*", true);
  34.             perms = pds.toPermissionSet();
  35.         }
  36.         else
  37.         {
  38.             perms = decodePermissionsFromFile(permfilename);
  39.         }
  40.     }
  41.  
  42.     private static PermissionSet decodePermissionsFromFile (String filename) throws IOException
  43.     {
  44.         PermissionDataSet pds = new PermissionDataSet();
  45.         String fmt;
  46.         if (filename.toLowerCase().endsWith(".ini"))
  47.             fmt = EncodeFormats.INI;
  48.         else
  49.             fmt = EncodeFormats.ASN1;
  50.         String tag = pds.mapFormat(fmt);
  51.         if (tag == null)
  52.             throw new Error("PermissionDataSet does not support format \""+fmt+"\"");
  53.         if (!pds.decode(tag, new FileInputStream(filename)))
  54.             throw new Error("failed to decode permissions from "+filename);
  55.         return pds.toPermissionSet();
  56.     }
  57.  
  58.  
  59.     // ClassLoader methods
  60.  
  61.     protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException
  62.     {
  63.         Class c = findLoadedClass(name);
  64.         if (c == null)
  65.         {
  66.             try
  67.             {
  68.                 c = findSystemClass(name);
  69.             }
  70.             catch (ClassNotFoundException e)
  71.             {
  72.                 String filename = dir + name.replace('.', File.separatorChar) + ".class";
  73.                 File f = new File(filename);
  74.                 try
  75.                 {
  76.                     long len = f.length();
  77.                     byte[] data = new byte[(int)len];
  78.                     InputStream is = new FileInputStream(f);
  79.                     is.read(data);
  80.                     is.close();
  81.  
  82.                     c = defineClass(name, data, 0, data.length, perms, new URLPrincipal(new java.net.URL("file:"+dir)));
  83.                 }
  84.                 catch (IOException ioe)
  85.                 {
  86.                     throw new ClassNotFoundException(name+": "+ioe.toString());
  87.                 }
  88.             }
  89.         }
  90.  
  91.         if (resolve)
  92.             resolveClass(c);
  93.  
  94.         return c;
  95.     }
  96. }
  97.  
  98.