home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Security / loader / SampleLoader.java < prev   
Encoding:
Java Source  |  2000-05-04  |  3.1 KB  |  100 lines

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