|
20 | 20 | import io.siddhi.annotation.Example; |
21 | 21 | import io.siddhi.annotation.Extension; |
22 | 22 | import io.siddhi.annotation.Parameter; |
23 | | -import io.siddhi.annotation.ReturnAttribute; |
24 | 23 | import io.siddhi.annotation.util.DataType; |
25 | 24 | import io.siddhi.core.config.SiddhiQueryContext; |
26 | | -import io.siddhi.core.event.ComplexEventChunk; |
27 | | -import io.siddhi.core.event.stream.MetaStreamEvent; |
28 | | -import io.siddhi.core.event.stream.StreamEvent; |
29 | | -import io.siddhi.core.event.stream.StreamEventCloner; |
30 | | -import io.siddhi.core.event.stream.holder.StreamEventClonerHolder; |
31 | | -import io.siddhi.core.event.stream.populater.ComplexEventPopulater; |
32 | 25 | import io.siddhi.core.exception.SiddhiAppRuntimeException; |
33 | 26 | import io.siddhi.core.executor.ExpressionExecutor; |
34 | 27 | import io.siddhi.core.query.processor.ProcessingMode; |
35 | | -import io.siddhi.core.query.processor.Processor; |
36 | | -import io.siddhi.core.query.processor.stream.StreamProcessor; |
| 28 | +import io.siddhi.core.query.processor.stream.function.StreamFunctionProcessor; |
37 | 29 | import io.siddhi.core.util.config.ConfigReader; |
38 | | -import io.siddhi.core.util.snapshot.state.State; |
39 | 30 | import io.siddhi.core.util.snapshot.state.StateFactory; |
40 | | -import io.siddhi.extension.io.file.util.Constants; |
41 | | -import io.siddhi.extension.io.file.util.VFSClientConnectorCallback; |
| 31 | +import io.siddhi.extension.util.Utils; |
42 | 32 | import io.siddhi.query.api.definition.AbstractDefinition; |
43 | 33 | import io.siddhi.query.api.definition.Attribute; |
44 | | -import io.siddhi.query.api.exception.SiddhiAppValidationException; |
| 34 | +import org.apache.commons.vfs2.FileObject; |
| 35 | +import org.apache.commons.vfs2.FileSystemException; |
45 | 36 | import org.apache.log4j.Logger; |
46 | | -import org.wso2.carbon.messaging.BinaryCarbonMessage; |
47 | | -import org.wso2.carbon.messaging.exceptions.ClientConnectorException; |
48 | | -import org.wso2.transport.file.connector.sender.VFSClientConnector; |
49 | 37 |
|
50 | | -import java.nio.ByteBuffer; |
51 | | -import java.nio.charset.StandardCharsets; |
52 | 38 | import java.util.ArrayList; |
53 | | -import java.util.HashMap; |
54 | 39 | import java.util.List; |
55 | | -import java.util.Map; |
56 | | - |
57 | | -import static io.siddhi.extension.io.file.util.Constants.WAIT_TILL_DONE; |
58 | | -import static io.siddhi.extension.util.Constant.CREATE; |
59 | | -import static io.siddhi.extension.util.Constant.CREATE_FOLDER; |
60 | 40 |
|
61 | 41 | /** |
62 | 42 | * This extension can be used to create a file or a folder. |
|
77 | 57 | type = DataType.STRING |
78 | 58 | ) |
79 | 59 | }, |
80 | | - returnAttributes = { |
81 | | - @ReturnAttribute( |
82 | | - name = "isSuccess", |
83 | | - description = "Status of the file or the directory creation.", |
84 | | - type = DataType.BOOL |
85 | | - ) |
86 | | - }, |
87 | 60 | examples = { |
88 | 61 | @Example( |
89 | 62 | syntax = "from CreateFileStream#file:create('/User/wso2/source/test.txt', false)", |
|
95 | 68 | ) |
96 | 69 | } |
97 | 70 | ) |
98 | | -public class FileCreateExtension extends StreamProcessor<State> { |
| 71 | +public class FileCreateExtension extends StreamFunctionProcessor { |
99 | 72 | private static final Logger log = Logger.getLogger(FileCreateExtension.class); |
100 | 73 |
|
101 | 74 | @Override |
102 | | - protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, |
103 | | - StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater, |
104 | | - State state) { |
105 | | - while (streamEventChunk.hasNext()) { |
106 | | - StreamEvent streamEvent = streamEventChunk.next(); |
107 | | - String fileSourcePath = (String) attributeExpressionExecutors[0].execute(streamEvent); |
108 | | - boolean isDirectory = (Boolean) attributeExpressionExecutors[1].execute(streamEvent); |
109 | | - VFSClientConnector vfsClientConnector = new VFSClientConnector(); |
110 | | - VFSClientConnectorCallback vfsClientConnectorCallback = new VFSClientConnectorCallback(); |
111 | | - BinaryCarbonMessage carbonMessage = new BinaryCarbonMessage(ByteBuffer.wrap( |
112 | | - fileSourcePath.getBytes(StandardCharsets.UTF_8)), true); |
113 | | - Map<String, String> properties = new HashMap<>(); |
114 | | - properties.put(Constants.URI, fileSourcePath); |
115 | | - properties.put(Constants.ACTION, CREATE); |
116 | | - if (isDirectory) { |
117 | | - properties.put(CREATE_FOLDER, "true"); |
118 | | - } else { |
119 | | - properties.put(CREATE_FOLDER, "false"); |
120 | | - } |
121 | | - try { |
122 | | - vfsClientConnector.send(carbonMessage, vfsClientConnectorCallback, properties); |
123 | | - vfsClientConnectorCallback.waitTillDone(WAIT_TILL_DONE, fileSourcePath); |
124 | | - } catch (ClientConnectorException e) { |
125 | | - throw new SiddhiAppRuntimeException("Failure occurred in vfs-client while creating the file " + |
126 | | - fileSourcePath, e); |
127 | | - } catch (InterruptedException e) { |
128 | | - throw new SiddhiAppRuntimeException("Failed to get callback from vfs-client for file " + |
129 | | - fileSourcePath, e); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - } |
134 | | - |
135 | | - @Override |
136 | | - protected StateFactory<State> init(MetaStreamEvent metaStreamEvent, AbstractDefinition inputDefinition, |
137 | | - ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, |
138 | | - StreamEventClonerHolder streamEventClonerHolder, |
139 | | - boolean outputExpectsExpiredEvents, boolean findToBeExecuted, |
140 | | - SiddhiQueryContext siddhiQueryContext) { |
141 | | - if (attributeExpressionExecutors.length == 2) { |
142 | | - if (attributeExpressionExecutors[0] == null) { |
143 | | - throw new SiddhiAppValidationException("Invalid input given to uri (first argument) " + |
144 | | - "file:create() function. Argument cannot be null"); |
145 | | - } |
146 | | - Attribute.Type firstAttributeType = attributeExpressionExecutors[0].getReturnType(); |
147 | | - if (!(firstAttributeType == Attribute.Type.STRING)) { |
148 | | - throw new SiddhiAppValidationException("Invalid parameter type found for uri " + |
149 | | - "(first argument) of file:create() function, required " + Attribute.Type.STRING + |
150 | | - " but found " + firstAttributeType.toString()); |
151 | | - } |
152 | | - if (attributeExpressionExecutors[1] == null) { |
153 | | - throw new SiddhiAppValidationException("Invalid input given to is.directory (second argument) " + |
154 | | - "file:create() function. Argument cannot be null"); |
155 | | - } |
156 | | - Attribute.Type secondAttributeType = attributeExpressionExecutors[1].getReturnType(); |
157 | | - if (!(secondAttributeType == Attribute.Type.BOOL)) { |
158 | | - throw new SiddhiAppValidationException("Invalid parameter type found for is.directory " + |
159 | | - "(second argument) of file:create() function, required " + Attribute.Type.BOOL + |
160 | | - " but found " + firstAttributeType.toString()); |
161 | | - } |
162 | | - } else { |
163 | | - throw new SiddhiAppValidationException("Invalid no of arguments passed to file:archive() function, " |
164 | | - + "required 2, but found " + attributeExpressionExecutors.length); |
165 | | - } |
| 75 | + protected StateFactory init(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, |
| 76 | + ConfigReader configReader, boolean outputExpectsExpiredEvents, |
| 77 | + SiddhiQueryContext siddhiQueryContext) { |
166 | 78 | return null; |
167 | 79 | } |
168 | 80 |
|
169 | 81 | @Override |
170 | 82 | public List<Attribute> getReturnAttributes() { |
171 | | - List<Attribute> attributes = new ArrayList<>(); |
172 | | - attributes.add(new Attribute("isSuccess", Attribute.Type.BOOL)); |
173 | | - return attributes; |
| 83 | + return new ArrayList<>(); |
174 | 84 | } |
175 | 85 |
|
176 | 86 | @Override |
177 | 87 | public ProcessingMode getProcessingMode() { |
178 | 88 | return ProcessingMode.BATCH; |
179 | 89 | } |
180 | 90 |
|
| 91 | + @Override |
| 92 | + protected Object[] process(Object[] data) { |
| 93 | + String fileSourcePath = (String) data[0]; |
| 94 | + boolean isDirectory = (Boolean) data[1]; |
| 95 | + FileObject rootFileObject = Utils.getFileObject(fileSourcePath); |
| 96 | + try { |
| 97 | + if (isDirectory) { |
| 98 | + rootFileObject.createFolder(); |
| 99 | + } else { |
| 100 | + rootFileObject.createFile(); |
| 101 | + } |
| 102 | + } catch (FileSystemException e) { |
| 103 | + throw new SiddhiAppRuntimeException("Failure occurred when creating the file " + fileSourcePath, e); |
| 104 | + } |
| 105 | + return new Object[0]; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected Object[] process(Object data) { |
| 110 | + return new Object[0]; |
| 111 | + } |
| 112 | + |
181 | 113 | @Override |
182 | 114 | public void start() { |
183 | 115 |
|
|
0 commit comments