Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取对象并导出到其类之外_Java - Fatal编程技术网

Java 获取对象并导出到其类之外

Java 获取对象并导出到其类之外,java,Java,我有这个类,我想将在其中创建的对象httpdStatistics stats导出到它的外部(另一个类)。哪一种可能是最好的方法 package org.w3c.jigsaw.status ; import java.util.Date; import org.w3c.tools.resources.Attribute; import org.w3c.tools.resources.AttributeHolder; import org.w3c.tools.resources.Attribut

我有这个类,我想将在其中创建的对象
httpdStatistics stats
导出到它的外部(另一个类)。哪一种可能是最好的方法

package org.w3c.jigsaw.status ;

import java.util.Date;

import org.w3c.tools.resources.Attribute;
import org.w3c.tools.resources.AttributeHolder;
import org.w3c.tools.resources.AttributeRegistry;
import org.w3c.tools.resources.FramedResource;
import org.w3c.tools.resources.IntegerAttribute;
import org.w3c.tools.resources.Resource;

import org.w3c.tools.resources.store.ResourceStoreManager;

import org.w3c.www.http.HTTP;
import org.w3c.www.http.HttpMessage;

import org.w3c.jigsaw.frames.HTTPFrame;

import org.w3c.jigsaw.http.Reply;
import org.w3c.jigsaw.http.Request;
import org.w3c.jigsaw.http.httpd;
import org.w3c.jigsaw.http.httpdStatistics;

import org.w3c.jigsaw.html.HtmlGenerator;

/**
 * This class exports the server statistics.
 * It makes available a bunch of various parameters about the current
 * server, and uses the Refresh meta-tag (as the ThreadStat) to 
 * make them redisplay.
 * <p>This would benefit from being an applet.
 */

public class StatisticsFrame extends HTTPFrame {
    private static int REFRESH_DEFAULT = 5;

    /**
     * Attribute index - Our refresh interval.
     */
    protected static int ATTR_REFRESH = -1 ;

    static {
    Attribute a   = null ;
    Class     cls = null ;
    try {
        cls = Class.forName("org.w3c.jigsaw.status.StatisticsFrame");
    } catch (Exception ex) {
        ex.printStackTrace() ;
        System.exit(1) ;
    }
    // The refresh interval attribute:
    a = new IntegerAttribute("refresh"
                 , new Integer(5)
                 , Attribute.EDITABLE) ;
    ATTR_REFRESH = AttributeRegistry.registerAttribute(cls, a) ;
    }

    static String time_tbl = ("<table border=\"1\" class=\"time\">"
                  + "<caption>Request processing times"
                  + "</caption><tr>"
                  + "<th>min"
                  + "<th>avg"
                  + "<th>max"
                  + "</tr><tr>") ;

    static String dyn_time_tbl = ("<table border=\"1\" class=\"time\">"
                  + "<caption>Dynamic request processing times"
                  + "</caption><tr>"
                  + "<th>min"
                  + "<th>avg"
                  + "<th>max"
                  + "</tr><tr>") ;

    static String sta_time_tbl = ("<table border=\"1\" class=\"time\">"
                  + "<caption>Static request processing times"
                  + "</caption><tr>"
                  + "<th>min"
                  + "<th>avg"
                  + "<th>max"
                  + "</tr><tr>") ;

    public void registerResource(FramedResource resource) {
    super.registerOtherResource(resource);
    }

    /**
     * Get the current set of statistics.
     * Display the collected statistics in an HTML table.
     * @param request The request to process.
     */

    public Reply get (Request request) {
    HtmlGenerator g = new HtmlGenerator("Statistics") ;
    int refresh = getInt(ATTR_REFRESH, REFRESH_DEFAULT);
    if (refresh > 0) {
        g.addMeta("Refresh", Integer.toString(refresh));
    }
    addStyleSheet(g);
    // Dump the statistics:
    httpdStatistics stats = ((httpd)getServer()).getStatistics() ;
    // Uptime:
    g.append("<h1>Server Statistics</h1>");
    long start_time = stats.getStartTime();
    long uptime     = (System.currentTimeMillis() - start_time) / 1000;
    g.append("<p>Your server was started on <span class=\"date\">");
    g.append(new Date(start_time).toString());
    long duptime = uptime / (3600L*24L);
    long htemp   = uptime % (3600L*24L);
    long huptime = htemp / 3600L;
    long mtemp   = htemp % 3600L;
    long muptime = mtemp / 60L;
    long suptime = mtemp % 60L;
    g.append("</span>\n<p>It has now been running for <span "+
         "class=\"uptime\">");
    g.append(Long.toString(duptime));
    g.append(" days, ");
    g.append(Long.toString(huptime));
    g.append(" hours, ");
    g.append(Long.toString(muptime));
    g.append(" minutes and ");
    g.append(Long.toString(suptime));
    g.append(" seconds.</span>\n");
    // Hits and bytes:
    long  nb_hits      = stats.getHitCount();
    long  dyn_hits     = stats.getDynamicHitCount();
    long  static_hits  = stats.getStaticHitCount();
    float static_pcent = 0;
    float dyn_pcent    = 0;
    if (nb_hits > 0) {
        static_pcent = ((float) static_hits / (float) nb_hits) * 100;
        dyn_pcent = ((float) dyn_hits / (float) nb_hits) * 100;
    }
    g.append("<ul><li>hits: ", Long.toString(nb_hits));
    g.append("  <ul>\n    <li>static: ",Long.toString(static_hits));
    g.append(" (", Float.toString(static_pcent));
    g.append("%)</li>\n    <li>dynamic: ", Long.toString(dyn_hits));
    g.append(" (", Float.toString(dyn_pcent));
    g.append("%)</li>\n  </ul>\n");
    long bytes = stats.getEmittedBytes();
    long kbytes = bytes / 1024;
    long mbytes = kbytes / 1024;
    long gbytes = mbytes / 1024;
        long tbytes = gbytes / 1024;
    if (tbytes != 0) {
        g.append("</li>\n<li>bytes: ", Long.toString( tbytes),"TB, ");
        g.append(Long.toString(gbytes % 1024), "GB, ");
        g.append(Long.toString(mbytes % 1024), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(bytes % 1024));
    } else if (gbytes != 0) {
        g.append("</li>\n<li>bytes: ", Long.toString(gbytes), "GB, ");
        g.append(Long.toString(mbytes % 1024), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(bytes % 1024));
    } else if (mbytes != 0) {
        g.append("</li>\n<li>bytes: ", Long.toString(mbytes), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(bytes % 1024));
    } else if (kbytes != 0) {
        g.append("</li>\n<li>bytes: ", Long.toString(kbytes), "KB, ");
        g.append(Long.toString(bytes % 1024));
    } else {
        g.append("</li>\n<li>bytes: ", Long.toString(bytes));
    }
    // avg hit/sec
    float avghits = 0;
    float avghitsday = 0;
    if (uptime > 0) {
        avghits = ((float) nb_hits) / ((float) uptime);
        avghitsday = ((float) nb_hits * 86400) / ((float) uptime); 
    }
    g.append("</li>\n<li>Average hits per second: ");
    g.append(Float.toString(avghits));
    g.append("</li>\n<li>Average hits per day: ");
    g.append(Integer.toString((int) avghitsday));
    // avg bytes/hit
    long avgbph;

    if (nb_hits > 0) {
        avgbph = bytes / nb_hits;
    } else {
        avgbph = 0;
    }
    kbytes = avgbph / 1024;
    mbytes = kbytes / 1024;
    gbytes = mbytes / 1024;
    if (gbytes != 0) {
        g.append("</li>\n<li>Average bytes per hit: ");
        g.append(Long.toString(gbytes), "GB, ");
        g.append(Long.toString(mbytes % 1024), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(avgbph % 1024));
    } else if (mbytes != 0) {
        g.append("</li>\n<li>Average bytes per hit: ");
        g.append(Long.toString(mbytes), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(avgbph % 1024));     
    } else if (kbytes != 0) {
        g.append("</li>\n<li>Average bytes per hit: ");
        g.append(Long.toString(kbytes), "KB, ");
        g.append(Long.toString(avgbph % 1024));
    } else {
        g.append("</li>\n<li>Average bytes per hit: ",
             Long.toString(avgbph));
    }
    // avg throughput
    long avgbps = 0;
    if (uptime > 0) {
        avgbps = bytes / uptime;
    }
    kbytes = avgbps / 1024;
    mbytes = kbytes / 1024;
    gbytes = mbytes / 1024;
    if (gbytes != 0) {
        g.append("</li>\n<li>Average bytes per second: ");
        g.append(Long.toString(gbytes),  "GB, ");
        g.append(Long.toString(mbytes % 1024), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(avgbps % 1024));
    } else if (mbytes != 0) {
        g.append("</li>\n<li>Average bytes per second: ");
        g.append(Long.toString(mbytes), "MB, ");
        g.append(Long.toString(kbytes % 1024), "KB, ");
        g.append(Long.toString(avgbps % 1024));
    } else if (kbytes != 0) {
        g.append("</li>\n<li>Average bytes per second: ");
        g.append(Long.toString(kbytes), "KB, ");
        g.append(Long.toString(avgbps % 1024));
    } else {
        g.append("</li>\n<li>Average bytes per second: ", 
             Long.toString(avgbps));
    }    
    g.append("</li>\n</ul>");
    // Request times:
    g.append(time_tbl) ;
    g.append("<td>"
         , Long.toString(stats.getMinRequestTime())
         , " <span class=\"unit\">ms</span>") ;
    g.append("</td>\n<td>"
         , Long.toString(stats.getMeanRequestTime())
         , " <span class=\"unit\">ms</span>") ;
    g.append("</td>\n<td>"
         , Long.toString(stats.getMaxRequestTime())
         , " <span class=\"unit\">ms</span>") ;
    g.append("</td>\n</table>\n") ;

    // static
    if (static_hits>0) {
        g.append(sta_time_tbl) ;
        g.append("<td>"
             , Long.toString(stats.getMinStaticRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n<td>"
             , Long.toString(stats.getMeanStaticRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n<td>"
             , Long.toString(stats.getMaxStaticRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n</table>\n") ;
    }

    // dynamic 
    if (dyn_hits>0) {
        g.append(dyn_time_tbl) ;
        g.append("<td>"
             , Long.toString(stats.getMinDynamicRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n<td>"
             , Long.toString(stats.getMeanDynamicRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n<td>"
             , Long.toString(stats.getMaxDynamicRequestTime())
             , " <span class=\"unit\">ms</span>") ;
        g.append("</td>\n</table>\n") ;
    }

    // Get Server internal Stats
    try {
        g.append(((httpd)getServer()).getHTMLStatus());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Reply reply = request.makeReply(HTTP.OK) ;
    reply.setNoCache();
    reply.setStream (g) ;
    reply.setDynamic(true);
    return reply ;
    }
}
package org.w3c.jigsaw.status;
导入java.util.Date;
导入org.w3c.tools.resources.Attribute;
导入org.w3c.tools.resources.AttributeHolder;
导入org.w3c.tools.resources.AttributeRegistry;
导入org.w3c.tools.resources.FramedResource;
导入org.w3c.tools.resources.IntegerAttribute;
导入org.w3c.tools.resources.Resource;
导入org.w3c.tools.resources.store.ResourceStoreManager;
导入org.w3c.www.http.http;
导入org.w3c.www.http.HttpMessage;
导入org.w3c.jigsaw.frames.HTTPFrame;
导入org.w3c.jigsaw.http.Reply;
导入org.w3c.jigsaw.http.Request;
导入org.w3c.jigsaw.http.httpd;
导入org.w3c.jigsaw.http.httpdStatistics;
导入org.w3c.jigsaw.html.HtmlGenerator;
/**
*此类导出服务器统计信息。
*它提供了一系列关于电流的各种参数
*服务器,并使用刷新元标记(作为ThreadStat)来
*让他们重新发现。
*这将受益于小程序。
*/
公共类StatisticsFrame扩展了HTTPFrame{
私有静态int刷新_默认值=5;
/**
*属性索引-我们的刷新间隔。
*/
受保护的静态int ATTR_REFRESH=-1;
静止的{
属性a=null;
类别cls=null;
试一试{
cls=Class.forName(“org.w3c.jigsaw.status.StatisticsFrame”);
}捕获(例外情况除外){
例如printStackTrace();
系统出口(1);
}
//刷新间隔属性:
a=新的整数属性(“刷新”
,新整数(5)
,属性。可编辑);
ATTR_REFRESH=AttributeRegistry.registerAttribute(cls,a);
}
静态字符串时间\u tbl=(“”
+“请求处理时间”
+ ""
+“分钟”
+“平均值”
+“最大值”
+ "") ;
静态字符串动态时间tbl=(“”
+“动态请求处理时间”
+ ""
+“分钟”
+“平均值”
+“最大值”
+ "") ;
静态字符串sta_time_tbl=(“”
+“静态请求处理时间”
+ ""
+“分钟”
+“平均值”
+“最大值”
+ "") ;
公共无效注册表资源(FramedResource资源){
超级注册表其他资源(资源);
}
/**
*获取当前的统计数据集。
*在HTML表中显示收集的统计信息。
*@param请求处理请求。
*/
公共回复获取(请求){
HtmlGenerator g=新HtmlGenerator(“统计”);
int refresh=getInt(ATTR\u refresh,refresh\u DEFAULT);
如果(刷新>0){
g、 addMeta(“刷新”,Integer.toString(刷新));
}
添加样式表(g);
//转储统计信息:
httpdStatistics stats=((httpd)getServer()).getStatistics();
//正常运行时间:
g、 附加(“服务器统计数据”);
长启动时间=stats.getStartTime();
长正常运行时间=(System.currentTimeMillis()-开始时间)/1000;
g、 追加(“您的服务器已在上启动”);
g、 追加(新日期(开始时间).toString());
长时间=正常运行时间/(3600L*24L);
长htemp=正常运行时间%(3600L*24L);
长huptime=htemp/3600L;
长mtemp=htemp%3600L;
长时间=mtemp/60L;
长期支持时间=mtemp%60L;
g、 追加(“\n它现在已经运行了一段时间”);
g、 追加(Long.toString(duptime));
g、 附加(“天,”);
g、 追加(Long.toString(huptime));
g、 附加(“小时,”);
g、 追加(Long.toString(muptime));
g、 附加(“会议记录和”);
g、 追加(Long.toString(suptime));
g、 追加(“秒。\n”);
//点击数和字节数:
long nb_hits=stats.getHitCount();
long dyn_hits=stats.getDynamicCount();
long static_hits=stats.getStaticHitCount();
浮动静态系数=0;
浮点数=0;
如果(nb_命中>0){
静态点击率=((浮动)静态点击率/(浮动)nb点击率)*100;
动态百分比=((浮动)动态点击次数/(浮动)nb点击次数)*100;
}
g、 追加(“
  • 点击次数:”,Long.toString(nb_点击次数)); g、 追加(“
      \n
    • static:”,Long.toString(static_hits)); g、 追加(“(”,Float.toString(static_pcent)); g、 追加(“%)
    • \n
    • 动态:,Long.toString(dyn_hits)); g、 追加(“(”,Float.toString(dyn_pcent)); g、 附加(“%)
    • \n
    \n”); long bytes=stats.getEmittedBytes(); 长KB=字节/1024; 长MB=千字节/1024; 长GB=MB/1024; 长T字节=GB字节/1024; 如果(T字节!=0){ g、 追加(“
  • \n
  • 字节:”,长.toString(tbytes),“TB”); g、 追加(长.toString(GB字节%1024),“GB,”); g、 追加(长.toString(MB字节%1024),“MB,”; g、 追加(长.toString(KB%1024),“KB,”; g、 追加(长.toString(字节%1024)); }else if(GB!=0){ g、 追加(“
  • \n
  • 字节:”,长.toString(GB),“GB”); g、 追加(长.toString(MB字节%1024),“MB,”; g、 追加(长.toString(KB%1024),“KB,”; g、 追加(长.toString(字节%1024)); }否则如果(MB!=0){ g、 追加(“
  • \n
  • 字节:”,长.toString(MB),“MB”); g、 追加(长.toString(KB%1024),“KB,”; g、 追加(长.toString(字节%1024)); }else if(千字节!=0){ g、 追加(“
  • \n
  • 字节:”,长.toString(KB),“KB”); g、 追加(长.toString(字节%1024)); }否则{ g、 追加(“
  • \n
  • 字节:”,Long.toString(字节)); } //平均命中率/秒 浮动平均值=0; 浮动平均日=0; 如果(正常运行时间>0){ avghits=((浮动)nb_点击次数)/((浮动)正常运行时间); avghitsday=((浮动)nb_命中率*86400
    public httpdStatistics getStats() {
      return ((httpd)getServer()).getStatistics();
    }
    
    httpdStatistics stats = getStats();