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

  1. // demo.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.lang.reflect.*;
  12.  
  13.  
  14. public class demo
  15. {
  16.     public static void main (String[] args)
  17.     {
  18.         if (args.length < 2)
  19.         {
  20.             System.err.println("usage: demo <permissions file> <class> <arguments ...>");
  21.             return;
  22.         }
  23.  
  24.         try
  25.         {
  26.             System.setErr(new PrintStream(new FileOutputStream("errors.txt"), true));
  27.     
  28.             SampleLoader loader = new SampleLoader("loaded", args[0]);
  29.  
  30.             Class c = loader.loadClass(args[1], true);
  31.  
  32.             System.out.print(c);
  33.             PermissionDataSet pds = PolicyEngine.getPermissionsOfClass(c);
  34.             if (pds.isFullyTrusted())
  35.             {
  36.                 System.out.println(" is fully trusted");
  37.             }
  38.             else
  39.             {
  40.                 FileIOPermission fileperm = (FileIOPermission)pds.find(PermissionID.FILEIO);
  41.                 if (fileperm == null)
  42.                 {
  43.                     System.out.println(" cannot perform file i/o");
  44.                 }
  45.                 else
  46.                 {
  47.                     System.out.println(" can read these files: "+fileperm.getReadableFiles(true));
  48.                 }
  49.             }
  50.  
  51.             Class paramTypes[] = new Class[1];
  52.             paramTypes[0] = args.getClass();
  53.             Method m = c.getMethod("main",paramTypes);
  54.             
  55.             String[] newargs = new String[args.length-2];
  56.             System.arraycopy(args, 2, newargs, 0, newargs.length);
  57.             Object[] invokeargs = new Object[1];
  58.             invokeargs[0] = newargs;
  59.  
  60.             StandardSecurityManager.installStandardSecurity();
  61.  
  62.             m.invoke(null, invokeargs);
  63.         }
  64.         catch (Throwable e)
  65.         {
  66.             System.err.println("Unexpected error:");
  67.             e.printStackTrace();
  68.         }
  69.  
  70.         System.out.println("Errors have been redirected to 'errors.txt'.");
  71.     }
  72. }
  73.  
  74.