Monday, 4 September 2017

INSTALL MYSQL ON CENTOS 6 WITH REMOTE ACCESS:-


rpm -qa | grep mysql
yum remove <ouput mysql label>
yum -y install epel-release; yum clean all
yum -y install mysql-server mysql pwgen supervisor bash-completion psmisc net-tools; yum clean all

#optional steps
mysql_install_db
chown -R mysql:mysql /var/lib/mysql
/usr/bin/mysqld_safe & sleep 10

mysqladmin -u root password jagdeep
#login to mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jagdeep' WITH GRANT OPTION; FLUSH PRIVILEGES;
>>OR without login
mysql -uroot -pjagdeep -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jagdeep' WITH GRANT OPTION; FLUSH PRIVILEGES;

#Update password:-
         mysql -u root -p
mysql> use mysql;
mysql> update user set password=PASSWORD("JAGDEEP") where User='root';
mysql> flush privileges;
mysql> quit
/etc/init.d/mysqld restart

#execute mysql db script after login in to mysql
        >> source /home/jagdeep/test.sql;


#HERE ADDING PORT IN FIREWALL
>>vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
# No need to add rule for output because in output accepting all
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

>>/etc/init.d/iptables restart
##IF YOU DON'T WANT TO ADD RULE IN FIREWALL THEN YOU  NEED TO STOP IPTABLES
>>/etc/init.d/iptables stop

[mysqld]
datadir=/var/lib/mysqld
socket=/var/lib/mysql/mysql.sock
user=root
symbolic-links=0
 lower_case_table_names=1
bind-address=0.0.0.0
Note:- lower_case_table_names is used for case insensitive.

#Mysql Admin command:-
    /usr/bin/mysqladmin -u root password 'new-password'
   mysqladmin -uroot -pjagdeep variables
   mysqladmin -uroot -pjagdeep version variables

 Note:-  By using "mysqladmin -uroot -pjagdeep variables" this command we can see default  variable values. If we want to override it, it can be done by my.cnf file.

Thursday, 31 August 2017

Vagrant

1. Need to download virtual machine from  https://www.virtualbox.org/wiki/Downloads
2. Download VM Package according to version https://www.virtualbox.org/wiki/Downloads
3. Download vagrant from  https://www.vagrantup.com/downloads.html

Firstly, install VM
Then install vagrant, which will be help to setup the vagrant environment for setup the VM
After installing vagrant , Run below mentioned command:-

> vagrant init
  - This command will generate the default vagrant file and directory structure
  - Edit the vagrant file and update below mentioned code in vagrant file

    VAGRANTFILE_API_VERSION = "2"


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define :chefMaster do |master|
  master.vm.hostname = "<here enter host i.e. abc.com>"
  master.vm.box      = "<here enter box name i.e. base>"
             master.vm.boot_timeout = <time out in number>
  master.vm.box_url  = "<here enter box image url>"
 master.vm.network :private_network, ip: "<here type ip address>"    
 master.vm.network :forwarded_port, guest: 443, host: 8443

 config.vm.provider :virtualbox do |master|
master.customize ["modifyvm", :id, "--memory", "1024"]
 end

end

end


> vagrant up
   - This command will download the vm at local and access it with mentioned ip in vagrant file.
   - Default credential is root/vagrant

Monday, 10 April 2017

Wednesday, 24 February 2016

Saturday, 10 May 2014

Date Time Examples

Get Time Before Week:-

Calendar calNow = Calendar.getInstance();        
calNow.add(Calendar.DAY_OF_MONTH, -7);
Date dateBeforeAWeek = calNow.getTime();    
Timestamp beforeWeekTime = new Timestamp(dateBeforeAWeek.getTime());

Convert timestamp to string:-

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);    
           String stringTimestamp =  sdf.format(new Date(timestamp.getTime()));

Time stamp to date:-

               new Date(timestamp.getTime());

Time stamp is equals:-

            (timestamp1.compareTo(timestamp2)==0)

Timestamp greater:-

           (timestamp1.compareTo(timestamp2)==1)

Date To time stamp:-

               new Timestamp(date.getTime());

Convert String Date To Timestamp:-

    SimpleDateFormat dateFormat;
Date date;
Timestamp timestamp=null;
try {
dateFormat = new SimpleDateFormat(dateformat);
date = dateFormat.parse(sDate);
timestamp = new Timestamp(date.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
  Date is equals:-
         
          (date1.compareTo(date2)==0)


Date comparison:- 
               date1 = Mon May 12 01:55:00 IST 2014
               date2 = =Sun May 11 11:06:29 IST 2014

                date1.before(date2); result is FALSE
                date1.after(date2);  result is TRUE

   


Thursday, 7 March 2013

iText API with Examples


Example of write text in pdf:
package com.jsd;

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class WriteTextOnPDF {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            // create a new document
            Document document = new Document( PageSize.A4, 20, 20, 20, 20 );
            PdfWriter.getInstance( document, new FileOutputStream( "D:\\3rules.pdf" ) );

            document.open();
           
            document.add( new Paragraph( "Hello, World!" ) );
            document.add( new Paragraph( "Hello, World!" ) );

            // add a couple of blank lines
            document.add( Chunk.NEWLINE );
            document.add( Chunk.NEWLINE );

            // add one more line with text
            document.add( new Paragraph( "Hello, World!" ) );

            document.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Example of Merge PDF:
package com.jsd;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class MergePDF {

    public static void main(String[] args) {
        try {
            List<InputStream> pdfs = new ArrayList<InputStream>();
            pdfs.add(new FileInputStream("d:\\3rules.pdf"));
            pdfs.add(new FileInputStream("d:\\rules.pdf"));
            OutputStream output = new FileOutputStream("d:\\merge.pdf");
            MergePDF.concatPDFs(pdfs, output, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void concatPDFs(List<InputStream> streamOfPDFFiles,
            OutputStream outputStream, boolean paginate) {

        Document document = new Document();
        try {
            List<InputStream> pdfs = streamOfPDFFiles;
            List<PdfReader> readers = new ArrayList<PdfReader>();
            int totalPages = 0;
            Iterator<InputStream> iteratorPDFs = pdfs.iterator();

            // Create Readers for the pdfs.
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
            }
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);

            document.open();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
            // data

            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            // Loop through the PDF files and add to the output.
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader = iteratorPDFReader.next();

                // Create a new page in the target for each source page.
                while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                    document.newPage();
                    pageOfCurrentReaderPDF++;
                    currentPageNumber++;
                    page = writer.getImportedPage(pdfReader,
                            pageOfCurrentReaderPDF);
                    cb.addTemplate(page, 0, 0);

                    // Code for pagination.
                    if (paginate) {
                        cb.beginText();
                        cb.setFontAndSize(bf, 9);
                        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                                + currentPageNumber + " of " + totalPages, 520,
                                5, 0);
                        cb.endText();
                    }
                }
                pageOfCurrentReaderPDF = 0;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
EXAMPLE CONVERT PDF TO TEXT :
package com.jsd;

/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.FilteredTextRenderListener;
import com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.RegionTextRenderFilter;
import com.itextpdf.text.pdf.parser.RenderFilter;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class GetTextCoordinates {

    /** The original PDF that will be parsed. */
    public static final String PREFACE = "d:/rules.pdf";
    /** The resulting text file. */
    public static final String RESULT = "d:/preface_clipped.txt";

    /**
     * Parses a specific area of a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        Rectangle rect = new Rectangle(70, 80, 490, 580);
        RenderFilter filter = new RegionTextRenderFilter(rect);
        TextExtractionStrategy strategy;
       
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
            out.println(PdfTextExtractor.getTextFromPage(reader, i, strategy));
        }
        out.flush();
        out.close();
        reader.close();
    }

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        new GetTextCoordinates().parsePdf(PREFACE, RESULT);
    }
}