23 April 2014

Install PPAs on latest Ubuntu releases

Sometimes, when you have the latest release of Ubuntu, and then try to install some PPA, you cannot install at get 404 error while executing (apt-get update)

This is because, the PPA structure requires build per each release, the following image will make it clear:


Currently we have a new Release of ubuntu (14.04) with code name Trusty Tahr, but the important part here is the first part which is "trusty", but it is not found in the PPA of "fogger app".

When you try to install this app on Ubuntu 14.04, you will get 404, file not found. So here we will talk about the solution..

Regularly, you issue the following commands to install the software from the ppa:

$ sudo add-apt-repository ppa:loneowais/fogger
$ sudo apt-get update  # here you will get the 404 error.
$ sudo apt-get install fogger # you will not be able to find the fogger to install because of the failure in the preceding step.


This is happens because when you try to add the ppa, it is added in the source list files under "trusty" which is not supported yet.
you can check the source file your self (in our case here's the source list of fogger):

$ cat /etc/apt/sources.list.d/fogger-app-authors-ppa-trusty.list 
deb http://ppa.launchpad.net/fogger-app-authors/ppa/ubuntu trusty main
deb-src http://ppa.launchpad.net/fogger-app-authors/ppa/ubuntu trusty main


The workaround to do is to make your distro point to the software of the previous ubuntu release, and this by editing the source list file to point to "saucy" instead. (notice the saucy has a release in the first PPA picture of fogger)

Let's change the source list file:

$ sudo gedit /etc/apt/sources.list.d/fogger-app-authors-ppa-trusty.list 
deb http://ppa.launchpad.net/fogger-app-authors/ppa/ubuntu saucy main
deb-src http://ppa.launchpad.net/fogger-app-autthors/ppa/ubuntu saucy main

(note we replaced "trusty" with "saucy", provided that "saucy" has a release in the PPA, if not try older version)

Now we can continue our installation:

$ sudo apt-get update
$ sudo apt-get install fogger


You should be able to install the software now. have fun!

Java8 vs Ruby (Files)

We talked about the the syntax similarities between Ruby and Java8 when dealing with collections in last post.

In this post, we will talk about manipulating files, specially reading files contents.

To read content of File:

In Ruby:
f=File.open "/bin/which"
f.each{|line| puts line}
f.close

In Java:
Stream<String> lines = Files.lines(Paths.get("/bin/which"));
lines.forEach(line -> System.out.println(line));

Very Similar!, Viva Java8

22 April 2014

Java8 vs Ruby (Collections)

I feel that the new additions to Java8 will move java forward many steps...

The following is just simple filtering, I just mapping numbers and filter it in both Java8 and Ruby .... Java8 looks pretty much simple as Ruby..

Ruby:
(1..10).to_a.map {|n| n * n}.select {|n| n > 5}.each{|n| puts n}


Java8:
IntStream.rangeClosed(1, 10).map(i -> i * i).filter(i -> i > 5)
                .forEach(i -> System.out.println(i));

Java8 looks very simple... (although the underlying API (stream-api) looks  complex :) )

21 April 2014

Java8 Stream api throughout the JDK

According to My first look to java8 lambda and streams, I can tell that this new features of Java8 will make our lives much fun and easier..

One thing is that, how many places in JDK I can use the funny stream-api with lambda in?

I have grepped the code (Using eclipse) to find interesting places where we can start using the stream-api.

Result of grep:

java.io.BufferedReader
java.nio.file.Files
java.util.Arrays
java.util.Collections.AsLIFOQueue
java.util.Collections.CheckedCollection
java.util.Collection
java.util.Collections
java.util.Collections.CopiesList
java.util.Collections.SetFromMap
java.util.Collections.SynchronizedCollection
java.util.Collections.UnmodifiableCollection
java.util.Collections.UnmodifiableMap.UnmodifiableEntrySet
java.util.jar.JarFile
java.util.regex.Pattern
java.util.zip.ZipFile

I can find many interesting places like the BufferedReader, Files and of course the Collection API.

I've some fun with the Stream-api with Files and BufferedReader classes ...
See inline comments for explanation


package helloJava8;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public class LambdaExpr {

    public static void main(String[] args) throws IOException {

        String searchPath = "/bin/";
        String command = "which";

        Optional<Path> commandFound =
        // return the Stream that I gonna walkthrough
        Files.list(Paths.get(searchPath))
        // map the path to the file name, ex from /bin/which to which
                .map(p -> p.getFileName())
                // match the command name to "which"
                .filter(s -> command.equals(s.toString()))
                // find first match and end the stream
                .findFirst();

        // check the optional, BTW, Optional is very interesting API too
        if (commandFound.isPresent()) {
            // regular Java7 code
            BufferedReader reader = new BufferedReader(new FileReader(searchPath + command));
            // here the interesting part, get Stream from the Buffered Reader
            // and here we go.
            reader.lines().forEach(l -> System.out.println(l));
            reader.close();
        }
    }
}



Thanks.

19 April 2014

Java 8 features

Hello folks,

I am very glad that java 8 is now released...

It contains many features among them are ... "Lambda expressions", "java.util.stream" and "java.time" packages.

See more here (java 8 release notes) ....

Both Lambda expressions and java.util.stream will provide a very good experience and make the Java language more modern (I believe if such decision not talked, many ppl would migrate to more modern langs like Scala).

And "java.time" is very straight forward api... see it here

I've tried java8 in Eclipse kepler, but it needed some patch to support java8 syntax

The following example illustrates Lambda expressions along with java.util.stream..

package helloJava8;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<Employee> list = new ArrayList<>();

        list.add(new Employee("Ali", 20, 2000));
        list.add(new Employee("Wael", 30, 3000));
        list.add(new Employee("Taher", 40, 1000));
        list.add(new Employee("Ibrahim", 10, 1500));

        System.out.println("Sorted:");
        list.stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName()))
                .forEach(o -> System.out.println(o));

        System.out.println("\nFiltered: (age >= 30) ");
        list.stream().filter(o -> o.getAge() >= 30).forEach(o -> System.out.println(o));

        System.out.println("\nMapping user to its salary then sort:");
        list.stream().map(o -> o.getSalary()).sorted().forEach(o -> System.out.println(o));
    }

    static class Employee {
        private String name;
        private int age;
        private int salary;

        public Employee(String name, int age, int salary) {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
        }
    }
}


You have to read more about lambda and streams here:
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html


That's all.

19 January 2014

jboss7_mod_util

Because the "modules" in jboss7 is the worst thing ever, it like manage maven dependencies by hand...

I decided to write some shell scripts to help me manipulate and organize the modules..

The first script is "mod_create", to create a module....
the second script is "mod_dependency_add", to add module dependency to existing module...

My plan is to add additional scripts for example ...

"mod_modify" to add jars to existing module

Any contributions are welcome .....

The project is on github at:

https://github.com/MuhammadHewedy/jboss7_mod_util

Thanks.

18 November 2013

Request WebService simply with cURL

If you have a webservice you need to consume, you can simply test it using cURL as follows:

curl
-d 'soap request, you can create yourself, or let any tool create it for you, Eclipse can do.'
-H "Content-Type: text/xml; charset=utf-8" -- Content type, and you should specify it.
then the webservice URL, for example http://www.w3schools.com/webservices/tempconvert.asmx


Example:

curl -d '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"> <Celsius>20</Celsius> </CelsiusToFahrenheit> </soap:Body> </soap:Envelope>' -H "Content-Type: text/xml; charset=utf-8" http://www.w3schools.com/webservices/tempconvert.asmx