Advanced Java Unit 3 Corba
Advanced Java Unit 3 Corba
1. Platform independence
2. Language independence
3. Distributed computing
4. Scalability
CORBA Java Drawbacks:*
1. Complexity
2. Performance overhead
3. Security concerns
CORBA Java Example:*
Server.java*
*Hello.idl*
idl public class Server {
module HelloApp { public static void main(String[] args) {
interface Hello { // Initialize ORB
string sayHello(); ORB orb = ORB.init(args, null);
};
}; // Create Hello object
HelloImpl hello = new HelloImpl();
*Purpose:*
1. Platform-independent
2. Language-independent
3. Formal syntax
4. Self-describing
*Types of IDLs:*
1. Interface definition
2. Method declarations
3. Parameter definitions
4. Return type specifications
5. Exception handling
Example (CORBA IDL):*
```
interface HelloWorld {
void sayHello(in string
name);
string getMessage();
};
*Benefits:*
1. Improved interoperability
2. Clear interface definitions
3. Reduced errors
4. Simplified maintenance
5. Enhanced reusability
// Get NamingContext
NamingContext namingContext =
NamingContextHelper.narrow(orb.resolve_initi
al_references("NameService"));
DL Packages
rg.omg.IDL: Defines IDL interfaces and classes.
Other Packages
rg.omg.CosNaming: Provides a naming service for CORBA objects
rg.omg.CosTransactions: Supports transactional operations.
ome key classes and interfaces in these packages include:
rg.omg.CORBA.ORB (Object Request Broker)
rg.omg.CORBA.Object
rg.omg.CORBA.Request
rg.omg.IDL.Interface
HelloClient.java (Client-side code)* // Resolve Hello reference
``` NameComponent[] name = new
import org.omg.CORBA.ORB; NameComponent[1];
import name[0] = new
org.omg.CosNaming.NameComponent; NameComponent("Hello");
import Hello hello =
org.omg.CosNaming.NamingContext; HelloHelper.narrow(namingContext.resolv
e(name));
public class HelloClient {
public static void main(String[] args) {
// Invoke sayHello method
try { String response = hello.sayHello("John");
// Create ORB System.out.println(response);
ORB orb = ORB.init(args, null); } catch (Exception e) {
System.out.println("Error: " +
// Get NamingContext e.getMessage());
NamingContext namingContext = }
NamingContextHelper.narrow(orb.resolve }
_initial_references("NameService")); }
```
This Java class looks up the `Hello`
reference in the NamingContext and
invokes the `sayHello` method.
Steps to run:*
*Output:*
Hello, John!
1. *Architecture*:
- CORBA: Decentralized, broker-based architecture (Object Request
Broker).
- RMI: Centralized, stub/skeleton-based architecture.
2. *Platform independence*:
- CORBA: Supports multiple platforms and languages.
- RMI: Primarily designed for Java.
3. *Interface definition*:
- CORBA: Uses IDL (Interface Definition Language).
- RMI: Uses Java interfaces.
4. *Communication protocol*:
- CORBA: Uses IIOP (Internet Inter-ORB Protocol).
- RMI: Uses JRMP (Java Remote Method Protocol).
5. *Security*:
- CORBA: Provides robust security features.
- RMI: Limited security features.
6. *Complexity*:
- CORBA: Steeper learning curve due to IDL and ORB.
- RMI: Simpler, more intuitive API.
* When to choose CORBA over RMI:*
1. Multi-language support.
2. Multi-platform support.
3. Robust security features.
4. Decentralized architecture.
1. Java-only applications.
2. Simple, lightweight distributed systems.
3. Easy-to-use API.
IIOP (Internet Inter-ORB Protocol) Implementation:
Implementation Steps*
1. *ORB Initialization*
- Initialize the ORB (Object Request Broker) on both client and server sides.
- Specify the IIOP port number.
2. *Object Registration*
- Register the CORBA object with the ORB on the server side.
- Obtain an Interoperable Object Reference (IOR) for the object.
3. *Client Request*
- Create a client request using the IOR.
- Marshal (serialize) the request data.
4. *Server Response*
- Receive the client request.
- Demarshal (deserialize) the request data.
- Process the request and send a response.
5. *Connection Establishment*
- Establish a connection between client and server using TCP/IP.
- Use the IIOP protocol for data exchange.
*IIOP Message Structure*
1. *Message Header*
- Version
- Message type (request or response)
- Message size
2. *Request or Response Data*
- Operation name
- Parameters (marshaled)
3. *Error Handling*
- Error codes
- Error messages
Java Implementation Example*
Using Java IDL (Interface Definition Language) and Java ORB:
// IDL file (Hello.idl)
module HelloApp {
interface Hello {
string sayHello(in string name);
};
};
// Server-side implementation (HelloImpl.java)
public class HelloImpl extends HelloPOA {
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
// Client-side code (HelloClient.java)
public class HelloClient {
public static void main(String[] args) {
// Initialize ORB
ORB orb = ORB.init(args, null);
// Obtain Hello object reference
Hello hello =
HelloHelper.narrow(orb.resolve_initial_references
("Hello"));
// Invoke sayHello method
String response = hello.sayHello("John");
System.out.println(response);
}
}
*Explanation:*
This line of code is used in the CORBA client to obtain a reference to the `Hello` object.
*Step-by-Step Breakdown:*
1. `orb.resolve_initial_references("Hello")`:
2. `HelloHelper.narrow(...)`:
1. The client asks the ORB to resolve the initial reference named "Hello".
2. The ORB returns a generic object reference.
3. The `HelloHelper.narrow` method converts the generic reference to a `Hello`
interface reference.
4. The resulting reference is assigned to the `hello` variable.
*Purpose:*
This line enables the client to access the `Hello` object's methods, such as `sayHello`,
through the `hello` variable. CORBA Naming Services:
*
CORBA Naming Services
// Create a name
Name name = new Name();
name.add(nameComponent);
*Name*
```
// Create a name component
NameComponent nc = new NameComponent("Documents");
// Create a name
Name name = new Name();
name.add(nc);
name.add(nc2);
In this example:
// Bind an object to the name
namingContext.bind(name, myObject); - `nc` and `nc2` are `NameComponent`s.
- `name` is a `Name` composed of `nc` and
`nc2`.
- `myObject` is bound to the `name`.
*Example 1: Simple Naming*
```
// Create a naming context
NamingContext namingContext =
NamingContextHelper.narrow(orb.resolve_initial_referenc
es("NameService"));
// Create a name
Name name = new Name();
name.add(nc);
```
// Create a naming context
NamingContext namingContext =
NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
// Create a name
Name name = new Name();
name.add(nc1);
name.add(nc2);
1. *Object-by-Value (OBV)*
2. *Object-by-Reference (OBR)*
```
// IDL file (Hello.idl) // Client-side code (HelloClient.java)
module HelloApp { public class HelloClient {
interface Hello { public static void main(String[] args) {
string sayHello(in string name); // Initialize ORB
}; ORB orb = ORB.init(args, null);
};
// Obtain Hello object reference
// Server-side implementation Hello hello =
(HelloImpl.java) HelloHelper.narrow(orb.resolve_initial_referen
public class HelloImpl extends HelloPOA { ces("Hello"));
public String sayHello(String name) {
return "Hello, " + name + "!"; // Invoke sayHello method
} String response = hello.sayHello("John");
} System.out.println(response);
}
}
Dynamic Invocation Interface (DII) Example // Client-side code (HelloClient.java)
(Java)* public class HelloClient {
public static void main(String[] args) {
// Initialize ORB
```
ORB orb = ORB.init(args, null);
// IDL file (Hello.idl)
module HelloApp { // Obtain Hello object reference
interface Hello { org.omg.CORBA.Object obj =
string sayHello(in string name); orb.resolve_initial_references("Hello");
};
}; // Create DynamicInvocationInterface
DynamicInvocationInterface dii =
DynamicInvocationInterfaceHelper.narrow(obj);
// Server-side implementation
(HelloImpl.java) // Invoke sayHello method dynamically
public class HelloImpl extends HelloPOA { Request request = dii.create_request("sayHello");
public String sayHello(String name) { request.add_in_arg("name", "John");
return "Hello, " + name + "!"; request.invoke();
} String response =
} request.return_value().extract_string();
System.out.println(response);
}
}
```
JAR file - Java archive file
output
added manifest
adding: Hello.class(in = 456) (out = 294)(deflated 35%)
adding: HelloWorld.class(in = 123) (out = 98)(deflated 20%)
*What is a Manifest File?]
A manifest file (MANIFEST.MF) is a text file contained within a JAR file.
It provides metadata about the JAR file's contents, such as:
1.Version information
2.Author and copyright details
3.Main class (entry point)
4.Classpath settings
Dependencies
Output:
Hello, World!
MULTI-TIER APPLICATION
DEVELOPMENT
Unit - 4
Multi-Tier Architecture
• Application Tier
• Java Servlets
• JavaServer Faces (JSF)
• Spring MVC
• Enterprise JavaBeans (EJB)
• Data Tier
• JDBC (Java Database Connectivity)
• Hibernate
• JPA (Java Persistence API)
Benefits of Multi-Tier Architecture
• Scalability
• Maintainability
• Flexibility
• Reusability
• Improved security
Server-Side Programming
• Java
• Python
• Ruby
• PHP
• Node.js (JavaScript)
• ASP.NET (C#, VB.NET)
• Go
Server-Side Frameworks:
• Servlets
• JSP (JavaServer Pages)
• ASP (Active Server Pages)
• CGI (Common Gateway Interface)
• FastCGI
Server-Side Programming Tasks: