KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > command > impl > GenericCommandProcessor


1 package com.coldcore.coloradoftp.command.impl;
2
3 import com.coldcore.coloradoftp.command.Command;
4 import com.coldcore.coloradoftp.command.CommandProcessor;
5 import com.coldcore.coloradoftp.command.Reply;
6 import com.coldcore.coloradoftp.factory.ObjectFactory;
7 import com.coldcore.coloradoftp.factory.ObjectName;
8 import com.coldcore.coloradoftp.filesystem.FailedActionException;
9 import org.apache.log4j.Logger;
10
11 /**
12  * @see com.coldcore.coloradoftp.command.CommandProcessor
13  */

14 public class GenericCommandProcessor implements CommandProcessor {
15
16   private static Logger log = Logger.getLogger(GenericCommandProcessor.class);
17
18
19   public void execute(Command command) {
20     //Execute command
21
Reply reply;
22     try {
23       reply = command.execute();
24     } catch (FailedActionException e) {
25       //Filesystem error
26
reply = getFileSystemReply(e);
27     } catch (Throwable JavaDoc e) {
28       //Command error
29
log.error("Command failed (ignoring)", e);
30       reply = ((Command) ObjectFactory.getObject(ObjectName.COMMAND_LOCAL_ERROR)).execute();
31     }
32
33     String JavaDoc debug = "[Command] "+command.getName()+" "+(command.getParameter()==null?"":command.getParameter())+"\n";
34     debug += reply==null?"[NO REPLY]":"[Reply] "+reply.getCode()+" "+(reply.getText()==null?"":reply.getText());
35     log.debug("Execution result:\n"+debug);
36
37     //Submit reply to control connection
38
try {
39       if (reply != null) command.getConnection().reply(reply);
40     } catch (Throwable JavaDoc e) {
41       log.error("Command's connection reply failed (ignoring)", e);
42     }
43   }
44
45
46   /** Get reply corresponding to a filesystem error
47    * @param e Filesystem exception
48    * @return Reply
49    */

50   protected Reply getFileSystemReply(FailedActionException e) {
51     Reply reply = (Reply) ObjectFactory.getObject(ObjectName.REPLY);
52     switch (e.getReason()) {
53       case INVALID_INPUT:
54         reply.setCode("553");
55         reply.setText(e.getText() == null ? "Cannot parse input." : e.getText());
56         return reply;
57       case NO_PERMISSIONS:
58         reply.setCode("550");
59         reply.setText(e.getText() == null ? "No permission." : e.getText());
60         return reply;
61       case NOT_IMPLEMENTED:
62         reply.setCode("504");
63         reply.setText(e.getText() == null ? "Not implemented." : e.getText());
64         return reply;
65       case PATH_ERROR:
66         reply.setCode("450");
67         reply.setText(e.getText() == null ? "Requested path error." : e.getText());
68         return reply;
69       case OTHER:
70         reply.setCode("450");
71         reply.setText(e.getText() == null ? "Unknown reason." : e.getText());
72         return reply;
73       default:
74         log.error("Filesystem failed (ignoring)", e);
75         return ((Command) ObjectFactory.getObject(ObjectName.COMMAND_LOCAL_ERROR)).execute();
76     }
77   }
78 }
79
Popular Tags