Rename multiple files

When we want to rename a file (with Linux or Mac OS), we  generally go to the directory of the file and we rename the file. Or for those who prefer doing with the terminal, we use mv

Capture d_écran 2017-04-27 à 22.55.03

And how about multiple file ? How about renaming all episode of Game of Throne ?Capture d_écran 2017-04-27 à 23.02.11

  • How can we remove [TOTO] for every file name ?
  • How can we rename secretFile for every file name ?
  • How can we remove the extension for every file name ?
  • How can we upper case every file name ?
  • and so on …

we generally do

  • one by one
  • or a bash script

are-you-serious-wtf-meme-baby-face

or

Boss-Baby-with-Briefcase01

To rename multiple files, we will use rename. But what’s rename ?

Rename is a program that renames files according to modification rules specified on the command line.

Install rename program

To install rename, we will use homebrew (a package manager for OSX. With homebrew we can install a lot of applications and libraries that would require manual compilation)

  1. open the terminal
  2. enter “brew install rename”
  3. done

Or use Cakebrew (The Mac App for Homebrew)

Capture d’écran 2017-04-27 à 22.27.10.png

Now let’s rename:

Append the string argument you supply to every filename: rename -a ‘ toto’ *
(-a: append the string argument you supply to every filename)

Capture d_écran 2017-04-27 à 23.17.09

Prepend the string argument you supply to every filename: rename -A ‘ toto’ *
(-A: prepend the string argument you supply to every filename)

Capture d’écran 2017-04-27 à 23.18.22.png

Convert file names to all lower case: rename -c -f *
(-c: convert file names to all lower case)
(-f: rename even when a file with the destination name already exists)

Capture d’écran 2017-04-27 à 23.20.15.png

Convert file names to all upper case: rename -C -f *
(-c: convert file names to all upper case)

Capture d’écran 2017-04-27 à 23.19.25.png

Remove the last extension from a filename, if there is any: rename -x *

Capture d’écran 2017-04-27 à 23.21.11.png

Replace all sequences of whitespace in the filename with single underscore characters: rename –nows *

Capture d’écran 2017-04-27 à 23.22.19.png

Replace each underscore in the filename with a space: rename –rews *

Capture d’écran 2017-04-27 à 23.23.21.png

Perform a simple textual substitution of “from” to “to”:
rename -s ‘want to change’ ‘to what we want’ *

Capture d’écran 2017-04-27 à 23.24.53.png

Add increment number for each file in the list from 1: rename -N …01 -X -e ‘$_ = “$_-$N”‘ *
(-X  stand for keep the extension)
(-e stand for expression)
($_ is the name of the file in the expression)
($N is the incrementation number in the expression)

Capture d’écran 2017-04-27 à 23.26.24.png

Add increment number for each file in the list from 3rename -N …03 -X -e ‘$_ = “$_-$N”‘ *

Capture d’écran 2017-04-27 à 23.27.29.png

Throw away all the existing filenames and simply number the files from 1rename -N …01 -X -e ‘$_ = “FileName-$N”‘ *

Capture d’écran 2017-04-27 à 23.28.43.png

That was only the surface of the iceberg. To see other parameter use:

man rename 

Cool isn’t it ?
So let rename it !
359207_m1469233525

Rename multiple files

IOS APP PROGRAMMATICALLY: UILabel, UITextField and UIButton

In this tutorial, we will add some basic elements to our view. We will use :

Capture d_écran 2017-04-09 à 16.50.11

  1. UILabel
  2. UITextfield
  3. UIButton

At the end, you will be able to change :

  1. change the background colour of the view
  2. add labels to view, change font, size, alignment and colour
  3. add textfield to the view (similar to login and password field)
  4. add button to the view, change button layout and add event on click button

For this tutorial, we use:

  1. Xcode 8.2
  2. Swift 3

Previous Tutorial:

UILabel

UILabel it’s a view that display read-only text (i.e: title, description, label, …). More information in Apple API Reference.

At the end of this section, we will have something like this :

Capture d_écran 2017-04-09 à 17.09.15We will use the same project that you can find on the previous tutorial.

Let’s start !

  1. select ViewController.swift on the project navigation.
    1.png
  2. remove comment in viewDidLoad and remove the function didReceiveMemoryWarning. We will do all programming logic in viewDidLoad. Capture d’écran 2017-04-09 à 17.18.27.pngWe will explain in another article the life cycle of the ViewController. For now, we didn’t need. However you can find information in Apple API Reference UIControllerView.
  3. change the colour background by adding in viewDidLoad:
    • let blueColor = UIColor(red: 0.0/255.0, green: 118.0/255.0, blue: 255.0/255.0, alpha: 1.0)

    • self.view.backgroundColor = blueColor

    • you can also use other default colour: self.view.backgroundColor = UIColor.red
      Capture d’écran 2017-04-09 à 17.30.00.png
  4. create a property for the label before the function viewDidLoad(). Note: UILabel is import within UIKit (UIKit.UILabel).
    • let label = UILabel()

    • Capture d’écran 2017-04-09 à 17.40.03.png
  5. add text to the label and customise it 
    • add text to the label

      self.label.text = “My first UILabel !”

    • change color text label

      self.label.textColor = UIColor.white

    • text alignment to center

      self.label.textAlignment = .center

    • change font to bold and change size to 20

      self.label.font = UIFont(name: “HelveticaNeue-Bold”, size: 20)

    • you can have more property by pressing cmd + click on the UILabel
  6. then add the label to the view
    • !!! we will add manually the constraint so we have to put autoresizingMaskIntoConstraints to false !!!

      self.label.translatesAutoresizingMaskIntoConstraints = false

    • add label to the view

      self.view.addSubview(self.label)

  7. the label didn’t show yet, we have to add where to display on the view. So for now, we will just put in the center of the view by adding some constraints. (we will explain constraints in another tutorial)
    • keep constraints in variable

      var constraints = [NSLayoutConstraint]()

    • center label from horizontal view

      constraints.append(NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0))

    • center label from vertical view

      constraints.append(NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))

    • active constraints

      NSLayoutConstraint.activate(constraints)

  8. we have done. Here is the result and codes.
    Capture d_écran 2017-04-09 à 17.09.15Capture d’écran 2017-04-09 à 18.04.48.png

UITextField

UITextField it’s a view that display an editable text area (i.e: login, password, email, …). More information in Apple API Reference UITextField.

At the end of this section, we will have something like this :Capture d_écran 2017-04-09 à 18.19.36

For this section of tutorial, you can use the empty project that you can find on the previous tutorial and do the point 1 & 2 of the previous section (UILabel). Or you can continue what was done in the previous section (UILabel). Just hide the label by adding

self.label.isHidden = true

Let’s start !

  1. create a property for the textfield before the function viewDidLoad(). Note: UITextField is import within UIKit (UITextField).
    • let textfield = UITextField()

  2. add more information in the textfield
    • background text in the textfield

      self.textfield.placeholder = “information text”

    • if it’s a password field or sensitive field, add isSecureTextEntry = true otherwise it is false by default

      self.textfield.isSecureTextEntry = false

    • change the enter text colour to blue

      self.textfield.textColor = UIColor.blue

    • change the enter text alignment to center

      self.textfield.textAlignment = .center

    • change the background field to white

      self.textfield.backgroundColor = UIColor.white

    • change the border to line

      self.textfield.borderStyle = .line

    • we remove the autocorrectionType (important when using for sensitive field as login and password)

      self.textfield.autocorrectionType = .no

    • display the clean field when we have something in the textfield

      self.textfield.clearButtonMode = .whileEditing

    • you can have more property by pressing cmd + click on the UITextField
  3. then add the textfield to the view
    • !!! we will add manually the constraint so we have to put autoresizingMaskIntoConstraints to false !!!

      self.textfield.translatesAutoresizingMaskIntoConstraints = false

    • add textfield to the view

      self.view.addSubview(self.textfield)

  4. add constraints [center to the screen with width=150 and high=50]
    (we will explain constraints in another tutorial)

    • keep constraints in variable (if not create yet)

      var constraints = [NSLayoutConstraint]()

    • variable metrics and views for width and high constraint

      let views = [“textfield”: self.textfield]
      let metrics = [“width150”: 150, “high50”: 50]

    • center textfield from horizontal view

      constraints.append(NSLayoutConstraint(item: self.textfield, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0))

    • center textfield from vertical view

      constraints.append(NSLayoutConstraint(item: self.textfield, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))

    • width of the textfield

      constraints += NSLayoutConstraint.constraints(withVisualFormat: “H:[textfield(width150)]”, options: [], metrics: metrics, views: views)

    • high of the textfield

      constraints += NSLayoutConstraint.constraints(withVisualFormat: “V:[textfield(high50)]”, options: [], metrics: metrics, views: views)

    • active constraints

      NSLayoutConstraint.activate(constraints)

  5. we have done. Here is the complete code.
    Capture d’écran 2017-04-09 à 18.56.42.png

UIButton

UIButton it’s a view that executes your custom code in response to user interactions. More information in Apple API Reference UIButton.

At the end of this section, we will have something like this :

Capture d_écran 2017-04-09 à 19.02.17

For this section of tutorial, you can use the empty project that you can find on the previous tutorial and do the point 1 & 2 of the previous section (UILabel). Or you can continue what was done in the previous section (UITextField). Just hide the textfield by adding

self.textfield.isHidden = true

Let’s start !

  1. create a property for the button before the function viewDidLoad(). Note: UIButton is import within UIKit (UIKit.UIButton).
    • let button = UIButton()

  2. add information for the button and customise it
    • button text

      self.button.setTitle(“confirm”, for: .normal)

    • change the colour of the text button (ie.: if you want to modify the text colour of the button when highlight, just add self.button.setTitleColor(UIColor.red, for: .highlight) )

      self.button.setTitleColor(UIColor.blue, for: .normal)

    • change the background of the button

      self.button.backgroundColor = UIColor.orange

    • we put the border edge colour of the button to black

      self.button.layer.borderColor = UIColor.black.cgColor

    • change the border size to 2

      self.button.layer.borderWidth = 2

    • round the corner of the button

      self.button.layer.cornerRadius = 3

    • you can have more property by pressing cmd + right click on the UIButton
  3. then add the button to the view
    • !!! we will add manually the constraint so we have to put autoresizingMaskIntoConstraints to false !!!

      self.button.translatesAutoresizingMaskIntoConstraints = false

    • add button to the view

      self.view.addSubview(self.button)

  4. add constraints [center to the screen with width=150 and high=50]
    (we will explain constraints in another tutorial)

    • keep constraints in variable (if not create yet)

      var constraints = [NSLayoutConstraint]()

    • variable metrics and views for width and high constraint

      let views = [“button”: self.button]
      let metrics = [“width150”: 150, “high50”: 50]

    • center button from horizontal view

      constraints.append(NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0))

    • center button from vertical view

      constraints.append(NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))

    • width of the button

      constraints += NSLayoutConstraint.constraints(withVisualFormat: “H:[button(width150)]”, options: [], metrics: metrics, views: views)

    • high of the button

      constraints += NSLayoutConstraint.constraints(withVisualFormat: “V:[button(high50)]”, options: [], metrics: metrics, views: views)

    • active constraints

      NSLayoutConstraint.activate(constraints)

  5. we have done. Here is the complete code.
    Capture d’écran 2017-04-09 à 19.24.26.png

Put all together (UILabel, UITextField, UIButton)

So now, we know how to add and display UILabel, UITextField and UIButton. We have all elements to do the interface :Capture d_écran 2017-04-09 à 16.50.11

  • UILabel that display What’s your name ?
  • UITextfield where we can add a name
  • UIButton to confirm the name

What we need to do :

  1. we need to add a second label to display the name after clicking on the button (try to do it yourself)
  2. add constraints to organise elements on the view
  3. add action on click button

 

 

1. we need to add a second label to display the name after clicking on the button

here is the code to the second label 🙂

Capture d_écran 2017-04-09 à 19.33.54

2. add constraints to organise elements on the view

So now, we just need to play with the constraint to display as we want. (we will explain constraints in another tutorial). Here is the code:

Capture d’écran 2017-04-09 à 19.40.06.png

Now if we launch the application, the display is as we want. However the button is not active. Nothing happen when we click on the button and the button doesn’t change colour for informing of the touch.

So let’s do it.

3. add action on click button

When we click on the button, we want to display our second label with the text “Your name is Toto”

  1. create a function with the wanted action (outside of the viewDidLoad function)

    func buttonAction(sender: UIButton) {
    self.label2.text = self.textfield.text
    }

    func : declaration of the function
    sender: UIButton : reference of the button that use this function
    self.label2.text = self.textfield.text : get the text of the textfield and copy to the label2 text

    here we will display only the name. we want to add “Your name is …”

    func buttonAction(sender: UIButton) {
    guard let name = self.textfield.text else {
    self.label2.text = “”
    }
    self.label2.text = “Your name is ” + name
    }

    We use a guard let because self.textfield.text is optional. Another way to do this:

    func buttonAction(sender: UIButton) {
    let name = self.textfield.text else {
    self.label2.text = “”
    return
    }
    self.label2.text = “Your name is ” + name
    }

    • Capture d’écran 2017-04-09 à 20.11.59.png
  2. bind the function with the button

    self.button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)

Capture d’écran 2017-04-09 à 20.10.47.png
So now when we click on the button, it will show above the button confirm : “Your name is …”

4. Change colour of the button for informing of the touch

to change background color while highlighted we have 3 ways to this:

  1. the easiest is by adding target
    • create function to change button background colour
      Capture d’écran 2017-04-09 à 20.32.07.png
    • add target
      Capture d’écran 2017-04-09 à 20.32.19.png
  2. use extension UIButton -> see UIButton+extension.swift
    • create a new class (call it UIButton+extension.swift)
      Capture d’écran 2017-04-09 à 20.34.27.png
    • implement the class
      Capture d’écran 2017-04-09 à 20.34.36.png
    • use new property for the button
      Capture d’écran 2017-04-09 à 20.35.17.png
  3. use property observer with highlighted property
    • create a new class (call it UIButton+inheritace.swift)
      Capture d’écran 2017-04-09 à 20.37.40.png
    • implement the class
      Capture d’écran 2017-04-09 à 20.37.49.png
    • create the button with the new class
      Capture d’écran 2017-04-09 à 20.38.18.png

 

CONGRATULATION

You have create your first application programmatically.

You can download the tutorial project:
https://github.com/ndtran/ToMyLab/tree/master/Tutorial2

NEXT STEP

The next step will be to understand NSConstraint and UIViewController

IOS APP PROGRAMMATICALLY: UILabel, UITextField and UIButton

How to install your Raspberry Pi OS (Raspbian) in 4 minutes

This tutorial will take you through the steps to install Raspbian, the official OS optimized for Raspberry Pi.

splash

Prerequisites

  • Rapsberry Pi (any model)
  • SD card (for Pi version 1), or micro SD card (for Pi version 2 & 3)
  • SD Card reader (for your Mac/PC from which you will format the SD card)
  • A Mac / PC / Linux computer.

Download, unzip and burn the image

Download the Raspbian image

Raspbian is declined in two version. Raspbian Jessie Lite comes as a minimal image based on Debian Jessie without graphical environment. So it is a light version (~308.5MB) and it is a good candidate if you intend to install a server. (Direct download to Raspbian Jessie Lite)
The second variant is Raspbian Jessie with PIXEL. Much heavier (~1.57GB) but comes with some extra features such as X-server and its component. (Direct download to Raspbian Jessie with PIXEL)Screen Shot 2017-04-02 at 17.27.27

Note that, at any time, you can install a desired package with apt-get, and go from the minimal image to the full Raspbian Jessie with Pixel if needed.
PIXEL is not a name but an acronym. It stands for Pi Improved Xwindows Environment, Lightweight, which makes fully sense in that context.

Unzip the image

On Linux, Unix and MacOS platform there is no problem to unzip the image. But if you have a Windows operating system, the specialized forums recommend you to use 7zip to decompress the file.

For this tutorial, I will uncompress the Raspbian Jessie Lite variant and will get:
Screen Shot 2017-04-03 at 21.18.19

Alternative way (for BSD or macOS): you can unzip and burn the image in one statement:

unzip -p 2017-03-02-raspbian-jessie-lite | dd of=/dev/sdb bs=1M

Time to use strong acid to cut the metal!

Etcher (meaning) is a fantastic tool (developed with electron) to easily burn images to SD cards & USB drives. You can download it from here: link

After the installation, select the image, connect the drive (SD card), and burn it. It’s easy and simple.

Screen Shot 2017-04-03 at 21.36.35!! Do not forget to safely eject your SD card !!

When it’s done, you are welcome to plug into your RPi and enjoy your newly born OS.

Alternative tool: I can also recommend you UNetbootin to burn images to USB drives or SD cards. 

 

How to install your Raspberry Pi OS (Raspbian) in 4 minutes

iOS App Programmatically: Working without the storyboard

You want to start a new iOS project without using the storyboard or nib ? This tutorial is for you.

In this tutorial, we will:

  1. Create new project
  2. Remove the storyboard
  3. Setup the AppDelegate.swift

For this tutorial, we use:

  1. Xcode 8.2
  2. Swift 3

Next tutorial:

Create new project

The first step, select “create a new Xcode project
iOS tuto1 - 1

Then select “Single View Application
iOS tuto1 - 2

Next, fill the textfields with your informations.

  •  Product Name: the name of your application
  • Organization Name: the name of your organization
  • Organization Identifier: generally, you inverse your website url name
  • Language: we will use “swift”
  • Devices: select iPhone or iPad or Universal (for iPhone and iPad)
  • In this tutorial, we will not use Core Data (iOS database)

iOS tuto1 - 3

Finally, save the project to your computer. If you use Git, check “Create Git repository” or you can also do it manually later.
iOS tuto1 - 4

The project navigator should look like this
iOS tuto1 - 5

Remove the storyboard

After creating the project, we will remove the storyboard from the project. So in the project navigator, delete “Main.storyboard” 

Then select “Move to Trash” on the alert window
iOS tuto1 - 6
Next, select “info.plist” in the project navigator and you will find “Main storyboard file base name”. Delete it.
iOS tuto1 - 7

We have now remove the storyboard from the project. You can launch the application however the application will be empty (black screen).

Set up the AppDelegate

The AppDelegate is the root object of your application. It has several important roles.

  • It contains your app’s startup code.
  • It responds to key changes in the state of your app.
  • More information here.

iOS tuto1 - 8

In the “didFinishLaunchingWithOptions” add the following line:

self.window = UIWindow(frame: UIScreen.main.bounds)

It will initialise the window with the size of the screen device.

guard let window = self.window else { fatalError(“no window”) }

A guard let to be sure that the window is not nil.

window.rootViewController = ViewController()

Set the root view controller of the app’s window.
ViewController() : instance of the ‘ViewController’ class which is locate in the project navigator. For less confusion, you can rename the class and the file name by MainViewController or MainVC.

window.makeKeyAndVisible()

Make the window visible.

The AppDelegate should be similar to this:
iOS tuto1 - 12

You can download the tutorial project:
https://github.com/ndtran/ToMyLab/tree/master/Tutorial1/

Next step

Now you have setup your project and you can start to modify the view programmatically. The next step will be to add some elements as UILabel, UIButton or UITextField, to the user interface.

Next tutorial: iOS App programmatically: UILabel, UITextField and UIButton

iOS App Programmatically: Working without the storyboard

Forget about Bash Shell, Zsh+Oh-My-Zsh is sexier!

For any developer, the terminal is THE most important daily tool. And Even if Bash and Zsh are both excellent Shells, we all have our preferences.

In my opinion, a product itself is nothing comparing to an ecosystem or a community. And this is why I love Zsh: this tool is great for my needs.

Four reasons to use Zsh over Bash:

  1. Autocorrection – try to type gir -m ‘my commit message’ in a Bash or in a Zsh to see the autocorrection in action.
  2. If you are on MacOS, your bash is old! Type a bash –version in your terminal to compare with the actual stable version 4.4.5
  3. Command completion – git and cd command completion are more powerful on Zsh than Bash.
  4. The greatest of the path replacement

Enough talk, let’s cut to the chase!

Prerequisites:

  • Internet connection
  • Homebrew
  • Admin rights

Install Zsh and Zsh-Completions

To check if you already have a version, type:

which zsh

I have already a version of Zsh on my MacOS but it isn’t the newest. So I decide to update this system-provided version using Homebrew and Zsh-Completions

brew install zsh zsh-completions

Change the default Shell to Zsh

To add or change the user database information and in particulary change the user shell, type this command:

chsh -s /usr/local/bin/zsh

Install Oh-My-Zsh

Now it’s time to get additional functionality with Oh-My-Zsh framework on top of Zsh. To do this, you will need to go back to your home directory and install Oh-My-Zsh:

cd ~
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

After a restart to load the Zsh shell, you can already notice the change:

screen-shot-2017-02-24-at-21-48-07

[Optionally] Apply another theme and configure your .zshrc

Open the .zshrc file with your favorite text editor, for example

vim ~/.zshrc

and apply for example the theme ‘agnoster‘: at the line 9 (in my version), change it with:

ZSH_THEME="agnoster"

save it.

Go to Powerline-Fonts, download it, unzipped, and install it:

unzip~/Downloads/fonts-master.zip 
./fonts-master/install.sh 

To have some eye friendly colour combinations, we have to install Solarized Light and Solarized Dark themes. Go to ethanschoonover.com/solarized, under the Download section get the lastest .zip file. Then:

unzip ~/Downloads/solarized

Go under Downloads/solarized/osx-terminal.app-colors-solarized/ and with the ‘alt’ key pressed (to bypass the macOS defenses) click on the Solarized Dark ansi.terminal and Solarized Light ansi.terminal

In the terminal preferences (Settings -> Profiles), click on Solarized Dark and then the Default button.

Screen Shot 2017-02-24 at 22.40.36.png

Finally, you should see your terminal ending like:

Screen Shot 2017-02-24 at 22.55.22.png

[Optionally] Add some plugins to your Oh-My-Zsh

You also can add some plugins to simplify your daily life. In my .zshrc file, I have replaced plugins=(git) with the following:

plugins=(git colored-man colorize github brew osx zsh-syntax-highlighting sublime xcode)

Reference

https://github.com/robbyrussell/oh-my-zsh

http://ohmyz.sh/

github.com/powerline/fonts

Forget about Bash Shell, Zsh+Oh-My-Zsh is sexier!

REST – the architectural style of the Web

There are numerous ways to bind a system to another such as sockets, named pipes, RPC, CORBA, RMI, Web services and shared memory to name a few of them. But why the Representational State Transfer (REST) has such an impact as architectural style for designing scalable distributed applications?

REST is an easy to use revolution

The best concept often comes with the simplest way to solve the problem. Although REST is not a standard, it is widespread due to its simplicity and ease to use and it is still a simpler alternative to SOAP, WSDL-based Web services.

Millions of people use the Web daily and thousands of us have studied what the Web is. But have we used the full potential of the World Wide Web just by jumping from one document to another through URLs over the network named Internet? Think wider!

Loose coupling

When two systems are so tight to each other, that they are not separable, then you have a monolith. This is the worst scenario of a good architecture principle which says that two systems must be independent as possible to be deployed, maintained, modified and removed with ease. This is why components communicate through interfaces and are so isolated from the others.

A web browser is loose coupled with a server since it is based on standard interfaces to communicate with a wide panel of servers, which provide a HTTP service.

Interoperability

Interoperability is the possibility that different systems can communicate through a determined common standard. In the Web ecosystem the standard are HTTP, URIs, XML, HTML, and so on. Most of the technologies on the Web communicate through HTTP such as SOAP, COM, CORBA and RMI. Quasi all systems support HTTP as application protocol and URIs as identifier mechanism.

Reusability

A lot of technologies solicit a massive reuse of other technologies to keep the software engineering as low as possible. A reuse of existing components is a common pattern to maximise the benefit on business and growth.

In the REST world, there is just ONE interface. Each client can use this only interface to communicate with the REST service.

Performance

The user of an application does not care about the complexity of a system. What matters is the response time. So the system has to answer in a small time box. The intern architecture and the implementation will determine if the overall system will be efficient, such as computing on a multitude of clusters. REST and HTTP have not a large influence on distributed systems.

REST helps this web infrastructure’s maturity such as cache mechanism and proxies.

Scalability

As the REST approach is completely stateless, meaning same request yields same result (idempotency) the question on where to hold the state or alter objects comes in place.[more to come]

Discoverability

[more to come]

Governance

[more to come]

REST – the architectural style of the Web

How to add a datasource to Wildfly

This tutorial explains 3 methods how to add a datasource to the application server Wildfly from Redhat. If you don’t have Wildfly installed on your machine, you can follow my other posts:

Prerequisites

Even if these steps are driver-agnostic, the example will use MySQL Connector (in other words, the JDBC Driver for MySQL) and Wildfly 10.0.0-Final.

For the next steps, you will need:

  • Wildfly installed and started
  • MySQL, MariaDB, PostgreSQL or any datasource compatible with Wildfly
  • The RDMS started and a database created (e.g. database: tutorial)

Notice that all these 3 methods have those steps in common:

  1. Add the JDBC Driver (a.k.a. Connector)
  2. Configure you datasource

Method 1: Add datasource via Admin-Console (the easy method)

1. Add the JDBC Driver

  1. Open the console from the browser (http://localhost:9990/console for a local server)Screen Shot 2016-07-23 at 16.21.50
  2. Then in Deployments, to add to open the wizardScreen Shot 2016-07-23 at 16.25.10
  3. After having clicked next, upload your Connector (i.e. JDBC Driver)Screen Shot 2016-07-23 at 16.26.50Here is the MySQL page to download the driver: MySQL Connector URL
  4. After uploading it, you may want to change the connector name. My recommendation is to name your connector with the provider-name, type and the project’s name that will use it, since your application server my have some projects with different versions of the same connector provider. Screen Shot 2016-07-23 at 16.34.17Note that the name is how the deployment is known to the users and must be unique. The runtime name is how is known to the server. This way you may have multiple names but refering to the same runtime name.
    Do not forget to enable your new deployment! (It should be enabled by default)
  5. Press finish to deploy your connector to your application server.

2. Configure you datasource

  1. Add a new datasource by clicking on ‘Start’ on the right of ‘Create a Datasource’. This will define a datasource to be used by deployed applications.
    Screen Shot 2016-07-23 at 16.48.22
  2. Then select ‘Subsystems’ and ‘Datasources’ and either Non-XA or XA depending of your project type. This tutorial needs a Non-XA datasource. Then click ‘Add’.
    Screen Shot 2016-07-23 at 16.53.52
  3. Then click ‘Add’, choose your datasource (MySQL Datasource for this tutorial) and click next.
    Screen Shot 2016-07-23 at 16.55.30
  4. Give to the attributes name and a JNDI name to your datasource. The JNDI name is like a name in a directory service allowing your programs to discover the data and object. Both name and JNDI name have to be unique. Then click ‘next’.
    Screen Shot 2016-07-23 at 17.00.40
  5. Then select your JDBC Driver you have deployed before. To do that, click on ‘Detected Driver’ and select it and click ‘next’
    Screen Shot 2016-07-23 at 17.27.23
  6. Then add the attributes you will need to connect to your RDMS server.
    Screen Shot 2016-07-23 at 17.53.37
  7. On the next screen, you can easily test your connection. In case of failure, the details will guide you to the solution.
    Screen Shot 2016-07-23 at 18.03.07

Pros / Cons

+ Easy to configure
+ No coding
– Need to use the management console, could be a security issue for a company
– Need to reboot your server to add a datasource.

Method 2: Add datasource via standalone.xml

The file that configures your server is named ‘standalone.xml’ and it is located in your wildfly installation directory under:

{WILDFLY_DIR}/standalone/configuration/standalone.xml

Important: The management console is updating the standalone.xml. Hence all we have done in method 1 is actually an abstraction of what we will do here.

1. Add the JDBC Driver

  1. With help of your window manager, go under
    {WILDFLY_DIR}/modules/system/layers/base/
  2. Then create a subdirectory for your JDBC Driver representing the package structure of your Connector. So create the subfolder:
    {WILDFLY_DIR}/modules/system/layers/base/com/mysql
  3. Copy your JDBC Driver into the directory you have just created.
  4. Create a XML file in the directory named module.xml.
  5. $ pwd
    /usr/local/opt/wildfly-as
    $ cd modules/system/layers/base/com/mysql/
    $ touch module.xml
  6. Then copy this into the module.xml you created:
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.3" name="com.mysql">
      <resources>
        <resource-root path="mysql-connector-java-5.1.39-bin.jar"/>
      </resources>
      <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
      </dependencies>
    </module>
  7. Edit the standalone.xml file located:
    {WILDFLY_DIR}/standalone/configuration/standalone.xml
  8. Find the <drivers> element (after the <datasource> element). Then add this snippet:
    <drivers>
     <driver name="mysql" module="com.mysql">
      <datasource-class>com.mysql.jdbc.Driver</datasource-class>
     </driver>
    </drivers>
  9. That’s it, the driver has been added to Wildfly. Now let’s configure the datasource.

2. Configure you datasource

  1. Just above the driver snippet, add this datasoure snippet:
    <datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySqlDS" enabled="true" use-ccm="true">
      <connection-url>jdbc:mysql://localhost:3306/mysqldb</connection-url>
      <driver>mysql</driver>
      <security>
        <user-name>root</user-name>
        <password></password>
      </security>
      <validation>
      <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
      <background-validation>true</background-validation>
      <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
      </validation>
     </datasource>

    Make sure that the name of the driver (between the <driver> element are the name given to your driver). You can also configure the username, password and database name.

  2. Restart Wildfly and verify by checking the connection to the database through the mangement console:
    Screen Shot 2016-07-23 at 18.03.07

Pros / Cons

+ Standanlone.xml file can be part of your deployment, which is handy.
– Critical if multiple application run on the same server.
– Need to reboot your server to add a datasource.

Method 3: Add datasource via CLI

CLI stands for Command Line Interface and is a command line management tool for JBoss Application Server. It allows a developer / operator to execute management operations and includes these features:

 

  • connect to the specific controller or server instance;
  • view the available managed components, their attributes and operations;
  • deploy and undeploy standard packages at runtime in standalone and domain modes;

The two main advantages of the CLI is:

  • You can automate your interactation with Wildfly through a script.
  • You can run this on a running Wildfly server.

The CLI is located at:

{WILDFLY_DIR}/bin/cli-client.sh

1. Add the JDBC Driver

  1. Start the command line interface with the following command:
    ./jboss-cli.sh --connect controller=127.0.0.1
  2. Then install the JDBC jar by loading it into a module.
    module add --name=com.mysql --resources=~/Downloads/mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar --dependencies=javax.api,javax.transaction.api

    Important: pay attention to where to add a space, and where not to add a space, this is scripting 😉

2. Configure you datasource

  1. Add the driver to the data-sources subsystem.
    /subsystem=datasources/jdbc-driver=mysql:add(driver-name="mysql",driver-module-name="com.mysql",driver-class-name=com.mysql.jdbc.Driver)

    It returns {“outcome” => “success”} in case of success.

  2. Create the datasource that uses the JDBC driver we just installed.
    data-source add --jndi-name=java:/MySqlDS --name=MySqlPool --connection-url=jdbc:mysql://localhost:3306/mysqldb --driver-name=mysql --user-name=root
  3. Test your connection and that’s it.Screen Shot 2016-07-23 at 18.03.07

    Pros / Cons

    + Best option from a dev-ops perspective. A script can be run before deploying an application and the script can be part of your deployment as well.
    + Plays very well with technologies like Docker.
    + Configuration can be changed at runtime! So no reboot required.

References

byteslounge: container vs application managed entity manager

mastertheboss: configuring a datasource with postgresql and wildfly

Thanks to

  • David Kühner for the technical review

How to add a datasource to Wildfly

How to install MariaDB on Mac

Like the Wildfly Application Server installation, we will install MariaDB through Homebrew, the package management software.

Compiling MariaDB

Open a terminal and type

macbookpro$ brew update
macbookpro$ brew search mariadb

mariadb                                   mariadb-connector-c                                                                                                   
homebrew/versions/mariadb100              Caskroom/cask/navicat-for-mariadb                                                                                     
macbookpro$ brew install mariadb
==> Installing dependencies for mariadb: openssl
==> Installing mariadb dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2h_1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2h_1.el_capitan.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2h_1: 1,691 files, 12M
==> Installing mariadb
==> Downloading https://homebrew.bintray.com/bottles/mariadb-10.1.15.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mariadb-10.1.15.el_capitan.bottle.tar.gz
==> /usr/local/Cellar/mariadb/10.1.15/bin/mysql_install_db --verbose --user=philippe.wanner --basedir=/usr/local/Cellar/mariadb/10.1.15 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To have launchd start mariadb now and restart at login:
  brew services start mariadb
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mariadb/10.1.15: 573 files, 137.1M

Run the MariaDB installer

macbookpro$ unset TMPDIR
macbookpro$ mysql_install_db

Then you see the following

Installing MariaDB/MySQL system tables in '/usr/local/var/mysql' ...
2016-07-03 13:37:37 140735229489152 [Note] /usr/local/Cellar/mariadb/10.1.15/bin/mysqld (mysqld 10.1.15-MariaDB) starting as process 13274 ...
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: The InnoDB memory heap is disabled
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Memory barrier is not used
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Compressed tables use zlib 1.2.5
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Using SSE crc32 instructions
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Completed initialization of buffer pool
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Highest supported file format is Barracuda.
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: 128 rollback segment(s) are active.
2016-07-03 13:37:37 140735229489152 [Note] InnoDB: Waiting for purge to start
2016-07-03 13:37:37 140735229489152 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.30-76.3 started; log sequence number 1616819
2016-07-03 13:37:37 123145313034240 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK
Filling help tables...
2016-07-03 13:37:39 140735229489152 [Note] /usr/local/Cellar/mariadb/10.1.15/bin/mysqld (mysqld 10.1.15-MariaDB) starting as process 13278 ...
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: The InnoDB memory heap is disabled
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Memory barrier is not used
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Compressed tables use zlib 1.2.5
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Using SSE crc32 instructions
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Completed initialization of buffer pool
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Highest supported file format is Barracuda.
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: 128 rollback segment(s) are active.
2016-07-03 13:37:39 140735229489152 [Note] InnoDB: Waiting for purge to start
2016-07-03 13:37:39 140735229489152 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.30-76.3 started; log sequence number 1616829
2016-07-03 13:37:39 123145313034240 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK
Creating OpenGIS required SP-s...
2016-07-03 13:37:42 140735229489152 [Note] /usr/local/Cellar/mariadb/10.1.15/bin/mysqld (mysqld 10.1.15-MariaDB) starting as process 13282 ...
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: The InnoDB memory heap is disabled
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Memory barrier is not used
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Compressed tables use zlib 1.2.5
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Using SSE crc32 instructions
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Completed initialization of buffer pool
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Highest supported file format is Barracuda.
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: 128 rollback segment(s) are active.
2016-07-03 13:37:42 140735229489152 [Note] InnoDB: Waiting for purge to start
2016-07-03 13:37:42 140735229489152 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.30-76.3 started; log sequence number 1616839
2016-07-03 13:37:42 123145313034240 [Note] InnoDB: Dumping buffer pool(s) not yet started
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/local/Cellar/mariadb/10.1.15/bin/mysqladmin' -u root password 'new-password'
'/usr/local/Cellar/mariadb/10.1.15/bin/mysqladmin' -u root -h Philippes-MacBook-Pro.local password 'new-password'

Alternatively you can run:
'/usr/local/Cellar/mariadb/10.1.15/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr/local/Cellar/mariadb/10.1.15' ; /usr/local/Cellar/mariadb/10.1.15/bin/mysqld_safe --datadir='/usr/local/var/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/Cellar/mariadb/10.1.15/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from MariaDB
Corporation Ab. You can contact us about this at sales@mariadb.com.
Alternatively consider joining our community based development effort:
http://mariadb.com/kb/en/contributing-to-the-mariadb-project/

Start MariaDB Server

You have two way to do this:

Run MariaDB through Homebrew

[Once] Install the brew services (if not yet done)

macbookpro$ brew tap homebrew/services
==> Tapping homebrew/services
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
Checking connectivity... done.
Tapped 0 formulae (32 files, 46.2K)

Run brew service MariaDB

macbookpro$ brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)

Note that the following brew services command also exist:

  • brew services restart mariadb
  • brew services stop mariadb

Run MariaDB in background process

macbookpro$ mysql.server start
Starting MySQL
. SUCCESS!

Use MariaDB

macbookpro$ mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.1.15-MariaDB Homebrew

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
How to install MariaDB on Mac