⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletcontainer.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/ServletContainer.cpp,v $
 * $Date: 2003/05/13 18:42:19 $
 *
 Description:
	ServletContainer module.

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include "ServletContainer.h"

/*____________________________________________________________________________*\
 *
 Description:
	Documentation
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	ServletContainer

Description:
	The ServletContainer is the Pi3 object, which contains the ServletContext
within the JVM. It also maintains the HttpSessions, the Servlet instances
including their ServletConfig and the default JspFactory. In Pi3Web 2.0 only
one ServletContainer could exist. Thus, only one JVM could serve Servlet requests.

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>JvmNumber
<TD>-
<TD>Any number
<TD>Number of JVMs
<TD>1

<TR>
<TD>JvmOption
<TD>-
<TD>Quoted string
<TD>JVM startup option(s)
<TD>"-Djava.compiler=NONE"

<TR>
<TD>JvmLocalReferences
<TD>-
<TD>Any number
<TD>Number of local references
<TD>128

<TR>
<TD>JvmIgnoreUnrecognized
<TD>-
<TD>Empty string
<TD>Ignore invalid JVM options
<TD>""

<TR>
<TD>LogFile
<TD>-
<TD>Valid filename
<TD>Servlet log file
<TD>"../Logs/servlet.log"

<TR>
<TD>Expression
<TD>-
<TD>Valid Pi3Expression
<TD>Expression in log messages
<TD>"[$t|$P|$k|$T] "

<TR>
<TD>OpenMode
<TD>-
<TD>Either "a" or "c"
<TD>Mode to open log file
<TD>"a"

<TR>
<TD>DocBase
<TD>-
<TD>Valid path
<TD>Document root for web application
<TD>".\WebRoot2"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<H4>Description of Options</H4>
<H5>JvmNumber</H5>
The number of JVMs for this <TT>ServletContainer</TT>. Only 1 is valid in Pi3Web 2.0.

<H5>JvmOption</H5>
The value has to be a quoted string of format <TT>"key=value"</TT>.
The key and the value are forwarded as configuration options
to the JVM on startup. For possible values refer to the JDK
documentation. JvmOption could be set multiple times for a
list of options.

<H5>JvmLocalReferences</H5>
The number of local references maintained by the JVM. For possible
values refer to the JDK documentation. The default value is 16.
To less local references will be complained by the JVM in console
messages.

<H5>JvmIgnoreUnrecognized</H5>
If set, invalid options will be ignored by the JVM.
If not set, the JVM will complain unknown or invalid options.
If <TT>JvmIgnoreUnrecognized</TT> is set, the empty string
value must be there.

<H5>LogFile</H5>
If set, all Servlet log messages will be written to the given file.
If not set, logging will be disabled.

<H5>Expression</H5>
If set, the Expression will be evaluated and written in front of
any log message. This will only take place, if an Http object is
available, thus only valid at request time but not at startup or
shutdown time.

<H5>OpenMode</H5>
The file mode for opening the log file. If "a", new log messages will
be appended to the log. If "c" is set, a new logfile will be created
each time the server will be restarted.

<H5>
	DocBase
</H5>
The value will be used to physical resolve a URL path by the ServletContext.
So a web application stores all resources within the document base (the web
root of the application). This is also a security feature of the Servlet API.

Phase:
	Server startup and shutdown

Returns:
	PIAPI_COMPLETED, PIAPI_ERROR

Note:
Example:
	<PRE>
	&ltObject&gt
		Name ServletContainer
		Class ServletContainerClass
		JvmNumber 1
		JvmOption "-Djava.compiler=NONE"
		JvmOption "-verbose:jni"
		JvmOption "-Djava.class.path=servlet.jar;Pi3Servlet.jar"
		JvmOption "-Djava.library.path=."
		JvmLocalReferences 128
		JvmIgnoreUnrecognized ""
		LogFile "../Logs/servlet.log"
		Expression "[$t|$P|$k|$T] "
		OpenMode "a"
		DocBase ".\WebRoot2"
	&lt/Object&gt
	</PRE>
/*___+++HTMLDOC_END+++___*/
#endif


/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
int ServletContainer::LoadExpression( const char *pName, Pi3Expression **ppExpression,
	FnPi3Write *pFunctions, const char *pValue, PIOStrStream &os )	
	{
	assert( ppExpression );
	Pi3Expression *pExpression = *ppExpression;
	if ( pExpression )
		{
		os << "'" << pName << "' may only be specified once." << ends;
		CONFIG_ERR( Object(), os.str() );
		return 0;
		};
	Pi3String *pError = Pi3String_new( 0 );
	pExpression = Pi3Expression_new( pValue, pFunctions, pError );
	if ( !pExpression )
		{
		os << "Error parsing expression: " <<
			Pi3String_getPtr( pError ) << ends;
		CONFIG_ERR( Object(), os.str() );
		Pi3Expression_delete( pExpression );
		Pi3String_delete( pError );
		return 0;
		};
	Pi3String_delete( pError );
	*ppExpression = pExpression;
	return 1;
	};


int ServletContainer::Parameter( const char *pVariable, const char *pValue,
	const char *pWhere )
{
	assert( pVariable && pValue );
	PIOStrStream os;
	os << pWhere << "Servlet: ";

	if ( !PIUtil_stricmp( KEY_CONF_JVMOPTION, pVariable ) )
		{
		const char *opt = PIUtil_strdup((const char *)DeQuote( pValue ));
		options[iOptions++].optionString = (char *)opt;
		}
	else if ( !PIUtil_stricmp( KEY_CONF_EXPRESSION, pVariable ) )
		{
		LoadExpression( pVariable, &pExpr, NULL, pValue, os );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_OPENMODE, pVariable ) )
		{
		sOpenMode = pValue;
		}
	else if ( !PIUtil_stricmp( KEY_CONF_SERVLETLOGFILE, pVariable ) )
		{
		sLogfileName = (const char *)DeQuote( pValue );
		}
	else if ( !PIUtil_stricmp( KEY_CONF_JVMNUMBER, pVariable ) )
		{
		numJVM = atoi(pValue);
		// We currently support only 1 JVM
		if (numJVM > MAX_JVMNUMBER)
			{
			os << "Only " << MAX_JVMNUMBER <<
				" JVMs supported by the ServletContainer" << ends;
			CONFIG_WARN( Object(), os.str() );
			numJVM = MAX_JVMNUMBER;
			};
		}
	else if ( !PIUtil_stricmp( KEY_CONF_JVMIGNOREUNRECOGN, pVariable ) )
		{
		ignoreUnrecognized = 1;
		}
	else if ( !PIUtil_stricmp( KEY_CONF_JVMLOCALREFS, pVariable ) )
		{
		numLocalRefs = atoi(pValue);
		if (numLocalRefs > MAX_LOCAL_CAPACITY)
			{
			os << "Only " << MAX_LOCAL_CAPACITY <<
				" local references supported by the ServletContainer" << ends;
			CONFIG_WARN( Object(), os.str() );
			numLocalRefs = MAX_LOCAL_CAPACITY;
			};

		}
	else if ( !PIUtil_stricmp( KEY_CONF_DOCBASE, pVariable ) )
		{
		Pi3String *s = Pi3String_new("");
		HTTPCore_relativeToAbsolutePath( PIObject_getDB( pObject ), pValue, s );
		sDocBase = (const char *)s;
		Pi3String_delete(s);
		}
	else
		{
		os << "Unknown directive '" << pVariable <<	"'" << ends;
		CONFIG_ERR( Object(), os.str() );
		return 0;
		};

		return 1;
	}

ServletContainer::ServletContainer( PIObject *pObj, int iArgc, const char *ppArgv[] )
	:
	pObject( pObj ),
	iOK( 1 ),
	pServlets( PIDB_new( 0, "Servlets" )),
	jvm( 0 ),
	jSessionPool( 0 ),
	jJspFactory( 0 ),
	jContext( 0 ),
	options(),
	iOptions( 0 ),
	numJVM( 1 ),
	numLocalRefs( 16 ),
	ignoreUnrecognized( 0 ),
	sOpenMode( "a" ),
	logfile( PIPLATFORM_FD_INVALID ),
	sLogfileName( 0 ),
	pExpr( 0 ),
	sDocBase( 0 ),
	pStartupTID( PIThread_getSystemHandle( PIThread_getCurrent()))
{
	if ( !PIObject_readParameters( pObject, iArgc, ppArgv, ParameterFn,
		this ) )
		{
		iOK = 0;
		}
	else
		{
		iOK = 1;
		};

	if (!iOK) return;

	JavaVMInitArgs vm_args; /* JDK 1.2 VM initialization arguments */

	vm_args.version = JNI_VERSION_1_2;
	vm_args.options = options;
	vm_args.nOptions = iOptions;
	vm_args.ignoreUnrecognized = ignoreUnrecognized;

	PIOStrStream os;

	if (sLogfileName.Len()) {
		logfile = PIFile_open(sLogfileName, sOpenMode);
		if ( logfile == PIPLATFORM_FD_INVALID )
			{
			os << "ServletContainer: Could not open file '" << sLogfileName << 
				"', error '" << PIPlatform_getLastError() << "'" << ends;
			iOK = 0;
			};
	};

	int iRet = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
	if ( iRet ) {
		os << "ServletContainer: ErrorCode " << iRet <<
			" returned by 'JNI_CreateJavaVM'." << ends;
		iOK = 0;
	} else {
		if (createSessionPool( env )) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -