Showing posts with label JAR (file format). Show all posts
Showing posts with label JAR (file format). Show all posts

Wednesday, May 30, 2012

J2EE Tutorial-9(Creating the Enterprise Bean)

An enterprise bean is a server-side component that contains the business logic of an application. At runtime, the application clients execute the business logic by invoking the enterprise bean's methods. The enterprise bean in our example is a stateless session bean called ConverterEJB. The source code for ConverterEJB is in thej2eetutorial/examples/src/ejb/converter directory.

Coding the Enterprise Bean


The enterprise bean in this example requires the following code:

  • Remote interface

  • Home interface

  • Enterprise bean class


Coding the Remote Interface


A remote interface defines the business methods that a client may call. The business methods are implemented in the enterprise bean code. The source code for the Converter remote interface follows.

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;

public interface Converter extends EJBObject {
public BigDecimal dollarToYen(BigDecimal dollars)
throws RemoteException;
public BigDecimal yenToEuro(BigDecimal yen)
throws RemoteException;
}


Coding the Home Interface


home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The ConverterHome interface contains a single create method, which returns an object of the remote interface type. Here is the source code for the ConverterHome interface:

import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException, CreateException;
}


Coding the Enterprise Bean Class


The enterprise bean class for this example is called ConverterBean. This class implements the two business methods, dollarToYen and yenToEuro, that the Converter remote interface defines. The source code for the ConverterBean class follows.

import java.rmi.RemoteException; 
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;

public class ConverterBean implements SessionBean {

BigDecimal yenRate = new BigDecimal("121.6000");
BigDecimal euroRate = new BigDecimal("0.0077");

public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2,BigDecimal.ROUND_UP);
} public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2,BigDecimal.ROUND_UP);
} public ConverterBean() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}


Compiling the Source Files


Now you are ready to compile the remote interface (Converter.java), home interface (ConverterHome.java), and the enterprise bean class (ConverterBean.java).

  1. In a terminal window, go to the j2eetutorial/examples directory.

  2. Type the following command:

       ant converter




This command compiles the source files for the enterprise bean and the J2EE application client. It places the resulting class files in thej2eetutorial/examples/build/ejb/converter directory (not the src directory). For more information about ant, see How to Build and Run the Examples.




Note: When compiling the code, the preceding ant task includes the j2ee.jar file in the classpath. This file resides in the lib directory of your J2EE SDK installation. If you plan on using other tools to compile the source code for J2EE components, make sure that the classpath includes the j2ee.jar file.




Packaging the Enterprise Bean


To package an enterprise bean, you run the New Enterprise Bean wizard of the deploytool utility. During this process, the wizard performs the following tasks:

  • Creates the bean's deployment descriptor

  • Packages the deployment descriptor and the bean's classes in an EJB JAR file

  • Inserts the EJB JAR file into the application's ConverterApp.ear file


After the packaging process, you can view the deployment descriptor by selecting ToolsDescriptor Viewer.

To start the New Enterprise Bean wizard, select FileNewEnterprise Bean. The wizard displays the following dialog boxes.

  1. Introduction dialog box

    1. Read the explanatory text for an overview of the wizard's features.

    2. Click Next.



  2. EJB JAR dialog box

    1. Select the Create New JAR File In Application button.

    2. In the combo box, select ConverterApp.

    3. In the JAR Display Name field, enter ConverterJAR.

    4. Click Edit.

    5. In the tree under Available Files, locate the j2eetutorial/examples/build/ejb/converter directory. (If the converter directory is many levels down in the tree, you can simplify the tree view by entering all or part of the converter directory's path name in the Starting Directory field.)

    6. Select the following classes from the Available Files tree and click Add: Converter.classConverterBean.class, and ConverterHome.class. (You may also drag and drop these class files to the Contents text area.)

    7. Click OK.

    8. Click Next.



  3. General dialog box

    1. Under Bean Type, select the Session radio button.

    2. Select the Stateless radio button.

    3. In the Enterprise Bean Class combo box, select ConverterBean.

    4. In the Enterprise Bean Name field, enter ConverterEJB.

    5. In the Remote Home Interface combo box, select ConverterHome.

    6. In the Remote Interface combo box, select Converter.

    7. Click Next.



  4. Transaction Management dialog box

    1. Because you may skip the remaining dialog boxes, click Finish.



Tuesday, May 29, 2012

J2EE Tutorial-5 (Development Roles)

Reusable modules make it possible to divide the application development and deployment process into distinct roles so that different people or companies can perform different parts of the process.

The first two roles involve purchasing and installing the J2EE product and tools. Once software is purchased and installed, J2EE components can be developed by application component providers, assembled by application assemblers, and deployed by application deployers. In a large organization, each of these roles might be executed by different individuals or teams. This division of labor works because each of the earlier roles outputs a portable file that is the input for a subsequent role. For example, in the application component development phase, an enterprise bean software developer delivers EJB JAR files. In the application assembly role, another developer combines these EJB JAR files into a J2EE application and saves it in an EAR file. In the application deployment role, a system administrator at the customer site uses the EAR file to install the J2EE application into a J2EE server.

The different roles are not always executed by different people. If you work for a small company, for example, or if you are prototyping a sample application, you might perform the tasks in every phase.

J2EE Product Provider


The J2EE product provider is the company that designs and makes available for purchase the J2EE platform, APIs, and other features defined in the J2EE specification. Product providers are typically operating system, database system, application server, or Web server vendors who implement the J2EE platform according to the Java 2 Platform, Enterprise Edition Specification.

Tool Provider


The tool provider is the company or person who creates development, assembly, and packaging tools used by component providers, assemblers, and deployers. See the section Tools for information on the tools available with J2EE SDK version 1.3.

Application Component Provider


The application component provider is the company or person who creates Web components, enterprise beans, applets, or application clients for use in J2EE applications.

Enterprise Bean Developer


An enterprise bean developer performs the following tasks to deliver an EJB JAR file that contains the enterprise bean:

  • Writes and compiles the source code

  • Specifies the deployment descriptor

  • Bundles the .class files and deployment descriptor into an EJB JAR file


Web Component Developer


A Web component developer performs the following tasks to deliver a WAR file containing the Web component:

  • Writes and compiles servlet source code

  • Writes JSP and HTML files

  • Specifies the deployment descriptor for the Web component

  • Bundles the .class.jsp.html, and deployment descriptor files in the WAR file


J2EE Application Client Developer


An application client developer performs the following tasks to deliver a JAR file containing the J2EE application client:

  • Writes and compiles the source code

  • Specifies the deployment descriptor for the client

  • Bundles the .class files and deployment descriptor into the JAR file


Application Assembler


The application assembler is the company or person who receives application component JAR files from component providers and assembles them into a J2EE application EAR file. The assembler or deployer can edit the deployment descriptor directly or use tools that correctly add XML tags according to interactive selections. A software developer performs the following tasks to deliver an EAR file containing the J2EE application:

  • Assembles EJB JAR and WAR files created in the previous phases into a J2EE application (EAR) file

  • Specifies the deployment descriptor for the J2EE application

  • Verifies that the contents of the EAR file are well formed and comply with the J2EE specification


Application Deployer and Administrator


The application deployer and administrator is the company or person who configures and deploys the J2EE application, administers the computing and networking infrastructure where J2EE applications run, and oversees the runtime environment. Duties include such things as setting transaction controls and security attributes and specifying connections to databases.

During configuration, the deployer follows instructions supplied by the application component provider to resolve external dependencies, specify security settings, and assign transaction attributes. During installation, the deployer moves the application components to the server and generates the container-specific classes and interfaces.

A deployer/system administrator performs the following tasks to install and configure a J2EE application:

  • Adds the J2EE application (EAR) file created in the preceding phase to the J2EE server

  • Configures the J2EE application for the operational environment by modifying the deployment descriptor of the J2EE application

  • Verifies that the contents of the EAR file are well formed and comply with the J2EE specification

  • Deploys (installs) the J2EE application EAR file into the J2EE server

Receiving mail with attachment using javamail API

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;

class ReadAttachment{
public static void main(String [] args)throws Exception{

String host="mail.javatpoint.com";
final String user="sonoojaiswal@javatpoint.com";
final String password="xxxxx";//change accordingly

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host );
properties.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});

Store store = session.getStore("pop3");
store.connect(host,user,password);

Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);

Message[] message = folder.getMessages();
for (int a = 0; a < message.length; a++) {
System.out.println("-------------" + (a + 1) + "-----------");
System.out.println(message[a].getSentDate());

Multipart multipart = (Multipart) message[a].getContent();

for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
InputStream stream = bodyPart.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));

while (br.ready()) {
System.out.println(br.readLine());
}
System.out.println();
}
System.out.println();
}

folder.close(true);
store.close();
}
}

LOAD THE JAR FILE :  C:\> set classpath=mail.jar;activation.jar;

COMPILE THE SOURCE FILE: C:\> javac ReadAttachment.java

RUN BY :C:\> java ReadAttachment