Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2010
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. package de.fu.alp5.foreign.distributed;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.StreamTokenizer;
  7. import java.net.InetAddress;
  8. import java.net.UnknownHostException;
  9. import java.rmi.AlreadyBoundException;
  10. import java.rmi.NotBoundException;
  11. import java.rmi.RemoteException;
  12. import java.rmi.registry.LocateRegistry;
  13. import java.rmi.registry.Registry;
  14. import java.rmi.server.UnicastRemoteObject;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.concurrent.CopyOnWriteArraySet;
  18. import java.util.concurrent.CountDownLatch;
  19.  
  20. /**
  21.  * Analyzes a text for foreign words.
  22.  *
  23.  * @author Konrad Reiche
  24.  *
  25.  */
  26. public class TextAnalyzerImpl implements TextAnalyzer {
  27.  
  28.     List<String> text;
  29.     List<String> dictionary;
  30.     List<String> foreignDictionary;
  31.     CopyOnWriteArraySet<String> globalForeignWordSet;
  32.  
  33.     /**
  34.      * Converts the given text files to lists of Strings. Binds and export the
  35.      * current instance to the RMI registry in order to be accessible. If the
  36.      * TextAnalyzer is already bound ignore the exceptions as the current
  37.      * instance is still exported.
  38.      */
  39.     public TextAnalyzerImpl(String textPath, String dictionaryPath,
  40.             String foreignDictionaryPath) throws IOException {
  41.  
  42.         text = convertTextFileToList(textPath);
  43.         dictionary = convertTextFileToList(dictionaryPath);
  44.         foreignDictionary = convertTextFileToList(foreignDictionaryPath);
  45.         globalForeignWordSet = new CopyOnWriteArraySet<String>();
  46.  
  47.         TextAnalyzer stub = (TextAnalyzer) UnicastRemoteObject
  48.                 .exportObject((TextAnalyzer) this);
  49.  
  50.         try {
  51.             LocateRegistry.getRegistry().bind("TextAnalyzer", stub);
  52.         } catch (AlreadyBoundException e) {
  53.             // ignore
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Starts the analyzing. A CountDownLatch is used to synchronize the
  59.      * termination of the method call. The parameter numberOfThreads determines
  60.      * the number of TextAnaylzerWorker to be spawned.
  61.      *
  62.      * A list of the worker host is passed in order to look up every worker
  63.      * object. When the worker object is retrieved it is initialized.
  64.      *
  65.      * For every Worker a new Thread is spawned and an anonymous Runnable is
  66.      * instantiated because the run method cannot be invoked remotely.
  67.      *
  68.      * When the analyzing is finished the instance of this object is unbound and
  69.      * unexported.
  70.      */
  71.     public void analyze(int numberOfThreads, List<String> hostList)
  72.             throws RemoteException, UnknownHostException, InterruptedException {
  73.  
  74.         final CountDownLatch countDownLatch = new CountDownLatch(
  75.                 numberOfThreads);
  76.  
  77.         for (int i = 0; i < numberOfThreads; ++i) {
  78.             Registry registry = LocateRegistry.getRegistry(hostList.get(i));
  79.             final TextAnalyzerWorker worker;
  80.  
  81.             try {
  82.                 worker = (TextAnalyzerWorker) registry
  83.                         .lookup("TextAnalyzerWorker");
  84.             } catch (NotBoundException e1) {
  85.                 System.err.println("There is no TextAnalyzerWorker on "
  86.                         + hostList.get(i));
  87.                 continue;
  88.             }
  89.  
  90.             worker.initialize(numberOfThreads, i, InetAddress.getLocalHost()
  91.                     .getHostAddress());
  92.  
  93.             new Thread(new Runnable() {
  94.  
  95.                 @Override
  96.                 public void run() {
  97.  
  98.                     try {
  99.                         worker.analyze();
  100.                         countDownLatch.countDown();
  101.                     } catch (RemoteException e) {
  102.                         System.err.println(e);
  103.                     }
  104.  
  105.                 }
  106.             }).start();
  107.         }
  108.  
  109.         countDownLatch.await();
  110.  
  111.         try {
  112.             LocateRegistry.getRegistry().unbind("TextAnalyzer");
  113.         } catch (NotBoundException e) {
  114.             System.err.println("TextAnalyzer was not bound");
  115.         }
  116.  
  117.         UnicastRemoteObject.unexportObject((TextAnalyzer) this, true);
  118.     }
  119.  
  120.     /**
  121.      * Converts an arbitrary text file to a list of Strings by using
  122.      * StreamTokenizer.
  123.      */
  124.     public static List<String> convertTextFileToList(String path)
  125.             throws IOException {
  126.  
  127.         List<String> result = new ArrayList<String>();
  128.         FileInputStream textInputStream = new FileInputStream(path);
  129.         StreamTokenizer streamTokenizer = new StreamTokenizer(
  130.                 new InputStreamReader(textInputStream));
  131.         streamTokenizer.lowerCaseMode(true);
  132.  
  133.         int tokenByte = streamTokenizer.nextToken();
  134.         while (tokenByte != StreamTokenizer.TT_EOF) {
  135.             if (tokenByte == StreamTokenizer.TT_WORD) {
  136.                 result.add(streamTokenizer.sval);
  137.             }
  138.             tokenByte = streamTokenizer.nextToken();
  139.         }
  140.  
  141.         return result;
  142.     }
  143.  
  144.     /**
  145.      * Returns a String with all foreign words found in the given text.
  146.      */
  147.     @Override
  148.     public String toString() {
  149.         String result = new String();
  150.  
  151.         for (String foreignWord : globalForeignWordSet) {
  152.             result = result.concat(foreignWord + "\n");
  153.         }
  154.  
  155.         return result;
  156.     }
  157.  
  158.     @Override
  159.     public void addForeignWord(String foreignWord) {
  160.         globalForeignWordSet.add(foreignWord);
  161.     }
  162.  
  163.     @Override
  164.     public CopyOnWriteArraySet<String> getGlobalForeignWordSet() {
  165.         return globalForeignWordSet;
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement