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

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