SHARE
TWEET

FileSenderService.java

olegtikhonov Apr 1st, 2013 153 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Licensed under the Apache License, Version 2.0 (the "License");
  3.  *  you may not use this file except in compliance with the License.
  4.  *  You may obtain a copy of the License at
  5.  *
  6.  *       http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  *  Unless required by applicable law or agreed to in writing, software
  9.  *  distributed under the License is distributed on an "AS IS" BASIS,
  10.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  *  See the License for the specific language governing permissions and
  12.  *  limitations under the License.
  13.  */
  14.  
  15. package org.example.tcpserver;
  16.  
  17. import java.io.File;
  18. import java.util.concurrent.ExecutorService;
  19. import java.util.concurrent.Executors;
  20. import org.apache.log4j.Logger;
  21. import org.springframework.integration.Message;
  22. import org.springframework.integration.channel.DirectChannel;
  23.  
  24. /**
  25.  * Defines a sender server service for simulating a machine behavior.
  26.  * Default accept massage is <code>TrxConfigList</code>.
  27.  * You can change it by using  set/get SearchKey function.
  28.  */
  29.  
  30. public class FileSenderService {
  31.     /* Local logger */
  32.     private final Logger LOG = Logger.getLogger(FileSenderService.class);
  33.     /* To be read */
  34.     private File file;
  35.     /* A channel that sends messages. */
  36.     private DirectChannel sender;
  37.     /* Accepted string */
  38.     private String SEARCH_KEY = "GIMMY";
  39.     /* Thread pool for executing tasks */
  40.     private ExecutorService es = Executors.newFixedThreadPool(1);
  41.  
  42.  
  43.     /**
  44.      * Sends reply to the client.
  45.      *
  46.      * @param appropriateData a data to be sent.
  47.      *
  48.      * @return a String.
  49.      */
  50.     public void send(Message<byte[]> msg) {
  51.         byte[] appropriateData = msg.getPayload();
  52.         try {
  53.             String key = new String(appropriateData, "UTF-8");
  54.             LOG.info("got.message" + " [" + key + "]");
  55.             /* If message accepted */
  56.             if (key.contains(SEARCH_KEY)) {
  57.                 LogReader lr = new LogReader(sender, msg);
  58.                 lr.setPath2File(getFile().getAbsolutePath());
  59.                 es.execute(lr);
  60.             }
  61.         } catch (Throwable e) {
  62.             LOG.error(e);
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Gets a search key.
  68.      *
  69.      * @return a search key.
  70.      */
  71.     public String getSearchKey() {
  72.         return SEARCH_KEY;
  73.     }
  74.  
  75.     /**
  76.      * Sets a search key.
  77.      *
  78.      * @param searchKey for authorizing the client.
  79.      */
  80.     public void setSearchKey(String searchKey) {
  81.         this.SEARCH_KEY = searchKey;
  82.     }
  83.  
  84.     /**
  85.      * Gets a file.
  86.      *
  87.      * @return a path to the file.
  88.      */
  89.     public File getFile() {
  90.         return file;
  91.     }
  92.  
  93.     /**
  94.      * Sets a file to be sent.
  95.      *
  96.      * @param file to be sent.
  97.      */
  98.     public void setFile(File file) {
  99.         this.file = file;
  100.     }
  101.  
  102.     public void setSender(DirectChannel sender) {
  103.         this.sender = sender;
  104.     }
  105. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top