1 5 6 package org.infohazard.maverick.flow; 7 8 import java.util.Map ; 9 10 import javax.servlet.ServletConfig ; 11 import javax.servlet.ServletException ; 12 13 import org.infohazard.maverick.util.XML; 14 import org.jdom.Element; 15 16 30 public abstract class AbstractControllerFactory implements ControllerFactory 31 { 32 35 protected static final String ATTR_CONTROLLER_CLASS = "class"; 36 37 40 static class NullController implements ControllerSingleton 41 { 42 43 public void init(Element controllerNode) {} 44 45 46 public String go(ControllerContext cctx) throws ServletException 47 { return "NO CONTROLLER DEFINED"; } 48 } 49 50 53 protected static final Controller nullController = new NullController(); 54 55 59 public void init(Element factoryNode, ServletConfig servletCfg) throws ConfigException 60 { 61 } 63 64 71 public Controller createController(Element controllerNode) throws ConfigException 72 { 73 if (controllerNode == null) { 75 return nullController; 76 } 77 78 Class controllerClass = getControllerClass(controllerNode); 80 Controller controller = getControllerInstance(controllerNode, controllerClass); 82 initializeController(controllerNode, controller); 84 85 return controller; 86 } 87 88 94 protected Class getControllerClass(Element controllerNode) throws ConfigException 95 { 96 if(controllerNode == null) return null; 97 98 String className = controllerNode.getAttributeValue(ATTR_CONTROLLER_CLASS); 99 if (className == null || className.trim().equals("")) 100 { 101 throw new ConfigException( 102 "Controller element must have " + ATTR_CONTROLLER_CLASS 103 + " attribute: " + XML.toString(controllerNode)); 104 } 105 106 Class controllerClass; 107 try 108 { 109 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 110 if (classLoader == null) 111 { 112 classLoader = AbstractControllerFactory.class.getClassLoader(); 113 } 114 controllerClass = classLoader.loadClass(className); 115 } 116 catch (ClassNotFoundException ex) { throw new ConfigException(ex); } 117 return controllerClass; 118 } 119 120 128 protected Controller getControllerInstance( 129 Element controllerNode, 130 Class controllerClass) 131 throws ConfigException 132 { 133 Controller controller = null; 134 try 135 { 136 if (ControllerSingleton.class.isAssignableFrom(controllerClass)) 137 { 138 controller = createSingletonController(controllerNode, controllerClass); 139 } 140 else 141 { 142 controller = createThrowawayController(controllerNode, controllerClass); 143 } 144 } 145 catch (InstantiationException ex) { throw new ConfigException(ex); } 146 catch (IllegalAccessException ex) { throw new ConfigException(ex); } 147 148 controller = decorateController(controllerNode, controller); 150 151 return controller; 152 } 153 154 161 protected Controller decorateController( 162 Element controllerNode, 163 Controller controller) 164 throws ConfigException 165 { 166 Controller decorated = controller; 167 Map params = XML.getParams(controllerNode); 168 if (params != null) { 170 decorated = new ControllerWithParams(controller, params); 171 } 172 return decorated; 173 } 174 175 184 protected Controller createSingletonController( 185 Element controllerNode, 186 Class controllerClass) 187 throws ConfigException, 188 InstantiationException , 189 IllegalAccessException 190 { 191 return (ControllerSingleton)controllerClass.newInstance(); 192 } 193 194 204 protected Controller createThrowawayController( 205 Element controllerNode, 206 Class controllerClass) 207 throws ConfigException, 208 InstantiationException , 209 IllegalAccessException 210 { 211 return new ThrowawayControllerAdapter(controllerClass); 212 } 213 214 223 protected void initializeController( 224 Element controllerNode, 225 Controller controller) 226 throws ConfigException 227 { 228 controller = getControllerUndecorated(controller); 229 if(controller instanceof ControllerSingleton) 230 { 231 ((ControllerSingleton)controller).init(controllerNode); 232 } 233 } 235 236 241 protected Controller getControllerUndecorated(Controller controller) 242 { 243 Controller concreteController = controller; 244 if(controller instanceof ControllerWithParams) 245 { 246 concreteController = ((ControllerWithParams)controller).getDecorated(); 247 } 248 return concreteController; 249 } 250 } 251 | Popular Tags |