Year: 2016

Ubuntu with Windows 10 dual Boot

Ubuntu with Windows 10 dual Boot

Lets do Some Real work

STEP 1:Download Ubuntu

Download Ubuntu from http://www.ubuntu.com/download/desktop.
Once you have downloaded the ISO of Ubuntu 16.04 or 14.04, go to this page and download the latest version of Universal USB Installer.

STEP 2:Download Universal USB Installer http://www.pendrivelinux.com/universal-usb-installer-easy-as-1-2-3/

STEP 3: CREATING THE BOOTABLE USB

Plugin the USB drive in the computer and run Universal USB Installer. You need to do the following things now:

Select Ubuntu under step 1
Browse to the location of downloaded Ubuntu ISO in Step 2 section
In Step 3, select the USB drive and also check the option to format it make sure its FAT32

Step 4: Backup your Windows 10 operating system (optional but highly recommended)

Step 5: Create two partition by shrinking your drive for linux ( 1 for ex2 and 2nd for Swap )

Step 6:We will be using those partition as we boot with usb and see ubuntu install screen

Step 7:Now Restart windows and press F12 ,F2 to go to Boot up screen or Bios setting

Step 8:Stay with UEFI setting as thats the safest way to install another OS and Linux are UEFI safe

Step 9 :If you are using some version of Unix which is not UEFi compatible switch off the SAFE boot menu

Now let live USB run

Step 10:if you receive this error When I try to boot from the USB I get the error msg:

Could not find kernel CONFILE
boot:

  • Try to recreate the USb and make sure its in FAt 32 mode

Step 11: Now you see the Ubuntu screeen Ubuntu will recognize the windows system and give you option on any non Winodws OS disk In UEFI Mode

Choose the “Install Ubuntu alongside Windows Boot Manager” option.

Something else Option is not good for Dual Boot it will hide one of OS .

 

ubuntuinstall5

 

Step 12:You can go to advance and see any other Partition Pick those two partition createat step 5

 

ubuntu-advanced-screen

 

Let it finish make sure you have selected

Choose the “Install Ubuntu alongside Windows Boot Manager” option. otherwise you may not be able to boot into dual mode and

Linux will be only OS recognized by computer .

 

ubuntuinstall2

 

 

ubuntuinstall6

 

If you want to do manual partitions like i do

Please visit

 

http://askubuntu.com/questions/343268/how-to-use-manual-partitioning-during-installation

Another topic for those who doesnot want to Install ubuntu and use Windows 10 feature

The Windows 10 feature Windows Subsystem for Linux:
This can give you ability to run Linux bash command but thats not what you want.

For those who want to stick with Windows Subsystem for Linux
You do this by going “System Settings > Advanced Windows Update options” and selecting your Insider Preview update setting to the far right. This sets your Windows 10 update to the the Fast Ring.

After this check for updates, apply all of them, and restart your PC. This gives you its beta operating system so don’t do this on your production machine. Microsoft makes many updates to its Fast Ring releases and adding WSL, Ubuntu, and Bash are only part of them.

Then, you must turn on Developer Mode via Settings > Update & security >
For developers. Next, search for “Windows Features” and choose “Turn Windows features on or off” and
enable Windows Subsystem for Linux

color combination and create new color from two given color in .net

Lets discuss color capabilities in .net

Github link

For most people Color is RGB or ARGB

In.net

Color is a struct. This type provides a standard way to specify and mutate colors in the C# language. By adding a reference to the System.Drawing assembly, you can access this type and avoid writing your own color routines.

Colors can be represented as ARGB values, which store the alpha transparency, as well as the red, green and blue values. These values are stored as bytes which gives them a range of 0 to 255 inclusive.

So in order to make new color ( thats what this code does) suppose you have two colors color a and color b

Step 1

Please take the color a and  convert into RGB component via  Private Class colorcomponent

Step 2

do the same step with color b

now for individual R  take the average or fraction s if you are creating more than 2 color

Step 3

suppose R component is 100 for color a

suppose R component is 150 for color b

Step 4

then the middle color should have R value as 125

you may create 4 intermediate color in  that case  your will have R  as 110 120 130 140

Step 5 do same for G a nd B also and combine them to color back see colorcomponent class

2016-10-10_11-50-30

color-combination-demo-in-net

please download zip code file

Asp.net code

<p> How to use this class “colorcomponent” </p>
Dim colora As Color
colora = Color.Red
Dim colorb As Color
colorb = Color.Blue
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolor = 4
Dim outColor(numberofcolor + 1) As Color
ReDim outColor(numberofcolorinbetween + 1)
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolorinbetween + 1)

vb.net code for desktop application

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb))
ListBox1.Items.Add(ColorTranslator.ToHtml(colora))

End Sub
Private Class colorcomponent
Dim r As Integer
Dim g As Integer
Dim b As Integer
Dim a As Integer

Public Sub setcolor(C As Color)
Me.r = C.R
Me.g = C.G
Me.b = C.B
End Sub

Public Function getcolor(colorcomponent1 As colorcomponent) As Color
getcolor = Color.FromArgb(colorcomponent1.r Mod 256, colorcomponent1.g Mod 256, colorcomponent1.b Mod 256)

End Function
Public Function getcolorwithalpha(colorcomponent1 As colorcomponent, alpha As Byte) As Color
getcolorwithalpha = Color.FromArgb(alpha, CByte(colorcomponent1.r Mod 256), CByte(colorcomponent1.g Mod 256), CByte(colorcomponent1.b Mod 256))

End Function

Public Function getcolorarray(colorcomponent1 As colorcomponent, colorcomponent2 As colorcomponent, numberofcolorsneededinbetween As Double) As Color()

Dim outcolorcomponent As New colorcomponent()
Dim outColor(numberofcolorsneededinbetween) As Color
outColor(0) = getcolor(colorcomponent1)
If numberofcolorsneededinbetween > 0 Then
For i As Double = 1 To numberofcolorsneededinbetween – 1
If (colorcomponent1.r – colorcomponent2.r <> 0) Then
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.r = (colorcomponent1.r)
End If
If (colorcomponent1.g – colorcomponent2.g <> 0) Then
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.g = (colorcomponent1.g)
End If
If (colorcomponent1.b – colorcomponent2.b <> 0) Then
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.b = (colorcomponent1.b)
End If
outColor(i) = getcolor(outcolorcomponent)
Next
End If
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2)

Return outColor
End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolors = 4
Dim outColor(numberofcolors + 1) As Color
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1)
ListBox1.Items.Clear()
Dim i = 1
For Each genfcolor As Color In outColor
Dim t As New TextBox()

Dim j = 100
‘ t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor
t.Text = ColorTranslator.ToHtml(genfcolor)
‘Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor))
‘ t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
Me.Controls.Add(t)
t.Location = New Point(8 * 50, 4 + i * 30)
Me.Show()
i += 2
Next

End Sub

End Class

c# code
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
private void Form1_Load(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb));
ListBox1.Items.Add(ColorTranslator.ToHtml(colora));

}
private class colorcomponent
{
int r;
int g;
int b;

int a;
public void setcolor(Color C)
{
this.r = C.R;
this.g = C.G;
this.b = C.B;
}

public Color getcolor(colorcomponent colorcomponent1)
{
return Color.FromArgb(colorcomponent1.r % 256, colorcomponent1.g % 256, colorcomponent1.b % 256);

}
public Color getcolorwithalpha(colorcomponent colorcomponent1, byte alpha)
{
return Color.FromArgb(alpha, Convert.ToByte(colorcomponent1.r % 256), Convert.ToByte(colorcomponent1.g % 256), Convert.ToByte(colorcomponent1.b % 256));

}

public Color[] getcolorarray(colorcomponent colorcomponent1, colorcomponent colorcomponent2, double numberofcolorsneededinbetween)
{

colorcomponent outcolorcomponent = new colorcomponent();
Color[] outColor = new Color[numberofcolorsneededinbetween + 1];
outColor(0) = getcolor(colorcomponent1);
if (numberofcolorsneededinbetween > 0) {
for (double i = 1; i <= numberofcolorsneededinbetween – 1; i++) {
if ((colorcomponent1.r – colorcomponent2.r != 0)) {
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.r = (colorcomponent1.r);
}
if ((colorcomponent1.g – colorcomponent2.g != 0)) {
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.g = (colorcomponent1.g);
}
if ((colorcomponent1.b – colorcomponent2.b != 0)) {
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.b = (colorcomponent1.b);
}
outColor(i) = getcolor(outcolorcomponent);
}
}
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2);

return outColor;
}
}

private void Button1_Click(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
colorcomponent coloracomponent = new colorcomponent();
colorcomponent colorbcomponent = new colorcomponent();
coloracomponent.setcolor(colora);
colorbcomponent.setcolor(colorb);
dynamic numberofcolors = 4;
Color[] outColor = new Color[numberofcolors + 2];
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1);
ListBox1.Items.Clear();
dynamic i = 1;
foreach (Color genfcolor in outColor) {
TextBox t = new TextBox();

dynamic j = 100;
// t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor;
t.Text = ColorTranslator.ToHtml(genfcolor);
//Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor));
// t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
this.Controls.Add(t);
t.Location = new Point(8 * 50, 4 + i * 30);
this.Show();
i += 2;
}

}
public Form1()
{
Load += Form1_Load;
}

}

 

Mockito how to mock and get proper values for ENUM

JavaMockito code  download

 

First of all Mockito can create mock data which can be integer long etc It cannot create right enum as enum has specific number of ordinal name value etc so if i have enum
public enum HttpMethod {

GET, POST, PUT, DELETE, HEAD, PATCH;

} so i have total 5 ordinal in enum HttpMethod but mockito does not know it .Mockito creates mock data and its null all the time and you will end up in passing a null value . So here is proposed solution that you randomize the ordinal and get a right enum which can be passed for other test

import static org.mockito.Mockito.mock;

import java.util.Random;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.amazonaws.HttpMethod;




//@Test(expected = {"LoadableBuilderTestGroup"})
//@RunWith(PowerMockRunner.class)
public class testjava {
   // private static final Class HttpMethod.getClass() = null;
    private HttpMethod mockEnumerable;

    @Test
    public void setUpallpossible_value_of_enum () {
        for ( int i=0 ;i<10;i++){
        String name;
        mockEnumerable=    Matchers.any(HttpMethod.class);
        if(mockEnumerable!= null){
            System.out.println(mockEnumerable.ordinal());
         System.out.println(mockEnumerable.name());

         System.out.println(mockEnumerable.name()+"mocking suceess");
        }
        else
        {

                //Randomize all possible  value of  enum 
                Random rand = new Random();
                int ordinal = rand.nextInt(HttpMethod.values().length); 
                // 0-9. mockEnumerable=
                mockEnumerable= HttpMethod.values()[ordinal];
             System.out.println(mockEnumerable.ordinal());
             System.out.println(mockEnumerable.name());

        }
        }
    }







    @Test
    public void setUpallpossible_value_of_enumwithintany () {
        for ( int i=0 ;i<10;i++){
        String name;
        mockEnumerable=    Matchers.any(HttpMethod.class);
        if(mockEnumerable!= null){
            System.out.println(mockEnumerable.ordinal());
         System.out.println(mockEnumerable.name());

         System.out.println(mockEnumerable.name()+"mocking suceess");
        }
        else
        {
            int ordinal;
                //Randomize all possible  value of  enum 
                Random rand = new Random();
                int imatch =  Matchers.anyInt();
                if(  imatch>HttpMethod.values().length)
                     ordinal = 0    ;
                else
                 ordinal = rand.nextInt(HttpMethod.values().length);

                // 0-9. mockEnumerable=
                mockEnumerable= HttpMethod.values()[ordinal];
             System.out.println(mockEnumerable.ordinal());
             System.out.println(mockEnumerable.name());

        }

        }

    }
}

Output : 0 GET 0 GET 5 PATCH 5 PATCH 4 HEAD 5 PATCH 3 DELETE 0 GET 4 HEAD 2 PUT

Free ebook: Introducing Microsoft SQL Server 2016

http://blogs.msdn.com/b/microsoft_press/archive/2015/12/22/free-ebook-introducing-microsoft-sql-server-2016-mission-critical-applications-deeper-insights-hyperscale-cloud-preview-edition.aspx

 

 

https://blogs.msdn.microsoft.com/microsoft_press/2016/02/02/free-ebook-introducing-microsoft-sql-server-2016-mission-critical-applications-deeper-insights-hyperscale-cloud-preview-2/

 

Pre-Signed URL (AWS)

 

Pre-Signed URL (AWS)

pre-signed URL  is URL  which   is used  to give   access to person for some time with authentication token /Signature  and expiry  on.it  . The user  need not  have the AWS console Sign in credentials .This URL  has Following format  query string with

Http url resource + AWSAccessKeyId+ Expires+Signature

AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459944479&Signature=vba%2BH0F0p9b02n2qyhTFY4Bxjkg%3D

Example

https://my-first-s3-bucket-e3ee683e-b260-4aad-923b-31fa838c6a2e.s3.amazonaws.com/PresignedUrlAndUploadObject.txt?AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459897385&Signature=zJhX0CfSnD6QFgD6fzOlfqk%2FsxM%3D

https://my-first-s3-bucket-e3ee683e-b260-4aad-923b-31fa838c6a2e.s3.amazonaws.com/MyObjectKey?AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459879476&Signature=5Bh1AUuF3U5Vjw0Ah7EdojE9XDY%3D

https://my-first-s3-bucket-63529645-5e01-4406-bf85-75ffc0fd00b1.s3.amazonaws.com/PresignedUrlAndUploadObject.txt?AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459879632&Signature=ypaabCtSnztLp%2FpzxjT2ZvMxhkg%3D

https://my-first-s3-bucket-63529645-5e01-4406-bf85-75ffc0fd00b1.s3.amazonaws.com/PresignedUrlAndUploadObject.txt?AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459883171&Signature=VvcDoikAKnnMWAPuVIg18bG3FcE%3D

https://my-first-s3-bucket-e3ee683e-b260-4aad-923b-31fa838c6a2e.s3.amazonaws.com/PresignedUrlAndUploadObject.txt?AWSAccessKeyId=ACCESSKEYXXXX&Expires=1459897385&Signature=zJhX0CfSnD6QFgD6fzOlfqk%2FsxM%3D

 

Sometime Pre-Signed URL gives error downloading the file  with below mentioned Message.

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

 

The  error is when you send a presigned url which  has been  created for HTTP PUT to upload a file and you are  trying to view the file  via Browser .

URL are different for PUT .GET ,….other DELETE etc VERBS

generatePresignedUrlRequest.setMethod(HttpMethod.PUT);  This Line has different HttpMethod verbs

So make sure  the URL  youa re using is  for  right Verb

To generate a  URL  for  download via browser  you  have to comment this   line

//generatePresignedUrlRequest.setMethod(HttpMethod.PUT);

Aa We   knoe  pre-signed URL gives you access to the object identified in the URL, provided that the creator of the pre-signed URL has permissions to access that object.

A pre-signed URL  can be made for HTTP PUT  get or download via browser and all may  have different urls

generatePresignedUrlRequest.setMethod(HttpMethod.PUT);  This Line has different HttpMethod verbs

where PUT is used to upload file via url  HTTP PUT method.

 

So Its not mandatory to  upload  an object to get a pre-signed url .

We can get  pre-signed url    for existing object also in S3 to send it to user to download files from S3 so that they can download file from browser .

It is Something similar to google drive where  user shares a link and the link owner can  see the File even he does not have google account. GOOGLE DRIVE get shareable link

 

Example code for generating pre-signed url    for existing object also in S3

 

Create an instance of the AmazonS3 class.
Generate a pre-signed URL by executing the AmazonS3.generatePresignedUrl method.

You provide a bucket name, an object key, and an expiration date by creating an instance of the GeneratePresignedUrlRequest class. You don’t have  to  specify the HTTP verb PUT when creating this URL as you are not  upload an object.

Anyone with the pre-signed URL can upload an object.

The upload creates an object or replaces any existing object with the same key that is specified in the pre-signed URL.

 

 

public static String generatepreassignedkeyforexistingfile (String bucketName ,String objectKey) {

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

Region usWest2 =  Region.getRegion( Regions.US_WEST_2

);

s3client.setRegion(usWest2);

URL url = null;

try {

System.out.println(“Generating pre-signed URL.”);

java.util.Date expiration = new java.util.Date();

long milliSeconds = expiration.getTime();

milliSeconds += 1000 * 60 * 60; // Add 1 hour.

expiration.setTime(milliSeconds);

 

GeneratePresignedUrlRequest generatePresignedUrlRequest =

new GeneratePresignedUrlRequest(bucketName, objectKey);

//generatePresignedUrlRequest.setMethod(HttpMethod.PUT);

generatePresignedUrlRequest.setExpiration(expiration);

 

url = s3client.generatePresignedUrl(generatePresignedUrlRequest);

 

 

 

System.out.println(“Pre-Signed URL = ” + url.toString());

} catch (AmazonServiceException exception) {

System.out.println(“Caught an AmazonServiceException, ” +

“which means your request made it ” +

“to Amazon S3, but was rejected with an error response ” +

“for some reason.”);

System.out.println(“Error Message: ” + exception.getMessage());

System.out.println(“HTTP  Code: ”    + exception.getStatusCode());

System.out.println(“AWS Error Code:” + exception.getErrorCode());

System.out.println(“Error Type:    ” + exception.getErrorType());

System.out.println(“Request ID:    ” + exception.getRequestId());

} catch (AmazonClientException ace) {

System.out.println(“Caught an AmazonClientException, ” +

“which means the client encountered ” +

“an internal error while trying to communicate” +

” with S3, ” +

“such as not being able to access the network.”);

System.out.println(“Error Message: ” + ace.getMessage());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return “Pre-Signed URL = ” + url.toString();

}

Lets  see  what can  we do from Eclipse UI

so in order to use Eclispe  we have to  install new software

http://aws.amazon.com/eclipse – http://aws.amazon.com/eclipse

 

installamazonsdktoeclipse

 

Once you finish this step

you can  install explorer view  to see s3 bucket and files

Go to explorer view by Show AWS explorer view in AWS toolbar icon

installamazonsdktoeclipse1

It will ask you for credential for aws console   once you place those credentials  you can choose your region

and see all your AWS resource

So here we are   just talking about S3  but same can be use to create table, Query for Dynamodb also .

So right click  on S3 node gives  you menu to create new  bucket Delete  etc

 

 

now in this screenshot  you can see  i went  to bucket screen

On click bucket

installamazonsdktoeclipse2

Developer can open  contents of bucket in Open in bucket editor screen

We have another option  to upload file

Just drag the file  from desktop to the  screen and you will see file is getting uploaded to s3 bucket which was mentioned in bucket editor screen

its asking for objectkey  wich is filename in this context  and it will upload a file

 

installamazonsdktoeclipse3

In java  you can upload file  via these 3 lines

System.out.println(“Uploading a new object to S3 from a file\n”);
s3.putObject(new PutObjectRequest(bucketName, key, SampleFile));

SampleFileis a File object

and

S3 is  AmazonS3Client

AmazonS3 s3 = new AmazonS3Client(credentials);

Some code fragments to  loop the s3  file and summary are mentioned below to do

the process in java

AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider(“default”).getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
“Cannot load the credentials from the credential profiles file. ” +
“Please make sure that your credentials file is at the correct ” +
“location (C:\\Users\\Jitender.Thakur\\.aws\\credentials), and is in valid format.”,
e);
}

AmazonS3 s3 = new AmazonS3Client(credentials);
Region usWest2 =  Region.getRegion( Regions.US_WEST_2
);
s3.setRegion(usWest2);

  1. To delete  a bucket   you have to delete all files in Bucket first .When  bucket is Empty delete Bucket

 

System.out.println(“Listing objects”);
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(” – ” + objectSummary.getKey() + ”  ” +
“(size = ” + objectSummary.getSize() + “)”);
//new method
generatepreassignedkeyforexistingfile(bucketName,objectSummary.getKey()) ;
s3.deleteObject(bucketName, key);

}
System.out.println();

then

s3.deleteBucket(bucketName);

 

  • Check all buckets you have

for (Bucket bucket : s3.listBuckets()) {
System.out.println(” – ” + bucket.getName());

}

Creating bucket with check for existing bucket as bucket should be globally unique

 try {
credentials = new ProfileCredentialsProvider(“default”).getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
“Cannot load the credentials from the credential profiles file. ” +
“Please make sure that your credentials file is at the correct ” +
“location (C:\\Users\\Jitender.Thakur\\.aws\\credentials), and is in valid format.”,
e);
}

AmazonS3 s3 = new AmazonS3Client(credentials);
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
tx = new TransferManager(s3);

 private void createAmazonS3Bucket() {
try {
if (tx.getAmazonS3Client().doesBucketExist(bucketName) == false) {
tx.getAmazonS3Client().createBucket(bucketName);
}
} catch (AmazonClientException ace) {
//   JOptionPane.showMessageDialog(frame, “Unable to create a new Amazon S3 bucket: ” + ace.getMessage(),
//    “Error Creating Bucket”, JOptionPane.ERROR_MESSAGE);
}
}

 

how to see where the log is ? Logger in slf4j,

Logger in slf4j, could you explain and give explain on how to use it? (e.g. how to see where the log is)

 

 

So  Like any  logger library  we have a configuration file where we place the locationof logs  it can be DB text file Console out put  DEbugger on statments

 

 

 

Logger in slf4j, could you explain and give explain on how to use it? (e.g. how to see where the log is)

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

private final Logger log = LoggerFactory.getLogger(getClass());

log.info(ex.getMessage());

 

 

Using slf4j with Simple logger

Create a Maven based project and this in your pom.xml.

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.5</version>

</dependency>

Now you may use Logger in your Java code like this.

package deng;

import org.slf4j.*;

public class Hello {

static Logger LOGGER = LoggerFactory.getLogger(Hello.class);

public static void main(String[] args) {

for (int i = 0; i < 10; i++)

if (i % 2 == 0)

LOGGER.info(“Hello {}”, i);

else

LOGGER.debug(“I am on index {}”, i);

}

}

The above will get your program compiled, but when you run it, you will see these output.

bash> java deng.Hello

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

What it’s saying is that at runtime, you are missing the logging “implementation” (or the logger binding), so slf4j simply use a “NOP” implmentation, which does nothing. In order to see the output properly, you may try use an simple implementation that does not require any configuration at all! Just go back to your pom.xml and add the following:

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-simple</artifactId>

<version>1.7.5</version>

</dependency>

Now you see logging output on STDOUT with INFO level. This simple logger will default show any INFO level message or higher. In order to see DEBUG messages, you would need to pass in this System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup.

Using slf4j with Log4j logger

Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is to replace slf4j-simple with another popular logger implementation, such as the Log4j.

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.5</version>

</dependency>

Again, we must configure logging per implementation that we picked. In this case, we need an

src/main/resources/log4j.properties file.

log4j.rootLogger=DEBUG, STDOUT

log4j.logger.deng=INFO

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender

log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout

log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) – %m%n

src/main/resources/log4j.properties file for  writing into file  and stdout

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

 

 

 

Re-run your program, and you should see similar output.

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-jdk14</artifactId>

<version>1.7.5</version>

</dependency>

Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as src/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is an example to get you started:

 

.level=INFO

handlers=java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level=FINEST

deng.level=FINEST

Using slf4j with Logback logger

The logback logger implementation is a super dupa quality implementation. If you intend to write serious code that go into production, you may want to evaluate this option. Again modify your pom.xml to replace with this:

<dependency>

<groupId>ch.qos.logback</groupId>

<artifactId>logback-classic</artifactId>

<version>1.0.13</version>

</dependency>

Here is a sample of configuration src/main/resources/logback.xml to get things started.

<configuration>

<appender name=”STDOUT” class=”ch.qos.logback.core.ConsoleAppender”>

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} – %msg%n</pattern>

</encoder>

</appender>

 

<logger name=”deng” level=”DEBUG”/>

 

<root level=”INFO”>

<appender-ref ref=”STDOUT” />

</root>

</configuration>

 

 

for db logging

with

slf4j-api-1.7.5.jar

The next step was to change logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource
            class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <driverClass>org.postgresql.Driver</driverClass>
            <url>jdbc:postgresql://localhost:5432/simple</url>
            <user>postgres</user>
            <password>root</password> <!-- no password -->
        </connectionSource>
    </appender>

    <!-- the level of the root level is set to DEBUG by default. -->
    <root level="TRACE">
        <appender-ref ref="stdout" />
        <appender-ref ref="db" />
    </root>
</configuration>

As seen here, I have created two appenders – a console appender and a database appender.
The database appender here requires the JDBC driver, the jdbc url and the db credentials. An additional property is the connectionSource which is actually the type of Connection wrapper that we would like to use. Logback provides a few options here and I went with the DriverManagerConnectionSource class.
The next step was to write a test class to test the code:

public class SampleTestDbAppender {

   private static final Logger logger = LoggerFactory.getLogger(TestDbAppender.class);

   public  SampleTestDbAppender () {

 logger.info("Class instance created at {}",  DateFormat.getInstance().format(new Date()));
   }

   public void doTask() {
      logger.trace("In test  doTask");
      logger.trace("doTask test  complete");
   }

   public static void main(String[] args) {
      logger.warn("Running test code...");
      new TestDbAppender().doTask();
      logger.debug("test Code execution complete.");
   }

}

 

get script from here

 

https://github.com/qos-ch/logback/tree/master/logback-classic/src/main/resources/ch/qos/logback/classic/db/script
This resulted in three tables:

 

 

— Logback: the reliable, generic, fast and flexible logging framework.
— Copyright (C) 1999-2010, QOS.ch. All rights reserved.
— See http://logback.qos.ch/license.html for the applicable licensing
— conditions.
— This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender
— The event_id column type was recently changed from INT to DECIMAL(40)
— without testing.
DROP TABLE logging_event_property
DROP TABLE logging_event_exception
DROP TABLE logging_event
CREATE TABLE logging_event
(
timestmp DECIMAL(20) NOT NULL,
formatted_message VARCHAR(4000) NOT NULL,
logger_name VARCHAR(254) NOT NULL,
level_string VARCHAR(254) NOT NULL,
thread_name VARCHAR(254),
reference_flag SMALLINT,
arg0 VARCHAR(254),
arg1 VARCHAR(254),
arg2 VARCHAR(254),
arg3 VARCHAR(254),
caller_filename VARCHAR(254) NOT NULL,
caller_class VARCHAR(254) NOT NULL,
caller_method VARCHAR(254) NOT NULL,
caller_line CHAR(4) NOT NULL,
event_id DECIMAL(40) NOT NULL identity,
PRIMARY KEY(event_id)
)
CREATE TABLE logging_event_property
(
event_id DECIMAL(40) NOT NULL,
mapped_key VARCHAR(254) NOT NULL,
mapped_value VARCHAR(1024),
PRIMARY KEY(event_id, mapped_key),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
)
CREATE TABLE logging_event_exception
(
event_id DECIMAL(40) NOT NULL,
i SMALLINT NOT NULL,
trace_line VARCHAR(254) NOT NULL,
PRIMARY KEY(event_id, i),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
)

 

 

While this was the console appender, The db appender wrote entries to the table:

Please check 3 tables