Wednesday, 28 January 2015

Data-structures,sorting & searching with java Part1

Overview :- We will start with sorting techniques. To start fast we will pick the easiest sorting technique like Bubble  sort



Java program :- 


public class BubbleSort {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        //plain logic for bubble sort
        int a [] = {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};
        int count =0;
        for(int i=0;i<a.length;i++){
        count=count+1;
        for(int j=i+1;j<a.length;j++){
        count=count+1;
        if(a[i]>a[j]){
        int temp;
        temp =a[j];
        a[j] = a[i];
        a[i] = temp;      
        }
        }
        }
        System.out.println(" count via bubble sort first approach :- "+count);
        for(int i=0;i<a.length;i++){      
        System.out.println(" elements :- "+a[i]);
        }
        int b [] =  {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};    
        fasterBubbleSort(b);
        int c [] =  {4,30,-3,7,1,20,12,50,-9,-1,89,14,13};  
        bubbleSorting(c);
}
        //alternate logic for bubble sort
public static void bubbleSorting(int[] a){
int count =0;
for(int i=0;i<a.length;i++){
        count=count+1;
        for(int j=0;j<(a.length-i-1);j++){
        count=count+1;
        if(a[j]>a[j+1]){
        int temp;
        temp =a[j+1];
        a[j+1] = a[j];
        a[j] = temp;      
        }
        }
       }
System.out.println("count via alternate bubble sort :-"+count);
}
       //smarter logic for bubble sort
public  static void fasterBubbleSort(int[] testArray) {
System.out.println("length :-"+testArray.length);
     boolean isSwapped = true;
     int i = 0;
     int count =0;
     while (isSwapped ) {
           isSwapped = false;
           i++;
           for (int j = 0; j < testArray.length - i; j++) {
            count=count+1;
                 if (testArray[j] > testArray[j + 1]) {                        
                       int temp = testArray[j];
                       testArray[j] = testArray[j + 1];
                       testArray[j + 1] = temp;
                       isSwapped = true;
                 }
           }              
     }
     System.out.println("count via faster bubble sort :-"+count);
}

}


Simple explanation :- Take first element and go to all other elements and check > or < based on condition{ascending/descending} swap it. Like that do it for every element that's it. Don't confuse your selves with time complexity . Its nothing but how much time it is taking to execute your code. And how it will work when inputs are big. To know the time complexity just count how many loops the program is executing.

Now time complexity :- Now about the time complexity  for the bubble sort by leaving smart approach. n+n-1 + n-2... = n(n-1)/2 its our old school formula. The same thing happens here because we will be taking each value in array and compare with all other elements that exactly follows the sequence i have mentioned above.so n^2 /2 - n/2. When n^2 is very big we can remove n/2. So now we have to complexity of n^2/2.  



Saturday, 10 January 2015

Invoking web-service to consume web methods(Basic Web-Services Part4)


1)Using SoapUI Tool  :- Create new project in tool then provide WSDL url and project name then click ok.


Now click on any methods on left hand side and then select any request to view output


2)Using Java Client :-  We need to write a java client to invoke the web-service

package com.krishna.webservice.basics;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class WebServiceClient {

public static void main(String args[]){

try {
URL url = new URL("http://127.0.0.1:9876/ws?wsdl");
QName qname = new QName("http://basics.webservice.krishna.com/","NewJoinesImplService");
Service service = Service.create(url, qname);
JoiningFormalities joiningFormalities= service.getPort(JoiningFormalities.class);
System.out.println("Message :-"+joiningFormalities.showWelcomeGreetings());
System.out.println("Role :-"+joiningFormalities.getEmployeeRole());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Output :- 


First Webservice program for a fresh Learner(Basic WebService Part3)

Web-service :- Its a service built and deployed in server. Which can be accessed by any type of application irrespective of language platform. For more information please refer to my previous tutorials.

Service end point interface :-  Create an interface which a blue print for your web-service. If you annotate your Interface with @WebService then your interface is eligible for web service. If you annotate your methods as @WebMethod then methods inside the interface are exposed as web-service methods.

package com.krishna.webservice.basics;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface JoiningFormalities {

@WebMethod
public String getEmployeeRole();

    @WebMethod
public String showWelcomeGreetings();
}

Service implementation bean :- We need to create implementation class for our service interface. If we annotate our class with @webservice and point it to our interface then it will become implementation of our service.

package com.krishna.webservice.basics;

import javax.jws.WebService;

@WebService(endpointInterface="com.krishna.webservice.basics.JoiningFormalities")
public class NewJoinesImpl implements JoiningFormalities {

@Override
public String getEmployeeRole() {  
return "Team Lead";
}

@Override
public String showWelcomeGreetings() {
return "Welcome Krishna";
}

}

Publishing web-service :- Generally we publish webservices on application servers but using Endpoint class we can publish to our personal computer. (127.0.0.1) is our computer loopback address. 

package com.krishna.webservice.basics;

import javax.xml.ws.Endpoint;

public class PublishWebservice {
public static void main(String args[]){
 
Endpoint.publish("http://127.0.0.1:9876/ws", new NewJoinesImpl());
}
}

Testing Webservice by opening a browser :- 


WSDL file :- 

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://basics.webservice.krishna.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://basics.webservice.krishna.com/" name="NewJoinesImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://basics.webservice.krishna.com/" schemaLocation="http://127.0.0.1:9876/ws?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="getEmployeeRole">
<part name="parameters" element="tns:getEmployeeRole"></part>
</message>
<message name="getEmployeeRoleResponse">
<part name="parameters" element="tns:getEmployeeRoleResponse"></part>
</message>
<message name="showWelcomeGreetings">
<part name="parameters" element="tns:showWelcomeGreetings"></part>
</message>
<message name="showWelcomeGreetingsResponse">
<part name="parameters" element="tns:showWelcomeGreetingsResponse"></part>
</message>
<portType name="JoiningFormalities">
<operation name="getEmployeeRole">
<input message="tns:getEmployeeRole"></input>
<output message="tns:getEmployeeRoleResponse"></output>
//List of operations under this webservice
</operation>
<operation name="showWelcomeGreetings">
<input message="tns:showWelcomeGreetings"></input>
<output message="tns:showWelcomeGreetingsResponse"></output>
</operation>
</portType>
<binding name="NewJoinesImplPortBinding" type="tns:JoiningFormalities">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="getEmployeeRole">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
<operation name="showWelcomeGreetings">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
//Service end point declaration
<service name="NewJoinesImplService">
<port name="NewJoinesImplPort" binding="tns:NewJoinesImplPortBinding">
<soap:address location="http://127.0.0.1:9876/ws"></soap:address>
</port>
</service>
</definitions>


Sunday, 4 January 2015

Working with MLS in web content managment

MLS :- Multilingual site contains information localized across different users and different languages. MLS is available out of the box from portal8.0.0.1 and for below versions. We need to download it from greenhouse.

Step1(Registering MLS to portal) :-  To register you can run below two tasks{Make sure WasPassword and PortalAdminPwd is set in wkplc file before running below tasks}

  • ConfigEngine.bat register-wcm-mls
  • ConfigEngine.bat deploy-wcm-mls

Step2(Registering to virtual portal ):-  If you have virtual portal you need run below extra config engine task.

ConfigEngine.bat import-wcm-mls-data  -DVirtualPortalContext=dpath

Step3(Configuring Meber fixer tool ) :-  Run below task for Member fixer tool.

ConfigEngine.bat run-wcm-admin-task-member-fixer -DPortalAdminId=wpsadmin -DPortalAdminPwd=wpsadmin -DWasUserId=wpsadmin -DWasPassword=wpsadmin -Dlibrary=MLConfiguration_v7 -Dfix=true -DinvalidDn=update -DmismatchedId=update -DaltDn=update

Step4 :- Now navigate to administration you can find MLS copy link under administration using which we can copy library's from one language to other.

Creating credential vault slot for syndication

Overview :- To create a syndicator using which we can get all libraries from configured system.

Step1 :- Navigate till portal server bin and save below xml as CreateSyndSlot.xml

<?xml version="1.0" encoding="UTF-8"?>
<request
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="PortalConfig_8.0.0.xsd"
    type="update" create-oids="true">

    <!-- Sample for creating a new credential vault slot. This script creates a     -->
    <!-- credential vault resource and a shared slot in the Default Admin Segment   -->
    <portal action="locate">
         <credential-segment action="locate" adapter-type="default" name="DefaultAdminSegment"
             user-mapped="false">
             <description>Default Admin Segment</description>
             <credential-slot action="update" active="false" name="syndication-slot"
                 resource="syndication-resource" secrettype="userid-password" system="true">
                 <localedata locale="en">
                     <description>used for syndicator and subscriber pair</description>
                 </localedata>
                 <password-secret action="create" external-id="padmin"
                     user="padmin">padmin</password-secret>
             </credential-slot>
         </credential-segment>
    </portal>
</request>

Step2 :- Execute below command
xmlaccess.bat -in CreateSyndSlot.xml -out slot-out.xml -url http://localhost:10039/wps/config -user wpsadmin -password wpsadmin

Step3 :- Now navigate to portal administration click on subscribers and click on subscriber then enter the syndicator url , select above credential slot click ok then select the library you want to syndicate. Then select allitems then click ok.

Step4 :- After completing all these click on refresh button syndication will become active when it will become idle syndication is done.


Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...