jsp预编译工具

作者:网络 来源:佚名 更新时间:2008-07-09 11:17:26 点击:

一个可以进行jsp预编译工作的程序. 下面是read.me 以及 java 源程序.
author: ugorji dick-nwoke [email protected]
date: jan 16. 2002

---------------------------------------------------
post server startup jsp file compiler and validator
---------------------------------------------------
this utility helps to
1. simulate a precompilation of jsp´s after the server
has already started up.
this way, we do not have to wait for all the jsp´s to be precompiled
before starting up the server.
2. test the validity of all the jsp´s. if any jsp does not compile, a
response code of 500 is returned. the tool lists that jsp, so it can
be worked on.

the list of jsp targets to be precompiled must be supplied in a plain
text file.

a sample is below:
#---------------------------------------------
/console/index.jsp
/console/standards/home.jsp
#---------------------------------------------

all the targets defined in there a hit sequentially, the response code
monitored and such information relayed to the user.

usage:
java testjspfiles <fname>

if you specify no parameters, this help will be displayed
at least one parameter, the filename should be specified

below simulates the default argument list (if no parameters are passed in)
jsp-pages.txt

if form based authentication is required, then send the username and parameters

system properties, passed using the -d flag, are used to set the argument
list. they are listed below:
host the hostname or ip address of the machine
port the port the server is listening on
debug if true, extra information like the actual output string
is written to std error stream
if authentication is required to view some of the pages, the following
will be required.
username the username to login as
password the password for that user
formauthtarget the j_security_check target. by default, it is
/j_security_check. it can be specified as the login
page may vary.

sample usage is below:
java
-ddebug=false
-dhost=localhost
-dport=7701
-dusername=system
-dpassword=system_password
-dformauthtarget=/console/login/j_security_check
testjspfiles
jsp-pages.txt

at the end, we tell you how many pages were ok (returned 200)
and how many were broken (including list of broken pages).
this will help you know which pages are broken immediately.

any pages already precompiled will not precompile again.
thus, there is no performance hit on running this script
multiple times.

**** note ****
the file passed should have the full path of pages to precompile
example scenario:
webapp context-path is /console
to precompile the jsp pages, index.jsp and standards/home.jsp in there
file should be as below
#---------------------------------------------
/console/index.jsp
/console/standards/home.jsp

#---------------------------------------------

enjoy. - ugorji [email protected]

// package weblogic.qa.taskmgr;

import java.util.*;
import java.io.*;
import java.net.*;


/**
* precompiles all the jsp and tell us which are broken
* <xmp>to see usage information: java weblogic.qa.taskmgr.testjspfiles</xmp>
* fname is the list of files with the paths of jsp files to precompile
* host and port are the server host and port respectively
*
* currently, this is only geared towards form-based authenticated sites
*
* @author ugorji dick-nwoke [email protected]
* @version 1.0, august 3, 2001
*/
public class testjspfiles
{
private static string help_message = null;

public boolean debug = false;
public list jspfiles;
public string host;
public int port;
public string username;
public string password;
public string cookiestring;
public string formauthtarget = "/j_security_check";

// set the help message
static {
string lsep = system.getproperty ("line.separator");
help_message =
"usage: " + lsep +
"java [-dhost=] [-dport=] [-dusername=] [-dpassword=] [-dformauthtarget=] testjspfiles <fname> " + lsep +
"defaults:" + lsep +
" -dhost=localhost" + lsep +
" -dport=80" + lsep +
" -dusername=" + lsep +
" -dpassword=" + lsep +
" -dformauthtarget=/j_security_check" + lsep +
" file should be of the form below:" + lsep +
"#---------------------------------------------" + lsep +
"/console/index.jsp" + lsep +
"/console/standards/home.jsp" + lsep +
"#---------------------------------------------" + lsep +
"";
}

public string tostring () {
string s = host + ":" + port + " [user/pass=" + username + "/" + password + "]";
return s;
}

public void run ()
throws exception
{
cookiestring = getcookiestring ();
if (debug) system.err.println (cookiestring);
list badfiles = new arraylist ();
list goodfiles = new arraylist ();
int numgood = 0;
int numbad = 0;

int sz = jspfiles.size ();
for (int i = 0; i < sz; i++) {
string file = null;
try {
file = (string) jspfiles.get (i);
int respcode = getresponsecode (file);
if (respcode == 200) {
system.out.println ("good: " + file + " --- got respcode " + respcode);
goodfiles.add (file);
numgood++;
}
else {
system.out.println ("error: " + file + " --- got respcode " + respcode);
badfiles.add (file);
numbad++;
}
}
catch (exception exc) {
system.out.println ("error: " + file + " --- got exception " + exc);
badfiles.add (file);
numbad++;
}
if (debug) {
system.err.println ("=============================================");
}
}

// output stats
system.out.println ("good files: " + numgood);
system.out.println ("bad files: " + numbad);
system.out.println ("---------------------------------------");
for (int i = 0; i < numbad; i++) {
system.out.println (badfiles.get (i) );
}

}

public int getresponsecode (string file)
throws exception
{
int respcode = -1;
socket s = new socket (host, port);
printwriter out = new printwriter( new outputstreamwriter( s.getoutputstream() ) );
string target = file + "?jsp_precompile=true";
out.print ("get " + target + " http/1.0" + " ");
out.print ("cookie: " + cookiestring + " ");
out.print ("connection: close ");
out.print ("host: " + host + " ");
out.print (" ");
out.flush ();
bufferedreader buffreader = new bufferedreader( new inputstreamreader( s.getinputstream() ) );
string line = buffreader.readline();
if( line != null ) {
stringtokenizer st = new stringtokenizer( line.trim() );
st.nexttoken();
respcode = integer.parseint (st.nexttoken());
}
while( (line = buffreader.readline()) != null ) {
if( line.trim().length() == 0 ) break;
}
stringbuffer buf = new stringbuffer ();
while( (line = buffreader.readline()) != null ) {
buf.append (line).append (" ");
}
if (debug) {
system.err.println (target);
system.err.println (respcode);
system.err.println (buf.tostring () );
}
buffreader.close ();
out.close ();
s.close ();
return respcode;
}

public string getcookiestring ()
throws exception
{
string cookieline = null;
string c = null;
socket s = new socket (host, port);
printwriter out = new printwriter( new outputstreamwriter( s.getoutputstream() ) );
string target = formauthtarget;
// out = new printwriter (system.out, true);
out.print ("post " + target + " http/1.0" + " ");
out.print ("connection: close ");
out.print ("host: " + host + " ");
out.print ("content-type: application/x-www-form-urlencoded " );
string poststring = "j_username=" + username + "&j_password=" + password;
out.print ("content-length: " + poststring.length() + " " );
out.print (" ");
out.print (poststring + " " );
out.print (" ");
out.flush ();
bufferedreader buffreader = new bufferedreader( new inputstreamreader( s.getinputstream() ) );
string line = null;
while( (line = buffreader.readline()) != null ) {
if (debug) system.err.println (line);
if( line.trim().length() == 0 ) break;
if( line.tolowercase().startswith( "set-cookie:" ) ) {
cookieline = line.substring( "set-cookie:".length() ).trim();
int semicolon = cookieline.indexof( ";" );
if( semicolon != -1 ) {
cookieline = cookieline.substring( 0, semicolon ).trim();
}
break;
}
}
buffreader.close ();
out.close ();
s.close ();
return cookieline;
}

public int getresponsecode0 (string file)
throws exception
{
httpurlconnection urlconn = null;
try {
string target = file + "?jsp_precompile=true";
url url = new url ("http", host, port, target);
urlconn = (httpurlconnection) url.openconnection ();
urlconn.connect ();
// inputstream is = urlconn.getinputstream ();
// while ( is.read (b) != -1 ) { }
int respcode = urlconn.getresponsecode ();
return respcode;
}
finally {
try { if (urlconn != null) urlconn.disconnect (); }
catch (exception exc2) { }
}
}

/** <xmp>usage: java weblogic.qa.taskmgr.testjspfiles <fname> <host> <port> </xmp> */
public static void main (string[] args)
throws exception
{
if (args.length == 0) {
system.out.println (help_message);
system.exit (0);
}

string fname = "jsp-pages.txt";
string thost = "localhost";
int tport = 80;
string tuser = null;
string tpass = null;
string tformauthtarget = "/j_security_check";
boolean tdebug = false;

string tmpstr = null;
if ( (tmpstr = system.getproperty ("host") ) != null)
thost = tmpstr;
if ( (tmpstr = system.getproperty ("port") ) != null)
tport = integer.parseint (tmpstr);
if ( (tmpstr = system.getproperty ("username") ) != null)
tuser = tmpstr;
if ( (tmpstr = system.getproperty ("password") ) != null)
tpass = tmpstr;
if ("true".equals (system.getproperty ("debug")) )
tdebug = true;
if ( (tmpstr = system.getproperty ("formauthtarget") ) != null)
tformauthtarget = tmpstr;

if (args.length > 0)
fname = args [0];
if (args.length > 1)
thost = args [1];
if (args.length > 2)
tport = integer.parseint (args [2]);
if (args.length > 3)
tuser = args [3];
if (args.length > 4)
tpass = args [4];

filereader fr = new filereader (fname);
bufferedreader br = new bufferedreader (fr);
list files = new arraylist ();
string line = null;
while ( (line = br.readline () ) != null ) {
line = line.trim ();
if (line.length() == 0 || line.startswith ("#") )
continue;
files.add (line);
}
fr.close ();
br.close ();

testjspfiles test = new testjspfiles ();
test.host = thost;
test.port = tport;
test.jspfiles = files;
test.username = tuser;
test.password = tpass;
test.formauthtarget = tformauthtarget;
test.debug = tdebug;

system.out.println ("file&: " + fname);
system.out.println ("testjspfiles: " + test);
test.run ();

}


}

# top level files
/console/index.jsp
/console/help.jsp

# other stuff
/webapp1/thankyou.jsp
/webapp2/thankyou2.jsp