1 /**
2 This file is part of GoldenGate Project (named also GoldenGate or GG).
3
4 Copyright 2009, Frederic Bregier, and individual contributors by the @author
5 tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 individual contributors.
7
8 All GoldenGate Project is free software: you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as published
10 by the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GoldenGate is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GoldenGate . If not, see <http://www.gnu.org/licenses/>.
20 */
21 package goldengate.ftp.core.command;
22
23 import goldengate.common.command.CommandInterface;
24 import goldengate.common.exception.InvalidArgumentException;
25 import goldengate.common.file.SessionInterface;
26 import goldengate.ftp.core.config.FtpConfiguration;
27 import goldengate.ftp.core.session.FtpSession;
28
29 /**
30 * Abstract definition of an FTP Command
31 *
32 * @author Frederic Bregier
33 *
34 */
35 public abstract class AbstractCommand implements CommandInterface {
36 /**
37 * Code of Command
38 */
39 private FtpCommandCode code;
40
41 /**
42 * String attached to the command
43 */
44 private String command;
45
46 /**
47 * Argument attached to this command
48 */
49 private String arg;
50
51 /**
52 * The Ftp SessionInterface
53 */
54 private FtpSession session;
55
56 /**
57 * Internal Object (whatever the used). This has to be clean by Business
58 * Handler cleanSession.
59 */
60 private Object object;
61
62 /**
63 * Extra allowed nextCommand
64 */
65 private FtpCommandCode extraNextCommand = null;
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see
71 * goldengate.common.command.CommandInterface#setArgs(goldengate.common.
72 * session.Session, java.lang.String, java.lang.String, java.lang.Enum)
73 */
74 public void setArgs(SessionInterface session, String command, String arg,
75 @SuppressWarnings("rawtypes") Enum code) {
76 this.session = (FtpSession) session;
77 this.command = command;
78 this.arg = arg;
79 this.code = (FtpCommandCode) code;
80 }
81
82 /*
83 * (non-Javadoc)
84 *
85 * @see
86 * goldengate.common.command.CommandInterface#setExtraNextCommand(java.lang
87 * .Enum)
88 */
89
90 public void setExtraNextCommand(@SuppressWarnings("rawtypes") Enum extraNextCommand) {
91 if (extraNextCommand != FtpCommandCode.NOOP) {
92 this.extraNextCommand = (FtpCommandCode) extraNextCommand;
93 } else {
94 this.extraNextCommand = null;
95 }
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see
102 * goldengate.common.command.CommandInterface#isNextCommandValid(goldengate
103 * .common.command.CommandInterface)
104 */
105 public boolean isNextCommandValid(CommandInterface newCommandArg) {
106 AbstractCommand newCommand = (AbstractCommand) newCommandArg;
107 Class<? extends AbstractCommand> newClass = newCommand.getClass();
108 // Special commands: QUIT ABORT STAT NOP
109 if (FtpCommandCode.isSpecialCommand(newCommand.getCode())) {
110 return true;
111 }
112 if (extraNextCommand != null) {
113 if (extraNextCommand.command == newClass) {
114 return true;
115 }
116 if (code.nextValids != null && code.nextValids.length > 0) {
117 for (Class<?> nextValid: code.nextValids) {
118 if (nextValid == newClass) {
119 return true;
120 }
121 }
122 }
123 return false;
124 }
125 if (code.nextValids == null || code.nextValids.length == 0) {
126 // Any command is allowed
127 return true;
128 }
129 for (Class<?> nextValid: code.nextValids) {
130 if (nextValid == newClass) {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 public Object getObject() {
138 return object;
139 }
140
141 public void setObject(Object object) {
142 this.object = object;
143 }
144
145 public String getArg() {
146 return arg;
147 }
148
149 public String[] getArgs() {
150 return arg.split(" ");
151 }
152
153 public int getValue(String argx) throws InvalidArgumentException {
154 int i = 0;
155 try {
156 i = Integer.parseInt(argx);
157 } catch (NumberFormatException e) {
158 throw new InvalidArgumentException("Not an integer", e);
159 }
160 return i;
161 }
162
163 public String getCommand() {
164 return command;
165 }
166
167 public boolean hasArg() {
168 return arg != null && arg.length() != 0;
169 }
170
171 /**
172 *
173 * @return the current FtpSession
174 */
175 public FtpSession getSession() {
176 return session;
177 }
178
179 // some helpful functions
180 /**
181 *
182 * @return The current configuration object
183 */
184 public FtpConfiguration getConfiguration() {
185 return session.getConfiguration();
186 }
187
188 public void invalidCurrentCommand() {
189 session.getRestart().setSet(false);
190 session.setPreviousAsCurrentCommand();
191 }
192
193 /**
194 *
195 * @return The FtpCommandCode associated with this command
196 */
197 public FtpCommandCode getCode() {
198 return code;
199 }
200 }