Extended struts plug-in Interface

Source: Internet
Author: User

1. Use the <plugin> Mechanism

Use the newly added org. Apache. Struts. Action. plugin interface in struts1.1 to provide the <plugin> extension mechanism.
Any Java class can be used as your own plugin, as long as the class implements the plug-in interface mentioned above.

The plugin Interface contains two methods:
/**
* The Org. Apache. Struts. Action. plugin Interface
*/
Public interface plugin {
/**
* Receive notification that the specified sub-applicaiton is being
* Started up.
*
*/
Public void Init (actionservlet servlet, applicationconfig config)
Throws servletexception;

/**
* Receive notification that our owning sub-application is being
* Shut down.
*/
Public void destroy ();
}
When the struts application is started, actionservlet uses the init () method to instance every plugin. Struts supports one or more plugins. If your application uses multiple modules, it can also be extended to "sub-application module" (sub-Application: struts1.1 new feature, which will be introduced in future articles ). Once you implement the Struts PlugIn interface, you can put something to be initialized in the init () method. (This will be a very good place to "place" the database connection. Of course, you can also initialize the database connection through datasource .), For the destory () method, I think you must understand that it will be called at the end of your application. Here you can close your database connection initialized in the init () method.

2. Add your plugin in the struts-config.xml

This will be a very simple task. The following code:
<Plug-in
Classname = "com. ifreeway. RMS. v12.action. plugins. initdatasource"/>

[Document: programming Jakarta Struts]

Below I will give a simple example of using plugin to implement the connection pool.

Package com. ifreeway. RMS. v12.action. plugins;
Import java. Io. file;
Import java. util. List;
Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;
Import javax. servlet. servletexception;
Import org. Apache. Struts. Action. actionservlet;
Import org. Apache. Struts. Action. plugin;
Import org. Apache. Struts. config. moduleconfig;
Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. Input. saxbuilder;

Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

/**
* Class or interface discription
* @ Author $ Author: Jack $
* @ Version $ revision: 1.0 $ <br/>
* $ ID: initdatasource. Java 2003-5-7 10:03:31 Jack exp.
*/
Public class initdatasource implements plugin {
Static string contextfactory =
"Com. Sun. JNDI. RMI. Registry. registrycontextfactory ";
Private context CTX = NULL;
/**
* Commons logging instance.
*/
Private Static log = logfactory. getlog (initdatasource. Class );
/**
* Receive notification that the specified sub-applicaiton is being
* Started up.
*
*/
Public void Init (actionservlet servlet, moduleconfig config)
Throws servletexception {
Try {
// Parse WEB-INF/database. xml
String dbfile =
Servlet. getservletcontext (). getrealpath (
"/WEB-INF/database. xml ");

File file = new file (dbfile );

Saxbuilder builder = new saxbuilder ();

Document Doc = builder. Build (File );

Element rootelm = Doc. getrootelement ();
Element normalelm = rootelm. getchild ("host ");
Element dbuserelm = rootelm. getchild ("user ");

List dbuserlist = dbuserelm. getchildren ();
Element dbuser = NULL;

For (INT I = 0; I <dbuserlist. Size (); I ++ ){
Dbuser = (element) dbuserlist. Get (I );
Context CTX = new initialcontext ();
CTX. addtoenvironment (
Javax. Naming. Context. initial_context_factory,
Contextfactory );
// Drivermanager. setlogstream (system. Out); // to create more info
// For Technical Support
// Create a datasource with pooling
Com. inet. TDS. pdatasource DS = new COM. inet. TDS. pdatasource ();
DS. setservername (normalelm. getchildtext ("servername "));
DS. setdatabasename (dbuser. getattributevalue ("dbname "));
DS. setuser (dbuser. getattributevalue ("username "));
DS. setpassword (dbuser. getattributevalue ("password "));
DS. setlogintimeout (10 );
DS. setmaxpoolsize (6); // default 0
DS. setinitialpoolsize (5); // default 0
DS. setminpoolsize (3); // default 0
DS. setmaxidletime (300); // plexa default 600
DS. setdescription ("A Test Data Source ");
// Ds. setsqlserver7modus (false );
CTX. BIND (dbuser. getattributevalue ("jndiname"), DS );
}

} Catch (exception e ){
Log.info (E );
}
}
/**
* Receive notification that our owning sub-application is being
* Shut down.
*/
Public void destroy (){
Try {
If (CTX! = NULL ){
CTX. Close ();
CTX = NULL;
}
} Catch (namingexception ex ){
Ex. printstacktrace ();
}
}
}

[In this example, besides the commonly used jar in struts, it also includes: JDOM. Jar providerutil. Jar rmiregistry. Jar JNDI. jar]

The code for adding plugin to the struts-config.xml is the same as above.

You don't have to worry about the specific implementation of this example. You just need to understand the struts extension mechanism.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.