Testing in Java

Leave a comment

Abstract Class Vs Interface in Java

Leave a comment

Now I would like to make introduction about the difference between Abstract Class and Interface in Java.
For Abstract Class may contains abstract method and abstract property, but Interface may contains only normal method without encapsulation and no method body implementation.
We use abstract class and interface as base class in Java.

 //Abstarct Class
public abstract class Entity {
private int id;
private Color c;
public void setID(int id) { this.id = id }
public int getID() { return id; }
public void setColor(Color c) { this.c = c; }
public Color getColor() { return c; }
public abstract void paint(Graphics g);
}

//Interface

public interface Entity {
void setID(int id) ;
int getID();
void setColor(Color c);
Color getColor() ;
void paint(Graphics g);
}

J2EE Service Architecture

Leave a comment

Containers provide underlying services to their deployed components. As a developer, you can concentrate on implementing business logic rather than solving technical problems faced in enterprise applications. This pic shows you the services provided by each container. For example, web and EJB containers provide connectors to access EIS, but not the applet container or the ACCs. Java EE offers the following services:

  • JTA: This service offers a transaction demarcation API used by the container and the application. It also provides an interface between the transaction manager and a resource manager at the Service Provider Interface (SPI) level.
  • JPA: Standard API for object-relational mapping (ORM). With its Java Persistence Query Language (JPQL), you can query objects stored in the underlying database.
  • JMS: JMS allows components to communicate asynchronously through messages.It supports reliable point-to-point (P2P) messaging as well as the publish-subscribe (pub-sub) model.
  • Java Naming and Directory Interface (JNDI): This API, included in Java SE, is used to access naming and directory systems. Your application uses it to associate (bind) names to objects and then to find these objects (lookup) in a directory. You can look up datasources, JMS factories, EJBs, and other resources. Omnipresent in your code
    until J2EE 1.4, JNDI is used in a more transparent way through injection.
  • JavaMail: Many applications require the ability to send e-mails, which can be implemented through use of the JavaMail API.
  • JavaBeans Activation Framework (JAF): The JAF API, included in Java SE, provides a framework for handling data in different MIME types. It is used by JavaMail.
  • XML processing: Most Java EE components can be deployed with optional XML deployment descriptors, and applications often have to manipulate XML documents. The Java API for XML Processing (JAXP) provides support for parsing documents with SAX and DOM APIs, as well as for XSLT. The Streaming API for XML (StAX) provides a pull-
    parsing API for XML.
  • JCA: Connectors allow you to access EIS from a Java EE component. These could be databases, mainframes, or Enterprise Resource Planning (ERP) programs.
  • Security services: Java Authentication and Authorization Service (JAAS) enables services to authenticate and enforce access controls upon users. The Java Authorization Service Provider Contract for Containers (JACC) defines a contract between a Java EE application server and an authorization service provider, allowing custom authorization
    service providers to be plugged into any Java EE product.
  • Web services: Java EE provides support for SOAP and RESTful web services. The Java API for XML Web Services (JAX-WS), replacing the Java API for XML-based RPC (JAX-RPC), provides support for web services using the SOAP/HTTP protocol. The Java API for RESTful Web Services (JAX-RS) provides support for web services using the REST style.
  • Management: Java EE defines APIs for managing containers and servers using a special management enterprise bean. The Java Management Extensions (JMX) API is also used to provide some management support.
  • Deployment: The Java EE Deployment Specification defines a contract between deployment tools and Java EE products to standardize application deployment.

J2EE Architecture

Leave a comment

Java EE is a set of specifications implemented by different containers. Containers are Java EE runtime environments that provide certain services to the components they host such as life-cycle management, dependency injection, and so on. These components use well-defined contracts to communicate with the Java EE infrastructure and with the other components.
They need to be packaged in a standard way (via archive files) before being deployed. Java EE is a superset of the Java SE platform, which means Java SE APIs can be used by any Java EE components.
Figure 1-2 shows the logical relationships between containers. The arrows represent the protocols used by one container to access another. For example, the web container hosts servlets, which can access EJBs through RMI-IIOP.

First example of Scala in Hello World

Leave a comment

As a first example, we use the standard Hello world program to demonstrate the use of the Scala tools without knowing too much about the language.

object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
  }

UDP Client and Server in Ruby

Leave a comment

In the previous section we looked at creating a basic TCP client using net/telnet. However, to demonstrate a basic client/server system, UDP is an ideal place to start. Unlike with TCP, UDP has no concept of connections, so it works on a simple system where messages are passed from place to another with no guarantee of them arriving. Whereas TCP is like making a phone call, UDP is like sending a postcard in the mail.
Creating a UDP server is easy. Let’s create a script named udpserver.rb:

require 'socket'
s = UDPSocket.new
s.bind(nil, 1234)
5.times do
  text, sender = s.recvfrom(16)
  puts text
end

This code uses Ruby’s socket library, a library that provides the lowest-level access to your operating system’s networking capabilities. socket is well suited for UDP, and in this example you create a new UDP socket and bind it to port 1234 on the local machine. You loop five times, accepting data in 16-byte chunks from the socket and printing it to the screen.

■ Note The reason for looping just five times is so that the script can end gracefully after it receives five
short messages. Later, however, we’ll look at ways to keep servers running permanently.

Now that you have a server, you need a client to send data to it. Let’s create udpclient.rb:

require 'socket'
s = UDPSocket.new
s.send("hello", 0, 'localhost', 1234)

This code creates a UDP socket, but instead of listening for data, it sends the string “hello” to the UDP server on localhost at port 1234. If you run udpserver.rb at the same time as udpclient.rb, “hello” should appear on the screen where udpserver.rb is running. You have successfully sent data across a network (albeit on the same machine) from a client to a server using UDP.
It’s possible, of course, to run the client and server on different machines, and if you have multiple machines at your disposal, all you need to do is change ‘localhost’ on the send method to the hostname or IP address of the machine where udpserver.rb is running.
As you’ve seen, UDP is simple, but it’s possible to layer more-advanced features on top of it. For example, because there is no connection involved, you can alternate between client and server modes with a single program, accomplishing a two-way effect.
You can demonstrate this easily by making a single program send and receive UDP data to and from itself:

require 'socket'
host = 'localhost'
port = 1234
s = UDPSocket.new
s.bind(nil, port)
s.send("1", 0, host, port)
5.times do
  text, sender = s.recvfrom(16)
  remote_host = sender[3]
  puts "#{remote_host} sent #{text}"
  response = (text.to_i * 2).to_s
  puts "We will respond with #{response}"
  s.send(response, 0, host, port)
end

■ Note In a real-world situation you would typically have two scripts, each on a different machine and
communicating between each other, but this example demonstrates the logic necessary to achieve that
result on a single machine for ease of testing.

UDP has some benefits in speed and the amount of resources needed, but because it lacks a state of connection and reliability in data transfer, TCP is more commonly used. Next we’ll look at how to create some simple TCP servers to which you can connect with net/telnet and other applications.

Uploading Files in Ruby

Leave a comment

Uploading files to an FTP server is only possible if you have write permissions on the server in the directory to which you want to upload. Therefore, none of the examples in this section will work unedited, as you can’t provide an FTP server with write access (for obvious reasons!).
Uploading is the exact opposite of downloading, and net/ftp provides putbinaryfile and puttextfile methods that accept the same parameters as getbinaryfile and get-textfile. The first parameter is the name of the local file you want to upload, the optional second parameter is the name to give the file on the remote server (defaults to thesame as the uploaded file’s name if omitted), and the optional third parameter for putbinaryfile is the block size to use for the upload. Here’s an upload example:

require 'net/ftp'
ftp = Net::FTP.new('ftp.domain.com')
ftp.passive = true
ftp.login
ftp.chdir('/your/folder/name/here')
ftp.putbinaryfile('local_file')
ftp.close

Older Entries