private Object getOrCreateController(MethodAction methodAction) { Object controller = controllerInstances.get(methodAction.type()); if (controller == null) { synchronized (controllerInstances) { controller = controllerInstances.get(methodAction.type()); if (controller == null) { controller = createController(methodAction); controllerInstances.put(methodAction.type(), controller); } } } return controller; }
@Override public MethodAction createActionIfPossible(String actionName) { // will resolve if both a class and method name can be parsed, and a valid class with that // method name can be loaded String methodName = MethodAction.methodNameForAction(actionName); String className = MethodAction.classNameForAction(actionName); if (StringUtils.isEmpty(methodName) || StringUtils.isEmpty(className)) { return null; } try { Class<?> clazz = Class.forName(className); // TODO - Restricted in GAE - why is this better? // ClassLoaderUtil.loadClass(className); Method method = ReflectUtil.findMethod(clazz, methodName); if (method == null) { return null; } MethodAction methodAction = new MethodAction(clazz, method, findInterceptors(method)); // force instantiation of controller - this allows controllers to be injected into eachother // and also flushes out instantiation issues at startup Object controller = createController(methodAction); controllerInstances.put(methodAction.type(), controller); return methodAction; } catch (BaseException e) { throw e; } catch (Exception e) { return null; } }
<T> T createController(MethodAction actionMethod) { Class<T> type = actionMethod.type(); if (!injectionContext.contains(type)) { injectionContext.inject(type).as(type); } try { return injectionContext.get(type); } catch (Exception e) { throw new ActionException( e, "Failed to create controller %s: %s", type.toString(), e.getMessage()); } }