Thursday, March 31, 2011

Using the PhoneGap API

 

We are a little behind in posting Hiedi Utley's PhoneGap Tutorial Series.  Or she is a very fast writer.
In this post, Hiedi discusses the features of PhoneGap API.



Using the PhoneGap API

Welcome to the second installment of my PhoneGap Tutorial Series! This post is a continuation of PhoneGap Project Structure and Internals so if you haven’t already, you may want to peruse that post before continuing on.
We already know that PhoneGap is an open source framework for writing applications using typical web technologies like HTML, CSS, and JavaScript and that the PhoneGap architecture supplies JavaScript wrappers to access native phone features. What you may NOT know is how extensive those features really are or how to actually use them.
This article is all about getting familiar with the current features of the PhoneGap API and how to use them with the iPhone iOS.


Read more..

Monday, March 21, 2011

What is in my Spring Context?

When we are learning Spring, there are times we are not sure which beans are in the Spring context. Especially when we use convenient features of Spring 3 such as <context:annotation-driven/> or <context:component-scan/>. As developers, we want to inspect the classes that are automatically registered providing the opportunity to look at the source code and javadocs to see what each class does.


JUnit Tests get Application Context

Our JUnit tests have the ability to inject the Application Context. This is one of the many benefits of @RunWith(SpringJUnit4ClassRunner.class)


@ContextConfiguration(value = "/META-INF/spring/test-app-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class BeansInContextTest {

@Autowired
ApplicationContext applicationContext;



Simple Code

Let's loop through all the beans that are defined or discovered with the following code.


@Before
public void setup() {
logger.debug("********************");
String[] beans = applicationContext.getBeanDefinitionNames();
for (String o : beans) {
logger.debug("________________________");
logger.debug("BEAN = " + o);
logger.debug("\tType = " + applicationContext.getType(o));
String[] aliases = applicationContext.getAliases(o);
if (aliases != null && aliases.length > 0) {
for (String a : aliases) {
logger.debug("\tAliased as: " + a);
}
}
}

logger.debug("********************");
logger.debug("*** Number of Beans = {} ***",
applicationContext.getBeanDefinitionCount());
logger.debug("********************");
}
}




What is the Alias?

We can define beans with "id" which by XML law requires it to be unique per file. This restricts us in the ability to use special symbols for the name should we want to. I recommend using "id" unless absolutely necessary. However, should we what to refer to the bean with multiple names or use special symbols, we can use the "name" attribute. By comma separating the tokens, we have the opportunity to refer to a bean with any of the tokens provided.
<bean name="mailServer/default, mailServer" class="..."/>
If no, "id" is provided, the first token becomes the primary and the subse quent tokens become aliases. Does this matter in the usage of the beans? No. But to find "all" the names of the beans, we would display the aliases in addition to the bean names.

The Full Test Code

package com.chariot.sample;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(value = "/META-INF/spring/test-app-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class BeansInContextTest {
private final Logger logger = LoggerFactory
.getLogger(BeansInContextTest.class);

@Autowired
ApplicationContext applicationContext;

@Before
public void setup() {
logger.debug("********************");
String[] beans = applicationContext.getBeanDefinitionNames();
for (String o : beans) {
logger.debug("________________________");
logger.debug("BEAN = " + o);
logger.debug("\tType = " + applicationContext.getType(o));
String[] aliases = applicationContext.getAliases(o);
if (aliases != null && aliases.length > 0) {
for (String a : aliases) {
logger.debug("\tAliased as: " + a);
}
}
}

logger.debug("********************");
logger.debug("*** Number of Beans = {} ***",
applicationContext.getBeanDefinitionCount());
logger.debug("********************");
}
}


Sample Bean Context


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<bean id="myBean" class="MyCoolBean"/>

</beans>


Experiment and Have Fun

Add beans to the config file. Import other config files and see what happens.

Wanna Learn More?

Learn more about what goes on Spring Context by attending a Core Spring Training class at Chariot.





Thursday, March 17, 2011

PhoneGap Tutorial Series – #1 Project Structure and Internals

Heidi Utley, one of the members of our mobile practice, has been working with PhoneGap for one of our clients. She decided to create a series of posts focusing on using PhoneGap for iPhone development.

This first one discusses the project structure.  Future posts will review the PhoneGap API, installing a third party plugin, and last building your own PhoneGap plugin.

What is PhoneGap?


PhoneGap is an open source framework for writing applications using typical web technologies like HTML, CSS, and javascript. The PhoneGap architecture supplies javascript wrappers that allow a developer to access native phone features (like contacts, GPS, and the camera) by writing their app against the PhoneGap javascript API.
The basic gist is that the typical web developer will be able to write a standalone HTML/CSS/javascript application that runs in a WebKit browser with access to native functions just by calling the PhoneGap javascript. PhoneGap aides the developer by abstracting the complexities of native development by allowing the developer to write the app in well-known technology without having to learn the intricacies of each mobile platform and thereby hiding the complexity of writing the same application multiple times in various native languages (like Objective-C for the iPhone or Java for Android).

Read more...

Stay tuned as we have more discussions around mobile development. Our team continues with their research, for Chariot's own projects (our conference app for Philly ETE) and for our clients. 

Thursday, March 10, 2011

Xcode3 – Debugging iOS Unit Tests

A recent blog post by Hiedi Utley (no, she is not related to our knee challenged second baseman), Chariot's newest team addition.  Hiedi has been working in our mobile practice.  She reviews some of the challenges of being a Java developer and moving to an iOS environment.


Here is an excerpt from this post:



So you have some unit tests (yeah!) and they are failing …. now what?


Over the course of my career these past few years, I have become a developer that writes more test code than production code in an effort to never have to spend the wee hours of the morning debugging a horrible production issue. As a java developer turned mobile developer, there are several things that I miss about coding in Java but the number one thing is unit tests.
Don’t get me wrong – I know what you are thinking – probably something along the lines of: “Do a little research dummy! Xcode has a nice Unit Test Bundle target that we can use to run unit tests! All you have to do is write them!” Right…..
Or maybe after a little more googling around you may even point me to this excellent Apple documentation that explains the process of setting up and running my newly created unit tests but there is a huge section missing from this guide…


Monday, March 7, 2011

Mobile Design Part 3

 The next installment of Kevin Griffin's blog on Mobile Design.

Cognitive Effort.

In this installment of my thoughts on mobile design, I thought we would take a look at the concept of ‘cognitive effort’. I think I first heard that term used in reference to design from William Van Hecke (@fetjuel) when he spoke at Voices that Matter in Philly. When a guy from The OmniGroup talks to be about making great apps, I tend to shut up and listen.
We have all thought about cognitive effort at some point:
  • “Does this screen look too busy?”
  • “We should move this functionality out of this screen” are signs you thinking about it.
What increases cognitive effort?
Every question, action, dialog, gesture, tap, shake, voice communiqué we ask the user ultimately ends in some kind of effort be it physical, cognitive or verbal. Sometimes we intentionally make these things progressively harder, games for example. Sometimes we unintentionally make these things progressively harder, continually adding functionality to a screen.

Read more...

Thursday, February 17, 2011

JUnit & Spring – What You Don’t Know

 Gordon Dickens, Chariot architect and one of our education trainers, recently wrote this blog post on JUnit and Spring.


When using JUnit in Spring there are several features added that many developers are not aware of.
First, if you are including the Spring Context in your tests, it becomes an Integration Test, no longer a Unit Test.

1. Default Searching of Context File(s)

To instruct Spring to load beans for the tests in the class, we annotate the class with @ContextConfiguration

1a. No File Specified

@ContextConfiguration – with no parameters, (by Default) looks for the config file as the same name as the class with the suffix “-context.xml“. For example,
Using
?
1
2
3
4
5
6
7
package com.gordondickens.sample;
@ContextConfiguration
public class UtilsTest {
  ...
}
...
Is equivalent to
?
1
2
3
  ...
@ContextConfiguration("classpath:/com/gordondickens/sample/UtilsTest-context.xml")
  ...


 Read more...

Thursday, February 10, 2011

What's Coming in Spring 3.1

Spring 3.1.0 M1 is expected to be released on Feb 10, 2011. There are many exciting new features expected for the final release which is scheduled for June 2011.

Environment Profiles

Many times we want separate configuration for different environments such as development, qa, production. Also, we may want to provide different bean configuration for traditional or cloud environments as well.
  • Beans can be configured in XML with the additional "profile" attribute
  • Activate 1..n profiles in code or with java arg -Dspring.profiles.active="dev,qa"
  • ParamName for DispatcherServlet "spring.profiles.active"
  • Integration Tests Class GenericXmlApplicationContext
  • Nested "beans" in other words, <beans/> tag can have other <beans/> tags within allowing multiple bean sets for different profiles in the same config file
  • The variable “environment” is already registered for SpEL

@Profile(“development”)
public class MyClass() { ... }

<beans profile=“development” … />

#{environment[‘prop.key’]}
public String myProp;


The "c" Namespace

Much like the existing "p" namespace, the "c" namespace supports constructor arguments. This has the additional benefit of allowing us to reference the constructor arguments by name.

<bean class=“…” c:age=“35” c:amount=“50000”/>

Cache Abstractions

  • Cache Abstractions will be available – not just EHCache, also for Gemfire, Coherence
  • Package: org.springframework.cache
  • Annotation Package: org.springframework.cache.annotation
  • <cache: &/gt; namespace

<cache:annotation-driven> //referencing "cacheManager"

@Cacheable //field or method

@Cacheable(condition=“name.length < 10”) //conditional

@CacheEvict // remove field from cache
public void clearMyField() { ... }

Servlet 3.0

  • ServletContainerInitializer – XML Free config (no web.xml)
  • package org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  • Async request processing
  • Standard file upload support

HTTP Session Improvements

  • Share information across multiple browser instances
  • Uses Shared Cookie
  • package org.springframework.web.bind.support.SessionAttributeStore
  • Conversation "scope"
  • Useful for MVC & JSF, Web Flow

Groovy Support

  • Groovy/Grails BeanBuilder inclusion
  • <lang:groovy>
  • Groovy based template files (alternative to Velocity and Freemarker)

Feature Configuration

For Bean @Configuration - To support applications with Java based configuration.
  • package org.springframework.context.annotation
  • Picked up via AnnotationConfigApplicationContext // Standalone application context, accepting annotated classes as input
  • Allows scanning for @Configuration classes
  • @FeatureConfiguration // class level - specify the name of the Spring bean definition
  • @Feature // Method w/i @FeatureConfiguration Class

Customizable @MVC

  • Custom @ExceptionHandler support for @Controller methods
  • Custom @ExceptionHandler support for @RequestBody & @ResponseBody
  • You can write a BeanPostProcessor to change AnnotationMethodHandlerAdapter after construction
  • Auto-detection of custom HttpMessageConverters - for example custom MyMappingJacksonHttpMessageConverter
  • Support for custom message codes resolver with <mvc:annotation-driven/>

<mvc:annotation-driven>
<mvc:custom-web-argument-resolvers>
<bean class="org.example.MyWebArgumentResolver"/>
</mvc:custom-web-argument-resolvers>
</mvc:annotation-driven>

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>

<mvc:annotation-driven message-codes-resolver="org.example.MyMessageCodesResolver"/>

REST Enhancements

  • Interceptors for RestTemplate
  • ClientHttpRequestInterceptor
  • bufferRequestBody property to the SimpleClientHttpRequestFactory to handle large payloads with RestTemplate
  • New WebRequest.checkNotModified(String) // only had long support previously
  • New @ResponseEntity
  • To support OAuth with ClientHttpRequestInterceptors

Summary

Many of the items included here are specific features for 3.1.0.M1, there are many more improvements coming for MVC customization and REST improvements in M2.