KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > ModuleManager


1 /*
2  * Created on Nov 19, 2004
3  */

4 package com.openedit;
5
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Set JavaDoc;
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 /**
27  * @author Matthew Avery, mavery@einnovation.com
28  */

29 public class ModuleManager implements BeanFactoryAware, ShutdownList
30 {
31     protected BeanFactory fieldBeanFactory;
32     protected Set JavaDoc 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 JavaDoc 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 JavaDoc methodName = inAction.getMethodName();
45         execMethod(module,methodName, inAction,inReq);
46         
47     }
48
49     public void execute( String JavaDoc 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 JavaDoc mod = inFullName.substring(0, dot);
60             String JavaDoc function = inFullName.substring(dot + 1);
61             Object JavaDoc module = getBean(mod);
62             execMethod(module,function,inReq);
63         }
64     }
65     
66     protected void execMethod(Object JavaDoc module, String JavaDoc methodName,WebPageRequest inReq ) throws OpenEditException
67     {
68         execMethod(module,methodName,null,inReq);
69     }
70     protected void execMethod(Object JavaDoc module, String JavaDoc 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             //we only want to support webpagereq method for security reasons
88
Method JavaDoc method = module.getClass().getMethod( methodName, new Class JavaDoc[]{ WebPageRequest.class } );
89             if ( method != null )
90             {
91                 method.invoke( module, new Object JavaDoc[]{ inReq } );
92             }
93         }
94         catch ( InvocationTargetException JavaDoc ex)
95         {
96             Throwable JavaDoc 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 JavaDoc 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 JavaDoc actions = inPage.getPageActions();
120         if (actions == null)
121         {
122             return;
123         }
124         for (Iterator JavaDoc 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 JavaDoc actions = inPage.getPathActions();
137         List JavaDoc copy = condenseActions(actions );
138         for (Iterator JavaDoc iter = copy.iterator(); iter.hasNext();)
139         {
140             //TODO: Add mime type checks to speed this up
141
PageAction pageAction = (PageAction) iter.next();
142             executePageAction( pageAction, inPageRequest );
143             if ( inPageRequest.hasRedirected() || inPageRequest.hasForwarded() )
144             {
145                 return;
146             }
147         }
148         //This includes request actions
149
String JavaDoc[] actionNames = inPageRequest.getRequestActions();
150         if ( actionNames != null && actionNames.length > 0)
151         {
152             //check permissions
153
boolean ok = false;
154             String JavaDoc reqactions = (String JavaDoc)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 JavaDoc condenseActions(List JavaDoc inActions)
180     {
181         //remove any duplicates keeping the ones on the end first
182
if( inActions.size() < 2)
183         {
184             return inActions;
185         }
186         List JavaDoc copy = new ArrayList JavaDoc(inActions.size() );
187         Set JavaDoc cancellist = new HashSet JavaDoc( 2 );
188         for (int i = inActions.size()-1; i >= 0; i--) //start at the end from /sub/sdfds.xconf first
189
{
190             PageAction pageAction = (PageAction) inActions.get(i);
191             String JavaDoc 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 JavaDoc 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++) //is this same action in here twice?
209
{
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 JavaDoc getBean( String JavaDoc inBeanName )
228     {
229         try
230         {
231             Object JavaDoc 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     /**
245      * returns "null" if the module does not exist.
246      *
247      * @author Matthew Avery, mavery@einnovation.com
248      */

249     public BaseModule getModule( String JavaDoc inName )
250     {
251         Object JavaDoc 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     /**
268      * @param inKey
269      * @return
270      */

271     public boolean containsModule(String JavaDoc inKey)
272     {
273         return getBeanFactory().containsBean(inKey);
274     }
275
276     public Set JavaDoc getLoadedBeans()
277     {
278         if (fieldLoadedBeans == null)
279         {
280             fieldLoadedBeans = new HashSet JavaDoc();
281         }
282         return fieldLoadedBeans;
283     }
284     public void addForShutdown(Shutdownable inAble)
285     {
286         getLoadedBeans().add(inAble);
287     }
288 }
289
Popular Tags