create new url protocol to load spring configuration resource from the relative path

i had a problem with spring framework, as you know while we are defining dependency over the xml documents, it becomes less changeable unless you know about my recent post about ext-util namespace handler and if you know any other alternatives.

before digging inside the problem better i clarify my context -

i have been working with one of my projects, where i have a several configurations which suppose to be different in different deployment environment, for example – to make my service avail, user has to define his database, his content index directory, server port, ip address and few more.

all these configurations were kept within the spring bean declaration scope. though few of them were moved to the properties file through ext-util namespace handler.

previously to make those configuration changeable i kept those files under WEB-INF/classes so it overrides the default bundled resource.

but still it wasn’t user friendly, since my user has to change one xml document to add more index services also he has to edit different configurations from java properties file.

again when i started bundling jetty with my whole application i found it is tricker to keep those xml outside of application, since i don’t wanna make my user confused with lot of xml files. and i have to keep few xml out of  my classpath context, so spring has to load my configuration from my “config/index-configuration/” directory.

though by default spring can do it over “file:///absolute/path/to/file.xml” since spring is using java URL class to locate this resource.  but in my case i can’t put any aboslute path.
so i found a simple way out there to support relative path reference.

previously i had the following xml document -

now i have replaced them with the following change -



if you observe closely you will find “extfile:///” protocol reference, which is not standard url protocol. so to make it work i used the following bunch of code and configuration -

1. i have created a class “Handler” under the “com.ideabase.repository.core.protocols.extfile” package.
this package and class name is conventional, though you can change them lets sun explain about it.

2. i wrote the following code -

public class Handler extends URLStreamHandler {
protected URLConnection openConnection(final URL pURL) throws IOException {
final String path = pURL.getPath();
final URL resourceUrl;
if (path.startsWith(“/./”)) {
resourceUrl = new File(path.substring(1, path.length())).toURL();
} else {
throw new RuntimeException(“Unsupported url schema. currently you can ” +
“use only extfile:///./path/to/somewhere/in”);
}
return resourceUrl.openConnection();
}
}
3. i registered my protocol package through the system property -

System.setProperty(“java.protocol.handler.pkgs”, “com.ideabase.repository.core.protocols”);

now have a look on the main applicationContext.xml



if you observe closely again you could find the difference, on the normal case i have used relative path reference. so it must be from classloader or if it is loaded through file system based resource locator it will be located from the file system.

but now i can use cross context (relative file system and class loader path reference) resource file path reference. you can see i have imported “indexer-beans.xml” from the application relative path “config/index-configuration/…” where “data-access-layer-beans.xml” is loaded from the class loader.
now have a quick look on my configuration directory, to get an idea how i have improved configuration ability.
picture-1

hope this will help you to solve similar problem.

sqabd – lightning talks: what’s preventing me to write nearly accurate code

wow, i didn’t thought that moment would be too short. thanks goes to sajjad bhai and whole volunteering team, without them we were not suppose to get that WoWsome evening.

those who bored by my annoying, prolonging and noisy session, i am apologized. good suggestion might help me to understand how i can improve :)

here is my presentation slide, hope this will help you guys to check through about “what is preventing me” and “how to write nearly accurate code”

best of luck

upcoming project mojar_workflow, workflow engine in ruby

hi,
we just kicking start a new open source ruby based workflow engine project mojar workflow.
we named it after our deshi word “mojar” reason is very clear to
spread out this word.

mojar workflow, is integral solution to execute a flow of business
rules. for example -

you have an action where you have the following set of rules -
1. start transaction
2. verify user account
3. verify user balance
4. verify user dues
5. reduce dues from balance
6. complete transaction

after few days you got a new requirement, where you suppose to reduce
user dues by the 10% because of company new discount policy.
so you have to implement the following rules -
1. start transaction
2. verify user account
3. verify user balance
4. verify user dues
5. reduce dues by 10% of discount
5. reduce discounted dues from balance
6. complete transaction

to implement such scenario you have to again code in your stable
release. but using mojar workflow, you can add that new concern from
the abstract flow maintenance layer. where you can define this flow in
yaml file or an xml document.

keep your eyes on -
http://rubyforge.org/projects/mojarworkflow/

best wishes,

mac dashboard widget for somewhere in… blog

somewhere in… blog, is the most popular bangladeshi blog community.
on my spare time i like to keep in touch with this community activities. so here is a widget i have developed as my holiday product, which will help you to keep in touch with this community without going through the site.

currently this widget has the following features –
1. all activities through recently posted comment
2. online users log.

download links -
[download]

installation –
1. extract the downloaded archive file.
2. double click on “somewherein-latest-activity.wdgt” file.
screen snaps –

blog-activity-widget-3

blog-activity-widget-2

builder pattern with fluent interface just an example while i was doing by routine work :)

today while i was working with one of my projects, i was wondering how this came up with a bit more fluent with my credential object.
just i can’t resists myself to share this code with every one.

assertNotNull(mProfileManager.authenticate(
Credential.Builder
.aNew().userName(userName).password(password).build()));

uncaught exception: Permission denied to get property Object.constructor

seems you are getting “uncaught exception: Permission denied to get property Object.constructor” while you are  executing some javascript which was instantiating some objects and during that time it stucked with some exception.

for example

function get(pUpdate, pUrl, pParams) {
new Ajax.Updater(pUpdate, pUrl, {parameters: pParams});
}

if you want to regenerate this exception you can invoke this function with the following arguments -

get(“abc”, “http://abc”)

so why this crap ?

the main reason is in your code where you mentioned about “parameters” and which is not supposed to be null. you better write the following code -

function get(pUpdate, pUrl, pParams) {
var options = new Array();
if (pParams != null) {
options["parameters"] = pParams;
}

new Ajax.Updater(pUpdate, pUrl, options);
}
that will take away your javascript exception.

best wishes,

“ext-util:resource” new spring namespace for loading property from different scope i.e. system, test, dev

within spring container i was getting problem with managing multi level configuration.
for example while i am developing i got a specific filesystem path, database configuration and so on.
this configuration are mostly get changed while i an deploying them on the test or production environment.

this type of scenario is common on most of the projects. managing different scoped configuration is one of the bothering stuff. so i was always thinking some simpler way with in spring container.

so i wrote “ext-util” namespace handler, which is locating property from multi scoped resource.
lets give you a more details use case -

in my test environment i have a property “index.directory=/Users/…/index”
in my test server i use “index.directory=/var/index”
in my production server i use “index.directory=/nfs/index”

here i have a bean which need this property.

you can see the hardcoded configuration. which is not easy to manage.
thats why here my “ext-util” goes with the simplest solution -

now my property resource is locating from “test” scope.

you might think, this “scope=’test’” is again hardcoded stuff, so i also support the following value -
scope=”sys:env”, now you have to define “-Denv=test” while you running your application, and spring bean will find the right property.

so what ever you put over your spring are no longer hardcoded as long as you locate them through “ext-util” which is stands for “extended utility”.

here is my attached source code – (i just extracted them from my on going project, thats why no build script or prebuilt package or maven artifact is bundled)

how to use -
include the source file with in your project
keep “META-INF” while you building your jar or keep them inside your classpath.
change your spring configuration -

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:lang=”http://www.springframework.org/schema/lang”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:ext-util=”http://dev.somewherein.net/schema/ext-util”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://dev.somewherein.net/schema/ext-util http://dev.somewherein.net/resources/schemas/ext-util-0.1.xsd“/>
basic properties of ext-util -

1. from-property – which property you are looking for.

2. or-default – define default value if no property is found from default scope (system properties) this value is returned.

3. scope – define scope name, by default whatever you set except “sys:” prefixed string, will search for “classpath:”. also you can use “sys:abc” where abc is located from system properties.

4. path-prefix – you can define scope properties path prefix, this prefix can be “file:///, classpath:”. this value also can be prefixed by “sys:abc” where abc is located from system properties.

[download source code]
[download missing xsd file]

best wishes,

rails plugin symlinked broken on 1.2.5, fixed from 2.0

i was trying to build a rails plugin. my project was in different directory so i symlinked the directory under “vendor/plugins/..”. but i couldn’t find it working.

so after passing few times, i could successfully run my plugin under rails 2.0-RC2. so later i compared lookup.rb file from the 1.5 and 2.0-RC2 release.

the defecting code was the following lines – (1.5)

def use_component_sources!
# ….
sources < < PathSource.new(:lib, “#{::RAILS_ROOT}/lib/generators”)
sources << PathSource.new(:vendor, “#{::RAILS_ROOT}/vendor/generators”)
sources << PathSource.new(:plugins, “#{::RAILS_ROOT}/vendor/plugins/**/generators”)
# ….
end

the fixed version – (2.0-RC2)

def use_component_sources!
# …

sources < < PathSource.new(:lib, “#{::RAILS_ROOT}/lib/generators”)
sources << PathSource.new(:vendor, “#{::RAILS_ROOT}/vendor/generators”)
sources << PathSource.new(:plugins, “#{::RAILS_ROOT}/vendor/plugins/*/**/generators”)
sources << PathSource.new(:plugins, “#{::RAILS_ROOT}/vendor/plugins/*/**/rails_generators”)
end
# …
end

i also checked out rails bug tracker i found a bug was pointed to this issue and apparently which was fixed on the following change set.
http://dev.rubyonrails.org/changeset/6101

refactormycode.com the best place to Refactor :your => ‘code’

i must start with saying “WoW”, the idea is great, though it was possible with previous code snippet type site.
the difference here is it is specialized on “Refactor my code”. the idea is neat and very clear more over very specific.

you will post your old nice or crappy code and through challenge to the community to come up with nice refactoring suggestion.
i would say i really like this idea.

i wish soon they will become the most popular development tool.
what if, they have integration with intelliJ idea, with “send to refactor my code” and a periodical update will display all the suggested refactorings.
check it out now

simple fragment cache implementation on ruby on rails

i was getting serious performance problem with one of my projects. so i came up with a simple fragment cache implementation on ruby on rails.

after implementing this stuff, i replaced “render(:partial => …)” with the following method -

render_from_cache_or_render(:cache_key =>”cache key”, :cache_expire_after => ConstantHelper::TAG_CLOUD_EXPIRED_IN, # minutes :partial => “….”)

let’s have a look on my implementation -

def render_from_cache_or_render(p_args)

return render(p_args) if true == p_args[:cache_off]

# check from cache
cache_key = p_args[:cache_key]
cached_content = CacheService.get_cache(cache_key)

if not cached_content.nil? and not cached_content.empty?
return cached_content
else
content = render(p_args)
# cache expire time if defined
cache_expire_time_in_minutes = p_args[:cache_expire_after] || 60
CacheService.add_cache(cache_key, cache_expire_time_in_minutes, content)
return content
end
end

actually, my implemented “CacheService” class is simply storing all cache in a hash map.
when some cache was requested for peek, cache expiry was checked before returning the cached value.

for CacheService implementation look at the bottom of my post.

anyway, after implementing and utilizing this stuff, i gained 70+ requests capability per second. fyi, before applying cache it was around 10 per second.

module Cache
class Item
attr_accessor :key, :expire_time, :content, :created_on

def initialize(p_key, p_expire_time, p_content)
@key = p_key
@expire_time = p_expire_time * 60 # in minutes
@content = p_content
@created_on = Time.now
end
end
end

class CacheService
@@CACHES = {}
@@CACHE_EXPIRE_TIMES = {}

def self.add_cache(p_key, p_expire_time, p_content)
cache_item = Cache::Item.new(p_key, p_expire_time, p_content)
@@CACHES[p_key.to_sym] = cache_item
end

def self.get_cache(p_key)
# load content from cache
cached_content = @@CACHES[p_key.to_sym]
return nil if cached_content.nil?

# verify cache validity
return cached_content.content if not expired?(cached_content)
return nil
end

private
def self.expired?(p_cache)
# find time difference
time_difference = Time.now – p_cache.created_on
return true if time_difference > p_cache.expire_time
end
end

best wishes,

my tweets

  • @ReDrUmNZ after increasing buffer to 2 mins it's all gone. 1 week ago
  • @ReDrUmNZ i had a video wall kinda thing where around 6 small live streams were auto playing, due to 0 buffer it was leaking sound "hizzsss" 1 week ago
  • @ReDrUmNZ fyi, hi i got that video hissing things solved. it was due to buffer :) 1 week ago
  • lesson learned #Android #html5 #VIDEO doesn't work if you have none supported source 1 month ago
  • @ReDrUmNZ hi i'm using several VideoDisplay and a big VideoPlayer for showing whenever those are dbl clicked. do u think it has reltn too ? 1 month ago

 

May 2012
S S M T W T F
« Aug    
 1234
567891011
12131415161718
19202122232425
262728293031  

Flickr Photos

Beauty Lassi and Faluda - http://www.khadok.com/t/restaurants/beauty-lassi-and-faluda-old-dhaka/65?l=en

Beauty Lassi and Faluda - http://www.khadok.com/t/restaurants/beauty-lassi-and-faluda-old-dhaka/65?l=en

Khazana http://www.khadok.com/t/restaurants/khazana-a-treasure-of-indian-cuisine/249?l=en

Khazana http://www.khadok.com/t/restaurants/khazana-a-treasure-of-indian-cuisine/249?l=en

Khazana http://www.khadok.com/t/restaurants/khazana-a-treasure-of-indian-cuisine/249?l=en

More Photos
Follow

Get every new post delivered to your Inbox.