[Software Architect Series Tutorial - 4] Application of Software Architecture Mode in J2EE

xiaoxiao2021-03-06  45

This article describes the basic theory of background and architectural mode generated by the software architecture. Key points to introduce the structure, implementation, advantages and disadvantages of the pipeline system architecture mode, and then analyze it as an example to apply this architect mode as an example, and finally expound in other J2EE applications (JBoss and Axis) in other J2EE applications as an example. Practice. Software System Architecture 1. Software architecture generates the background after the software crisis in the 1960s, enabling people to pay attention to the research of software engineering. Software experts from different applications summarize a large number of valuable knowledge. At the beginning, people put the focus of software design on the selection of data structures and algorithms, such as Knuth proposed data structure algorithm = program. However, with the size of the software system is getting bigger and more complex, the structure of the software system is increasingly important. The extent of the software crisis is increasingly, and the existing software engineering method is distinct to this. For large-scale complex software systems, the software architecture has become more important than the algorithm and data structure of the program. In this context, people recognize the importance of the software architecture and believe that the software architecture system, in-depth research will become the most promising way to improve software production efficiency and software crisis. At this time, the research on the software architecture is like the spring bamboo shoots, and there is a phenomenon of homing, such as Rational Company proposes "Architecture-centric" unified software development process (RUP). 2. A core problem of software architecture mode software design is to use a repeated architecture, that is, the software reuse can reach the architectural level. That is, it is possible to use the same architecture in different software systems. Based on this purpose, many scholars began research and practical software architecture model issues. 8 architectural modes in the Layers, Pipes and Filters, Black Boards, Agents ( Broker, Model - View-Controller, Indicated - Abstract-Abstract-Control, Microconuclear (Microkernel), and Reflection. J2EE System Architecture At present, J2EE technology has become the preferred platform for enterprise-class applications, and more and more software systems constructed based on J2EE technology. J2EE represents the advanced software architecture idea, many software architectural models are widely used in J2EE, and will introduce each architectural mode in J2EE from this article. Pipes and Filters 1, outlined pipes and filter architecture modes are a model provided for processing data streams. It consists of filters and pipes. Each processing step is packaged in a filter assembly, and data is transmitted through a pipe between adjacent filters. Each filter can be modified separately, and the order between them can be configured. The following figure is a schematic diagram of the pipe / filter mode. An example of a typical pipeline / filter architecture is a program written in UNIX shell. Unix provides both a symbol to connect each component (UNIX process), but also provides a process runtime mechanism to implement pipe. Another famous example is a traditional compiler. The traditional compiler has been considered a pipe system, in which the output of a phase (including the speech analysis, grammar analysis, semantic analysis, and code generation) is another phase of input.

2, the problem, if you are developing a system that must handle or convert input data streams. It is not easy to implement such a system as a single component. There are several reasons: the system must be collaborated by several developers, and the entire system task is naturally broken down into several processing phases, and the demand is easy to change. So you have to plan for future flexibility through replacement or reorder processing steps. By adding such flexibility, it is possible to use existing processing components. The design of the system, especially the internal connection of the processing step, must consider the following factors: Future system upgrades By replacing certain processing steps, or restructuring steps. Different contexts are more likely to reuse than large components. Unconnected processing steps are unable to share information. There are different input data sources, and the final result can be output or stored in a variety of ways. 3. Solutions and structural pipes and filter architecture patterns divide system tasks into several independent processing steps. These steps are connected by a system stream connection. The output of a step is the input to the next step. Each processing step is implemented by a filter component, which handles or transforms data, and the input of the system can be a variety of data sources. This architecture mode has many characteristics, as follows: The filter is running independently. In addition to input and output, each filter is not affected by any other filter. In design, the filter is not Share any status information. Independence is also manifested in its upstream and downstream connections of its processing. Its designs and use does not exert restrictions on any filter connected to it, and the only concern is its input data, and then processed Processing, finally generating data output. 4, non-software description system based on various fluid work, generally adopting a processing structure connected by a conveying pipeline. For example, in the heating system we see in winter, the processor includes heaters, filters, regulating valves, flow meters, etc. Each processor has fluid inlets and outlets, which are connected together through various pipes. Such a structure can also be seen in the city's tap water system. See below: 5, Advantages and Disadvantages 5.1 Advantages Addition by using filter exchange Add flexibility to increase the rapid prototypes of the reuse of the flexible filter assembly by recombine 5.2 Disadvantages Sharing status information or expensive or unfair data Convert extra overhead. Error Handling Servlet2.3 Filter 1. Servlet Filter Overview All people who have developed J2EE's Web Application know that the following cases often need to access the identity authentication application level when accessing a specific resource (web page, JSP page, servlet) The access resource audit and records of the application range to the resource encrypted, which is built on the customized encryption scheme, and the timely conversion of the accessed resource, including the dynamic output from servlet and JSP in servlet2.3. It is difficult to implement, but the Java Servlet 2.3 specification has added a lot of exciting features, one of which is filter, in fact, the application we said in J2EE in J2EE, which is the application of pipeline and filter architecture in J2EE. Practice. By using this mode, the Web Application Developer can cut the request before the request is reached, and the response will be modified after processing the request. The structural diagram is as follows:

A Java class that performs a filter must implement a Javax.Servlet.Filter interface. This interface contains three methods: init (filterConfig): This is the initialization method called by the container. It guarantees that the container is called before the first DOFILTER () call is called. It can get the Filter initialization parameters specified in the web.xml file. DOFILTER (ServletRequest, servletResponse, filterchain): This is a method of completing filtering behavior. It is also a method of the previous filter. The introduced Filterchain object provides information to be called by the subsequent filters. DESTROY (): The container is called before the Filter instance is destroyed, and all activities in DOFILTER () are called after the instance is terminated. 2, the Filter chain introduces all the filters obey the calling filter chain and is executed by defining a clear interface. WebApplication can specify a number of filters to complete the relevant work. Then they make up a filter chain to complete the work. The structure is as follows:

3, Example 3.1 Simple Filter There are two Filter filters in PetStore 1.3.1. One filter, the transformation of the coding of the character set, if you often encounter Chinese characters, you only need to configure it to GBK Can. It reads the configuration information of these parameters from web.xml, and then performs encoding. The other is a security check fliter, which is responsible for security check which pages can be performed, which cannot be. They make up a Filter chain, structural diagram, and implementation code as follows:

public class EncodingFilter implements Filter {private FilterConfig config = null; // default to ASCII private String targetEncoding = "ASCII"; public void init (FilterConfig config) throws ServletException {this.targetEncoding = config.getInitParameter ( "encoding");} / / filter implemented in the character set encoding conversion public void doFilter (ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) srequest; request.setCharacterEncoding (targetEncoding); // move on to the next chain.doFilter (srequest, sresponse);} public void destroy () {............... ..}} public class SignOnFilter implements Filter {public void init (FilterConfig config) throws ServletException {this.config = config; URL protectedResourcesURL = null; try {protectedresourcesurl = config.getServletContext (). getresource ("/ web-inf / signon-config.xml); ...............} catch (java.net. Malformedurlexception ex) {system.out.println ("Signonfilter: Ma lformed URL exception: " ex);}} public void destroy () {config = null;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {........}} The container parses the filter configuration information through the configuration descriptor web.xml file in the web application. There are two new tags related to the filter: and . Tag is a filter definition that must have a and sub-elements. child elements give a name related to the filter instance. Specifies the implementation class loaded by the container. You can freely include a sub-element provides an initialization parameter for a filter instance.

The tag represents a filter mapping, specifying the subset of the filter that generates its role.

Encodingfilter Encoding filter no description com.sun.j2ee.blueprints.encodingfilter.web.encodingfilter Encoding UTF-8 signonfilter Signon filter no description com.sun.j2ee.blueprints.signon.web.signonfilter encodingfilter / * signonfilter / * 3.2 The complex Filter is an example of PetStore,

Demonstrate the functionality of modifying character encoding and secure authentication via fliter. One will provide an example demonstration by modifying the return data (transfers the Response string to capital).

public class UCaseResponse extends HttpServletResponseWrapper {public UCaseResponse (HttpServletResponse response) {super (response);} public PrintWriter getWriter () throws IOException {return new UCaseWriter (super.getWriter ());}} public class UCaseWriter extends PrintWriter {public UCaseWriter (Writer Out) {super (out); public void write (int C) {super.write (character.touppercase ((char) c));} public void write (Char buf [], int off, int LEN) {for (INT i = 0; I

Designing and implementing filter design Each Filter has an independent feature such as encoding transformation, security check, and so on. And each fliter should implement a Javax.Servlet.Filter interface. The deployment of the processing pipeline filter is to configure in Web.xml, describe the implementation class of the filter and their MAP relationships to determine their order. Other Application Example 1, JBoss If you know the EJB, you should know that the customer calls EJB actually not directly reference the EJB instance (EJB Instance). It intercepts the client's request through the container, then follow the EJB descriptor to do many of the work Such as, safety check, transaction processing, thread concurrent processing, etc. You can make developers only care about your business logic, do not need to be implemented for complex basic services. Make developers from cumbersome work. Concentrately handle your business logic, its structure diagram is as follows: I am fortunate to read JBoss source code, analyze the implementation of JBoss EJB containers. EJB container is implemented by many interceptors, each interceptor process must The function is forwarded to the next interceptor after an end, and the last interceptor will truly call the EJB instance. The structure of its EntityBean container is as follows:

We look at the LOG interceptor

public class LogInterceptor extends AbstractInterceptor {public Object invoke (Invocation invocation) throws Exception {boolean trace = log.isTraceEnabled (); // Log call details if (callLogging) {......} // for log handling process ends To forward the request to the next interceptor Return getNext (). Invoke (invocation); these interceptors are configured in the StandardJboss.xml file, as follows:

org.jboss.ejb.plugins.LogInterceptor org.jboss.ejb.plugins.SecurityInterceptor org.jboss.ejb.plugins.TxInterceptorCMT < / interceptor> org.jboss.ejb.plugins.MetricsInterceptor org.jboss.ejb.plugins.EntityLockInterceptor org.jboss.ejb.plugins .EntityInstanceInterceptor org.jboss.resource.connectionmanager.CachedConnectionInterceptor org.jboss.ejb.plugins.EntitySynchronizationInterceptor org.jboss.ejb.plugins.cmp .jdbc.jdbcrelationInterceptor This is the most clever place in the JBoss container architecture. The initial architecture is proposed by the geniors of the Juvenile Rickard Oberg. In fact, this architecture applies our discussion pipes and filter mode, In fact, each interceptor is a filter that brought JBoss to JBoss: Make the system architecture easier to understand because each filter completes a single function. Make the system more modular, facilitating the system's module reuse and extension, if you want to add a feature to add an interceptor interface that implements the Interceptor interface, then configure it in the StandardJboss.xml file. Easy to handle the system, if an error (ERROR) or exception is found in an interceptor, simply return. 2, AXIS is in a couple, and the pipeline and filter mode are also applied to AXIS .aixs Is an open source of Apache implementation servers. Simply put, AXIS is to process Message, which first intercepts the client's request, then forwards to the real implementation of the service logic to process the client's request, after which a series of Handler is processed. It is very similar to the EJB container. In fact, The application of the pipe and filter mode, Handler is a filter. Its processing order mainly considers the two aspects to deploy the descriptor (Deployment configuration) The other is the client or the server.

The object of Handler is MersSageContext, which consists of three important parts, one is a Request Message, one is response message, there are many properties. We have studied the source code analysis, on the server side, there is a Transport Listener who listens to the client's request, can pass a variety of protocols, once there is a customer request, it will generate a Message object according to the protocol, and then set it Go to MessageContext, then call a series of Handler for processing. Its structural diagrams are as follows: Related Design Mode When using pipes and filter modes, the following GOF design mode is usually used. 1. The intent of the CHAIN ​​OF RESPONSIBILITY duties is to cause multiple objects to handle requests, thereby avoiding coupling relationships between requesting senders and recipients. Connect these objects into a chain and deliver the request along this chain until there is an object to process it. In fact, pipes and filter modes are abstraction of the duties chain mode and apply it to the software architecture. 2, command mode The intent of the command mode will be packaged into an object, so that you can use different requests to parameterize the customer; queuing or logging the request log, and supports the revocable operation. Each filter generally uses a command mode in the pipe and filter mode to encapsulate the request to a command. 3, Decorator) The intent of decorative model is dynamically add some additional responsibilities to an object. In terms of increased function, Decorator mode is more flexible compared to generated subclasses. In the pipe and filter mode, the request is often required in each filter, or modify the request, which is usually used. If the decorative mode is used. Such as servlet filter Javax.Servlet.http.httpservletRequestWrapper, Javax.Servlet.http.httpservletResponseWrapper is the application of decorative mode. Summary This article discusses the solution and filter mode solution, and its advantages and disadvant, then use the J2EE specification Servlet Filter to describe how to apply this mode, at the same time Simply introduced in JBoss, AXIS.

转载请注明原文地址:https://www.9cbs.com/read-57771.html

New Post(0)