java - take an object and export outside of its class -
i have class , want export outside of (to class), object httpdstatistics stats
created in it. best way make this?
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; /** * class exports server statistics. * makes available bunch of various parameters current * server, , uses refresh meta-tag (as threadstat) * make them redisplay. * <p>this benefit being 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 = null ; class cls = null ; try { cls = class.forname("org.w3c.jigsaw.status.statisticsframe"); } catch (exception ex) { ex.printstacktrace() ; system.exit(1) ; } // refresh interval attribute: = 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); } /** * current set of statistics. * display collected statistics in html table. * @param request request process. */ public reply (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 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 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 been running <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 , "); g.append(long.tostring(suptime)); g.append(" seconds.</span>\n"); // hits , 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") ; } // 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 ; } }
right looks extracting data httpdstatistics
htmlgenerator
object, using build reply
. want make httpdstatistics
object accessible outside class, right?
i don't know happens inside getserver().getstatistics()
, add "getter" method return that? use getter internally in get()
, avoid code duplication. like:
public httpdstatistics getstats() { return ((httpd)getserver()).getstatistics(); }
and in get()
:
httpdstatistics stats = getstats();
Comments
Post a Comment