Saturday 10 September 2016

Ubuntu fix suspension on ThinkPads

Under ubuntu and ThinkPads T460 series there is a problem of screen not waking up after sleep. How to test\reproduce:

sleep 3; xset dpms force off

to fix it just create file

/etc/pm/config.d/config

and put there one line

DISPLAY_QUIRK_S3_BIOS="true"

Thursday 10 July 2014

fixing suspend on ubuntu

On lenovo Y500 with Ubuntu 14.04 suspend is not working properly. Reinstallation of video drivers does not really affect situation. What worked for me is taken from here http://ubuntuforums.org/showthread.php?t=1978290

Step 1: Open up a terminal and create a scrip file as follows:

sudo gedit /etc/pm/sleep.d/20_custom-ehci_hcd

Step 2: paste script

#!/bin/sh
# File: "/etc/pm/sleep.d/20_custom-ehci_hcd".
TMPLIST=/tmp/ehci-dev-list

case "${1}" in
        hibernate|suspend)

        ;;
        resume|thaw)

     chvt 1
     chvt 7
        ;;
esac

Step 3: Give the script permission to run by typing the following in the terminal:

sudo chmod 755 /etc/pm/sleep.d/20_custom-ehci_hcd

Sunday 29 June 2014

mini-dinstall upstart

After installation and configuration of mini-dnstall it's a nice ida to have upstart service.
Take a look here for example
https://github.com/aakhmerov/examples/blob/mini-dinstall-upstart/mini-dinstall
see in README.md installation instructions for ubuntu
sudo cp ./mini-dinstall /etc/init.d
sudo chmod 755 /etc/init.d/mini-dinstall
sudo chown root:root /etc/init.d/mini-dinstall
sudo update-rc.d mini-dinstall defaults
initial version of upstart taken from http://burtonini.com/computing/mini-dinstall

Thursday 17 January 2013

JS module for Java developers : modular Backbone with testing

Ammount of JS code in applications across the globe increases fast. This fact has been wiedly illustrated by professionals working in industry, for instance here and here. This fact raises requirement to build and maintain modular js code with dependencies management and testing capabilities without loss of integration with common best practices, one of wich is Maven usage in Java world. Solution to that problem set is going to be a subject of this post description.

Task outline and framewor set

Task: create Maven project with dependency management, containing structured JS code and htm markup with templating and testing support. The main reason behid creation of such project is the desire to deliver static content, like js files and html markup, with Apache HTTP or Nginx servers without usage of Java web container or full EE server. And provide REST service in separate modules deployed in appropriate environment. After some research I've came up with following set of required components: Description of each framework is out of scope for this topic, but feel free to read more about each of them if you see some new names here.

Maven project

Maven is providing us clean and strict way of project description, with fixed directory layout, and files locations. We start with creation of pom.xml file describing the project, with name, group and version filled out. Packaging is chosen to be .war as it's a web module eventually. Once basic information is present, we have to add some plugins:
  • maven-jasmine-plugin - for test launches with appropriate configuration to force RequireJS usage
     
            com.github.searls
            jasmine-maven-plugin
            1.2.0.0
            true
            
                
                    
                        test
                    
                
            
            
                ${project.basedir}/src/main/webapp/js
                ${project.basedir}/src/test/js
                FIREFOX_3
                
                REQUIRE_JS
    
                
                    <source>libs/jasmine/jasmine-jquery-1.3.1.js</source>
                
                
                libs/require/require.js
            
           
     
  • maven-war-plugin to make sure that project is going to be successfully built even if web.xml, which is a standard java web module descriptor, not present.
    
        org.apache.maven.plugins
        maven-war-plugin
        2.1
        
            false
        
    
    
  • maven-resources-plugin to provide Jasmine specs (tests) with all required resources
    
        maven-resources-plugin
        
            
                copy-js-files
                generate-test-resources
                
                    copy-resources
                
                
                    ${project.build.directory}/jasmine
                    
                        
                            src/main/webapp
                            false
                        
                    
                
            
        
    
    

once we're finished we have to create appropriate folder structure:
.
├── README.md
├── aqua-jasmine-web.iml
├── pom.xml
└── src                                                //root folder (maven structure)
    ├── main                                           //main sources (maven structure)
    │   └── webapp                                     //web application resources (maven structure)
    │       ├── css
    │       │   ├── application.css
    │       │   ├── bootstrap.css
    │       │   ├── chosen.css
    │       │   ├── docs.css
    │       │   ├── expressmon.css
    │       │   ├── graphs.css
    │       │   ├── jquery.ui.all.min.css
    │       │   ├── style.css
    │       │   └── styles.css
    │       ├── imgs
    │       │   └── 334.gif
    │       ├── index.html                              //one-pager index
    │       ├── js                                      //all js files root
    │       │   ├── app.js                     
    │       │   ├── collections                         //Backbone collections folder
    │       │   │   └── projects                        //projects pages collections
    │       │   │       └── TestProjectsCollection.js
    │       │   ├── libs                                //all js libs
    │       │   │   ├── backbone
    │       │   │   │   └── backbone-min.js
    │       │   │   ├── handlebars
    │       │   │   │   └── handlebars.js
    │       │   │   ├── jasmine
    │       │   │   │   └── jasmine-jquery-1.3.1.js
    │       │   │   ├── jquery
    │       │   │   │   ├── jquery-min.js
    │       │   │   │   └── jquery-serialize.js
    │       │   │   ├── require
    │       │   │   │   ├── require.js
    │       │   │   │   └── text.js
    │       │   │   └── underscore
    │       │   │       └── underscore-min.js
    │       │   ├── main.js                             //js entry point file (RequireJS enforced)
    │       │   ├── models                              //Backbone models
    │       │   │   └── projects                        //models for projects pages
    │       │   │       └── ProjectModel.js
    │       │   ├── router.js
    │       │   └── views                               //Backbone views
    │       │       ├── layout                          //layout views
    │       │       │   ├── EmptyContent.js
    │       │       │   ├── EmptyFooter.js
    │       │       │   ├── NavigationHeader.js
    │       │       │   └── PageLayoutView.js
    │       │       └── projects                        //projects pages views
    │       │           ├── ProjectView.js
    │       │           └── TestProjectsView.js
    │       └── templates                               //static html templates
    │           ├── layout                              //layout htmls (header,footer,etc...)
    │           │   ├── emptyContentTemplate.html
    │           │   ├── footerTemplate.html
    │           │   ├── navigationTemplate.html
    │           │   └── simpleTemplate.html
    │           └── projects                            //projects pages templates
    │               ├── projectTemplate.html
    │               └── testProjectsTemplate.html
    └── test                                            //tests sources (maven structure)
        └── js                                          //jasmine tests
            ├── layout
            │   └── AboutLayout.js
            └── projects

The whole project starts at main.js file, this is forced by RequireJs. It contains declarations of shortcuts, which are reused in dependencies declarations of other modules

require.config({
    paths: {
        jquery: 'libs/jquery/jquery-min',
        underscore: 'libs/underscore/underscore-min',
        backbone: 'libs/backbone/backbone-min',
        handlebars: 'libs/handlebars/handlebars',
        templates: '../templates',
        text: 'libs/require/text'

    },
    shim: {
        handlebars: {
            exports: 'Handlebars'
        }
    }

});

and invocation of application initialization

require([
    // Load our app module and pass it to our definition function
    'app'

], function(App){
    // The "app" dependency is passed in as "App"
    // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
    App.initialize();
});
Application initialization is pretty straight forward and simply invokes Backbone router initialization which declares actions that should be taken once certain url of application is reached.

Templates and Layouts

Static .html markup is loaded throug RequireJs text plugin and passed as a template field value to requesting Backbone View component, which gets compiled into Handlebars template during View instantiation. Markup itself looks as follows:

This template will be converted to valid markup during rendering, which will require you to provide handlebars template wrapping object id and title fields values.

As none of listed frameworks provides us with layout principle, we are going to introduce one by hand. We add Page View which is a simple composition of header, footer and content views, we put default initialization code into page object, which can be overriden through options during object instantiation.

define([
    'jquery',
    'underscore',
    'backbone',
    'views/layout/NavigationHeader',
    'views/layout/EmptyContent',
    'views/layout/EmptyFooter',
    'text!templates/layout/simpleTemplate.html' ,    
    'handlebars'
], function($, _, Backbone,NavigationHeader,EmptyContent,EmptyFooter,simpleTemplate){

    var PageLayoutView = Backbone.View.extend({

        template : Handlebars.compile(simpleTemplate),
        //defaults to NavigationHeader view function
        headerContent : NavigationHeader,
        //defaults to EmptyContent view function
        mainContent :  EmptyContent,
        //defaults to EmptyFooter view function
        footerContent : EmptyFooter,

        initialize : function(options) {

            //instantiate appropriate views based on component functions
            if (options.mainContent != undefined && options.mainContent != null) {
                this.mainContent = options.mainContent;
            }

            if (options.headerContent != undefined && options.headerContent != null) {
                this.headerContent = options.headerContent;
            }

            if (options.footerContent != undefined && options.footerContent != null) {
                this.footerContent = options.footerContent;
            }
        },

        render: function(){
            //compile handlebars template with appropriate markup of components
            var html = this.template();
            //append appropriate content to root element right away after compilation
            $(this.el).html(html);

            this.headerView = new this.headerContent({el : '#header'});

            this.mainView = new this.mainContent({el : '#mian'});

            this.footerView = new this.footerContent({el : '#footer'});

            this.headerView.render();
            this.mainView.render();
            this.footerView.render();
            return this;
        }

    });
    return PageLayoutView;
});

Testing

Testing is suggested to be performed throug the Jasmine framework, which provides fairly rich capabilities. Set up of tests relies on the same RequireJS style. The only thing that I had to add is logging capabilities, as HtmlUnit sandbox, which will be running specs code doesn't have one provided. One extra thing to notice for Mavn users is that jasmine-maven-plugin has a bdd goal which will allow you to open jasmine test report in browser and rerun your jasmine tests once you refresh browser tab\window. This approach is really a time saver for those who believe TDD.

URL's

You can grab sources and boilerplate here.
Russian translation

References:
  • http://searls.github.com/jasmine-maven-plugin/
  • http://pseudobry.com/jasminemavenrequirejscoverage/
  • http://backbonetutorials.com/organizing-backbone-using-modules/

Friday 7 December 2012

Basic vi colors on mac os x

vi ~/.vimrc

paste lines:

set nocompatible
syntax on
:wq
enjoy

Sunday 25 November 2012

Testing with embedded mongodb

If you have to use embedded mongodb instance for testing there is a foursquare implementation called Fongo. You will have to install it locally or deploy it into your intranet repository in order to use with maven.

In order to use it with spring-data-mongodb you can use something like


1:  <bean id="mongoOperations" class="ru.sample.dal.utils.ImMemoryMongoFactory" factory-method="provide" scope="prototype"/>
2:  
3:  <bean id="mongoTemplate" class="ru.sample.dal.utils.ImMemoryMongoFactory" factory-method="provide" scope="prototype"/>
4:  
5:  public class ImMemoryMongoFactory {
6:    private static Fongo fongo = new Fongo("name");
7:  
8:    public static MongoOperations provide() {
9:      return new MongoTemplate(fongo.getMongo(), "database");
10:    }
11:  }


and just autore MongoOperations in your custom repository implemenations. Basic implementation of repositories should start using those beans by default too

Saturday 24 November 2012

Stable WADL Maven plugin.

If your project is currently using

<dependency>
<groupId>org.jvnet.ws.wadl</groupId>
<artifactId>wadl-maven-plugin</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
 and you are looking for stable version of a plugin, you should be using following:

<groupId>org.jvnet.ws.wadl</groupId>
<artifactId>wadl-client-plugin</artifactId>
 <version>1.1.3</version>
this is the most recent and least bogous version of plugin. Nothat you will have to refactor code if you was using client generated by wadl plugin. Also note that to reach endpoints with compound paths like
"/{classPath}/{methodPath}"  you will have to create java instance of generated client's {classPath} instance first and get reference to {methodPath} inner class instance from it in order for client to build URI's appropriately.