Saturday, June 13, 2015

Enable Jetty request log in Maven

For development I use Jetty locally via jetty-maven-plugin. One of the nice features it has WebSocket support so there is no need to use standalone application servers like Wildfly.

If you check jetty documentation you'll see that request logging is configured in jetty's context xml file. Fortunately if you don't want to add another xml to your project there is another option.

Add the following configuration to your project's pom.xml:

The implementation attribute value of the requestLog property can be one of these: AsyncNCSARequestLog, NCSARequestLog or Slf4jRequestLog. Note that options vary depending on the implementation. I choose NCSARequestLog because it produces logs in NCSA common log format

The log file will be created in the root directory of your project when jetty is started.

Tuesday, June 9, 2015

Lets try that golang hype

Recently I've decided to take a look at golang. I didn't know where to start because I didn't have any problem to solve. So I briefly outlined the first steps to cover. I want to create a simple web service or a couple of services for some SPA application. But first I need to get more familiar with golang environment and tools.

Installation

I opened the golang home page and quickly viewed its content.

It happened that my laptop has funtoo linux installed. I quickly typed in the console the following to install the golang package:

$ emerge -av dev-lang/go

I wasn't quite sure funtoo has golang in its repository. Fortunately there was golang version 1.4.2 which is the most recent stable. There is a lot of files installed but I was interested in binaries first:

$ equery f dev-lang/go | grep /usr/bin/
/usr/bin/go
/usr/bin/gofmt

Not much but lets run them and see its output:

$ go

The usage section briefly explains the commands available. I noticed few commands that do not belong to a compiler usually: clean, run, test, and get. Looks like it's a compiler, a runtime, a package manager and a test runner altogether which is not following the UNIX-way. I decided to figure out it later.

Lets see what is gofmt:

$ gofmt

...which outputs nothing but just waiting for input. Running with -h gives a bit more info. As far as I see it is a some kind of a linter for golang sources. I wonder why it is in a separate executable and not included into the go. Leaving it for now.

Write some code

There is nothing special about it just create a .go file and paste the example from golang site. I skipped the formatting for a purpose:

$ vim /tmp/hello_world.go

Now lets get back to gofmt and see how it works, the -w option overwrites file's content instead of writing formatted code to stdout:

$ gofmt -w hello_world.go

Works as expected but I noticed that it does not add a new line to the end of file:

By the way if you use vim you can add a hook for formatting with gofmt after buffer write:

:set autoread | autocmd BufWritePost *.go :silent !gofmt -w %

Build and run it

I've seen earlier from the go executable output that I can run the source file right away by using run command option:

$ go run hello_world.go
Hello, World!

Pretty straightforward but I noticed a small delay before program output probably caused by intermediate building or interpreting step of the run command. Also there was no binary file created.

Running with the build command option finally produced a binary:

$ go build hello_world.go

The size of the binary is about 2M:

$ ll hello_world
-rwxr-xr-x 1 user users 1.9M Jun 9 13:29 ./hello_world

Also it's not linked dynamically to anything:

$ ldd hello_world
not a dynamic executable

and is a 64-bit executable:

$ file hello_world
hello_world: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

Summary

As a conclusion I think golang tools are friendly and simple to start with. Except a few things I noticed. These are camel notation for functions and a single executable for building, testing, running and managing packages for your golang programs.

If you like you could try golang online without having it installed locally.

Monday, June 25, 2012

Lenovo T410 nVidia Display Brightness in Linux Mint

Linux Mint uses opensource nouveau driver for nVidia graphics and works fine. There is no additional configuration needed. But if you use binary driver you may experience that brightness function keys do not work.

First of all make sure to run nvidia-xconfig in order to create xorg.conf. This step is optional if you use the driver installer. The installer provides a wizard where you can allow to create customized xorg.conf.

Edit /etc/X11/xorg.conf and add the following option to the Device section:

Section "Device"
    ...
    Option "RegistryDwords" "EnableBrightnessControl=1"
EndSection

You might have to restart X server to use new settings.

Monday, June 18, 2012

Deploy Rails 3 Application to Google App Engine Using Warbler

So far Google App Engine does not support ruby/rails applications but there is a way to build a Java servlet using warbler and deploy it to GAE.

Prerequisites

  • GAE SDK 1.6.6
  • Ruby 1.8.7 with rubygems
  • Bundler 1.1.4

Now we're going to install required tools. I already have an account created on GAE but if you don't you should sign-up first to GAE.

Get the latest SDK and extract it locally:

$ sudo unzip google_appengine_1.6.6.zip -d /opt

Install Ruby, Rubygems and Bundler

$ sudo apt-get install ruby rubygems -y

$ sudo gem install bundler

Create a Rails 3 application

Make an application directory and init a Gemfile.

$ mkdir warbler-test && cd warbler-test && bundle init

Add a Rails gem dependency and install it using bundler in a separate directory called vendor. This will make sure your gems are isolated from the system. Also we'll need an extra gem called therubyracer for generating assets. Your Gemfile should look like this:

# A sample Gemfile
source "https://rubygems.org"

gem "rails"
gem "therubyracer", :platforms => :ruby

$ bundle install --path vendor

Bootstrap the Rails 3 application in the current directory (warbler-test). The rails bootstrap script will ask to overwrite the Gemfile. It is safe to allow:

$ bundle exec rails new .

Add warbler gem dependency to the end of Gemfile:

...
# Use warbler to create war
group :development do
  gem "warbler"
end

Run bundle install once again since the Gemfile was changed by the rails script. It is not necessary to specify --path vendor because bundler already configured the application config to use isolated directory

Run the rails server and verify your application is available locally at http://0.0.0.0:3000

$ bundle exec rails server

Deploy the application

When you see it is ready to be deployed, create a war file using warbler. This will create warbler-test.war file taking your current directory as a file name:

$ bundle exec warble war

The final step is to create appengine-web.xml deployment descriptor file and upload using GAE SDK tools.

Extract just generated war file

$ unzip warbler-test.war -d war

Create the descriptor with the following content by changing the [web_app_id] to your GAE application id:



  [web_app_id]
  1
  false

Finally, deploy your application. This step will take some time, probably a couple of minutes to complete:

$ /opt/appengine-java-sdk-1.6.6/bin/appcfg.sh update war

After the deployment completes you should be able to access your application on GAE. Note the prefixed version in the URL, it is a <version>1</version> that we configured in the appengine-web.xml descriptor. In my case the URL is http://1.warbler-test.appspot.com/

Thursday, June 14, 2012

Outline Single Tab in Firefox with ColorfulTabs

Sometimes it happens that you need to pay a special attention on site you are working with. It could be a production site where you have a read/write account but should be careful to prevent of doing changes by mistake. I have found a simple way to outline such sites with color in Firefox using ColorfulTabs add-on.

After the installation you may see a default coloring of tabs like this:

As for me that number of colors is a bit annoying and does not look like I want it to be. So I want to outline only domains I need to pay my attention on.

Open preferences of ColorfulTabs add-on. In the General section change the Use Default Palette to Generate Colors By Domain Hostname:

Then remove everything in Presets section and add this instead:

Now you got two domain patterns (that are regular expressions) and tab color chosen. Make sure the order of domain patterns is correct - the most restrictive patterns should go lower. The .* means any domain and .*massivecoding.blogspot.com is my special site.

Finally, the add-on behaves as expected:

Wireshark For Non-Superusers

A couple days ago I was need to see how the RTMP streaming protocol works. I decided to use Wireshark which is an awesome tool to analyze network traffic with nice user interface and live capturing behavior powered with filtering options.

First, you need to install it. I use Linux Mint which provides Wireshark from repository:

$ sudo apt-get install wireshark -y

This package provides dumpcap utility to capture and dump packets. In Debian and compatible distros such as LMDE it does not allow to use the utility for non-root users by default. You may see the warning box after Wireshark start in this case:

In order to change this I did the following:

Run the configuration script wizard for the package and answered Yes to allow usage for non-superusers:

$ sudo dpkg-reconfigure wireshark-common

If you get this warning after a new capture start, you should add your user to the wireshark group:

by doing the following:

$ sudo gpasswd -a <user> wireshark

After logging out and logging in I was able to use Wireshark.

Tuesday, June 12, 2012

NetBeans SQL Error On Non-null Date Values

I have a column of date type in MySQL database which is not null and value is set to default '0000-00-00'. When I try to fetch a row with a date values composed entirely of zeros in NetBeans's Database Explorer I get the following error:

Error code 0, SQL state S1009: Value '0000-00-00' can not be represented as java.sql.Date Line 1, column 1

This is an error from the JDBC driver which is used by NetBeans to connect MySQL server. The default behavior of the database driver is to raise an exception. To change this you need to pass an additional property to the JDBC connection URL:

jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull

Other possible values of the property are the default exception and round.

More info can be found here: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html