Posts

Shift operator in java

Shift operator in java Java supports two types of shift operator (Logical Shift and Arithmetic shift). Logical Shift : In logical shift simply all the bits except the extreme left or right bit is moved by one place. and The extra bit from the other end is replaced with zero. For Example for The number 4 (binary = 0000 0100) Logical Shift ( >>>, << )                              original number        0 000 0100              (Decimal 4)                              after left shift           000 0100 0              (Decimal 8)                              original number         ...

Formating XML for Pretty Printing

The following Class is an example which we can use to Pretty Printing XML. method parseXmlString() reads a String and build a Document Object. method formatXMLString() uses Transformer Object's Outputproperties to set features like Encoding, Indentation etc. public class XmlFormatter { public String formatXMLString(String string) throws Exception { final Document document = parseXmlString(string); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "no"); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); } private Document parseXmlString(String in) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstan...

Choosing Java Collection

The following section summarizes the various Collection Classes as of Java 7 and their important attributes Including HashMap which is not in Collection Framework. HashMap. It provides constant time operation for operations like get and put and remove. Iteration over the Collection values requires time proportional to the capacity of the Map.(values()). Default load factor is .75. HashMap does not maintain the order. HashMap is not synchronized. LinkedHashMap LinkedHashMap orders are maintained It provdes  constant-time performance for operations like get, put and remove. Performace is slightly slower then HashMap due to the overhead of maintaining Linked List. Re-insertion does not effect the insertion order. Iteration over the collection values is proportional to the size of the map    regardless capacity . TreeMap Red-Black tree based implementation. Implement the SortedMap Interface. Map is ascending Key order of the natural order. It has log(n) ...

Difference between wait() and sleep methods in Java

The following section captures the main difference between wait and sleep method wait method wait sends the current thread to non-runnable  state thread releases the lock on the object before going to non-runnable state. wait ()  method is defined on Object class wait ()  is a instance method wait() should be called when the thread has already  acquired  the lock on the object the thread will be in non-runnable state untill the time is elapsed or another thread  invoked  notify() / notifyAll() method on the object sleep ()  method sleep ()  method will send the current thread to non-runnable state for the specified time. sleep() method is defined in Thread class sleep() method is a Static method. sleep ()  does not cause the thread to releases the lock on the object

JAX-WS Webservice Endpoint and Client

JAX-WS based Webservice and Client Design Options. JAX-WS stands for Java Api for XML Web services. JAX-WS supports both SOAP 1.1 and SOAP 1.2. The following section tries to snapshot the different ways we can use JAX-WS to write both Web Services and Clients. What is Web service Endpoint? A web service endpoint is an server side resource that can be referenced by client and to which web services messages can be addressed. Generally, web services will have endpoint and a WSDL file to share its descriptions with clients. Clients use the web service endpoint description to generate/write code that can send SOAP messages to and receive SOAP messages from the web service endpoint. In JAX-WS the endpoints are annotated with the javax.jws.WebService annotation or   javax.jws.WebServiceProvider annotation, but not both at a time. Services designe options  . JAX-WS broadly supports two Webservice  models, SEI Interface Annotated with the javax.jws.WebServic...

Multiple Expression Execution on XPath

How to specify Multiple XPath Expression at a time Sometimes we need to specify  multiple  XPath  e xpressions to evaluate XML file. For instance let's consider the following XML File. <FieldCollection> <VarificationField> <CreditCardNumber>378291726006009</CreditCardNumber> <CreditCardCVV>1234</CreditCardCVV> <fieldName>CreditCard</fieldName> </VarificationField> </FieldCollection> Say we need to select two different nodes say  CreditCardNumber and  CreditCardCVV out of a xml file and mask them. We can use "|" separator for specifying more then one path, like as follows. //*/CreditCardNumber/text() | //*/CreditCardCVV/text() As shown above we piped two different XPath to build a single XPathExpression which would be used to evaluate the XML documents. String path = "//*/CreditCardNumber/text() | //*/CreditCardCVV/text() "; XPathFactory factory = XPathFactory.newInstance...

Publishing Web Service Quickly with Java Technology

javax.xml.ws.EndPoint can be used to publish a webservice without any Application Server or Servlet Container. It uses HTTP Server that comes with Java. The static method  publish takes the String endpoint address and web service implementor class object as parameter. public class EndpointPublish { public static void main(String[] args) { Endpoint.publish("http://localhost:8088/ws/SiddMyTestWS", new SimpleHellowServiceImpl ()); } } For example just running the above stand alone java program, we are able to publish a webservice with the address   http://localhost:8088/ws/SiddMyTestWS . Where  MyWSTest is the implemented Endpoint class. Implemented EndPoint class in JAX-WS should have the following properties The implemented class should be annotated  javax.jws.WebService or javax.jws.WebServiceProvider. The implemented class should not be final or abstract . Exposed web service method should be annotated with javax.jws.WebMethod. Web service meth...