Enterprise Java Beans
Enterprise Java Beans
software. EJB is a server-side software element that summarizes business logic of an application.
Enterprise Java Beans web repository yields a runtime domain for web related software elements
including computer reliability, Java Servlet Lifecycle (JSL) management, transaction procedure and
other web services. The EJB enumeration is a subset of the Java EE enumeration.
The EJB enumeration was originally developed by IBM in 1997 and later adopted by Sun
Microsystems in 1999 and enhanced under the Java Community Process.
The EJB enumeration aims to provide a standard way to implement the server-side business software
typically found in enterprise applications. Such machine code addresses the same types of problems,
and solutions to these problems are often repeatedly re-implemented by programmers. Enterprise Java
Beans is assumed to manage such common concerns as endurance, transactional probity and security in
a standard way that leaves programmers free to focus on the particular parts of the enterprise software
at hand.
To run EJB application we need an application server (EJB Container) such as Jboss, Glassfish,
Weblogic, Websphere etc. It performs:
1. Life cycle management
2. Security
3. Transaction management
4. Object pooling
2. Message Driven Bean: Like Session Bean, it contains the business logic but it is invoked by passing
message.
3. Entity Bean: It summarizes the state that can be remained in the database. It is deprecated. Now, it is
replaced with JPA (Java Persistent API). There are two types of entity bean:
JavaBeans are designed for a single EJBs are remotely executable components
6.
process and are localized or business objects.
JavaBeans has components bridges.A ActiveX controls are designed for the
8. JavaBeans can also be arranged as an desktop so EJB cannot be deployed as an
ActiveX control. ActiveX control.
JavaBean
In any programming language, reusability is the main concern. To achieve the same concern, Java
introduced the concept of JavaBean. It is a software component that has been designed to be reusable in a
variety of environments. In this section, we will dive into the topic and understand the horizons of
concept in this what is JavaBeans, its advantages, disadvantages, Life cycle.
In other words, JavaBeans are classes that encapsulate multiple objects into a single object. It also helps
in accessing these object from multiple places. It contains several elements like Constructors, getter and
setter methods and much more.
//Employee.java
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
How to access the JavaBean class?
To access the JavaBean class, we should use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the object. The feature can be
of any Java data type, containing the classes that you define.
A JavaBean property may be read, write, read-only, or write-only. JavaBean features are accessed
through two methods in the JavaBean's implementation class:
1. import java.lang.reflect.Field;
2. // Class to find the property name of an object that holds a given value
3. public class PropertyNameFinder {
4. // Method to return the name of the property whose value matches the given value
5. public static String getPropertyName(Object obj, Object value) {
6. // Loop through all declared fields of the object's class
7. for (Field field : obj.getClass().getDeclaredFields()) {
8. // Make the field accessible, allowing access to private fields
9. field.setAccessible(true);
10. try {
11. // Check if the current field's value is equal to the provided value
12. if (field.get(obj).equals(value)) {
13. // Return the name of the field if the value matches
14. return field.getName();
15. }
16. } catch (IllegalAccessException e) {
17. // Handle the exception if the field is not accessible
18. System.out.println("Access to the field was denied.");
19. } catch (NullPointerException e) {
20. // Handle the case where the field's value is null to avoid crashing the program
21. System.out.println("A field value was null.");
22. }
23. }
24. // Return null if no matching field is found
25. return null;
26. }
27. // Main method to test the getPropertyName method
28. public static void main(String[] args) {
29. // Create an instance of Person
30. Person person = new Person("John Doe", 30);
31. // Try to find the field name that has the value "John Doe"
32. String fieldName = getPropertyName(person, "John Doe");
33. // Print the result, expected to be "name"
34. System.out.println(fieldName); // Outputs: name
35. // Try to find the field name that has the value 30
36. fieldName = getPropertyName(person, 30);
37. // Print the result, expected to be "age"
38. System.out.println(fieldName); // Outputs: age
39. }
40. // Nested static class Person with private fields
41. static class Person {
42. private String name;
43. private int age;
44. // Constructor to set the name and age of the person
45. public Person(String name, int age) {
46. this.name = name;
47. this.age = age;
48. }
49. }
50. }
Output:
name
age
2. setPropertyName()
For example, if the property name is firstName, the method name would be setFirstName() to write that
property. This method is called the mutator.
1. import java.lang.reflect.Field;
2. public class PropertySetter {
3. // Method to set the property of an object based on the property name and the value provided
4. public static void setPropertyName(Object obj, String propertyName, Object value) {
5. try {
6. // Retrieve the Field object for the specified property from the class of the provided object
7. Field field = obj.getClass().getDeclaredField(propertyName);
8. // Make the field accessible, even if it is private or protected
9. field.setAccessible(true);
10. // Check if the value is compatible with the field type before setting it
11. if (isCompatible(value, field.getType())) {
12. // Set the field's value for the given object to the specified value
13. field.set(obj, value);
14. } else {
15. // Throw an exception if the value is not compatible with the field type
16. throw new IllegalArgumentException("Incompatible value type provided.");
17. }
18. } catch (NoSuchFieldException e) {
19. // Handle case where the field does not exist in the object
20. System.out.println("Field not found: " + propertyName);
21. } catch (IllegalAccessException e) {
22. // Handle case where the field is inaccessible or final
23. System.out.println("Failed to access or modify the field: " + propertyName);
24. } catch (IllegalArgumentException e) {
25. // Handle other illegal argument issues, such as type incompatibility
26. System.out.println(e.getMessage());
27. }
28. }
29. // Helper method to check if the provided value is compatible with the field's type
30. private static boolean isCompatible(Object value, Class<?> type) {
31. // Check if value is null (null can be assigned to any non-primitive field)
32. if (value == null) return true;
33. // Check if the value instance matches the field's type or its wrapper class
34. return type.isInstance(value) || (type.isPrimitive() && wrap(type).isInstance(value));
35. }
36. // Method to wrap primitive types in their corresponding wrapper classes
37. private static Class<?> wrap(Class<?> type) {
38. // Return the wrapper class for int types
39. if (type == int.class) return Integer.class;
40. // Return the wrapper class for double types
41. else if (type == double.class) return Double.class;
42. // Add more primitives if necessary
43. // Return the original type if it's not a primitive needing wrapping
44. return type;
45. }
46. \ public static void main(String[] args) {
47. // Create a new Person object
48. Person person = new Person("John Doe", 30);
49. // Display the person's name before modification
50. System.out.println("Before: " + person.getName());
51. // Change the person's name using the setPropertyName method
52. setPropertyName(person, "name", "Jane Doe");
53. // Display the person's name after modification
54. System.out.println("After: " + person.getName()); // Should output: Jane Doe
55. }
56. // Person class with private fields and a constructor
57. static class Person {
58. private String name;
59. private int age;
60. public Person(String name, int age) {
61. this.name = name;
62. this.age = age;
63. }
64. // Getter method for the name field
65. public String getName() {
66. return name;
67. }
68. }
69. }
Output: