Monday, October 29, 2012

OSGI

OSGi- Open Service Gateway initiative, OSGi defines a complete and dynamic component called Bundle.

Bundle (a jar) contains a manifest file MANIFEST.MF that specifies bundles it depends on, exposed API, excecution environment, etc.,.

Creating Bundles
A Sample MANIFEST.MF built using maven bundle plugin
Manifest-Version: 1.0
Export-Package: demo.osgi.sort
Private-Package: demo.osgi.activator
Built-By: Murali
Tool: Bnd-0.0.238
Bundle-Name: SomeOSGiBundle
Created-By: Apache Maven Bundle Plugin
Bundle-Version: 0.0.1.SNAPSHOT
Build-Jdk: 1.6.0_20
Bnd-LastModified: 1349471796912
Bundle-ManifestVersion: 2
Bundle-Activator: demo.osgi.activator.DemoBundleActivator
Bundle-SymbolicName: sample.osgi.SomeOSGiBundle
Import-Package: demo.osgi.sort,org.osgi.framework;version="1.4"
generated for below configuration-
<plugin>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>1.4.0</version>
 <extensions>true</extensions>
 <configuration>
  <instructions>
   <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
   <Bundle-Name>${project.name}</Bundle-Name>
   <Bundle-Version>${project.version}</Bundle-Version>
   <Bundle-Activator>demo.osgi.activator.DemoBundleActivator</Bundle-Activator>
   <Private-Package>demo.osgi.activator</Private-Package>
   <Export-Package>demo.osgi.sort</Export-Package>
  </instructions>
 </configuration>
</plugin>

Deploy bundles 
Eclipse Equinox and Apache Felix are 2 OSGi implementations.

Felix Gogo Shell- osgi command shell, to install, update, remove bundles.
Felix Remote Shell - Telnet to remote osgi instance to administer.

Service Platform
OSGi runtime is a service platform where a bundle provide for/ consume services from other bundle.
 

Sunday, October 28, 2012

Serialization

java. lang.Serializable is a marker interface. A class marked as Serializable is capable of being serialized by jvm provided mechanism.

Serialization mechanism serializes the state of the object including all the member variables except fields marked static or volatile.

During de-serialization volatile variables are assigned default values.

The default de/serialization mechanism can be overridden by providing two method implementations to the object

private void readObject(java.io.ObjectInputStream s){}
private void writeObject(java.io.ObjectOutputStream s){}

Please refer HashMap api for better understanding.

private void writeObject(java.io.ObjectOutputStream s){
   // below statement gives you the default behavior, skips volatile/static var
   s.defaultWriteObject();
}
private void readObject(java.io.ObjectInputStream s){
   // below statement gives you the default behavior, skips volatile/static var
   s.defaultReadObject();
}

A more customized implementation is in HashMap-
private void writeObject(java.io.ObjectOutputStream s){
   // below statement gives you the default behavior, skips volatile/static var
   s.defaultWriteObject();
   // write any other field
   s.writeInt(anIntegerProperty);
   s.writeObject(composedObject);
}
private void readObject(java.io.ObjectInputStream s){
   // below statement gives you the default behavior, skips volatile/static var
   s.defaultReadObject();
   //read any other field
   anIntegerProperty=s.readInt();
   composedObject=(ComposedObject)s.writeObject(); //typecast
}
A generated serialVersionUID for a serializable class is for JVM to check during deserialization that serialized content is compatible with object being constructed.
serialVersionUID can be same for compatible class version. This by default is written out to the stream during serialization.

Thursday, October 25, 2012

Modify XML using StAX


Sample XML
<?xml version="1.0"?>
<connection>
    <number id="1"></number>
    <number id="2"></number>
    <number id="3"></number>
</connection>
ModifyXML.java
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class ModifyxmlSelf {
 public static void main(String[] args) {
  XMLInputFactory ifactory=XMLInputFactory.newFactory();
  XMLOutputFactory ofactory=XMLOutputFactory.newFactory();
  StreamSource source =new StreamSource("connection.xml");
  StreamResult result=new StreamResult("connection.xml");
  try {
   XMLEventReader in=ifactory.createXMLEventReader(source);
   XMLEventWriter out=ofactory.createXMLEventWriter(result);
   while(in.hasNext()){
    XMLEvent e=in.nextEvent();
    if(e.isStartElement() && ((StartElement)e).getName().getLocalPart().equalsIgnoreCase("Number")){
     Attribute a=((StartElement)e).getAttributeByName(new QName("id"));
     if(a.getValue().equalsIgnoreCase("3")){
      in.next();
      continue;
     }
    }
    out.add(e);
   }
   in.close();
   out.close();
  } catch (XMLStreamException e) {
   e.printStackTrace();
  }
 }
}

output XML
<?xml version="1.0"?>
<connection>
    <number id="1"></number>
    <number id="2"></number>
</connection>

Saturday, August 18, 2012

Using File Resources

Loading a file in Java should be done through, either:
Class.getResource()
Class.getClassLoader().getResourceAsStream();

These two vary in the way they locate the file.
Class.getResource() searches file relative to package location of the class.

ClassLoader search happens by delegating the findResource to parent ClassLoader before this classloader itself does try. Specifying absolute path does not work.

Interface Vs Abstract Class

Interface Vs Abstract Class
A frequent question in interviews is when one should choose Interface and when to go with abstract class?
Interface specifies a "Is-Capable-of" doing something.
Abstract specifies a "Is-a" relation.

I would define a interface when there is necessarily nothing in common but the implementation should do somthing specific.
e.g. Iterator inteface- no specific on what type of data structure and what condition would it iterate.
e.g. Actor who is also a Director.

Abstract is to provide a default behavior. If I expect the system to undergo changes and requires a default behaviour over which special behaviour can be built.

Actor Director example