Tuesday 21 March 2017

Find longest sequence of zeros in binary representation of an integer


 A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps.

Write a function:

int solution(int N);

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.

Solution:


 public class Test {  
      private static long maxConsecutiveZeroes(int x) {  
           boolean flagOne = false;  
           long count = 0, max = 0;  
           //Loop until x becomes 0.  
           while (x != 0) {  
                if ((x & 1) == 1) {  
                     flagOne = true;  
                     if (count > max) {  
                          max = count;  
                     }  
                     count = 0;  
                } else if (flagOne) {  
                     count += 1;  
                } else {  
                     count += 1;  
                }  
                x = x >> 1;//Move ht binary digits to right side bit by bit.  
           }  
           return max;  
      }  
      public static void main(String strings[]) {  
           System.out.println(maxConsecutiveZeroes(16));  
           System.out.println(maxConsecutiveZeroes(9));  
           System.out.println(maxConsecutiveZeroes(14));  
           System.out.println(maxConsecutiveZeroes(222));  
           System.out.println(maxConsecutiveZeroes(3));  
           System.out.println(maxConsecutiveZeroes(4));  
           System.out.println(maxConsecutiveZeroes(1041));  
      }  
 }  

Output:


 4  
 2  
 1  
 1  
 0  
 2  
 5  

Enjoy Coding.

CheckMarx Secure Code Analyzer


  1. Login into CheckMarx using your Credentials.
  2. Go to Projects & Scans and select " Create New Project ".



  3. After select, you need to give project name, Select Preset in the field (General Tab). As shown below.
    Example :- Project Name : Universal Payment
                      Preset : APPSEC-Java-JS



  4. Click on Next. you can see Location tab, Click Source Control then click on Select button. new window will popup in there you need to select folder for Source Control i.e. SVN, GIT etc. 
  5. In Port number by default 8080 will show, we need to change 8080 to 443.
  6. Select " Required Authentication " , Enter your User Name and Password (Enterprise ID and Password). and Click on OK.



  7. After Clicking OK you can see another window where you need to select " Root " folder of Project and Click on OK.


  8. After Clicking OK, you can see Source Control got selected, Then click on Next.


  9. In next Scheduling tab select " None " and click on Next.


  10. Next in Advanced Actions tab Enter email to Example : your_email Id and select run post scan action (New_Eport_Script: PrepareAgregatedResult4Splunk.bat [XML_output]). and click on Next.


  11. In next Custom Fields Tab, select Next, and in Data Retention tab select Finish button. you can see Project got Added.


  12. Click on Full scan. it will take time to scan Project.


  13. After completed scan, click on View Project Scan.


  14. After Clicking view project scan, you can see below window. click on Create Report.


  15. After Clicking create report you can see popup window. in there form Report Format select format which is you wants to be created Example : PDF, CSV etc. and click on Generate Report Button.


  16. After Clicking Generate report it will take some time to get generate the report and will download in your local machine (Download folder).

Enjoy Learning.

Cron job to run a jar file


If the requirement is to run the jar file on daily basis, we need to create a cron job.
To create a cron job, follow below steps:


  • Open terminal.
  • Type crontab -e ,It will open an editor to edit cron job.
  • If you want to run your jar on daily basis , put 0 0 * * * java -jar Sample.jar
  • For ex: 0 0 * * * java -Dlog4j.debug -DlogDir=./ -jar /opt/appl/giftcard/valuelinkSimulator/valuelink-simulator.jar.
  • Save the cron job.
We can also view the job using crontab -l command.

Enjoy Learning.

How to indent XML files automatically when opened in Sublime Text 2


To indent and beautify XML, Use SublimeText Indent plugin:sublimetext_indentxml.

  • Download the Package and Extract to Sublime Installation directory,inside package folder.For ex:C:\softwares\Sublime Text 2.0.2 x64\Data\Packages.
  • Open any XML file or create a new file and paste any XML into it.
  • Press Ctrl-K, F to indent it.

Enjoy Learning.

Monday 20 March 2017

MySQL Workbench Connection over SSH

Setting up MySQL Client

  • Install MySQL Workbench
  • To start MySQL in Local machine, Go To Start-> All Programs->MySQL->MySQL Workbench
  • OR
  • You can search in “Start” as MySQL Workbench in your Machine e.g Click OS Start button-> write MySQL Workbench
  • After MySQL started in your local machine you can see below page.
                     

  • Click on “+” button side to MySQL Connections as shown in below image
                   

  • It will open a new pop up page name “ Setup New Connection ” as shown in below image.
                   
  • Click on Connection Method dropdown, Choose “Standard TCP/IP over SSH” as shown below
                   


  • After selecting “Standard TCP/IP over SSH” you will see the below page.

                     


  • Note : If all options are not visible you can expand the small pop up page
  • Put these details in the new Pop up page ( Setup New Connection )
  • Connection Name : SampleConnection
  • SSH Hostname : servername.com
  • SSH Username : Your user name
  • Password : Your password
  • Password always needs to store in vault ( MySQL Secure place )
  • Follow same “Store in Vault …” Procedure as described above
  • Put password in the Password field
  • Press OK
  • MySQL Hostname : db servername.com
  • Username :db user name
  • Password :db password
  • Password always needs to store in vault ( MySQL Secure place )
  • Follow same “Store in Vault …” Procedure as described above
  • Default Schema : schema name(optional)
  • Press “Test Connection” You will see a connection success message.
  • Press "OK" Button
  • After Pressing "Ok" ButtonSampleConnection will be visible in below MySQL Connection Area

Enjoy Reading.


Adding Wildfly Server Plugin in Eclipse Luna


When we try to add a Wildfly 8 server in Eclipse Luna,By default it does not show Wildfly Servers.
In order to add Wildlfy Server, Please follow below Steps:

  • Go to Help–>Eclipse Market Place 

  • Search for Jboss Tools.

  • Find JBoss Tools(Luna) 4.2.3.Final and select only JbossAs tools and Jboss Webservice tools.

  • Click Next, agree to the license terms, install, restart Eclipse.

  • After Eclipse restarts:

  • Go to your Servers view and right-click.

  • Select "WildFly 8.x", click Next.

  • Set the Home Directory appropriately.

  • Click Finish.

  • Enjoy Reading. 

    Creating a self-signed certificate on Windows 7


    Using the Internet Information Services(IIS)

    By default, IIS 7.5 is not installed on Windows® 7 Professional, Enterprise, or Windows® 7 Ultimate. You can install IIS by clicking Windows Features in Advanced Options under Programs in Control Panel.

    Installing IIS using UI

    1. Click Start and then click Control Panel.
    2. In Control Panel, click Programs and then click Turn Windows features on or off.
    3. In the Windows Features dialog box, click Internet Information Services and then click OK.


    Creating Self signed Certificate

    4. Click Start, and in the Start Search box, type inetmgr and press ENTER.
    5. Click the computer name (top of the tree on top-left).
    6. Double click 'server certificates' from center pane.
    7. Click on the option of 'create self-signed certificate' from the right pane.
    8. Give some name for the certificate and click Ok.

    Exporting the certificate to save locally

    9. In the IIS console under Server Certificates, you would be able to see the certificate you created just now.
    10. Double-click on the certificate that you created which will open up the certificate in separate window.
    11. Go to the details tab and click Copy to File.. which will open a Certificate export Wizard in separate window.
    12. Click Next.
    13. Select "No,do not export the private key" and click Next again.
    14. Select DER encoded binary x.509(.CER) and click Next.
    15. Give the name(eg: LocalSSLCertificate) and path where you want to save this certificate locally and click Next.
    16. Click Finish.
    17. Repeat steps 9-16 with step 13 as Select "Yes, export the private key" and step 14 as Select Personal Information Exchange -PKCS #12(.pfx)
    18. For exporting the the certificate as pfx, you will be prompted to enter and re-enter password. Remember the password which will be used later (eg:changeit)

    Creating the keystore file

    Very important do not miss this step --> Navigate to the bin folder of your respective JRE/JDK (i.e. C:\Program Files\Java\jdk1.6.0_37\bin for Windows 7).  Failure to follow this step will yield the following error: keytool' is not recognized as an internal or external command, operable program or batch file' when attempting to run the below command requiring the keytool.exe file to be present.

    Next Go to Start->All Programs->Accessories
    Right click on Command Prompt and select Run as administrator

    Run the following commands.

    keytool -import -keystore jboss.jks -file D:localSSLcertificate.cer
    Enter keystore  password: changeit
    Re-enter  new password: changeit
    Trust this certificate? [no]:  y
    Certificate was added to keystore

    keytool -importkeystore -keystore jboss.jks -srckeystore D:localSSLcertificate.pfx -srcstoretype PKCS12
    Enter destination keystore password: changeit
    Enter source keystore password: changeit

    You will need to put the jboss.jks file in a place where all your current and future JBoss 7 installations can use it, as follows:

    1. In your Projects directory, create the directory Jboss
    2. Create the directory certs in the jboss directory you created above
    3. copy the file jboss.jks  to the jboss/certs directory you created above.
    Enjoy Reading.

    GitLab Code Chekout,Checkin and Merge Request

    This summary is not available. Please click here to view the post.

    How to Check if MySQL Instance is read only


    In order to find out if your DB server in read mode or read/write mode.

    Follow the below Steps:

    • Login into your DB Server using MySQL Tools,Mysqlem etc.
    • Run below queries:
      • SELECT @@global.read_only;
      • If the Output is 1, it means DB server is read only.
      • else if Output is 0, it means DB server is both read/write.




    Enjoy Reading.