Wednesday, January 21, 2015

Workspace Grid In Fedora 21

If you are running Fedora 21, you know that the built in workspace functionality sucks.

Thankfully, there's a simple workaround to get back your old fashioned grid workspaces!



Instructions:

1. Download Gnome Tweak Tool
2. Download the workspace extension code
3. Copy the workspace-grid@mathematical.coffee.gmail.com/ directory to ~/.local/share/gnome-shell/extensions/
4. Open Gnome Tweak Tool and enable the plugin

Hope that helps! Please let me know in the comment section if you have problems.


Sunday, March 16, 2014

Web Project Seed

I've created a template web project for easily getting started with new web applications. The image underneath shows a rough architecture of how it works.



The technogies included are listed below:
  1. Groovy
  2. Java (8)
  3. Spring (4)
  4. MongoDB
  5. Gradle
  6. Maven
  7. Node.js
  8. gulp.js

The code

Grab the code from GitHub here.

You may use this project as a starting point in any of your new web applications.
Happy coding!

Saturday, July 27, 2013

npm update error in Linux


Error

If you get the following error while updating npm:

error: unpacking of archive failed on file /usr/lib/node_modules/npm/doc: cpio: rename
or
Error: Cannot find module '../lib/npm.js'

Solution

Simply try to remove npm with your package manager, if that's how you installed it, or manually remove it. Then remove the following:

/usrlib/node_modules/npm
and
/usr/bin/npm

That should fix the broken packages, and you can try to reinstall npm!

Hope that helps!

Thursday, April 11, 2013

AngularJS controller

So I spent quite a lot of time figuring out how to set up an AngularJS controller without polluting the default namespace/scope. Here is the recipe with a simple click listener showing that it works:

index.html

<doctype html>
<html ng-app="main">
  <head></head>
  <body ng-controller="mainController">
    <button ng-click="log()">Log</button>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

main.js

var main = angular.module('main', []);

main.controller('mainController', function($scope) {
  $scope.log = function() {
    console.log('Logging a simple message...');
  }
});

You can easily see which names map to which implementation, e.g. ng-app="main" is angular.module('main', []);, so on and so fourth.

Note: It is extremely important to invoke angular.module with the second argument (in this case, the empty array), otherwise, it is not going to work.

Wednesday, April 3, 2013

Git push to Amazon EC2

If you have an Amazon EC2 instance, it is fairly straightforward to use Git to push your changes to it. The steps to do so are listed below.

On the server

1. Create the directory which will be the project root directory
mkdir myproject

2. Step into the directory and initialize a bare repository
cd myproject
git init --bare

3. Create post-receive and make it executable. This will ensure everything that is pushed is automatically incorporated into the project folder you created.
touch hooks/post-receive
chmod +x hooks/post-receive

4. Edit hooks/post-receive to include the Git work tree where everything that is pushed is eventually put.
vim hooks/post-receive # ...And add the below statement to it
GIT_WORK_TREE=/home/ec2-user/myproject git checkout -f

If you've carefully followed every step, this is all you need to do on the server.

On the client

1. Inside your Git project, add the address of the server
git remote add ec2 ec2-user@your-amazon-address:myproject

2. Finally, you should be able to push to your Amazon EC2 server
git push ec2 master

If you've carefully followed every step, this is all you need to do on the client.

Now you can check that everything you pushed is there in your project folder on the server.

Thanks for reading! Let me know in the comments below if you are having problems with any part of this tutorial.

Sunday, February 17, 2013

Simple RMI Tutorial

I have tried to make this RMI tutorial as short as possible, so that you may get started creating your own RMI applications as soon as possible.

The Callee


1. Fist of all you will have to create a project, e.g. rmi-callee.

2. Inside foo you will need to define a remote interface which the invoker may use.

For this example, we have a method greet in the interface Greetable which will reside in the default package (directly within src/):

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Greetable implements Remote {
    String greet() throws RemoteException;
}

3. The next step is to actually implement the interface in our class, again in the default package:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

class Greeter extends UnicastRemoteObject implements Greetable {

    public Greeter() throws RemoteException { super(); }

    @Override
    public String greet() throws RemoteException { return "Hi"; }

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

        // Bind an instance to rmiregistry
        Naming.bind("rmi://localhost/Greeter", new Greeter());
    }
}


Now that you have your class exposing some functionality, you may want to check if it works. From the command line create a script that will run your class named run.sh. Note: if you are using Windows, you might want to download MinGW to make your life a bit simpler.

4. First, create the JAR file containing the interface. PS: You should to be in the root of your project to do this:
cd rmi-callee/
jar cvf g.jar -C bin/ Greetable.class

5. Create the script to run the program:
touch run.sh
chmod +x run.sh

6. Inside run.sh:
java -cp bin/\
     -Djava.rmi.server.codebase=file:your/path/rmi-callee/g.jar\
     Greeter

7. Start rmiregistry as a background process:
rmiregistry &

8. Start the program:
./run.sh

It should now be running. If you get an exception, you are not doing it right. Be sure you have followed every step thoroughly, and are running each command from the right directory.

The Caller


Now you would like to create a caller which invokes the callee.

1. Create another project, e.g. rmi-caller.

2. Manually copy the Greetable interface from the first project into this one.

3. In the default package, create a class Main:
import java.rmi.Naming;

public class Main {

    public static void main(String[] args) throws Exception {
        Greetable g = (Greetable) Naming.lookup("rmi://localhost/Greeter");
        System.out.println(g.greet());
    }
}

4. Create the script to run the program:
cd rmi-caller/
touch run.sh
chmod +x run.sh

5. Inside run.sh:
#!/usr/bin/sh
java -cp bin/ Main

6. Run the example:
./run.sh

It should now invoke the callee if it is running, and output Hi. If you get an exception, you are not doing it right. Be sure you have followed every step thoroughly, and are running each command from the right directory.

Summary and code

I hope this tutorial will help you develop RMI systems. You can find the source code to rmi-callee and rmi-caller below:

Saturday, October 27, 2012

Ruby get method arguments

So, say you are writing some Ruby code, and want to get an overview of the arguments for a method, dynamically.

Consider this method with arguments a and b:

def foobar a, b
end

Using method(...), we can pass in the special variable __method__ to get the current method.

In turn, we can get the parameters of the method with .parameters, and we can then iterate over the parameters.

Now our code would look something like this:

def foobar a, b
  method(__method__).parameters.each do |arg|
    # ...
  end
end

In order to get the correct parameter name with it's associated value, the final solution would be:

def foobar a, b
  method(__method__).parameters.each do |arg|
    val = eval arg[1].to_s
    puts "#{arg[1]}: #{val}, #{val.class}"
  end
end

Happy coding!

Tuesday, October 9, 2012

Validate XML and DTD

Back to school again, I found that I needed to write some XML and DTD by hand.

The most important aspect of the process is to have a valid XML document as well as a valid DTD. There are perhaps some online tools for this, but none of them are very intuitive.

However, there is a tool named xmllint which does exactly this.

It is included with libxml (package), so there's a great chance you already have it.

Here is an example on how to use it:

editors.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT editors (editor)>
<!ELEMENT editor  (title, desc?)>
<!ELEMENT title   (#PCDATA)>
<!ELEMENT desc    (#PCDATA)>

editors.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE editors SYSTEM "editors.dtd">
<editors>
  <editor>
    <title>Vim
    <!--Powerful text editor-->
  </editor>
</editors>

And to validate the DTD and XML, execute the following from your command line:
xmllint --valid editors.xml  --dtdvalid editors.dtd

Note: use can use xmllint to validate XML Schemas just use the above command and replace --dtdvalid with with -schema and provide a schema file instead of the DTD file.

Hope that helps!

Wednesday, August 8, 2012

sed replace example

The following command is probably one of the most useful commands one can use to edit parts of files.

grep -lr "int main()" * | xargs sed -i -e 's/foo/bar/'

This will replace all occurrences of foo with bar in all files recursively if they contain int main(). E.g.

int main() {
    int foo = 0;

    return foo;
}

Will be changed to:

int main() {
    int bar = 0;

    return bar;
}

Sunday, July 8, 2012

iptables redirect port

Sometimes you may want to allow users access certain ports. To do so run the following command:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

This will allow all HTTP (80) traffic to be redirected to port 3000.

Friday, July 6, 2012

jQuery single draggable in droppable

jQuery UI is a library that works with jQuery which allows you to easily drag and drop elements.

However, sometimes when you have more draggables and wish to let only one reside inside a droppable, you may need to take actions.

Here is some self explaining code that does just that:

$('.item').draggable({
  opacity: 0.35,
  revert: true,
  snap: true,
  zIndex: 2800
});

$('.box').droppable({
  greedy: true,
  drop: function(e, ui) {
    $(this).droppable('option', 'accept', ui.draggable);
    $(this).append(ui.draggable);
  },
  out: function(e, ui) {
    $(this).droppable('option', 'accept', '.item');
  }
});

Thursday, June 28, 2012

cannot load such file -- zlib

When getting started with Ruby, this error might appear:


ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NameError)
uninitialized constant Gem::Commands::InstallCommand


However, it is fairly simple to fix. Run the following commands from your terminal.

rvm pkg install zlib
rvm remove 1.9.3
rvm install 1.9.3

Hope that helps!

Thursday, June 21, 2012

Gnome Shell grid workspaces

By default, Gnome Shell tries to maintain only one workspace including the ones that are in use. This can be a great pain when being used to have some sort of structure on your desktop.

There is however a combination of extensions for Gnome Shell that allows you to configure your good 'ol 3x3 or 6x2 set of workspaces! Here are the links:


When both are installed, rigt click anywhere on the workspace indicator on the bottom right side of the screen and select how many workspaces (vertical and horizontal) you want.

Update:

You may also want to hide the bottom panel. By default this is not an option.
However, there are basically two fixes available.

1. With CSS (the most clean way)

Open, and edit ~/.local/share/gnome-shell/extensions/Bottom_Panel@rmy.pobox.com/stylesheet.css on line 6, change the height to 0px.

All credit goes to Brian McSweeney

2. With JavaScript (the alternative way)

For instructions, have a look at this comment at Ask Ubuntu.

All credit goes to Rasmus.

PS

In both cases, remember to update GNOME Shell (Alt + F2) and execute 'r' (for refresh).

Happy tweaking!

Saturday, June 9, 2012

Log out of basic auth with JavaScript

Ever wanted to log out of a basic authentication session with only JavaScript?

The following code requires jQuery, but it is of course possible to hack something together with plain JavaScript.

$.get(document.URL.replace(/(https?:\/\/)/, '$1logout@'));

Update:
I have figured out that this doesn't work all the time, and it seems unreliable. However, it may be used as a last resort.

Saturday, May 26, 2012

MySQL server socket

This error is pretty common:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
The solution is fairly simple, though. In the terminal type the follwing:
service mysqld restart

Wednesday, May 2, 2012

JavaScript capture groups

/\w+-(\d+)-\w+/.exec('foo-36-bar')[1];
//   ^   ^
//   |   |
//   L___L_________ The capture group
//

This JavaScript regex extracts the number 36 from the string.

P.S. The index is 1, because index 0 will yield the entire string.

Sunday, March 18, 2012

Kill a process on a specific port

Here is how you easily can kill processes running on a particular port on Linux:

lsof -ti :8080 | xargs kill -9

This is useful when for some reason your server has stopped working properly.

Thursday, March 15, 2012

Git default editor

Sometimes when installing a new distro and Git, the editor may not be correctly configured.

Therefore, you may want to run the following command to update it:
git config core.editor 'vim'

Wednesday, March 7, 2012

Simple JMS Queue Example

I have two example "applications" communicating using JMS.


The two projects are well documented. Have a look at the READMEs in both projects for details.

Sunday, March 4, 2012

Spotify Fedora Errors

Chances are that bugs may occur when using Spotify on Fedora.

112, 114 and 117 are all error codes.

Here is what you should do:
  1. Log out of Spotify
  2. Delete .config/spotify/ and .cache/spotify/ (usually located in the user's home folder).
  3. Log in to Spotify