Source code: recoin/web/ViewRepository.java
1
2 package recoin.web;
3
4 import java.io.IOException;
5 import java.rmi.RemoteException;
6 import java.util.Hashtable;
7 import java.util.Vector;
8
9 import javax.servlet.RequestDispatcher;
10 import javax.servlet.ServletConfig;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.apache.log4j.Logger;
17
18 import recoin.exception.RECOINException;
19 import recoin.group.ComponentSupport;
20 import recoin.group.ComponentView;
21 import recoin.group.InOutSupport;
22 import recoin.group.ModuleGroup;
23 import recoin.group.ModuleView;
24 import recoin.group.connector.AdminCommand;
25
26
27 /**
28 * The ViewRepository servlet is the single entry point to the webpages of the
29 * administration web application that administers the RECOIN repository.
30 * <br><br>
31 * This servlet possesses a AdminCommand through which it can communicate with
32 * the administration service inside the RECOIN framework. It serves as a
33 * controller that executes or forwards user commands and delegates the client
34 * to the appropriate pages.
35 * @author Jan H. Scheufen
36 * @version 0.2.9
37 */
38 public class ViewRepository extends HttpServlet
39 {
40 /**
41 * The logger for this class.
42 */
43 static Logger logger;
44 /**
45 * The URL of the remote administration service.
46 */
47 private String adminURL;
48 /**
49 * A RECOINException to store any Exception that occurs when the
50 * servlet is initiated.
51 */
52 private RECOINException initException;
53 /**
54 * The AdminCommand that handles the communication with the remote
55 * administration service.
56 */
57 private AdminCommand command;
58
59 /**
60 * Initializes the servlet with the specified ServletConfig.<br>
61 * During initialization the value for the <code>adminURL</code>
62 * field is read from the ServletConfig and the AdminCommand
63 * is created.
64 * @param the ServletConfig
65 */
66 public void init(ServletConfig config) throws ServletException
67 {
68 super.init(config);
69 // Initialize the logger for this class.
70 logger = Logger.getLogger( ViewRepository.class.getName() );
71 adminURL = config.getInitParameter("adminURL");
72 logger.debug("Initiated w/ init parameter adminURL: "+adminURL);
73 createAdminCommand();
74 }
75
76 /**
77 * Destroys the servlet.
78 */
79 public void destroy()
80 {
81 }
82
83 /**
84 * Creates the AdminCommand.
85 */
86 public void createAdminCommand()
87 {
88 logger.debug("Creating new AdminCommand to serve requests.");
89 try
90 {
91 command = new AdminCommand( adminURL );
92 }
93 catch (RECOINException e)
94 {
95 initException = e;
96 logger.error("Problems creating AdminCommand.",e);
97 }
98 }
99
100 /**
101 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
102 * methods.
103 * @param request the servlet request
104 * @param response the servlet response
105 */
106 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
107 throws ServletException
108 {
109 // set headers before accessing the Writer
110 response.setContentType("text/html");
111 String mode = request.getParameter("mode");
112 String type = request.getParameter("type");
113 logger.debug("ViewRepository called w/ request parameters mode="+mode+" type="+type);
114
115 // Direct to Error Page by default.
116 String url = "/exception.jsp";
117
118 if( initException != null )
119 {
120 request.setAttribute("Exception", initException);
121 logger.error("Exception occured in ViewRepository! Redirecting to Exception page.");
122 // reset initException
123 initException = null;
124 // just in case that remote admin wasn't up, reload adminCommand
125 createAdminCommand();
126 this.dispatch( url, request, response );
127 }
128 else
129 {
130 try
131 {
132 //##### VIEW ###############
133 if( mode.equals("view") )
134 {
135 //##### MODULES #####
136 if( type.equals("modules") )
137 {
138 url = "/manager/ViewModule?mode=list";
139 Hashtable modules = new Hashtable();
140 modules.put( ModuleGroup.CONNECTOR, command.getModulesByGroupname(ModuleGroup.CONNECTOR) );
141 modules.put( ModuleGroup.PREQUERY, command.getModulesByGroupname(ModuleGroup.PREQUERY) );
142 modules.put( ModuleGroup.ADAPTER, command.getModulesByGroupname(ModuleGroup.ADAPTER) );
143 modules.put( ModuleGroup.POSTRESULT, command.getModulesByGroupname(ModuleGroup.POSTRESULT) );
144 request.setAttribute("ModuleList", modules);
145 logger.debug("Sending ModuleList to url: "+url);
146 }
147 //##### COMPONENTS #####
148 else if( type.equals("components") )
149 {
150 url = "/manager/ViewComponent?mode=list";
151 Vector components = command.getComponents();
152 request.setAttribute("ComponentList", components);
153 logger.debug("Sending ComponentList to url: "+url);
154 }
155 //##### SUPPORTS #####
156 else if( type.equals("supports"))
157 {
158 url = "/manager/ViewComponentSupport?mode=list";
159 Vector supports = command.getComponentSupport();
160 request.setAttribute("ComponentSupportList", supports);
161 logger.debug("Sending ComponentSupportList to url: "+url);
162 }
163 //##### MODULE #####
164 else if( type.equals("module") )
165 {
166 int id = Integer.parseInt( request.getParameter("id") );
167 url = "/manager/ViewModule?mode=detail";
168 this.prepareModuleDetail( command.getModuleView(id), request );
169 }
170 //##### COMPONENT #####
171 else if( type.equals("component"))
172 {
173 int id = Integer.parseInt( request.getParameter("id") );
174 url = "/manager/ViewComponent?mode=detail";
175 prepareComponentDetail( command.getComponentView( id ), request );
176 }
177 //##### SUPPORT #####
178 else if( type.equals("support"))
179 {
180 url = "/manager/ViewComponentSupport?mode=detail";
181 int id = Integer.parseInt( request.getParameter("id") );
182 ComponentSupport cs = command.getComponentSupportView( id );
183 request.setAttribute("ComponentSupport", cs);
184 logger.debug("Sending ComponentSupport id="+cs.getID()+" to url: "+url);
185 }
186 }
187 //##### UPDATE ###############
188 else if( mode.equals("update") )
189 {
190 //##### UPDATE MODULE #####
191 if( type.equals("module") )
192 {
193 logger.debug("Updating Module");
194 ModuleView mv = (ModuleView)request.getAttribute("ModuleView");
195 if( mv == null )
196 logger.error("Request attribute 'ModuleView' is NULL!");
197 boolean ok = command.updateModule( mv );
198 if( ok )
199 {
200 url = "/manager/ViewModule?mode=detail";
201 this.prepareModuleDetail( command.getModuleView(mv.getID()), request );
202 }
203 else
204 {
205 request.setAttribute("Exception", new RECOINException("Module not updated"));
206 }
207 }
208 //##### UPDATE COMPONENT #####
209 else if( type.equals("component") )
210 {
211 logger.debug("Updating Component");
212 ComponentView cv = (ComponentView)request.getAttribute("ComponentView");
213 if( cv == null )
214 logger.error("Request attribute 'ComponentView' is NULL!");
215 boolean ok = command.updateComponent( cv );
216 if( ok )
217 {
218 url = "/manager/ViewComponent?mode=detail";
219 prepareComponentDetail( command.getComponentView( cv.getID() ), request );
220 }
221 else
222 {
223 request.setAttribute("Exception", new RECOINException("Component not updated"));
224 url = "/exception.jsp";
225 }
226 }
227 //##### UPDATE ATTRIBUTE #####
228 else if( type.equals("attribute") )
229 {
230 logger.debug("Updating attribute");
231 String action = request.getParameter("action");
232 int id = Integer.parseInt( request.getParameter("component_id") );
233 String name = request.getParameter("name");
234 boolean ok = false;
235 if( action.equals("Update") )
236 {
237 String value = request.getParameter("value");
238 logger.debug("Updating attribute");
239 ok = command.updateAttribute( id, name, value );
240 }
241 else if( action.equals("Delete") )
242 {
243 logger.debug("Deleting attribute");
244 ok = command.removeAttribute( id, name );
245 }
246 if( ok )
247 {
248 url = "/manager/ViewComponent?mode=detail";
249 prepareComponentDetail( command.getComponentView( id ), request );
250 }
251 else
252 {
253 request.setAttribute("Exception", new RECOINException("Attribute not updated"));
254 url = "/exception.jsp";
255 }
256 }
257 //##### UPDATE SUPPORT #####
258 else if( type.equals("support") )
259 {
260 logger.debug("Updating Component Support");
261 ComponentSupport cs = (ComponentSupport)request.getAttribute("ComponentSupport");
262 if( cs == null )
263 logger.error("Request attribute 'ComponentSupport' is NULL!");
264 boolean ok = command.updateComponentSupport( cs );
265 if( ok )
266 {
267 // Since ComponentSupport already contains all needed data, no new ComponentSupportView
268 // is retrieved from the repository.
269 url = "/manager/ViewComponentSupport?mode=detail";
270 }
271 else
272 {
273 request.setAttribute("Exception", new RECOINException("Module not updated"));
274 url = "/exception.jsp";
275 }
276 }
277 //##### UPDATE I/O SUPPORT #####
278 else if( type.equals("iosupport") )
279 {
280 logger.debug("Updating InOutSupport");
281 String action = request.getParameter("action");
282 int id = Integer.parseInt( request.getParameter("id") );
283 int componentID = Integer.parseInt( request.getParameter("component_id") );
284 boolean ok = false;
285 if( action.equals("Update") )
286 {
287 int inID = Integer.parseInt( request.getParameter("insupport_id") );
288 int outID = Integer.parseInt( request.getParameter("outsupport_id") );
289 InOutSupport ios = null;
290 if( outID == 0 )
291 ios = new InOutSupport( id, componentID, command.getComponentSupportView(inID) );
292 else if( outID >= 0 )
293 ios = new InOutSupport( id, componentID, command.getComponentSupportView(inID), command.getComponentSupportView(outID) );
294 logger.debug("Updating InOutSupport");
295 ok = command.updateInOutSupport( ios );
296 }
297 else if( action.equals("Delete") )
298 {
299 logger.debug("Deleting attribute");
300 ok = command.removeInOutSupport( id );
301 }
302
303 if( ok )
304 {
305 url = "/manager/ViewComponent?mode=detail";
306 prepareComponentDetail( command.getComponentView( componentID ), request );
307 }
308 else
309 {
310 request.setAttribute("Exception", new RECOINException("Attribute not updated"));
311 url = "/exception.jsp";
312 }
313 }
314 }
315 //##### DELETE ###############
316 else if( mode.equals("delete") )
317 {
318 int id = Integer.parseInt(request.getParameter("id"));
319 //##### MODULE #####
320 if( type.equals("module") )
321 {
322 logger.debug("Deleting module id="+id);
323 boolean ok = command.removeModule( id );
324 if( ok )
325 {
326 // servlet calls itself!
327 url = "/manager/ViewRepository?mode=view&type=modules";
328 }
329 else
330 {
331 request.setAttribute("Exception", new RECOINException("Component not updated"));
332 url = "/exception.jsp";
333 }
334 }
335 //##### COMPONENT #####
336 else if( type.equals("component") )
337 {
338 logger.debug("Deleting component id="+id);
339 boolean ok = command.removeComponent( id );
340 if( ok )
341 {
342 // servlet calls itself!
343 url = "/manager/ViewRepository?mode=view&type=components";
344 }
345 else
346 {
347 request.setAttribute("Exception", new RECOINException("Component not updated"));
348 url = "/exception.jsp";
349 }
350 }
351 //##### SUPPORT #####
352 else if( type.equals("support") )
353 {
354 logger.debug("Deleting component support id="+id);
355 boolean ok = command.removeComponentSupport( id );
356 if( ok )
357 {
358 // servlet calls itself!
359 url = "/manager/ViewRepository?mode=view&type=supports";
360 }
361 else
362 {
363 request.setAttribute("Exception", new RECOINException("Component not updated"));
364 url = "/exception.jsp";
365 }
366 }
367 }
368 //##### CREATE ###############
369 else if( mode.equals("create") )
370 {
371 //##### CREATE MODULE #####
372 if( type.equals("module") )
373 {
374 String groupname = request.getParameter("groupname");
375 ModuleView mv = command.createModule( groupname );
376 url = "/manager/ViewModule?mode=detail";
377 this.prepareModuleDetail( mv, request );
378 }
379 //##### CREATE COMPONENT #####
380 else if( type.equals("component") )
381 {
382 ComponentView cv = command.createComponent();
383 url = "/manager/ViewComponent?mode=detail";
384 prepareComponentDetail( cv, request );
385 }
386 //##### CREATE ATTRIBUTE #####
387 else if( type.equals("attribute") )
388 {
389 int id = Integer.parseInt( request.getParameter("component_id") );
390 String name = request.getParameter("name");
391 logger.debug("Deleting attribute name="+name+" in component id="+id);
392 boolean ok = command.createAttribute( id, name );
393 if( ok )
394 {
395 url = "/manager/ViewComponent?mode=detail";
396 prepareComponentDetail( command.getComponentView( id ), request );
397 }
398 else
399 {
400 request.setAttribute("Exception", new RECOINException("Attribute not created. Make sure no there are duplicate attribute names!"));
401 url = "/exception.jsp";
402 }
403 }
404 //##### CREATE SUPPORT #####
405 else if( type.equals("support") )
406 {
407 String classname = request.getParameter("classname");
408 ComponentSupport cs = command.createComponentSupport( classname );
409 request.setAttribute("ComponentSupport", cs);
410 url = "/manager/ViewComponentSupport?mode=detail";
411 logger.debug("Sending ComponentSupport id="+cs.getID()+" to url: "+url);
412 }
413 //##### CREATE I/O SUPPORT #####
414 else if( type.equals("iosupport") )
415 {
416 int component_id = Integer.parseInt( request.getParameter("component_id") );
417 int insupport_id = Integer.parseInt( request.getParameter("insupport_id") );
418 boolean ok = command.createInOutSupport( component_id, insupport_id );
419 if( ok )
420 {
421 url = "/manager/ViewComponent?mode=detail";
422 prepareComponentDetail( command.getComponentView( component_id ), request );
423 }
424 else
425 {
426 request.setAttribute("Exception", new RECOINException("Attribute not created. Make sure no there are duplicate attribute names!"));
427 url = "/exception.jsp";
428 }
429 }
430 }
431 //##### LOAD ###############
432 else if( mode.equals("load") || mode.equals("reload") )
433 {
434 if( type.equals("component") )
435 {
436 int moduleID = Integer.parseInt( request.getParameter("module_id") );
437 int componentID = Integer.parseInt( request.getParameter("component_id") );
438 boolean ok = command.loadComponent( componentID, moduleID );
439 if( ok )
440 {
441 url = "/manager/ViewModule?mode=detail";
442 this.prepareModuleDetail( command.getModuleView(moduleID), request );
443 }
444 else
445 {
446 request.setAttribute("Exception", new RECOINException("Unable to load Component id="+componentID));
447 url = "/exception.jsp";
448 }
449 }
450 }
451 //##### UNLOAD ###############
452 else if( mode.equals("unload") )
453 {
454 if( type.equals("component") )
455 {
456 int moduleID = Integer.parseInt( request.getParameter("module_id") );
457 int componentID = Integer.parseInt( request.getParameter("component_id") );
458 boolean ok = command.unloadComponent( componentID, moduleID );
459 if( ok )
460 {
461 url = "/manager/ViewModule?mode=detail";
462 this.prepareModuleDetail( command.getModuleView(moduleID), request );
463 }
464 else
465 {
466 request.setAttribute("Exception", new RECOINException("Unable to unload Component id="+componentID));
467 url = "/exception.jsp";
468 }
469 }
470 }
471 //##### DEPLOY ###############
472 else if( mode.equals("deploy") )
473 {
474 if( type.equals("module") )
475 {
476 int moduleID = Integer.parseInt( request.getParameter("id") );
477 boolean ok = command.deployModule( moduleID );
478 if( ok )
479 {
480 url = "/manager/ViewModule?mode=detail";
481 this.prepareModuleDetail( command.getModuleView(moduleID), request );
482 }
483 else
484 {
485 request.setAttribute("Exception", new RECOINException("Unable to deploy Module id="+moduleID));
486 url = "/exception.jsp";
487 }
488 }
489 }
490 //##### UNDEPLOY ###############
491 else if( mode.equals("undeploy") )
492 {
493 if( type.equals("module") )
494 {
495 int moduleID = Integer.parseInt( request.getParameter("id") );
496 boolean ok = command.undeployModule( moduleID );
497 if( ok )
498 {
499 url = "/manager/ViewModule?mode=detail";
500 this.prepareModuleDetail( command.getModuleView(moduleID), request );
501 }
502 else
503 {
504 request.setAttribute("Exception", new RECOINException("Unable to undeploy Module id="+moduleID));
505 url = "/exception.jsp";
506 }
507 }
508 }
509 }
510 catch (RemoteException e)
511 {
512 createAdminCommand();
513 request.setAttribute("Exception", new RECOINException("Remote Admin Service unreachable! Will try to reestablish connection. Try reloading this page!", e));
514 url = "/exception.jsp";
515 }
516
517 dispatch( url, request, response );
518 }
519
520 }
521
522 /**
523 * Uses a RequestDispatcher to forward the specified request and response to
524 * the specified URL.
525 * @param url a URL
526 * @param request the servlet request
527 * @param response the servlet response
528 */
529 private void dispatch(String url, HttpServletRequest request, HttpServletResponse response )
530 {
531 logger.debug("Dispatching request to to URL. "+url);
532 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( url );
533 try
534 {
535 dispatcher.forward( request, response );
536 }
537 catch (ServletException e)
538 {
539 logger.error("ServletException while dispatching request to URL: "+url, e);
540 }
541 catch (IOException e)
542 {
543 logger.error("IOException while dispatching request to URL: "+url, e);
544 }
545
546 }
547
548 /**
549 * Prepares the specified request by setting the necessary attributes using the
550 * specified ModuleView.
551 * @param mView the ModuleView
552 * @param request the servlet request
553 * @throws RemoteException
554 */
555 private void prepareModuleDetail( ModuleView mView, HttpServletRequest request ) throws RemoteException
556 {
557 if( mView == null )
558 logger.error("goToModuleDetail(): ModuleView is NULL !!!!");
559 request.setAttribute("ModuleView", mView);
560 Vector components = command.getAvailableComponents( mView.getID() );
561 request.setAttribute("ComponentList", components);
562 logger.debug("Preparing request with ModuleView id="+mView.getID()+" and available ComponentList.");
563 }
564
565 /**
566 * Prepares the specified request by setting the necessary attributes using the
567 * specified ComponentView.
568 * @param cView the ComponentView
569 * @param request the servlet request
570 * @throws RemoteException
571 */
572 private void prepareComponentDetail( ComponentView cView, HttpServletRequest request ) throws RemoteException
573 {
574 if( cView == null )
575 logger.error("goToComponentDetail(): ComponentView is NULL !!!!");
576 request.setAttribute("ComponentView", cView);
577 Vector ioSupports = command.getComponentSupport();
578 request.setAttribute("SupportList", ioSupports);
579 logger.debug("Preparing request with ComponentView id="+cView.getID()+" and SupportList.");
580 }
581
582 /**
583 * Handles the HTTP <code>GET</code> method.
584 * @param request the servlet request
585 * @param response the servlet response
586 */
587 protected void doGet(HttpServletRequest request, HttpServletResponse response)
588 throws ServletException, IOException
589 {
590 processRequest(request, response);
591 }
592
593 /**
594 * Handles the HTTP <code>POST</code> method.
595 * @param request the servlet request
596 * @param response the servlet response
597 */
598 protected void doPost(HttpServletRequest request, HttpServletResponse response)
599 throws ServletException, IOException
600 {
601 processRequest(request, response);
602 }
603
604 /**
605 * Returns a short description of the servlet.
606 * @return the description
607 */
608 public String getServletInfo()
609 {
610 return "Administration Controller.";
611 }
612
613 }