A dive into design patterns used in jdk – II

This is part two of design patterns used in JDK where I shall try to cover some more patterns definition and their usage in JDK. You can find the First part of this series Here.

Adapter – Convert the interface of a class into another interface clients expect. / Adapter lets classes work together, that could not otherwise because of incompatible interfaces. Note that there are two types of adapter, one is class level adapter and other is object level adapter. Examples –

  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)
  • javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()

Bridge – Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Examples –

  • JDBC-ODBC Bridge

It is quite difficult to understand from its definition So here are few links to refer –
Link 1 Link 2

Composite – Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly. Examples –

  • Includes in JSP
  • java.util.Map#putAll(Map)
  • java.util.List#addAll(Collection)
  • java.util.Set#addAll(Collection)
  • java.nio.ByteBuffer#put(ByteBuffer) (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
  • java.awt.Container#add(Component) (practically all over Swing thus)

A very detailed explanation about Composite pattern is Here

Decorator – add additional responsibilities dynamically to an object. Examples –

  • All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
  • Almost all implementations of java.util.List, Set and Map have a constructor taking an instance of same type.
  • java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.
  • javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper
  • Display tag custom tag library proved option to decorator for rendering of tables in JSP.
  • Any client side JavaScript library which has render-er or parser like Yahoo UI datatable, JQuery grid

Flyweight – use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary. Examples –

  • java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Long, Float and Double)
  • Java String creation. (Read about string creation in Java specification)
  • Swing borders

Proxy – provide a “Placeholder” for an object to control references to it. If you use spring framework, you should be very well acquainted with Proxy. Examples –

    • Java RMI
    • Spring AOP creates proxies for supplied objects
    • All libraries which use Java proxy , Reflection proxy and CGLIB proxy

Memento – capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.

      • java.util.Date (the setter methods do that, Date is internally represented by a long value)
      • All implementations of java.io.Serializable
      • All implementations of javax.faces.component.StateHolder
Posted in Design patterns | Tagged | 2 Comments

A dive into design patterns used in jdk – I

Design patterns is a very crucial topic in field of software engineering. Everybody wants to learn them in more and more depth. There is lot of literature available like GOF book and head first design patterns and other references. However some times it becomes difficult to map design patterns we use in our normal daily routine coding. We can find huge number of examples in books about the conceptual explanation and examples of design patterns but I would like to write down a short note on pattern definition and their standard usage in JDK.

This is just a page to revise in case you are planning for interview next day , provided you have already read patterns in details. I would not go with explanations and standard examples of coffee/tv remote or any fictional objects.

Pattern – “A pattern is a proven solution to a problem in a context.”
or in other words
“Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution.”

Singleton – Ensure that only one instance of a class is created and Provide a global access point to the object.
Examples –

  • java.lang.Runtime#getRuntime()
  • java.awt.Desktop#getDesktop()

Factory – Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
Examples –

  • java.lang.Object#toString() (overrideable in all subclasses)
  • java.lang.Class#newInstance()
  • java.lang.Integer#valueOf(String) (also on Boolean, Byte, Character, Short, Long, Float and Double)
  • java.lang.Class#forName()
  • java.lang.reflect.Array#newInstance()
  • java.lang.reflect.Constructor#newInstance()

Abstract Factory – Offers the interface for creating a family of related objects, without explicitly specifying their classes. We can simply say that it is higher level of abstraction of factory pattern.
Examples –

  • java.util.Calendar#getInstance()
  • java.util.Arrays#asList()
  • java.util.ResourceBundle#getBundle()
  • java.net.URL#openConnection()
  • java.sql.DriverManager#getConnection()
  • java.sql.Connection#createStatement()
  • java.sql.Statement#executeQuery()
  • java.text.NumberFormat#getInstance()
  • java.lang.management.ManagementFactory (all getXXX() methods)
  • java.nio.charset.Charset#forName()
  • javax.xml.parsers.DocumentBuilderFactory#newInstance()
  • javax.xml.transform.TransformerFactory#newInstance()
  • javax.xml.xpath.XPathFactory#newInstance()

Observer – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Examples –

  • java.util.Observer/java.util.Observable
  • All implementations of java.util.EventListener (practically all over Swing thus)
  • javax.servlet.http.HttpSessionBindingListener
  • javax.servlet.http.HttpSessionAttributeListener
  • javax.faces.event.PhaseListener

Builder – Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process. We can say the simplest builder will be similar to factory. Basically Builder allows you to give options for creating objects.
Examples –

  • java.lang.StringBuilder#append() (unsynchronized)
  • java.lang.StringBuffer#append() (synchronized)
  • java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
  • javax.swing.GroupLayout.Group#addComponent()
  • All implementations of java.lang.Appendable

Prototype – Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Examples –

  • java.lang.Object#clone() (the class has to implement java.lang.Cloneable)

Chain of Responsibiliy – It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.
– The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.

  • java.util.logging.Logger#log()
  • javax.servlet.Filter#doFilter()

In this I have covered seven patterns. Keep pinging the blog for next parts in this series. 🙂
This is not any reference or detailed documentation. You can always refer to more resources for detailed explanation of patterns.

Posted in Design patterns | Tagged , | 2 Comments

Setting Jboss 5.0 for standalone JMS consumers/publishers

Most of programmers get chance to work on MOM (Messaging oriented Middleware). Generally all queues,topics etc are maintained by Middleware team or some other team and developers don’t get much chance to play with topics/queues and setup same on their local machines.
In this article I shall explain how to setup JBoss 5.0 on local machine to work as destination for topics or queues.
To setup you need to have Jboss 5.0 , JDK 1.6 onyour machine. You will also need and IDE like Eclipse to write sample programs for consuming and publishing messages.

The Java Messaging Service (JMS) has been implemented by means of different providers in the releases 4.x, 5.x and 6.x of JBoss application server. Setup includes two basic steps

  1. Setup of destinations on JBoss AS server
  2. Setup of JBoss libraries on standalone client

1. Setup of destinations on JBoss AS server

In JBoss 5.0 we need to edit destinations-services.xml in the messaging folder the deploy folder. It may be present in deploy folder in some cases depending on your configuration. Add the following code respectively

For Queue –

<mbean xmbean-dd="xmdesc/Queue-xmbean.xml" name="jboss.messaging.destination:service=Queue,name=SourabhQueue1" code="org.jboss.jms.server.destination.QueueService">
  <attribute name="JNDIName">queue/SourabhQueue1</attribute>
  <depends optional-attribute- name="ServerPeer">jboss.messaging:service=ServerPeer</depends>

For Topic –

 <mbean xmbean-dd="xmdesc/Topic-xmbean.xml" name="jboss.messaging.destination:service=Topic,name=SourabhTopic1" code="org.jboss.jms.server.destination.TopicService">
  <attribute name="JNDIName">topic/SourabhTopic1</attribute>
  <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>

2.Setup of JBoss libraries on standalone client

Make sure you add JBoss libraries to your classpath.
How to get the initial context :

public static Context getInitialContext( ) throws javax.naming.NamingException {
   Properties p = new Properties( );
   p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
   p.put(Context.URL_PKG_PREFIXES," org.jboss.naming:org.jnp.interfaces");
   p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
   return new javax.naming.InitialContext(p);

Get the Connection factory/Connection and Session

    String queueName = "queue/SourabhQueue1";
    String topicName = "topic/SourabhTopic1";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection =  null;
    private Topic topic;
    private TopicSession topicSession;
    private QueueSession queueSession;
    connectionFactory = (ConnectionFactory) ctx.lookup("/ConnectionFactory");
    conn = connectionFactory.createQueueConnection();
    queueSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    topic = (Topic) ctx.lookup(topicName);
    MessageProducer topicPublisher = topicSession.createPublisher(topic);
    queueSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    MessageProducer queuePublisher = queueSession.createProducer(queue);

You can change the JNDI name of connection factory or you can create your own by editing connection-factories-service.xml in the same directory where you found destinations-services.xml .

You can get subscriber and listen in the same way once you get the session.

    MessageConsumer consumer = queueSession.createConsumer(queue);
    MessageConsumer consumer = topicSession.createConsumer(topic);

As always a best practice, make sure you close all resources in finally clause. Here you need to close InitialContext and Connection.

You can send and receive simple text messages like

    TextMessage message = queueSession.createTextMessage("Hello Sourabh Girdhar");

To receive messages, you need to implement MessageListener interface and override the onMessage() method as per JMS specification.

    public void onMessage(Message msg) {
	TextMessage message = (TextMessage) msg;
	String text = null;
		text = message.getText();
		System.out.println("Message received - " + text);
	}catch (JMSException jme){

In my next posts I shall be explaining about different patterns in Messaging. Keep looking 🙂

Posted in JMS, Middleware | Tagged , , | 5 Comments

>Digging into Tibco Rendezvous network details – II


Daemon Parameter –
The daemon parameter instructs the transport creation function about how and where to find the Rendezvous daemon and establish communication. Each Rendezvous transport establishes a communication conduit with theRendezvous daemon, as the following steps describe:
  1. The daemon process opens a (TCP) client socket, and waits for a client to request a connection.The -listen option of the Rendezvous daemon (rvd) specifies the socketwhere the Rendezvous daemon should listen for new client programconnections.
  2. The program calls the transport creation function, which contacts the daemon at the client socket specified in its daemon parameter. The daemon parameter of the transport creation function must correspond tothe -listen option of daemon process; that is, they must specify the same communication type and socket number.If no daemon process is listening on the specified client socket, then thetransport creation call automatically starts a new daemon process (which listens on the specified client socket), and then attempts to connect to it.
  3. The daemon process opens a conduit for private communication with the newtransport object in the program. All future communication uses that conduit.The request socket is now free for additional requests from other clienttransports.
  • Specifying a Local Daemon – For local daemons, specify the transport creation function‘s daemon parameter andthe -listen option to the daemon process as a (TCP) socket number; for example:”6555″.To use the default client socket, supply NULL as the daemon argument to thetransport creation function, and omit the -listen option to the daemon process.
  • Remote Daemon – In most cases, programs connect to a local daemon, running on the same host asthe program. Certain situations require a remote daemon, for example: The program runs on a laptop computer that is not directly connected to thenetwork. Instead, the laptop connects to a workstation on the network, andthe daemon runs on that workstation. Another example could be that the program connects to a network at a remote site.

For remote daemons, specify two parts (introducing the remote host name as thefirst part): Remote host name: Port number. For example:”purple_host:6555″

Network Parameter –

Every network transport object communicates with other transport objects over anetwork. On computers with only one network interface, the Rendezvous daemon communicates on that network without further instruction from the program. On computers with more than one network interface, the network parameter instructs the Rendezvous daemon to use a particular network for all communications involving this transport. To communicate over more than one network, a program must create a separate transport object for each network. The network parameter also specifies multicast addressing details
The network parameter consists of up to three parts, separated by semicolons—network, multicast groups, send address—as in these examples:

lan0 — > network only

lan0; –> one multicast group

lan0;,; –> two multicast groups, send address

lan0;; –>no multicast group, send address

  1. Part one identifies the network, which you can specify in several ways: – Host name, Host IP address, Network name, Network IP number, Interface name, Default TRDP daemons use the network interface which corresponds to the hostname of the system as determined by the C function gethostname(). PGM daemons use the default PGM multicast interface,
  2. Part Two—Multicast Groups – Part two is a list of zero or more multicast groups to join, specified as IP addresses, separated by commas. Each address in part two must denote a valid multicast address. Joining a multicast group enables listeners on the resulting transport to receive data sent to that multicast group.
  3. Part Three—Send Address, Part three is a single send address. When a program sends multicast data on the resulting transport, it is sent to this address. (Point-to-point data is not affected.) If present, this item must be an IP address—not a host name or network name. The send address need not be among the list of multicast groups joined in part two. If you join one or more multicast groups in part two, but do not specify a send address in part three, the send address defaults to the first multicast group listed in part two.
Posted in Middleware, RV, Tibco | 1 Comment

>Digging into Tibco Rendezvous network details – I


Tibco RV is is one of the most used messaging systems. Athough lot of people use RV but there are some concepts which are known mainly to admins. These are some concepts which are better to be known by developers also for better programming.

Rendezvous Daemon

The Rendezvous daemon is a background process that supports all Rendezvous communications. Distributed processes depend on it for reliable and efficient network communication. All information that travels between processes passes through a Rendezvous daemon as it enters a host computer or exits a sending process. Its functions are as following :-
• Transmits outbound messages from program processes to the network.
• Delivers inbound messages from the network to program processes.
• Filters subject-addressed messages.
• Shields programs from operating system idiosyncrasies, such as low-level sockets.

The Rendezvous daemon process, rvd, starts automatically when needed, runs continuously and may exit after a period of inactivity. We can run the deamon on our local machine or can even connect to another daemon running on some remote machine.

Transport –

Programs use transport objects to send messages and listen for messages. A transport determines three aspects of message delivery:
  1. Delivery scope—the potential range of its messages
  2. Delivery mechanism—the path (including software, hardware and networkaspects) that its messages travel
  3. Delivery protocol—the ways in which programs cooperate and shareinformation concerning message delivery. Various types of transport object combine these aspects to yield different qualities of service—for example, intra-process delivery, network delivery, reliabledelivery, virtual circuit, certified delivery, and distributed queue delivery.
Rendezvous software distinguishes between two broad classes of transports.

• Network transports – A network transport delivers messages across a network, to processes on one ormore hosts. It also can deliver messages to several processes on a single host. All messages pass through a Rendezvous daemon process.The service, network and daemon parameters of a network transport specifyits scope within the network

• Intra Process Transport – An intra-process transport delivers messages between program threads within asingle process.Intra-process messages do not pass through a Rendezvous daemon process;instead, they remain local within the program process. Intra-process messagesare significantly faster than network messages.Each program contains exactly one intra-process transport; the Rendezvousopen call creates it automatically. Its scope is the program process. Any threadcan dispatch intra-process events from an appropriate queue.

Service parameter

The service parameter instructs the Rendezvous daemon to use this servicewhenever it conveys messages on the transport. As a direct result, services divide the network into logical partitions. Eachtransport communicates on a single service; a transport can communicate onlywith other transports on the same service. To communicate on more than oneservice, a program must create a separate transport object for each service. Within each rvd process, all the transports that specify a given service mustspecify the same network parameter. That is, if the service parameters resolve tothe same UDP or PGM port, then the network parameters must also be identical.(This restriction extends also to routing daemons.)

For example, suppose that the program foo, on the computer named orange, hasa transport that communicates on the service svc1 over the network lan1. It isillegal for any program to subsequently create a transport (connecting to the samedaemon process on orange) to communicate on svc1 over any other network—such as lan2. Once rvd binds svc1 to lan1, that service cannot send outboundbroadcast messages to any other network. Attempting to illegally rebind a serviceto a new network fails; transport creation calls produce an error status(TIBRV_INIT_FAILURE).

The limitation is not as severe as it might seem at first, because it only affectsoutbound broadcast messages:

–> Point-to-point messages on the transport’s service travel on the appropriatenetwork (as determined by the operating system) irrespective of thetransport’s network parameter.

–> Inbound broadcast messages on the transport’s service can arrive from anynetwork, irrespective of the transport’s network parameter.

Rendezvous programs specify services in one of three ways, shown below inorder of preference:

  1. Service name – When you specify a service name, the transport creation function callsgetservbyname(), which searches a network database (such as NIS) or a flat file(such as services in the system directory).
  2. Port number – When you specify a port number, it must be a string representing a decimalinteger (for example, “7890”).
  3. Default – If you specify null, the transport creation function searches for the service namerendezvous. If getservbyname() does not find rendezvous, the Rendezvous daemoninstructs the transport creation function to use a hard default:

The TRDP daemon offers the default service 7500.The PGM daemon offers the default service 7550.

Posted in Middleware, RV, Tibco | 1 Comment

>Software/Package Management in Ubuntu/debian


Ubuntu’s package management system is derived from the same system used by the Debian GNU/Linux distribution. The package files contain all of the necessary files, meta-data, and instructions to implement a particular functionality or software application on your Ubuntu computer.
Debian package files typically have the extension ‘.deb’, and typically exist in repositories which are collections of packages found on various media, such as CD-ROM discs, or online. Packages are normally of the pre-compiled binary format; thus installation is quick and requires no compiling of software.

dpkg – dpkg is a package manager for Debian based systems. It can install, remove, and build packages, but unlike other package management system’s, it can not automatically download and install packages or their dependencies. I shall show examples of dpkg to manage locally installed packages:

To list all packages installed ==> dpkg -l
To list all files installed by package specified as argument ==> dpkg -L {package-name}
To install local package ==> sudo dpkg -i {package-name}
To unistall a package ==> sudo dpkg -r {package-name}

There are lot of other options also which we can use with dpkg. For more details refer to man page of dpkg.

Apt-Get – The apt-get command is a powerful command-line tool used to work with Ubuntu’s Advanced Packaging Tool (APT) performing such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.
Apt-get maintains a list of packages available on repositories specified in /etc/apt/sources.list file. Unlike dpkg Apt-get automatically download the package from repository if available.

To install a new package ==> sudo apt-get install {package-name}
To remove a package ==> sudo apt-get remove {package-name}
To update index of packages ==> sudo apt-get update
To upgrade packages ==> sudo apt-get upgrade
You can find more detailed information on apt-get by typing man apt-get.

There are other tools also available like Aptitude to do all this. Ubuntu have very good graphical interfaces to do all above written stuff but if you are a Linux fan, you would love to do it from Command-line

Managing repositories for apt-get – In addition to the officially supported package repositories available for Ubuntu, there exist additional community-maintained repositories which add thousands more potential packages for installation. Two of the most popular are the Universe and Multiverse repositories. These repositories are not officially supported by Ubuntu, but because they are maintained by the community they generally provide packages which are safe for use with your Ubuntu computer.

  • Packages in the Multiverse repository often have licensing issues that prevent them from being distributed with a free operating system, and they may be illegal in your locality.
  • Be advised that neither the Universe or Multiverse repositories contain officially supported packages. In particular, there may not be security updates for these packages.

You can add/remove repositories either from UI by going to System->Administration->Software Sources or by editing /etc/apt/sources.list file.

Posted in Apt-get, Linux, Package Management, Ubuntu | Leave a comment

>Link Files in Unix/Linux

>Links in Unix – Links are used in Unix/Linux for better managebility of file system.

Command we use for linking two files is ln.

ln -[options] existingfilename newfilename

Common options

-f —> Link files without questioning the user, even if the mode of target forbids writing.
-s —> Creates a symbolic link to existingfilename with the name of the newfilename.

Hard Link – A hard link is a reference to a file or directory that appears just like a file or directory, not a link. Hard links only work within a filesystem. In other words, don’t use hard links between mounted filesystems. A hard link is only a reference to the original file, not a copy of the file. Basically points to a file’s inode. So if you delete one file , it can still be accessed from other hard link available.

How do we check hard links
If we want to check hard links in one specific directory we can simple use ls -i command which list the files with their respective inode number.

If we want to find all files hard linked with one file we can use find command in following way –
find {location} -xdev -samefile {filename}

ls -lrt shows the number of links to a file. The column next to permissions shows number of links to that file.

Soft Link – A soft link/symbolic link is a pointer to another file or directory. It can be used just like the original file or directory. A symbolic link appears in a long listing (ls -l) with a reference to the original file/directory. A symbolic link, as opposed to a hard link, is required when linking from one filesystem to another and can be used within a filesystem as well.

How to find soft links
If we do ls -lrt in one directory it shows the soft links with target files.
Find all links in system – find {location} -type l

How to unlink files

unlink filename

PS: unlink and rm both remove the files, the difference is that unlink is a system call, rm is a shell utility that calls unlink.
Keep checking for more posts on Unix/Linux
Posted in Filesystem, Link, Linux, Unix | 2 Comments