1 4 package com.openedit; 5 6 import java.lang.reflect.InvocationTargetException ; 7 import java.lang.reflect.Method ; 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Set ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.springframework.beans.factory.BeanFactory; 18 import org.springframework.beans.factory.BeanFactoryAware; 19 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 20 21 import com.openedit.modules.BaseModule; 22 import com.openedit.page.Page; 23 import com.openedit.page.PageAction; 24 import com.openedit.page.PageRequestKeys; 25 26 29 public class ModuleManager implements BeanFactoryAware, ShutdownList 30 { 31 protected BeanFactory fieldBeanFactory; 32 protected Set fieldLoadedBeans; 33 34 private static final Log log = LogFactory.getLog(ModuleManager.class); 35 36 public void executePageAction( PageAction inAction, WebPageRequest inReq ) throws OpenEditException 37 { 38 39 Object module = getBean( inAction.getModuleName() ); 40 if ( module == null ) 41 { 42 throw new OpenEditException("Error attempting to execute page action " + inAction.getActionName() + ". No module found: " + inAction.getModuleName()); 43 } 44 String methodName = inAction.getMethodName(); 45 execMethod(module,methodName, inAction,inReq); 46 47 } 48 49 public void execute( String inFullName, WebPageRequest inReq) throws OpenEditException 50 { 51 int dot = inFullName.indexOf("."); 52 53 if (dot == -1) 54 { 55 throw new OpenEditException("No command found" + inFullName); 56 } 57 else 58 { 59 String mod = inFullName.substring(0, dot); 60 String function = inFullName.substring(dot + 1); 61 Object module = getBean(mod); 62 execMethod(module,function,inReq); 63 } 64 } 65 66 protected void execMethod(Object module, String methodName,WebPageRequest inReq ) throws OpenEditException 67 { 68 execMethod(module,methodName,null,inReq); 69 } 70 protected void execMethod(Object module, String methodName,PageAction inAction, WebPageRequest inReq ) throws OpenEditException 71 { 72 if (module instanceof Secured) 73 { 74 Secured secured = (Secured) module; 75 if (!secured.canRun(inReq, methodName)) 76 { 77 throw new OpenEditException("User does not have permission to run method " + methodName + " on " + module.getClass()); 78 } 79 80 } 81 if ( inAction != null) 82 { 83 inReq.setCurrentAction(inAction); 84 } 85 try 86 { 87 Method method = module.getClass().getMethod( methodName, new Class []{ WebPageRequest.class } ); 89 if ( method != null ) 90 { 91 method.invoke( module, new Object []{ inReq } ); 92 } 93 } 94 catch ( InvocationTargetException ex) 95 { 96 Throwable cause = ex.getTargetException(); 97 if ( cause instanceof OpenEditException) 98 { 99 throw (OpenEditException)cause; 100 } 101 else 102 { 103 cause.printStackTrace(); 104 throw new OpenEditException( cause); 105 } 106 } 107 catch (Exception e) 108 { 109 log.error(e); 110 if ( inReq != null ) 111 { 112 log.error("When loading: " + inReq.getPath() + "#" + module.getClass().getName() + "." + methodName); 113 } 114 throw new OpenEditException(e); 115 } 116 } 117 public void executePageActions( Page inPage, WebPageRequest inPageRequest ) throws OpenEditException 118 { 119 List actions = inPage.getPageActions(); 120 if (actions == null) 121 { 122 return; 123 } 124 for (Iterator iter = actions.iterator(); iter.hasNext();) 125 { 126 PageAction pageAction = (PageAction) iter.next(); 127 if ( inPageRequest.hasRedirected() || inPageRequest.hasForwarded() ) 128 { 129 return; 130 } 131 executePageAction( pageAction, inPageRequest ); 132 } 133 } 134 public void executePathActions( Page inPage, WebPageRequest inPageRequest ) throws OpenEditException 135 { 136 List actions = inPage.getPathActions(); 137 List copy = condenseActions(actions ); 138 for (Iterator iter = copy.iterator(); iter.hasNext();) 139 { 140 PageAction pageAction = (PageAction) iter.next(); 142 executePageAction( pageAction, inPageRequest ); 143 if ( inPageRequest.hasRedirected() || inPageRequest.hasForwarded() ) 144 { 145 return; 146 } 147 } 148 String [] actionNames = inPageRequest.getRequestActions(); 150 if ( actionNames != null && actionNames.length > 0) 151 { 152 boolean ok = false; 154 String reqactions = (String )inPageRequest.getContentPage().get(PageRequestKeys.ALLOWPATHREQUESTACTIONS); 155 if ( reqactions != null) 156 { 157 ok = reqactions.equalsIgnoreCase("true"); 158 } 159 else if ( inPageRequest.getUser() != null ) 160 { 161 ok = true; 162 } 163 if ( ok ) 164 { 165 for (int i = 0; i < actionNames.length; i++) 166 { 167 if ( actionNames[i].length() > 0 ) 168 { 169 executePageAction(new PageAction( actionNames[i] ), inPageRequest ); 170 if ( inPageRequest.hasRedirected() ) 171 { 172 return; 173 } 174 } 175 } 176 } 177 } 178 } 179 public List condenseActions(List inActions) 180 { 181 if( inActions.size() < 2) 183 { 184 return inActions; 185 } 186 List copy = new ArrayList (inActions.size() ); 187 Set cancellist = new HashSet ( 2 ); 188 for (int i = inActions.size()-1; i >= 0; i--) { 190 PageAction pageAction = (PageAction) inActions.get(i); 191 String cancel = pageAction.getConfig().getAttribute("cancel"); 192 if ( Boolean.parseBoolean(cancel) ) 193 { 194 cancellist.add(pageAction.getActionName()); 195 } 196 if( cancellist.contains(pageAction.getActionName())) 197 { 198 continue; 199 } 200 String allow = pageAction.getConfig().getAttribute("allowduplicates"); 201 if( Boolean.parseBoolean(allow)) 202 { 203 copy.add(pageAction); 204 } 205 else 206 { 207 boolean dup = false; 208 for (int j = 0; j < copy.size(); j++) { 210 PageAction dupAction = (PageAction) copy.get(j); 211 if ( dupAction.getActionName().equals(pageAction.getActionName())) 212 { 213 dup = true; 214 break; 215 } 216 } 217 if( !dup) 218 { 219 copy.add(pageAction); 220 } 221 } 222 } 223 Collections.reverse(copy); 224 return copy; 225 } 226 227 public Object getBean( String inBeanName ) 228 { 229 try 230 { 231 Object bean = getBeanFactory().getBean( inBeanName ); 232 if( bean != null) 233 { 234 getLoadedBeans().add(bean); 235 } 236 return bean; 237 } catch ( NoSuchBeanDefinitionException ex) 238 { 239 throw new OpenEditRuntimeException("Could not find bean named " + inBeanName); 240 } 241 } 242 243 244 249 public BaseModule getModule( String inName ) 250 { 251 Object bean = getBean( inName ); 252 if ( bean != null && bean instanceof BaseModule ) 253 { 254 return (BaseModule) bean; 255 } 256 return null; 257 } 258 protected BeanFactory getBeanFactory() 259 { 260 return fieldBeanFactory; 261 } 262 public void setBeanFactory(BeanFactory inBeanFactory) 263 { 264 fieldBeanFactory = inBeanFactory; 265 } 266 267 271 public boolean containsModule(String inKey) 272 { 273 return getBeanFactory().containsBean(inKey); 274 } 275 276 public Set getLoadedBeans() 277 { 278 if (fieldLoadedBeans == null) 279 { 280 fieldLoadedBeans = new HashSet (); 281 } 282 return fieldLoadedBeans; 283 } 284 public void addForShutdown(Shutdownable inAble) 285 { 286 getLoadedBeans().add(inAble); 287 } 288 } 289 | Popular Tags |