2011-09-15 1 views
3

J'ai écrit une applet Java qui teste la vitesse de la liaison utilisateur. Je rassemble un certain nombre d'informations sur l'ordinateur de l'utilisateur: la charge du processeur, la quantité de données téléchargées sur l'interface eth0 et la vitesse maximale de la carte NIC avec l'API SIGAR. Je déployé sur un serveur Web, mais lorsque je tente de l'exécuter je me suivais les informations d'erreur dans la console java:Les paramètres du système de lecture de l'applet Java avec l'API Sigar lève "AccessControlException: accès refusé"

java.lang.reflect.InvocationTargetException 
    at java.awt.EventQueue.invokeAndWait(Unknown Source) 
    at speedtester_pkg.AppletMain.init(AppletMain.java:80) 
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.ExceptionInInitializerError 
    at speedtester_pkg.Test.<init>(Test.java:54) 
    at speedtester_pkg.AppletMain.createGUI(AppletMain.java:282) 
    at speedtester_pkg.AppletMain$1.run(AppletMain.java:82) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission sigar.nativeLogging read) 
    at java.security.AccessControlContext.checkPermission(Unknown Source) 
    at java.security.AccessController.checkPermission(Unknown Source) 
    at java.lang.SecurityManager.checkPermission(Unknown Source) 
    at java.lang.SecurityManager.checkPropertyAccess(Unknown Source) 
    at java.lang.System.getProperty(Unknown Source) 
    at org.hyperic.sigar.Sigar.<clinit>(Sigar.java:78) 
    ... 17 more 

Je ne suis pas sûr que ce ne soit pas bloqué en raison de l'applet restrictions de sécurité, mais de l'utilisateur SIGAR forum (http://forums.hyperic.com/jiveforums/forum.jspa?forumID=2) J'ai l'impression, qu'il est possible de l'utiliser avec des applications web.

C'est ma classe qui utilise l'API Sigar:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package speedtester_pkg; 

import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.security.AccessController; 
import java.security.PrivilegedAction; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.hyperic.sigar.CpuPerc; 
import org.hyperic.sigar.NetInterfaceStat; 
import org.hyperic.sigar.Sigar; 
import org.hyperic.sigar.SigarException; 

/** 
* The logic of the speed test. Starts downloading threads and performs measurements. 
* 
* @author Karol Abramczyk 
*/ 
public class Test implements Runnable{ 

    private ArrayList <String> urlsValues; 
    private URL[] urls; 
    private File[] files; 
    private int stabilizationTimeSeconds, threadNo, allThreadsNo; 
    private boolean continueDownload; 
    public AppletMain applet; 
    private Sigar sigar; 
    private NetInterfaceStat nis; 
    private long downloadedDataOld, downloadedDataNew, uploadedDataOld, uploadedDataNew, dataDownloaded, 
      downloadingTime; 
    private long startTime, stopTime; 
    private double speed; 
// private String dir = "D:\\StraTJ_THD\\Abramczyk\\TestySpeedtestera\\"; 


    /** 
    * Creates new instance of the Test object 
    * @param gui the calling AppletGUI object 
    * @param urlsVal the list of urls to files to download 
    * @param time the time of the delay for line stabilization 
    */ 
    public Test(AppletMain applet, ArrayList<String> urlsVal, int time){ 
     try { 
      this.applet = applet; 
      this.urlsValues = urlsVal; 
      stabilizationTimeSeconds = time; 
      allThreadsNo = urlsVal.size(); 
      AccessController.doPrivileged(new PrivilegedAction() 
      { public Object run() 
       { 
       try { 
        sigar = new Sigar(); 
        nis = sigar.getNetInterfaceStat("eth0"); 
       } catch (SigarException ex) { 
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
       } 
       return null; 
       } 
      }); 


      urls = new URL[allThreadsNo]; 
      for (int j = 0; j < allThreadsNo; j++) { 
        urls[j] = new URL(urlsValues.get(j)); 
      } 
     } catch (MalformedURLException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    /** 
    * returns number of bytes downloaded so far 
    * @return number of bytes downloaded 
    */ 
    private long getDownloadedDataSize() 
    { 
     return nis.getRxBytes()*8; 
    } 

    /** 
    * returns number of bytes uploaded so far 
    * @return number of bytes uploaded 
    */ 
    private long getUploadedDataSize() 
    { 
     return nis.getTxBytes()*8; 
    } 

    /** 
    * saves data on the test start: time, downloaded and uploaded number of bytes 
    */ 
    private void getStartData() 
    { 
     downloadedDataOld = getDownloadedDataSize(); 
     uploadedDataOld = getUploadedDataSize(); 
     startTime = System.currentTimeMillis(); 
     System.out.println(nis.getRxBytes()); 
     System.out.println(downloadedDataOld); 
    } 

    /** 
    * saves dataon the test stop: time, downloaded and uploaded number of bytes 
    */ 
    private void getStopData() 
    { 
     stopTime = System.currentTimeMillis(); 
     downloadedDataNew = getDownloadedDataSize(); 
     uploadedDataNew = getUploadedDataSize(); 
    } 

    /** 
    * starts downloading all the files specified in input file 
    * @throws java.io.IOException 
    * @throws java.lang.InterruptedException 
    */ 
    public void startDownload() throws IOException, InterruptedException{ 
     continueDownload = true; 

     files = new File[allThreadsNo]; 
     for (int g = 0; g < allThreadsNo; g++) { 
      String name = "speedTest" + g + "_"; 
      files[g] = File.createTempFile(name, null); 
     } 

     Thread[] threads = new Thread[allThreadsNo]; 
     for(int i=0; i<allThreadsNo;i++) 
     { 
      threadNo = i; 
      threads[threadNo] = new Thread(new Download(this, threadNo)); 
      threads[threadNo].start(); 
     } 

    } 

    /** 
    * preforms mesurements of speed ralated values as speed, cpu load, nic card speed 
    * @throws org.hyperic.sigar.SigarException 
    * @throws java.lang.InterruptedException 
    */ 
    private void measure() throws SigarException, InterruptedException { 

     measureCPUload(); 

     measureLinkSpeed(); 

     System.out.println(dataDownloaded); 
     System.out.println(downloadingTime); 
     System.out.println(speed); 

     measureNICspeed(); 

    } 

    /** 
    * measures system CPU load and displays it in applet GUI 
    * @throws org.hyperic.sigar.SigarException 
    */ 
    private void measureCPUload() throws SigarException 
    { 
     CpuPerc cpuPerc = sigar.getCpuPerc(); 
     applet.setLabelProcessorLoad(CpuPerc.format(cpuPerc.getCombined())); 
     applet.setCpuLoad(CpuPerc.format(cpuPerc.getCombined())); 
    } 

    /** 
    * measures link speed as: 
    * (number_of_bytes_downloaded_at_the_end - number_of_bytes_downloaded_at_the_beginning)/test_time 
    * @throws org.hyperic.sigar.SigarException 
    */ 
    private void measureLinkSpeed() throws SigarException 
    { 
     nis = sigar.getNetInterfaceStat("eth0"); 
     getStopData(); 
     dataDownloaded = downloadedDataNew-downloadedDataOld; 
     downloadingTime = (stopTime - startTime)/1000; 
     speed = dataDownloaded/downloadingTime; 
     applet.setLabelLinkSpeed(formatSpeed(speed)); 
     applet.setSpeed(formatSpeed(speed)); 
    } 

    /** 
    * measures Network Card Interface speed 
    * @throws org.hyperic.sigar.SigarException 
    */ 
    private void measureNICspeed() throws SigarException 
    { 
     nis = sigar.getNetInterfaceStat("eth0"); 
     long nicSpeed = nis.getSpeed(); 
     applet.setLabelNIC(formatSpeed(nicSpeed)); 
     applet.setNICspeed(formatSpeed(nicSpeed)); 
    } 

    /** 
    * stops file download 
    */ 
    private void stopDownload() 
    { 
     continueDownload = false; 
    } 

    /** 
    * formats link speed to more readable format with units. For example "3.54324E7" 
    * would be formatted to "35.0 Mb/s" 
    * @param speed the link speed 
    * @return the more readable link speed with units 
    */ 
    public String formatSpeed(double speed) 
    { 
     double speedD; 
     int speedI; 

     if(speed>=1000000) 
     { 
      speedI = (int)speed/10000; 
      speedD = speedI/100; 
      return new String(speedD + " Mb/s"); 
     } 
     else if(speed<1000000 && speed>=1000) 
     { 
      speedI = (int)speed/10; 
      speedD = speedI/100; 
      return new String(speedD + " Kb/s"); 
     } 
     else 
     { 
      speedI = (int)speed*100; 
      speedD = speedI/100; 
      return new String(speedD + " b/s"); 
     } 

    } 

    /** 
    * starts the test thread 
    */ 
    public void run() { 
     try { 
      startDownload(); 
      getStartData(); 
      System.out.println("Sleep for " + stabilizationTimeSeconds + " seconds"); 
      Thread.sleep(stabilizationTimeSeconds * 1000); 
      System.out.println("Sleep finished"); 
      measure(); 
      stopDownload(); 
      Thread.sleep(1000 * files.length/2); 
      //   removeDownloadedFiles(); 
     } catch (SigarException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

    public URL[] getUrls() { 
     return urls; 
    } 

    public boolean isContinueDownload() { 
     return continueDownload; 
    } 

    public File[] getFiles() { 
     return files; 
    } 

    public int getStabilizationTimeSeconds() { 
     return stabilizationTimeSeconds; 
    } 

} 

Ceci est mon premier applet et je ne suis pas très familier avec les solutions Web, donc si quelqu'un pouvait me aider, je vous serais reconnaissant. J'ai chercheur à travers de nombreux sujets similaires, mais je n'ai pas trouvé de solution. J'ai essayé d'utiliser la méthode Priviliged() AccessCOntroller.do, mais cela n'a pas fonctionné aussi bien. J'ai signé mon fichier jar avec des classes d'applet. Je ne suis pas sûr si je devrais signer ces bibliothèques de jar, devrais-je? Mon fichier html est le suivant:

<HTML> 
<HEAD> 
    <TITLE>Speed Test</TITLE> 
</HEAD> 
<BODY> 


<P> 
<APPLET 
    code="speedtester_pkg.AppletMain" 
    archive="SpeedTester.jar,activation.jar,mail.jar,mailapi.jar,sigar.jar,smtp.jar" 
    width=440  
    height=600> 
</APPLET> 
</P> 

</BODY> 
</HTML> 

Répondre

3

L'applet doit être fiable pour faire certaines choses. Pour être approuvé, un applet (et les autres Jars dépendants) doit être:

  1. Signé numériquement (en utilisant un certificat de signature de code valide) par le développeur.
  2. OK par l'utilisateur final lorsque vous y êtes invité. Il est généralement avantageux de prévenir l'utilisateur de ce qui lui est demandé («cliquez sur OK à l'invite») et de la raison pour laquelle «l'applet peut fonctionner» avant de lancer une invite au visage.
+0

@ 1 C'est l'application Java Web Start. Est-il recommandé de solution pour de tels problèmes? Existe-t-il une solution qui me permet de conserver mon applet déployé sur une page Web? Est-ce que ça va m'aider si je viens de signer les pots dépendants et de les placer sur le serveur? – k4b

Questions connexes