GridPulse

as stimulating as black coffee and just as hard to sleep after.

Post new-year resolutions for 2010

leave a comment

I’m a little late with my new year’s resolutions so I’ll just call them Jan 4′th resolutions.

2010 resolutions

Photo by katerha

Last year’s recap:
- Quit smoking: done
- Learn Turkish or Arabic: not done, moved to 2010
- I try to learn a new language every year. 2009 was C# year: as done as you can learn a language in one year

So… my resolutions for 2010 are (drum roll):
- Learn Turkish or Arabic: hopefully a new project I have in Morocco will help me learn some Arabic
- The new language for this year is Scala
- Buy a car for the wifey
- Deliver better software

Written by Bogdan

January 4th, 2010 at 7:15 pm

Posted in My life, whatever

Tagged with

Get all the types contained in all the assemblies loaded, filtered by namespace

leave a comment

This quick snippet will give you all the types declared in all the assemblies loaded, filtered by namespace.

List types = new List();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly asm in assemblies)
{
        IEnumerable asmTypes = from t in asm.GetTypes()
              where t.IsClass
                && (t.Namespace != null && t.Namespace.StartsWith(targetNamespace))
              select t;
        types.AddRange(asmTypes);
}

I actually got to this point because of the way NUnit wraps an assembly in another AppDomain.

The snippet need Linq so it’s for 3.5 or newer.

Written by Bogdan

October 29th, 2009 at 9:05 am

How to solve BindException: Address already in use: JVM_Bind in JBoss on Windows

leave a comment

 

Sometimes, you’re just having a wonderful day and suddenly the worst just happens:

[ServiceController] Problem starting service jboss:service=Naming
    java.rmi.server.ExportException: Port already in use: 1198; nested exception is:
    java.net.BindException: Address already in use: JVM_Bind
    at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:243)
    ............

The regular solution is to check if the port is actually used. So fire up a command prompt and netstat –aon.

If you find the port, see what process is using it and kill it ;) .

If you don’t see the port you can reserve it by following the information in KB812873 – http://support.microsoft.com/kb/812873.

My ReservedPorts entry contains:

1433-1434

1097-1200

4543-4545

That should fix it. The bad thing is that it requires a restart.

Written by Bogdan

October 28th, 2009 at 1:09 pm

Posted in Development, Java

Tagged with ,

NanoDI, a small .NET Dependency Injection container

leave a comment

Some time ago I worked on some projects using ASP.NET that were mostly ASPX with some specialized ASHX’s (c# behind the scenes).
The handlers just generated some graphs or exported Excel files, regular code monkey style, no architecture, no plan, just write it fast – quick dirty hack, quick buck – and I always thought that these guys that accept .NET inferior stuff deserve what they get.

As time flew by, I started to get a taste of what .NET is all about, luring me to the dark side.

engine1

Photo by B-tal

At the beginning I felt like “this is what evil must taste like”, but I quickly got accustomed to all the new things and now, I’m playing with .NET stuff again, mostly C#, trying to level my skills and having a lot of fun in the process. I built things like the quick and dirty multi font viewer, buggy and poor, mostly because nobody uses it.

Now, I wanted to start something bigger, and nicer (I won’t say what) and I felt that I needed to do it the right way, you know, MAINTAINABLE.
I started looking for a DI container and I started with Spring and Pico. I’m pretty accustomed to pico and I’ve been using Spring since version 1 so I though I’d give them a try. Wrong, strange, alien, weird, huge, EVIL.

For what I wanted to do it really seemed this way. How’s about something that is 10 megs big because spring and dependencies is 7 megs big. And I’m not even going to use most of it, I just want some plain dependency injection and maybe some tooling , like fast i18n.

Problem solved, evil destroyed
I found the solution! Why not have some fun AND learn the inner workings of C# and .Net? Why not build my own? So I did!

NanoDI is a small dependency injection container and tooling for .NET C# projects that are small or that do not need the complexity of bigger IoC solutions.

NanoDI’s goal is to be small, fast and clean.

Links:
Nano Dependency Injection home
NanoDI Ohloh.net project page
NanoDI Google Code project

The code is junky by my standards but I’m slowly refactoring as my c# skills get better. Next week I’m going to finish scoping and start working on proxies and interceptors. You can help too!

Have a taste, have fun!

Written by Bogdan

September 10th, 2009 at 11:50 pm

Transactional unit tests that support scopes using JUnit and Spring

one comment

Most of the unit tests that you write don’t need support for transactions and scopes, but if you ever want to test your web frontend code properly you’ll hit into this one.

Writing transactional unit test with JUnit, Spring and Hibernate is easy, what you want to do is:

- Add a transaction management bean in your test context:

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

- Make your test extend AbstractTransactionalJUnit4SpringContextTests
- Annotate you test class with @TransactionConfiguration and @Transactional

If your transaction demarcation is correct( surrounding your dao or service ) you should be done. This is the cleanest way that I found.

Everything until now is fine and dandy. The problem arises when one of the injected beans is bound to a scope. It can be any scope but for this example I’ll take the simplest scope – the ’session’ scope.

In a non-transactional situation this should be pretty straight forward, just implement Scope and using a ConfigurableBeanFactory register it. The fact that we are extending and that our context is automatically created really kills the simple path.

You’ll get a org.springframework.beans.factory.BeanCreationException with a nested “java.lang.IllegalStateException: No Scope registered for scope ’session’”.

But do not despair, there is simple solution available and it’s called CustomScopeConfigurer.

The fastest and cleanest way to use it is:
- Create a simple class that will be used as our scope manager. Let’s call it MockSessionScope.

code

- In your test context, create a CustomScopeConfigurer that will automatically register your scope on context creation:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="session">
<bean class="com.gridpulse.xandria.translator.MockSessionScope"/>
</entry>
</map>
</property>
</bean>

Run your tests and you’re done!

Written by Bogdan

August 21st, 2009 at 4:45 pm