Java Web Applications web.xml Basic Fundamentals

0

Java Web Applications

      use a deployment descriptor file to determine how URLs map to servlets
      web.xml is the file is the deployment descriptor
      some URL’s require authentication
      resides in the WEB-INF folder
      describes the classes, resources and configuration of the application
      maps the URL of the request to the code that handles the request

    Example

    Below, the java class is mapped to the url’s here.

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
        <servlet>
            <servlet-name>comingsoon</servlet-name>
            <servlet-class>mysite.server.ComingSoonServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>comingsoon</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

    Example

    Below, the web.xml defines key value pairs

     <servlet>
            <servlet-name>redteam</servlet-name>
            <servlet-class>mysite.server.TeamServlet</servlet-class>
            <init-param>
                <param-name>teamColor</param-name>
                <param-value>red</param-value>
            </init-param>
            <init-param>
                <param-name>bgColor</param-name>
                <param-value>#CC0000</param-value>
            </init-param>
        </servlet>
    String teamColor = getServletConfig().getInitParameter("teamColor");
    

    JSP

    JSP’s are servlets also :). That define static content mixed with Java code.

    Filters

    A filter is a class that acts on a request like a servlet, but may allow the handling of the request to continue with other filters or servlets. A filter may perform an auxiliary task such as logging, performing specialized authentication checks, or annotating the request or response objects before calling the servlet.

    A filter implements javax.servlet.Filter, includes doFilter() method.

    Error Handlers

      <error-page>
            <error-code>500</error-code>
            <location>/errors/servererror.jsp</location>
        </error-page>
    

    Sensu Monitoring for mNox

    0

    Sensu Setup

    After a lot of research, we have realized that a good alternative to Nagios is Sensu. We did evaluate shinken, but the documentation sucks big time. Hence Sensu is a good alternative. Started off by watching some training videos on https://www.udemy.com/sensu-introduction

    Once the training is thru the concepts are clear. Without the introduction the remainder of the session cannot be understood.

    In my setup, I have 4 servers.

    1. Proxy – This is my http proxy
      • This server will run the sensu-server, sensu-api, redis, dashboard and rabbitmq-server.
      • This server will test reachability to the RDS, Elastic Cache and the other servers in my setup (#2, #3 and #4)
    2. Web Administration – This is my server that hosts the administration for my customer.
      • This server will serve as a sensu-client.
    3. Web Services – This is my server that hosts the webservices for my Web Administration and Mobile applications.
      • This too will serve as the sensu-client.
    4. RDS and Elastic Cache Services – This is the standard AWS services.
      • The Proxy server will test the reachability of RDS and Elastic Cache from time to time.
    5. I’ll one more external server to check the reachability of the proxy from the network. Perhaps another AWS EC2 on a different setup.
    Note, we also found that sensu-client takes up about 100Mb memory
    to operate. And sensu-server and sensu-api takes up about 300Mb 
    to operate. So if you have a aws micro with apache, mysql, redis, 
    running Sensu full stack is a bad idea.

    Now, to install sensu-client on the servers, run the following commands

    Install Sensu

    • Goto here to install sensu on all the servers.
    • Do not start the sensu-server and sensu-api service on the same server.
    • sudo apt-get install ruby2.0 ruby2.0-dev
    • sudo gem2.0 install sensu-plugins-disk-checks
    • sudo gem2.0 install sensu-plugins-memory-checks
    • sudo gem2.0 install sensu-plugins-network-checks
    • sudo gem2.0 install sensu-plugins-mysql-checks
    • sudo gem2.0 install sensu-plugins-cpu-checks
    • sudo gem2.0 install sensu-plugins-http-checks
    • sudo gem2.0 install sensu-plugins-process-checks
    • Once installed, copy the /etc/sensu/config.json.example to /etc/sensu/config.json

    To install the needed sensu plugins

    • goto here
    • search for (control f) the needed plugins
    • Install git on your ubuntu box (sudo apt-get install git)
    • git clone the needed projects
    • Once you have git cloned the need projects
    • Copy all the files from bin folder to /etc/sensu/plugins.

    Install RabbitMQ

    • https://sensuapp.org/docs/0.25/installation/install-rabbitmq-on-ubuntu-debian.html
    • Configure RabbitMQ for sensu-clients to connect
      • Change the username password on /etc/sensu/rabbitmq.json
      • This username password will be used by sensu client to connect to the rabbitmq server
      • sudo vi /etc/sensu/conf.d/rabbitmq.json
      • Remove localhost from rabbitmq and use the Internal IP address of the rabbitmq server
      • Rename user to “mcruiseon”
      • Rename password to “”
      • Retain the port to 5672
      • Open the port 5672 (only for the sensu-client IP Address)
      • At this time, errors on the sensu-client “Detected TCP connection failure” will go away
    • Configure RabbitMQ dasboard
      • Create one the rabbitmq web login password (replace username and password)
        • rabbitmqctl add_user username password
        • rabbitmqctl set_user_tags username administrator
        • rabbitmqctl set_permissions -p / username “.*” “.*” “.*”
      • On AWS
        • Open the 15672 port on the #1 server
        • Open on  your brower :15672
        • Login using the username / password you have set on rabbitmqctl (previous step)
    • Install Redis-server
      • sudo apt-get install redis-server
    • This link helped setup the dashboard

    My Proxy has the following conf files for configuring the sensu server

    • api.json
    {
     "api": {
     "host": "localhost",
     "port": 4567
     }
    }
    • rabbitmq.json
    {
     "rabbitmq": {
     "host": "0.0.0.0",
     "port": 5672,
     "vhost": "/sensu",
     "user": "yourownusername",
     "password": "yourownpassword"
     }
    }
    • redis.json
    {
     "redis": {
     "host": "localhost",
     "port": 6379
     }
    }

    My servers #2 #3 #4 are configured with sensu client. The following config file can be configured for each of my servers.

    {
    "rabbitmq": {
     "host": "ipaddress_of_sensu_server_Proxy_#1",
     "port": 5672,
     "user": "yourownusername",
     "password": "yourownpassword",
     "vhost": "/sensu"
     },
     "api": {
     "host": "localhost",
     "port": 4567
     },
     "checks": {
     "test": {
     "command": "echo -n OK",
     "subscribers": [
     "test"
     ],
     "interval": 60
     },
     "memory-percentage": {
     "command": "check-memory-percent.sh -w 50 -c 70",
     "interval": 10,
     "standalone": true,
     "subscribers": [
     "test"
     ]
     }
     },
     "client": {
     "name": "<name of this ubuntu box, and how you what to identify it>",
     "address": "127.0.0.1",
     "subscriptions": [
     "test"
     ]
     }
    }

    To check, just restart sensu-client service and run a tail on /var/log/sensu/sensu-client.log 

    I ran into some trouble with sensu, stackoverflow came to help. Check out the question here.

    After this I installed the dashboard on the Proxy #1 server.

    • sudo apt-get install uchiwa
    • sudo vi /etc/sensu/uchiwa.json
    • sudo update-rc.d uchiwa defaults
    • netstat -teapu | grep 3000
    {
     "sensu": [
     {
     "name": "Sensu",
     "host": "127.0.0.1",
     "port": 4567,
     "timeout": 10
     },
     {
     "name": "Site 2",
     "host": "api2.example.com",
     "port": 4567,
     "ssl": false,
     "path": "",
     "user": "",
     "pass": "",
     "timeout": 10
     }
     ],
     "uchiwa": {
     "host": "0.0.0.0",
     "port": 3000,
     "refresh": 10,
     "user":"logintoyourdashboard_notsame_asrabbitmq_dontconfuseyourselfhere",
     "pass":"passwordtoyourdashboard"
     }
    }

    Dont forget to open up the port 3000 on AWS.

    Thats it, you are done..  post questions here if you face any trouble. Also please provide feedback on this post.

    Checks

    Some sensu-checks need special ruby gems for installation

    check-ping needs

    • sudo gem install sensu-plugins-network-checks
    • sudo sensu-install -p network-checks
    • sudo  gem install net-ping

    AWS Handlers (for SES)

    • sudo gem2.0 install amazon-ses-mailer
    • sudo gem install aws-sdk

     

    Installing GenyMotion on your ubuntu 14

    0

    Android App
    – Added exception handling and error checking for request and response
    – Updated the Drive->Requirements->Detailed Requirements, added Section 13 for future exception handling rules
    – Installed GenyMotion Freemium, added all 4.x devices

    We can test the app on the virtualbox, we dont need to invest on devices, I think 🙂

    For installing GenyMotion
    https://www.genymotion.com/download/
    https://help.ubuntu.com/community/VirtualBox/Installation

    Install the Emulators 4.4 (only)
    After you load the emulator you will Google Play is not installed, for that the following links are useful.
    Works – http://stackoverflow.com/questions/21986237/installing-google-apps-on-genymotion
    Does not work – http://stackoverflow.com/questions/20121883/how-to-install-google-play-services-in-a-genymotion-vm-with-no-drag-and-drop-su

    http://filetrip.net/dl?4SUOrdcMRv
    https://www.androidfilehost.com/?fid=95832962473395379

    ld: library not found for -l

    0

    To build as libary

    Right Click->Properties->Build->Setting-Build Artifact->Dropdown to choose Shared/Static library

    Difference between Shared / Static library

    Now to include this library, copy the library to the project you want it and put it in a libs folder at the same level as the src level. Note, I am assuming that you have a src folder.

    On this project Right Click->Properties->Build->Setting->Libraries->

    1. in the -l area add name of library without the prefix lib and without the suffix .so
    2. in the -L area add ../libs, remember its 2 dots since the current folder is src, not the root of the project 🙂 YEAH, I tried ./libs for 1 hours before trying out ../libs

     

    Building our first C++ cuda library

    0

    So the first thing you do when you get CUDA ready is try your hands out on the sample code and NSight (the eclipse of CUDA). This goes smoothly, but as soon as you start writing cpp code for developing libraries, you can hit all kinds of issues. This post has fixes for those crazy issues that has no help on even StackOverflow.

    Reference

    #include "common.h"
    #include <cuda_runtime.h>
    #include <sys/time.h>
    #ifndef _TIMER_H
    #define _TIMER_H
    class Timer {
    public:
     enum class TIMER_FORMAT {
     MILLISECONDS, MICROSECONDS, NANOSECONDS, SECONDS
     };
    enum class PROCESSOR_CONFIG {
     HOST, DEVICE
     };
    __host__ Timer(PROCESSOR_CONFIG config);
     __host__ ~Timer();
     __host__ void StartTimer();
     __host__ void StopTimer();
     __host__ double GetElapsedTime(TIMER_FORMAT format);
    protected:
     cudaEvent_t m_eventStart;
     cudaEvent_t m_eventStop;
     PROCESSOR_CONFIG m_config;
     double m_startTimeInNS;
     double m_stopTimeInNS;
    };
    #endif // _TIMER_H
    
    Timer.cu
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    #include "Timer.cuh"
    
    Timer::Timer(PROCESSOR_CONFIG config) {
     if (config == Timer::PROCESSOR_CONFIG::DEVICE) {
     CHECK_API_FOR_ERROR(cudaEventCreate(&m_eventStart));
     CHECK_API_FOR_ERROR(cudaEventCreate(&m_eventStop));
     } else {
    #ifdef __COMPILE_FOR_WINDOWS__
     LARGE_INTEGER lpFrequency;
     if (QueryPerformanceFrequency(&lpFrequency) == 0) {
     printf("Could not query performance frequency on CPU");
     m_performanceFrequency = 0;
     }
     else
     m_performanceFrequency = lpFrequency.QuadPart;
    #endif
     m_startTimeInNS = 0;
     m_stopTimeInNS = 0;
     }
     m_config = config;
    }
    
    Timer::~Timer() {
    }
    
    void Timer::StartTimer() {
     if (m_config == Timer::PROCESSOR_CONFIG::DEVICE) {
     CHECK_API_FOR_ERROR(cudaEventRecord(m_eventStart));
     } else {
    timespec ts;
     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
     m_startTimeInNS = ts.tv_nsec;
    }
    }
    
    void Timer::StopTimer() {
     if (m_config == Timer::PROCESSOR_CONFIG::DEVICE) {
     CHECK_API_FOR_ERROR(cudaEventRecord(m_eventStop));
     CHECK_API_FOR_ERROR(cudaEventSynchronize(m_eventStop));
     } else {
    timespec ts;
     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
     m_stopTimeInNS = ts.tv_nsec;
    }
    }
    
    double Timer::GetElapsedTime(TIMER_FORMAT format) {
     double elapsedTimeInNanoseconds = 0;
    
     if (m_config == Timer::PROCESSOR_CONFIG::DEVICE) {
     float elapsedTimeInMilliseconds = 0;
     CHECK_API_FOR_ERROR(
     cudaEventElapsedTime(&elapsedTimeInMilliseconds, m_eventStart,
     m_eventStop));
     elapsedTimeInNanoseconds = elapsedTimeInMilliseconds * 1000.0 * 1000.0;
     } else
     elapsedTimeInNanoseconds = m_stopTimeInNS - m_startTimeInNS;
    
     switch (format) {
     case TIMER_FORMAT::MILLISECONDS:
     return elapsedTimeInNanoseconds / (1000 * 1000);
    
     case TIMER_FORMAT::MICROSECONDS:
     return (elapsedTimeInNanoseconds / 1000);
    
     case TIMER_FORMAT::NANOSECONDS:
     return (elapsedTimeInNanoseconds);
    
     case TIMER_FORMAT::SECONDS:
     return (elapsedTimeInNanoseconds * 1000 * 1000 * 1000);
    
     default:
     return 0;
     }
    }
    
    int main(int argc, char* argv[]) {
    
    }
    
    
    • Syntax Error : For some reason I got a “Syntax Error” on the constructor and destructor lines of my CPP code. To fix this, rename your .cpp as .cu. And rename your .h as .cuh.
    • My enums PROCESSOR_CONFIG, would not resolve at  Timer::PROCESSOR_CONFIG::DEVICE. I had a similar issue to this. To fix this I had to compile using a C11 directive. I used this, to fix it. Refer to my answer (Siddharth).
    • Finally I had to add a main (I need to get rid of this expectation soon).

    Getting Started with CUDA

    0

    Interesting and challenging as I would define it. So get ready for a ride. If you are a applications developer, switch off now. This is a system programmer post, and someone who enjoys toying with the command prompt :).

    To get started with CUDA, first thing you need is a 64bit OS. Without that CUDA wont work.

    Once you have a 64 bit OS, goto developer.nvidia.com, and follow the instructions. DONT download the run file, download the dpkg file for the installation. The run file is stupid and useless. Takes for ever to run and gets no results. You may also end up screwing up your OS.

    So I ran the following

    sudo dpkg -i cuda-repo-ubuntu1504-7-5-local_7.5-18_amd64.deb
    sudo apt-get update
    sudo apt-get install cuda
    sudo apt-get install nvidia-cuda-toolkit
    sudo apt-get install cuda-drivers
    sudo apt-get install nvidia-nsight
    export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib:$LD_LIBRARY_PATH
    cd /usr/local/cuda-7.5/
    sh cuda-install-samples-7.5.sh /home/<username>/development
    sudo apt-get install nvidia-352
    cd /home/<username>/development/<cuda samples folder>/
    make
    

    To check your installation

    lspci | grep -i nvidia 
    01:00.0 VGA compatible controller: NVIDIA Corporation GK110 [GeForce GTX 780] (rev a1)
    01:00.1 Audio device: NVIDIA Corporation GK110 HDMI Audio (rev a1)
    sudo dpkg --list | grep nvidia
     ii nvidia-352 352.63-0ubuntu0.15.04.1 amd64 NVIDIA binary driver - version 352.63
     ii nvidia-352-dev 352.63-0ubuntu0.15.04.1 amd64 NVIDIA binary Xorg driver development files
     ii nvidia-352-uvm 352.39-0ubuntu1 amd64 Transitional package for nvidia-352
     ii nvidia-cuda-dev 6.5.14-1 amd64 NVIDIA CUDA development files
     ii nvidia-cuda-doc 6.5.14-1 all NVIDIA CUDA and OpenCL documentation
     ii nvidia-cuda-gdb 6.5.14-1 amd64 NVIDIA CUDA Debugger (GDB)
     ii nvidia-cuda-toolkit 6.5.14-1 amd64 NVIDIA CUDA development toolkit
     ii nvidia-modprobe 352.39-0ubuntu1 amd64 Load the NVIDIA kernel driver and create device files
     ii nvidia-nsight 6.5.14-1 amd64 NVIDIA Nsight Eclipse Edition
     ii nvidia-opencl-dev:amd64 6.5.14-1 amd64 NVIDIA OpenCL development files
     ii nvidia-opencl-icd-352 352.63-0ubuntu0.15.04.1 amd64 NVIDIA OpenCL ICDThis should
     ii nvidia-prime 0.8.1 amd64 Tools to enable NVIDIA's Prime
     ii nvidia-profiler 6.5.14-1 amd64 NVIDIA Profiler for CUDA and OpenCL
     ii nvidia-settings 352.39-0ubuntu1 amd64 Tool for configuring the NVIDIA graphics driver
     ii nvidia-visual-profiler 6.5.14-1 amd64 NVIDIA Visual Profiler for CUDA and OpenCL
    cd ~/development/<cuda samples folder>
    ./deviceQuery

    If this displays an error than run

    sudo apt-get purge $(dpkg -l | awk '$2~/nvidia/ {print $2}') sudo add-apt-repository ppa:graphics-drivers/ppa 
    sudo apt-get update 
    sudo apt-get install cuda 
    sudo apt-get install nvidia-cuda-toolkit 
    sudo apt-get install cuda-drivers 
    sudo apt-get install nvidia-nsight

    Ideally ./deviceQuery should display

    ./deviceQuery Starting...
    CUDA Device Query (Runtime API) version (CUDART static linking)
    Detected 1 CUDA Capable device(s)
    Device 0: "GeForce GTX 780"
     CUDA Driver Version / Runtime Version 7.5 / 7.5
     CUDA Capability Major/Minor version number: 3.5
     Total amount of global memory: 3072 MBytes (3221028864 bytes)
     (12) Multiprocessors, (192) CUDA Cores/MP: 2304 CUDA Cores
     GPU Max Clock rate: 902 MHz (0.90 GHz)
     Memory Clock rate: 3004 Mhz
     Memory Bus Width: 384-bit
     L2 Cache Size: 1572864 bytes
     Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
     Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
     Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
     Total amount of constant memory: 65536 bytes
     Total amount of shared memory per block: 49152 bytes
     Total number of registers available per block: 65536
     Warp size: 32
     Maximum number of threads per multiprocessor: 2048
     Maximum number of threads per block: 1024
     Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
     Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
     Maximum memory pitch: 2147483647 bytes
     Texture alignment: 512 bytes
     Concurrent copy and kernel execution: Yes with 1 copy engine(s)
     Run time limit on kernels: Yes
     Integrated GPU sharing Host Memory: No
     Support host page-locked memory mapping: Yes
     Alignment requirement for Surfaces: Yes
     Device has ECC support: Disabled
     Device supports Unified Addressing (UVA): Yes
     Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
     Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
    deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 7.5, CUDA Runtime Version = 7.5, NumDevs = 1, Device0 = GeForce GTX 780
     Result = PASS