CONTENTS | PREV | NEXT | INDEXJ2EE BluePrints



Questions and Answers - Enterprise JavaBeans Tier - EJB Restrictions

What restrictions are imposed on enterprise beans? Why are these restrictions in place?

The EJB container is responsible for managing system-related functionality such as security, threading, resource pooling, and so on. In order to control these facets of component operation, the container places certain restrictions on the components it manages.

There is quite a list of restrictions on what enterprise beans can do. The restrictions' underlying intent is to ensure the portability, scalability, and interoperability of the application. The restrictions are discussed in more detail below. Each restriction falls into one or more of several categories: Specifically, enterprise beans should not: For more on these restrictions, see § 18.1.2 of the EJB 1.1 specification.

Are the restrictions mandatory? How are they enforced?

The restrictions are mandatory, since they are laid out in the specification as not optional. There are three basic types of enforcement of these restrictions.

For more on the specifics of EJB container security, see section 18.2.1.1 of the EJB 1.1 specification; especially table 10.

Do the restrictions that apply to EJBs also apply to helper and dependent classes ?

Yes. From the point of view of the container, the enterprise bean and its helper classes form a single functional unit. If an enterprise bean were not allowed to, say, open a file in the filesystem, and yet had access to a helper class that was allowed to do so, the restriction on the bean would be meaningless. Therefore, EJB component programming restrictions also apply to any helper or dependent classes the bean may use.

Why can't I use nonfinal static fields in my enterprise bean?

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVMTM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

It is acceptable practice to use static class fields if those fields are marked as final. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.

Why is thread creation and management disallowed?

The EJB specification assigns to the EJB container the responsibility for managing threads. Allowing enterprise bean instances to create and manage threads would interfere with the container's ability to control its components' lifecycle. Thread management is not a business function, it is an implementation detail, and is typically complicated and platform-specific. Letting the container manage threads relieves the enterprise bean developer of dealing with threading issues. Multithreaded applications are still possible, but control of multithreading is located in the container, not in the enterprise bean.

Why can an enterprise bean not listen to or accept connections on a socket?

Because if an enterprise bean is listening on a socket, it can't be passivated -- it must always be available. Enterprise beans can be network socket clients, and so they can use other network resources (including other enterprise bean servers) to do their jobs. Just as with a database connection, don't hang on to open client sockets across method calls; instead, open them, communicate through the socket, and close it before returning from the method.

Why can't EJBs read and write files and directories in the filesystem? And why can't they access file descriptors?

Enterprise beans aren't allowed to access files primarily because files are not transactional resources. Allowing EJBs to access files or directories in the filesystem, or to use file descriptors, would compromise component distributability, and would be a security hazard.

Another reason is deployability. The EJB container can choose to place an enterprise bean in any JVM, on any machine in a cluster. Yet the contents of a filesystem are not part of a deployment, and are therefore outside of the EJB container's control. File systems, directories, files, and especially file descriptors tend to be machine-local resources. If an enterprise bean running in a JVM on a particular machine is using or holding an open file descriptor to a file in the filesystem, that enterprise bean cannot easily be moved from one JVM or machine to another, without losing its reference to the file.

Furthermore, giving EJBs access to the filesystem is a security hazard, since the enterprise bean could potentially read and broadcast the contents of sensitive files, or even upload and overwrite the JVM runtime binary for malicious purposes.

Files are not an appropriate mechanism for storing business data for use by components, because they tend to be unstructured, are not under the control of the server environment, and typically don't provide distributed transactional access or fine-grained locking. Business data is better managed using a persistence interface such as JDBC, whose implementations usually provide these benefits. Read-only data can, however, be stored in files in a deployment JAR, and accessed with the getResource() or getResourceAsStream() methods of java.lang.Class.

Why isn't AWT available from within an enterprise bean?

Because EJBs are intended to be business-functionality-specific server extensions, not clients with user interfaces. The purpose of an enterprise bean is to perform some service on the server in response to a service request. This is a separate function from managing user interaction. Note that AWT and JFC/Swing may still be used to create user interfaces that access enterprise beans through a remote enterprise bean reference.

Why is there a restriction against using the Java Reflection APIs to obtain declared member information that the Java language security rules would not allow? Doesn't Java automatically enforce those rules?

Contrary to common belief, most of the Java Reflection API can be used from EJB components. For example, loadClass() and invoke() can both be used by enterprise beans. Only certain reflection methods are forbidden.

This restriction refers to the enabling of the security permission ReflectPermission. The suppressAccessChecks permission target name, when enabled, allows a class to use reflection to inspect, access, and modify protected and private members and methods in other classes. Obviously, if EJB components are using private or protected members or methods to manage sensitive information, this facility could be used to violate security mechanisms. Therefore, the EJB specification explicitly restricts usage of suppressAccessChecks, to prevent the security hole that would result. Denial of ReflectPermission is part of the standard security policy for an EJB container.

Why all the restrictions on creating class loaders and redirection of input, output, and error streams?

Class loading is allowed, but creating custom loaders is not, for security reasons. These restrictions exist because the EJB container has responsibility for class loading and I/O control. Allowing the EJB to perform these functions would interfere with proper operation of the Container, and are a security hazard.

The Java Pet Store has code that loads classes from inside an enterprise bean class using Class.forName(), in StateMachine.

Why can't I load native code?

Native code, if allowed in enterprise beans, could cause a host of problems, including but not limited to:

How does the container prevent native code from being called? The container has a SecurityManager whose standard policies (section § 18.2.1.1 of the specification, Table 10) don't set a checkLink permission for any native library. So, attempts at executing the load() or loadLibrary() methods of java.lang.Runtime result in a SecurityException.

The EJB specification states:

"Some Containers may allow the Deployer to grant more, or fewer, permissions to the enterprise bean instances than specified in Table 10. Support for this is not required by the EJB specification. Enterprise beans that rely on more or fewer permissions will not be portable across all EJB Containers."

This means that, if your container will let you grant permission to run native code, and you don't mind that your enterprise bean component is not portable not only by platform, but also possibly not by server, then you can run native methods in enterprise beans. Just be aware that you're locking yourself into a box, and taking on the liabilities (problems with security, stability, portability, scalability, etc.) that use of native code implies. we strongly recommend against using native code in enterprise beans.

By the way, it won't help to have your enterprise bean defer native calls to other classes, because instances of those classes will running in the same container, and so will share the enterprise bean's security restrictions. The enterprise bean specification states explicitly that helper classes of enterprise beans are under the same restrictions as enterprise beans themselves.

If you absolutely must have access to native code in your system, but want to keep your enterprise beans portable, consider wrapping the native code as an RMI technology-enabled object, and then using the service from your enterprise bean. This solution won't solve the security problem, and may still affect scalability, but at least if the native code crashes, it won't bring your entire application server down. Also, EJB servers from different vendors running on multiple platforms can use this new service. When in some future time portability becomes an issue (as it invariably does, for systems that make it into production), your enterprise bean functionality will port to new app servers or platforms with a minimum of pain, and you can focus on reimplementing native functionality as necessary.


CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.