Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.example.tcpserver;
- import java.io.File;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import org.apache.log4j.Logger;
- import org.springframework.integration.Message;
- import org.springframework.integration.channel.DirectChannel;
- /**
- * Defines a sender server service for simulating a machine behavior.
- * Default accept massage is <code>TrxConfigList</code>.
- * You can change it by using set/get SearchKey function.
- */
- public class FileSenderService {
- /* Local logger */
- private final Logger LOG = Logger.getLogger(FileSenderService.class);
- /* To be read */
- private File file;
- /* A channel that sends messages. */
- private DirectChannel sender;
- /* Accepted string */
- private String SEARCH_KEY = "GIMMY";
- /* Thread pool for executing tasks */
- private ExecutorService es = Executors.newFixedThreadPool(1);
- /**
- * Sends reply to the client.
- *
- * @param appropriateData a data to be sent.
- *
- * @return a String.
- */
- public void send(Message<byte[]> msg) {
- byte[] appropriateData = msg.getPayload();
- try {
- String key = new String(appropriateData, "UTF-8");
- LOG.info("got.message" + " [" + key + "]");
- /* If message accepted */
- if (key.contains(SEARCH_KEY)) {
- LogReader lr = new LogReader(sender, msg);
- lr.setPath2File(getFile().getAbsolutePath());
- es.execute(lr);
- }
- } catch (Throwable e) {
- LOG.error(e);
- }
- }
- /**
- * Gets a search key.
- *
- * @return a search key.
- */
- public String getSearchKey() {
- return SEARCH_KEY;
- }
- /**
- * Sets a search key.
- *
- * @param searchKey for authorizing the client.
- */
- public void setSearchKey(String searchKey) {
- this.SEARCH_KEY = searchKey;
- }
- /**
- * Gets a file.
- *
- * @return a path to the file.
- */
- public File getFile() {
- return file;
- }
- /**
- * Sets a file to be sent.
- *
- * @param file to be sent.
- */
- public void setFile(File file) {
- this.file = file;
- }
- public void setSender(DirectChannel sender) {
- this.sender = sender;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement