Tuesday, December 6, 2016

Add a new Ambari Service

1. get java home
    from resource_management import Script
   
    config = Script.get_config()
    java_home = config['hostLevelParams']['java_home']

    # /usr/lib/jvm/java

2. default log directory
/var/lib/ambari-agent/data


Friday, December 2, 2016

Add ports mapping to the HDP 2.5 VMware Sandbox

HDP 2.5 Sandbox VM:
    ssh root@IP -p 22

HDP 2.5 Sandbox Container
    ssh root@IP -p 2222

Note: here the IP is the public IP address for VM. It's NOT the IP for Container.

1) login to the Sandbox VM

$ ssh root@IP -p 22

2) Disable sandbox.service

$ systemctl disable sandbox.service

3) Reboot the VM

$ init 6

4) Modify sandbox start script

$ vi /root/start_scripts/start_sandbox.sh

Go search "-p 2222:22 \"
Add a new line after that, like:
    -p 1234:1234 \
save and exit.

5) Delete existing sandbox container

$ docker rm sandbox

6) Enable sandbox.service

systemctl enable sandbox.service

7) Reboot the VM

$ init 6

8) Verify new ports

$ docker ps |grep 1234

You should see that 1234 is now in the list.

9) Bring up webapp listening at port 1234


10) Verify that you can access IP:1234 via browser from a 3rd machine.


Wednesday, November 30, 2016

Paths in a JAR file

Suppose a JAR file has the following contents:
$ jar -jar simple-service-1.0-SNAPSHOT.jar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
   159 Mon Nov 28 09:22:20 EST 2016 META-INF/MANIFEST.MF
     0 Mon Nov 28 09:22:20 EST 2016 META-INF/
     0 Mon Nov 28 09:22:20 EST 2016 backup/
  2333 Mon Nov 28 09:22:20 EST 2016 backup/backupController.js
   231 Mon Nov 28 09:22:20 EST 2016 backup/backup.html
     0 Mon Nov 28 09:22:20 EST 2016 WEB-INF/
  1081 Mon Nov 28 09:22:20 EST 2016 WEB-INF/web.xml
     0 Mon Nov 28 09:22:20 EST 2016 com/
     0 Mon Nov 28 09:22:20 EST 2016 com/example/
  1727 Mon Nov 28 09:22:20 EST 2016 com/example/Main.class
   579 Mon Nov 28 09:22:20 EST 2016 com/example/MyResource.class
   556 Mon Nov 28 09:22:20 EST 2016 com/example/MyResource2.class
     0 Mon Nov 28 09:22:20 EST 2016 config/
  2117 Mon Nov 28 09:22:20 EST 2016 config/configController.js
   230 Mon Nov 28 09:22:20 EST 2016 config/config.html
     0 Mon Nov 28 09:22:20 EST 2016 restore/
  2137 Mon Nov 28 09:22:20 EST 2016 restore/restoreController.js
   232 Mon Nov 28 09:22:20 EST 2016 restore/restore.html
  2818 Mon Nov 28 09:22:20 EST 2016 index.html
  2821 Mon Nov 28 09:22:20 EST 2016 hdboost.js

1. Root of a JAR file:
1
2
3
4
5
// file:/home/fos/simple-service-1.0-SNAPSHOT.jar
String webdir = Main.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
// jar:file:/home/fos/simple-service-1.0-SNAPSHOT.jar!/ 
webdir = "jar:" + webdir + "!/";
webapp.setResourceBase(webdir);

2. Path of a resource in a JAR file:
take "index.html" for example:

1
2
// jar:file:/home/fos/simple-service-1.0-SNAPSHOT.jar!/index.html         
String webdir = Main.class.getClassLoader().getResource("index.html").toExternalForm();

Another example with "WEB-INF"

1
2
// WEB-INF: jar:file:/home/fos/simple-service-1.0-SNAPSHOT.jar!/WEB-INF/
String webdir = Main.class.getClassLoader().getResource("WEB-INF").toExternalForm(); 

Wednesday, November 23, 2016

combine two jar files

There are two jar files in current directory:

  • jetty-all-uber.jar
  • hello-world-0.1-SNAPSHOT.jar


1. make a temp directory
mkdir tmp
2. cd tmp
    jar -xf ../hello-world-0.1-SNAPSHOT.jar
    jar -xf ../jetty-all-uber.jar
3. cd ..
    jar -cvf my.jar -C tmp .

The above commands create a combined jar file my.jar in current directory.


Test HelloWorld with:
fos@ubuntu:~/dev/jetty-demo$ java -cp my.jar org.example.HelloWorld

If you want to make your jar file as an application, please refer to:
Running JAR-Packaged Software

with which you can run the HelloWorld with:
fos@ubuntu:~/dev/jetty-demo$ java -jar my.jar

Thursday, November 17, 2016

How to start http server with Python

Change to the directory where you want to start your http server:
Python2:
    python -m SimpleHTTPServer
Python3:
    python3 -m http.server

Thursday, November 3, 2016

How to handle CORS using JAX-RS with Jersey

In Jersey 2:



[root@quickstart example]# cat CORSFilter.java 
package com.example;

import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.*;

@Provider
public class CORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext request,
            ContainerResponseContext response) throws IOException {
        response.getHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHeaders().add("Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization");
        response.getHeaders().add("Access-Control-Allow-Credentials", "true");
        response.getHeaders().add("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS, HEAD");
    }
}

Reference:
http://stackoverflow.com/questions/28065963/how-to-handle-cors-using-jax-rs-with-jersey

Debug Javascript within FireFox

Just press F12. That's it.