I am not a big fan of .NET Web Forms, and as a Java developer I am fortunate to never be forced to work with it. Well this was true until I was asked to make code changes to a JSF application at work. JSF and Web Forms are both event driven web frameworks that store user state on the page. When a click on the page occurs it submits the users previous state and calls the event mapped to the click that was made on the page. The idea of both are to create a web page that interacts like a native desktop application, with the exception of the lengthy and obvious page refresh between clicks. The idea behind Web Forms when it was released was to take the large pool of Microsoft desktop developers and easily transition to web developers. From what I have read about JSF, it was modeled after component architecture of AWT. The only difference is that I would say there is not as large of a pool of Java GUI developers as Microsoft had. Most of the time you will hear me talk up Java and its frameworks in comparison to Microsoft; when comparing event driven frameworks I am going to say that Microsoft wins simply for the development experience. This most likely has a lot to do with Visual Studio and the framework being developed together as a pair where Java develops these items as isolated projects.
When we first started unit testing our EJB3 Stateless Session beans we ran into the problem on how to inject mocked objects into objects that were under test. There were a couple of techniques that attempted such as adding setters, which we didn't like because there was no way to prevent someone from calling them during production. The second option was to create a constructor with all the dependencies which solved the setter problem but this would require a lot more work since some beans could have ten or more dependencies.
The solution that worked best for us was inject the beans manually using refection. I built a small utility class to accomplish this and had evolved overtime to what it is today.
import java.lang.reflect.Field;
public class BeanUtil {
/**
* Attempts to inject a dependency into a object that doesn't have setter
* methods or constructors to do so. This will search for the first field
* that matches the dependency type.
*
* @param bean
* The object that needs a dependency injected into
* @param dependency
* The object that fulfills the required dependency
* @throws NoSuchFieldException
*/
public static void inject(Object bean, Object dependency)
throws NoSuchFieldException {
// since no field name was given we will look for the field via type
Field[] fields = bean.getClass().getDeclaredFields();
String fieldName = null;
for (Field field : fields) {
if (field.getType().isInstance(dependency)) {
fieldName = field.getName();
break;
}
}
// check to make sure we found a field
if (fieldName == null) {
throw new NoSuchFieldError("Unable to locate a field in "
+ bean.getClass().getName() + " that is an instance of "
+ dependency.getClass().getName());
}
inject(bean, dependency, fieldName);
}
/**
* Attempts to inject a dependency into a object that doesn't have setter
* methods or constructors to do so. This method should be used when you
* have two fields of the same type.
*
* @param bean
* The object that needs a dependency injected into
* @param dependency
* The object that fulfills the required dependency
* @param fieldName
* Name of the field that the dependency should be injected to.
* @throws NoSuchFieldException
*/
public static void inject(Object bean, Object dependency, String fieldName)
throws NoSuchFieldException {
// find filed and inject dependency
Field field = bean.getClass().getDeclaredField(fieldName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
try {
field.set(bean, dependency);
} catch (IllegalAccessException e) {
throw new RuntimeException("Should not happen since we changed "
+ "the accessibility of the object to public", e);
}
field.setAccessible(accessible);
}
/**
* Attempts to inject a dependencies into a object that doesn't have setter
* methods or constructors to do so. This will search for the first field
* that matches the dependency type.
*
* @param bean
* The object that needs a dependency injected into
* @param dependencies
* Objects that fulfills the required dependencies
* @throws NoSuchFieldException
*/
public static void inject(Object bean, Object... dependencies)
throws NoSuchFieldException {
for (Object dependency : dependencies) {
inject(bean, dependency);
}
}
}
Then to use it you can either static import the BeanUtil class or just call BeanUtil.inject()
Today was a big day for my little blog. I have been playing with Ruby on Rails the past 3 months in a effort to migrate my blog from the basic WordPress themed sight to my own custom template that runs on rails. So here it is in all its glory, and its been a interesting road.
Ruby is an interesting language that I really came to enjoy using. I now find myself reaching for my ruby tools before anything else when creating my one off scripts. At first the flexibility in the language syntax made the learning curve pretty steep. I learn a lot by reading existing code from people who know how to write code. Ruby is far from cut and dry in its syntax, from blocks with either do/end or curly braces, the numerus ways for creating arrays, to methods that have "=" at the end, missing parenthesis, with parenthesis and DSLs. Before I felt entirely comfortable with the syntax I spent a lot of time researching, trying to figure out the magic that happens in just a couple lines of code.
Rails really enabled me to develop new pages and services quickly. Now I was not able to out the gate quickly build pages, technically I was, but I wanted to really understand what was going on under the hood. Rails is truly convention based development, where I would say 80 to 90 percent of what you want to do falls into the conventions of typical web development. The exciting thing about rails is its ability to handle the other 10 to 20 percent in a clean consistent way.
I have a lot more to add to this blog, a lot of it will exist in the admin section, but I will be adding comments as well. I have enjoyed my time in Ruby and Rails and this will not be my last rails project. Please remember this site is still a work in progress and things might break from time to time.
I have had the experience the past couple of weeks of being invited to lunches where pizza was ordered for large groups. I really only enjoy meat on my pizza, and its us meat eaters that get shafted when someone else does the ordering. People who like other pizza such as supremes and Hawaiians typically also like just the meat pizza. When a order is placed I will typically say that I would like a pepperoni and they get one and the same will be done for the supremes and other specialty pizzas. The problem with this is that the specialty eater will take at least one slice on average from the pepperoni and the meat eater will not take any slices from the specialty pizza. When you are talking an average of 3 slices per person and 8 slices a pizza that means at most that single pepperoni can feed one meat eater and five specialty eaters now if you have two meat eaters then you will need to order a second pepperoni. This equation can start to get complicated so I am introducing a new project of mine; it will be a site where all this math will be done for you. I just purchased the domain pizzaequation.com.
Just before I left for my trip to Colorado I updated my nexus one to the Android 2.2 Froyo. One of the biggest features I was looking forward to was tethering. And what google was just take the existing WiFi radio and made it broadcast the mobile data connection. So as I sit in the passenger seat doing 78 MPH on highway 70 I simply enabled the MiFi tethering feature set the phone down, picked up my laptop and started blogging. I find using the WiFi to tether to a mobile device a simple but creative solution that requires no software and drivers.