Skip to main content

Posts

Showing posts from 2012

Java Advent Calendar

On 14.12.2012 I took part of initiative called Java Advent Calendar with a post about Functional Java collections. The idea for the Calendar is very simple, one technical article will be published per day between the 1st and 24th of December. This initiative is done for other languages like Perl or PHP and even some frameworks like Perl Dancer. You can directly sign up on the RSS feed or follow via e-mail. How can you help? Sharing on the social networks is highly appreciated. You can also visit the original hello world post or read on Wikipedia about Advent Calendars.

Temporary files and directories in Java 7 and before

Sometimes we want to create a temporary file, whether to save some data that gets written by some other application or just to temporary store stuff. Well, usually applications have their own temporary folder where they do this and it gets somehow configured. But why not use the underlying OS specific file like "/tmp/" in Linux so there must be some system property that has this info and there is. The key is "java.io.tmpdir" resulting in "/tmp" in my case or by code:
String tempDir = System.getProperty("java.io.tmpdir"); We can use tempDir  folder as a temporary place to store files, but there are a lot nicer ways to work with files like this even in JDK6 not just in JDK7:
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class TempFile { public static void main(String[] args) { try { // create a temp file File tempFile = File.createTempFile("old-file",…

Input this - input that, HTML5 new input types

We are used to inputting types like "submit", "radio" or "hidden" but these days there are more components that are so common that are part of almost every web framework out there no matter if it is in Java, Ruby, Python or any other language. I'll give a short overview of what is being added in HTML5. The current state of them is that well... they sort of work, basically not all of the features are supported in all the modern browsers or IE.
Opera seems to support most of them and they have some good initial look there.

Date selection related. Many times you need to create or integrate some control that will be a date-time picker, I've done this way too often and I'm sure that you also have. But why should we do something like this, this should be part of HTML and we should just style it with CSS. HTML5 makes this very simple with the date input types.
<input type="datetime" /> <input type="datetime-local" /> &…

Mustaches in the world of Java

Mustache is templating system with implementation in many languages including Java and JavaScript . The templates are also supported by various web frameworks and client side JS libraries.

Mustache has simple idea of "logic-less" system because it lacks any explicit control statements, like if, else or goto andalso it does not have for statement however looping and conditional calculation can be achieved using custom tags that work with lists and lambdas.


The name unfortunately has less to do with Tom Selleck but more with the heavy use of curly braces that look like mustache. The similarity is more than comparable.


Mustache has implementation for most of the widely used languages like:  Java, Javascript, Ruby,Net and many more.





The client side template's in JavaScript Let say that you have some REST service and you have created a book view object that has an additional function that appends amazon associates id to the book url:


var book = { id…

Google Guava for cleaner code

Some time ago I did a short talk on Google Guava for our local JUG. It was a basic intro into Guava and how it makes stuff simpler, better and cleaner.

It is true that there is an overlap with Apache commons but Guava is build with expectation that there is a Function and a Predicate class as well as various builders which makes it really cool and simple for many use cases.
The talked covered most of the com.google.common.base.* classes  and basic use of functions in collection and Google collections  and few other features that are part of Guava and I find them very useful.
Source code of the examples can be found on github and here is the actual presentation:
Google Guava for cleaner code
Note one last recommendation that is not just mine but  also part of Guava's wiki :
Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when…

Getting Real

When a colleague recommended a book from 37singals I was skeptical, to say the least, not because it is a bad company, but I do kind of find them very cocky and also my first impression was "Isn't that going to be a something about Ruby?". But  I gave it a try and liked it enough to write a few good words about it.
So what is this book about? In my opinion, it's about agile software development at its core and to quote the authors "Getting Real" is a book about:
The smarter, faster, easier way to build a successful web application. When I started reading it became obvious right away that this book is a great collection of short stories, tips, quotes and lessons from various companies and software developers and not just 37signals, although it had some very good ones from them also. In some places, the authors were direct about what is their standing, sometimes even cocky but definitely in a really good way.

Getting Real is about doing software with fewer f…

HTML 5 data-* attributes, how to use them and why

It is always tempting to add custom attributes in HTML so that you can use the data stored there to do X. But if you do that there is no way of knowing if your HTML attribute will not be overridden in the future and used for something else and additionally you will not be writing valid HTML markup that can pass HTML 5 validator and with that you can create some very bad side effects. That is why there is a spec in HTML 5 called custom data attributes that enable number of useful features.

You may go around and read the specs, but the basic idea is very simple, you can add any attribute that starts with "data-" and that attribute will be treated as non-visible data for that attribute. By non-visible I mean that it is not something that gets rendered to the client so it does not affect the layout or style of the page, but it is there in the HTML so in no way this is private.
So let's get right into it, the following snippet is a valid HTML5 markup

<div id="aweso…

Available meta data in Confluence and JIRA

Atlassian uses  something called AUI or Atlassian User Interface that is basically a set of reusable, cross-browser tested UI components (markup, CSS and Javascript) that are based on jQuery and jQueryUI. They are used in most of Atlassian products like JIRA an Confluence.
Common trick for saving some meta-data about the web app and the currently logged in user is used.
<meta name="ajs-context-path" content="/wiki"> <meta name="ajs-version-number" content="3.6.0"> <meta name="ajs-build-number" content="XXX"> <meta name="ajs-remote-user" content="someusername"> <meta name="ajs-static-resource-url-prefix" content="/confluence/s/en/2155/5/_"> There are a few utility functions that can be useful:
AJS.Data.get("some key") ex.AJS.Data.get("remote-user")
AJS.params.somekey ex. AJS.params.loggedInUser and if you want to see list of all availab…

NSND 2012 Кирилица - Struga

Nista se nece dogodite (NSND)  or roughly translated into English - Nothing will happen is a geek/hackers unconference  where people get together to share knowledge about pretty much anything, starting from programming and computer networks going to growing food on top of buildings and women's rights. The full list can be found on the flowing ether-pad http://piratepad.net/nsndstruga. This year the main event in Macedonia was in Struga and there were very cool talks. I missed some of them but  so here are few that I can take a note of:
Happy food production by Luca Pescatore(http://www.pescatoreluca.com)
He talked about aquaponics, windows farming, deep water culture, compositing and basically how to do farming almost anywhere and not just that but also how to farm efficiency. His main idea was to encourage everyone to just try things out. One cool experiment that he did was creating a small farm on top of building.



Why python has special methods and why in the world would I use the…

Git ignore for Java/JVM projects

Github has this great project hosted on ... GitHub.  What so great about it? Well, it contains git-ignore files for various development environments and programming languages. This gives you a great starting point when creating a new project in any language and any environment.

So this is my starting git ignore on every new java project:
*target* *.jar *.war *.ear *.class # eclipse specific git ignore *.pydevproject .project .metadata bin/** tmp/** tmp/**/* *.tmp *.bak *.swp *~.nib local.properties .classpath .settings/ .loadpath # Intellij .idea/ *.iml *.iws # External tool builders .externalToolBuilders/ # Locally stored "Eclipse launch configurations" *.launch
It covers maven based projects with eclipse and using of combination local.properties for overriding  other *.properties that I prefer. Additionally it removes all eclipse specific "settings" that I'm opposed to having them in version control since most of the time it makes no sense for them t…

JavaDoc incremental searchbar for Firefox and Chrome

Javadoc is very old but it is a great way to create documentation and now with Java 7 we have better CSS in Javadoc.

But there is one thing always missing in the generated documentation and that is a search bar in the left frame where the class list is displayed. Since there are tons of java projects and with that tons of Javadoc it's very probable that most of the Javadoc will remain a little bit ugly.  Because we cannot change the Javadoc on other sites we can change it  how they are displayed on our side.

There is this super awesome project on google code  where using GreaseMonkey and some client side JS they have added incremental search. So now when viewing Javadocs, this script replaces the package and class frames with a single search frame that allows searching incrementally through all packages and classes at once. There is plugin for Firefox and Chrome available.  You would need to have GreaseMonkey installed first on the Firefox version.
So now we have a search bar that…

Basic Authentication with RestTemplate

Spring Rest Templates are very good way of writing REST clients. By default they work with basic HTTP so if we need to use Basic Authorization we would need to init the rest template with custom HttpClient. This way the Rest Template will automatically use Basic Auth and append to the HTTP headers "Authorization: Basic BASE64ENCODED_USER_PASS".

HttpClient client = new HttpClient(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("USERNAME","PASS"); client.getState().setCredentials( new AuthScope("www.example.com", 9090, AuthScope.ANY_REALM), credentials); CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client); RestTemplate template = new RestTemplate(commons); SomeObject result = template.getForObject( "http://www.example.com:9090/",SomeObject.class );

In EE application this would probably be managed by DI framework like Spring Core and only initialized once sin…

Google is removing search results?

Google has begun filtering results for searches that involve copyright  violation. In my opinion this is just a beginning of a series of events that are going to happen with the pretense of anti-piracy that will end up hurting freedom of speech  and eventuality things will become like 1984 if they haven't already...
Here is the image that I got searching for "gotye somebody that i used to know mp3"




Update:
As Michael McGraw-Herdeg pointed out in the comments, this has been here for long time. I don't remember seeing this in the past but apparently there is a law preventing Google and any other companies from displaying links like this...

http://en.wikipedia.org/wiki/Chilling_Effects

http://www.nytimes.com/2002/04/22/business/new-economy-copyright-dispute-with-church-scientology-forcing-google-some.html?src=pm



Aria2 - awesome command line download manager

Back in my Windows days aka the dark ages, I was very into download managers.  This was probably because those days we had slow Internet like dial-up and cable with speeds up to 128 kbps down / 64 kbps up. So at this kind of speeds it makes more sense to use download managers, mostly  because you are waiting all the time and these managers made you feel that things moved faster even if sometimes that was not the case. Also, IE was very dominant browser at the time and when it crashed  you would have to re-download instead of just continue, that was the biggest reason why I was using FlashGet, Go!Zilla and of course Download Accelerator Plus.
I am still under the impression that most of these programs were "placebo" like, just making you feel that things are moving faster.


Nowadays download speed is rarely the issue, but sometimes we like to automate downloads. Great way to automate is using terminal based download manager. Aria2 has some very interesting features that we ca…