is above and below the «Dynamic» part Use of variables and resources makes refactoring harder In our example there are 6 main actions: 1. User retrieval 2. Connection to remote system 3. Dynamic call 4. Exception handling 5. Closing the connection 6. Action auditing (log) REPETITION CODE REPETITION
result = null; //1 Get the operator VoxxedOperator operator = loginManager.getOperator(task.getToken()); if (operator == null) { logger.warn("invalid operator token " + task.getToken()); return null; } try { //2 estabilish connection and get remote service facade connector = ConnUtility.getBEConnector(); VoxxedFacade service = ConnUtility.getVoxxedFacade(connector); //3 Set variables to task and run! task.setService(service); task.setOperator(operator); return task.call(); } catch (Exception e) { //4 log and handle exception logger.error("Error in retrieving objectType1 for user" + task.getToken(), e); result = null; } finally { //5 release the connection ConnUtility.releaseBEConnector(connector); } // 6 audit the action (log) not possible here return result; }
logMethodEntry public class LogInterceptor { private static final Logger LOGGER = Logger.getLogger(LogInterceptor.class); @AroundInvoke public Object logMethodEntry(InvocationContext ctx) throws Exception { //Execute the method Object b = ctx.proceed(); //Print method name and params. LOGGER.info(ctx.getMethod().getName() + " executed with params " + ctx.getParameters()); return b; } }
criteria) { Squid result = null; //1 Get the operator VoxxedOperator operator = loginManager.getOperator(token); if (operator == null) { logger.warn("invalid operator token " + token); return null; } //3 The real action! result = service.findSquidByCriteria(operator, criteria); //Exception and Connection close handling in interceptor. //Log handled in interceptor. return result; }
around or at WellD stand Other approaches: dependency injection, decorator, filters… Contacts: [email protected] Twitter: @Smirne WellD: www.welld.ch