Monday, November 23, 2015

Changes to .gitignore - Updating the Repository

I frequently start a git project without a .gitignore or often add entries to a .gitignore later. And then I have to update the repository to remove entries that match those entries. Here's the sequence I use:


git rm -r --cached .
 and then 
git add .git commit -m "..."git push...

Wednesday, November 11, 2015

HTML Flash Messages

Occasionally, and especially during the debugging phase of a development effort, I like to be able to create a message that appears in the web page on which I'm working, one that disappears after a few seconds. A flash message.

In the past, I've accomplished this in a variety of ways, including timeouts and so on. And some frameworks (Tapestry, e.g.) provide this kind of thing from the server as a built-in mechanism. But I'm just interested in doing this from within javascript.

Anyway, I was recently fooling around with prototypes and extending the javascript String object, and it occurred to me that a String extension to flash a message would be trivial to do, epsecially with JQuery to handle the timing parts. So I wrote this:

String.prototype.flash = function(){
    $(".flash").html(this).fadeIn("fast");
    setTimeout(function(){
        $(".flash").fadeOut("slow");
    }, 3500);
}
Coupled with a
element in the HTML in which to display the message


I am able to write flash messages off typical JS Strings:

("This is COOOOOL...").flash();
Which will show that message inside the DIV, then get rid of it after 3.5 seconds. I could extend it to parameterize the time and even the fade effects. But sometimes simpler is bester.

https://gist.github.com/scarton/98aa2588f1e66e022f64

Saturday, November 7, 2015

Creating an MD5 Hash From a String in Java

Using the MessageDigest Class, here's code to create an MD5 Hash of a String:


public static String makeAuthKey(String k) {
    MessageDigest hasher = MessageDigest.getInstance("MD5");
    byte[] kbh = hasher.digest(kb);
    String bs = new BigInteger(1,kbh).toString(16);
    return bs;
}


Java/Spring Caching

File it under "another thing I need to do a lot"... Some kind of caching of java objects.

Turns out there is a neat cache library from google called Guava that's very easy to inject into Spring and easy to configure and access.

The Spring beans look like this:


<bean id="cacheBuilderSpec"
  class="com.google.common.cache.CacheBuilderSpec"
  factory-method="parse"
  c:cacheBuilderSpecification="${cacheSpecification}" />

<bean id="cacheBuilderFromSpec"
  class="com.google.common.cache.CacheBuilder"
  factory-method="from"
  c:spec-ref="cacheBuilderSpec" />

<bean id="cache"
  factory-bean="cacheBuilderFromSpec"
  factory-method="build" />



And where the injected $cacheSpecification looks like this:

cacheSpecification=expireAfterAccess=12h,maximumSize=100

Using it in my code then becomes a simple matter of injecting the 'cache' bean into whatever class I'm using and then accessing it with these kinds of code:

cache.getIfPresent(object)
cache.invalidate(object)