|
In this issue
This issue covers:
The Using Annotations in Web Applications tip was developed using the NetBeans IDE and the Java EE 5 SDK. NetBeans IDE 5.5 with the NetBeans Enterprise Pack 5.5 is available in Java EE 5 SDK Update 2, which you can download from the Java EE Downloads page. The How to Get the Best Performance Out of a JPA Implementation tip was developed using an open source reference implementation of Java EE 5 called GlassFish. You can download GlassFish from the GlassFish Community Downloads page. You can download the sample archive for the tip Using Annotations in Web Applications. You can download the sample archive for the tip How to Get the Best Performance Out of a JPA Implementation. Any use of this code and/or information below is subject to the license terms.
Java EE 5 introduced annotations, a way to simplify the
development and configuration of enterprise applications. The
March 31, 2007 Tech Tip Using Security Annotations in Enterprise Beans showed how you can use annotations to simplify the development
of secure applications that use enterprise beans. Some of the
annotations in Java EE 5 are specific to web applications.
Examples of this type of annotation are Although you can specify annotations in various web application components, you can't specify an annotation in a JavaServer Pages (JSP) technology page. However, annotations are supported in web libraries. This means that you can specify annotations in tag libraries and servlet filters, among other places. This gives you a way of securing JSP pages and servlets, provided that you take one additional action: you need to specify appropriate security information in the web.xml file for the web application. This tip shows you how to construct a secure web application that includes servlets and JSP pages. By configuring the web.xml file for the web application, you can protect the servlets and JSP pages over a Secure Sockets Layer connection. This is a common security approach in a web production environment. A sample web application package accompanies the tip. The code examples in the tip are taken from the source code of the sample (which is included in the package). The Servlet Let's begin by examining the servlet for the web application. In this example, the servlet invokes an enterprise bean (in this case, a stateless session bean) with a local interface SlessLocal. The servlet passes a message object as a request attribute and then forwards it to a JSP page, display.jsp, for display. Here is the major part of the code for the servlet: @DeclareRoles({"arole"}) @RunAs("myrole") public class TestServlet extends HttpServlet { private @EJB SlessLocal slessLocal; public void service( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.isUserInRole("arole")) { String message = slessLocal.hello("World"); req.setAttribute("EJB_MESSAGE", message); } RequestDispatcher rd = req.getRequestDispatcher("display.jsp"); rd.forward(req, resp); } }The The The Enterprise Bean Here is the major part of the code for the stateless session bean: @Stateless @Local({SlessLocal.class}) public class SlessBean implements SlessLocal { @RolesAllowed(value={"myrole"}) public String hello(String message) { return "Hello, " + message + ", " + new Date(); } }
Notice that the hello method is protected through the use of
the The JSP and Tag Library
The JSP page, display.jsp, invokes an action in a JSP tag
library and passes the attribute <%@taglib prefix="di" uri="http://java.sun.com/techtip/webann/test-taglib"%> ... <di:displayInfo ejbMessage="${requestScope.EJB_MESSAGE}"/> ...The tag handler for the tag reads the value of the ejbMessage parameter and displays it in HTML. It also prints the login timeout for the DataSource. Here is the pertinent code in the tag handler: public class DisplayInfoTagHandler extends SimpleTagSupport { private @Resource(name="jdbc/__default") DataSource ds; ... public void doTag() throws JspException, IOException { try { JspWriter out = getJspContext().getOut(); int timeout = ds.getLoginTimeout(); if (ejbMessage != null && ejbMessage.length() > 0) { out.println( "<li> Ejb Message: " + ejbMessage); } out.println( "<li> DataSource login timeout: " + timeout); ... } public void setEjbMessage(String ejbMessage) { this.ejbMessage = ejbMessage; } }
The Notice too that the tag handler defines a setter method for the parameter ejbMessage. Securing the Servlet and JSP page This example takes the approach of protecting the war file for the application by requiring a username and password and by using SSL in the transport layer. The username and password is sent in clear text over the wire. There is no need to modify any Java code or JSP page in the application. To secure the servlet and JSP page in the application all you need to do is configure the web.xml file as follows:
Here is a snippet of the web.xml file: <security-constraint> < web-resource-collection> <web-resource-name>Servlet Application </web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>ttrole</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL </transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>default</realm-name> </login-config> <security-role> <role-name>ttrole</role-name> </security-role>In this example, only users of role "ttrole" can access the servlet and JSP page. Furthermore, only users who are also of role "arole" will cause the SlessLocal bean to be called. The Java EE environment uses roles for authorization. However in many operating system environments, users are associated with groups. The security-role-mapping provides a link between the concepts of user roles and principals/groups. In a Java EE 5 application server implementation such as the Sun Java System Application Server in the Java EE 5 SDK, you define the security-role-mapping in the sun-application.xml file. Here is an example: <sun-application> <security-role-mapping> <role-name>myrole</role-name> <principal-name>myuser</principal-name> </security-role-mapping> <security-role-mapping> <role-name>ttrole</role-name> <group-name>ttgroup</group-name> </security-role-mapping> <security-role-mapping> <role-name>arole</role-name> <principal-name>ttuser</principal-name> </security-role-mapping> </sun-application>Running the Sample Code
When you finish the application, you can undeploy it and remove the users you created as follows:
Shing Wai Chan is a member of the Java EE development team at Sun Microsystems. He has been a key contributor in Java EE security for the past few years.
The Java Persistence API, or simply Java Persistence, introduced in the Enterprise JavaBeans (EJB) 3.0 specification (JSR 220), simplifies the development of Java EE applications that use data persistence. Earlier Tech Tips, such as Converting a POJO to a Persistent Entity,Inheritance and the Java Persistence API,and Using Java Persistence With JavaServer Faces Technology, covered various aspects of the Java Persistence API. This tip discusses how to tune the Java Persistence implementation in GlassFish called Toplink Essentials. The example included in this tip is based on the customer-cmp sample bundled with GlassFish. You can download the samples from the GlassFish Samples page. The tip covers performance tuning for a Java Persistence implementation used in different modes: in-container and out-of container. The tip also uses the Faban framework to demonstrate different performance tuning parameters. Before discussing the tuning parameters, let's examine in-container and out-of container modes and the Faban framework. Two Modes of Running a Java Persistence Implementation You can use Java Persistence implementations that are compliant with JSR-220 in either of two modes: in-container and out-of-container. This is a marked difference between earlier enterprise Java Persistence implementations such as container-managed persistence in EJB 2.1. With EJB 2.1 container-managed persistence a Java Persistence implementation could only run using in-container mode. In-container mode means that the Java Persistence implementation is used inside of an EJB container. EntityManagers, objects that manage persistence entities, are usually created by the EJB container. Entity managers are typically JTA entity managers. In other words, they participate in transactions controlled by the Java Transaction Architecture (JTA). However, the Java Persistence implementation must support both JTA entity managers and resource-local entity managers, that is, entity managers that participate in transactions not managed by JTA. In out-of-container mode the Java Persistence implementation is either standalone or runs inside of a web container. The application is responsible for creating it's own entity managers. Transactions are usually resource-local. If the implementation runs standalone, it does not need to support JTA. The Faban Driver Framework Faban is an open-source driver framework. It lets developers focus on driver logic implementation so that they can quickly develop performance drivers. Faban takes care of common tasks such as load-generation, output formatting, and the timing of operations. For more about the Faban, see the Faban project page. Tuning the Sample Application The customer-cmp sample application interacts with a relational database to store and display information about customer subscriptions to periodicals. The database stores information such as a customer's name and address, as well as the type of periodical to which the customer is subscribed -- that is, magazine, journal, or newspaper. Users can submit requests to the application to display subscription-related information. This tip focuses on one of the operations supported by the application: finding a customer by his or her ID. Changing the Cache Size and Number of Connection Pools Let's examine the impact that specific tuning parameters have on the combined throughput of these operations. Specifically, let's examine the impact of changing the cache size in Toplink Essentials and resetting the number of connections in the connection pool in GlassFish for database calls. The Toplink Essentials implementation has a default cache size of 1000 per entity. For the purpose of demonstrating the effect of the cache setting, let's look at the transaction rate using the default cache size. Then let's change the cache size to 5000 and compare the results. To set the cache size, you need to update the persistence.xml
file in your project and set the <property name="toplink.cache.size.default" value="5000"/>
In this example, there are 20,0000 customer entities in the database. The cache setting applies to both in-container and out-of-container modes. The connection pool setting, however, is handled differently depending on whether you use in-container or out-of-container mode. For In-container mode, the connection pool setting is done in GlassFish and is controlled by the data-source configuration. In this mode, you can configure the connection pool using the GlassFish admin console as shown below.
For in-container mode, set the number of connections in the connection pool within the container to the number of request processing threads configured in an instance of GlassFish (in this case, 40). This is the maximum number of threads that can be active at any time and thus the maximum number of connections ever needed by Toplink Essentials for database calls. For out-of-container mode, the connection pool setting is done in the persistence.xml file. The Toplink Essentials implementation has a default connection pool setting of eight connections for both read and write connections. Let's run the application in out-of-container mode for eight users, so set the connection pool value to eight connections. Results Running Toplink Essentials using in-container mode produces results similar to the following:
Running Toplink Essentials using out-of-container mode produces results similar to the following:
* The default values are: toplink.jdbc.read-connections.min = 2 Note that when the default values are used, the connection pool is initialized with the default min setting and can scale to a maximum specified by the default max setting. By setting a connection pool value of 8, there are always be eight connections in the pool. The system configurations for the tests are as follows: Sun Fire V880, Solaris 10, 4 CPUs for System under test (SUT). Note that the driver and the SUT are the same system for the out-of-container test. The Driver system and SUT have a point-to-point ethernet connection, which means it has no external network traffic. Summary To get the best performance out of your Java Persistence implementation, it's important to understand the tuning parameters provided by the implementation. The cache setting is an important tuning parameter provided by the Toplink Essentials implementation. Tuning the connection pool setting provided by GlassFish is also important in getting the best performance out of any Java Persistence implementation used in GlassFish. If you use a Java Persistence implementation outside of the EJB container, the cache setting and connection pool setting again play an important role in the performance of the implementation. However, in this case you set the connection pool value in the persistence.xml file. There are other parameters that might impact the performance of a Java Persistence implementation. One is the statement cache. This needs to set explicitly for some databases such as an Oracle database. The statement cache is turned on by default in the Java DB database bundled with GlassFish. In addition, your Java Virtual Machine (JVM)* settings can play an important role in performance tuning. For detailed information on JVM tuning, see the Java Tuning White Paper In running the application for this tip, the JVM options were set to the following values: -server -XX:+AggressiveHeap -Xmx2500m -Xms2500m -Xss128k -XX:+DisableExplicitGC
* As used in this document, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform. For More Information For more information about Java Persistence, see The Java Persistence API - A Simpler Programming Model for Entity Persistence and the Java Persistence API FAQ. For more information about Toplink Essentials, see TopLink Essentials - The Java Persistence API Implementation at GlassFish. Running the Sample Code To install and run the sample code that accompanies this tip:
Rahul Biswas is a member of the Java Performance Engineering group at Sun. He is currently involved in the performance improvement of the persistence implementation in GlassFish. Developers Assistance
Need programming advice on Java EE? Try Developer Expert Assistance | ||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||