2011-09-15 5 views
3

사용자 링크 속도를 테스트하는 Java 애플릿을 작성했습니다. 나는 시스템 정보 prom 사용자의 컴퓨터를 수집한다 : CPU로드, SIGAR API를 통한 eth0 인터페이스 및 NIC 카드 최대 속도에서 다운로드 된 데이터의 양. 나는 웹 서버에 배포하지만 난 그것을 실행하려고하면 나는 자바 콘솔에서 오류 정보를 다음 얻을 :이 보안 제한을 애플릿으로 인해 차단되지 않은 경우 확실하지 않다Sigar API로 시스템 애플릿을 읽는 Java 애플릿은 "AccessControlException : access denied"를 발생시킵니다.

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 

을하지만 SIGAR의 사용자로부터 포럼 (http://forums.hyperic.com/jiveforums/forum.jspa?forumID=2) 웹 응용 프로그램에서 사용할 수 있다는 인상을 받았습니다.

/* 
* 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; 
    } 

} 

이 내 첫 애플릿 나는 누군가가 나를 도울 수 있다면 있도록 웹 솔루션에 익숙하지 않다, 내가 감사하겠습니다 :

는 SIGAR API를 사용하여 내 클래스입니다. 나는 비슷한 주제로 수색 해왔다. 그러나 해결책을 찾지 못했다. 나는 AccessCOntroller.do Priviliged() 메서드를 사용하려고 시도했으나 제대로 작동하지 않았다. jar 파일에 애플릿 클래스로 서명했습니다. 그 항아리 도서관에 서명해야할지 모르겠다. 그렇지? 내 html 파일은 다음과 같습니다.

<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> 

답변

3

애플릿은 특정 작업을 수행 할 때 신뢰할 수 있어야합니다. 신뢰할 수 있으려면 애플릿 (및 다른 종속 Jar)이 다음과 같아야합니다.

  1. 개발자는 디지털 서명 (유효한 코드 서명 인증서 사용)해야합니다.
  2. 메시지가 표시되면 최종 사용자가 확인했습니다. 일반적으로 프롬프트를 던지기 전에 사용자에게 필요한 것이 무엇인지 (예 : '확인을 클릭하면 확인'을 클릭하십시오), 왜 (애플릿이 제대로 작동하는지) 미리 경고합니다.
+0

@ 1 Java Web Start 응용 프로그램입니다. 이러한 문제에 대해 권장되는 솔루션입니까? 웹 페이지에 애플릿을 배포 할 수있는 솔루션이 있습니까? 내가 단지 의존 항아리에 서명하고 그들을 서버에 놓는다면 도움이 될 것입니까? – k4b