Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 22nd, 2010  |  syntax: Java  |  size: 3.93 KB  |  views: 130  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package de.fu.alp5.foreign.distributed;
  2.  
  3. import java.io.IOException;
  4. import java.rmi.AlreadyBoundException;
  5. import java.rmi.NotBoundException;
  6. import java.rmi.RemoteException;
  7. import java.rmi.registry.LocateRegistry;
  8. import java.rmi.server.UnicastRemoteObject;
  9. import java.util.HashSet;
  10. import java.util.List;
  11. import java.util.Set;
  12. import java.util.concurrent.CountDownLatch;
  13.  
  14. /**
  15.  * Analyzing a list of words by their language. Foreign words are collected in a
  16.  * list. Each worker has an Integer identifying the access space of the text to
  17.  * analyze in order to balance work load between WordAnalyserWorker.
  18.  *
  19.  * @author Konrad Reiche
  20.  *
  21.  */
  22. public class TextAnalyzerWorkerImpl  implements TextAnalyzerWorker {
  23.  
  24.         List<String> text;
  25.         List<String> dictionary;
  26.         List<String> foreignDictionary;
  27.         Set<String> localForeignWordSet;
  28.  
  29.         int accessIndex;
  30.         int numberOfThreads;
  31.         CountDownLatch countDownLatch;
  32.  
  33.         TextAnalyzer remoteTextAnalyzer;
  34.  
  35.         /**
  36.          * Exports and binds a stub of this instance in order to provide remote
  37.          * access.
  38.          */
  39.         public TextAnalyzerWorkerImpl(String textPath, String dictionaryPath,
  40.                         String foreignDictionaryPath) throws IOException {
  41.                 super();
  42.                 this.text = TextAnalyzerImpl.convertTextFileToList(textPath);
  43.                 this.dictionary = TextAnalyzerImpl
  44.                                 .convertTextFileToList(dictionaryPath);
  45.                 this.foreignDictionary = TextAnalyzerImpl
  46.                                 .convertTextFileToList(foreignDictionaryPath);
  47.                 this.localForeignWordSet = new HashSet<String>();
  48.  
  49.                 TextAnalyzerWorker stub = (TextAnalyzerWorker) UnicastRemoteObject
  50.                                 .exportObject((TextAnalyzerWorker) this);
  51.  
  52.                 try {
  53.                         LocateRegistry.getRegistry().bind("TextAnalyzerWorker", stub);
  54.                 } catch (AlreadyBoundException e) {
  55.                         // ignored
  56.                 }
  57.         }
  58.  
  59.         /**
  60.          * Starts the analyzing. Every worker has assigned words to check. The words
  61.          * to check are determined by the given value of accessIndex.
  62.          *
  63.          * When finished the instance of this object is unbound and unexported.
  64.          */
  65.         public void analyze() throws RemoteException {
  66.  
  67.                 for (int i = accessIndex; i < text.size(); i += numberOfThreads) {
  68.                         String word = text.get(i);
  69.                         checkWord(word);
  70.                 }
  71.  
  72.                 UnicastRemoteObject.unexportObject((TextAnalyzerWorkerImpl) this, true);
  73.                 try {
  74.                         LocateRegistry.getRegistry().unbind("WordAnalyzerWorker");
  75.                 } catch (NotBoundException e) {
  76.  
  77.                 }
  78.         }
  79.  
  80.         /**
  81.          * Initializes the Worker with needed informations.
  82.          */
  83.         public void initialize(int numberOfThreads, int accessIndex,
  84.                         String remoteHost) throws RemoteException {
  85.                 this.numberOfThreads = numberOfThreads;
  86.                 this.accessIndex = accessIndex;
  87.                 try {
  88.                         this.remoteTextAnalyzer = (TextAnalyzer) LocateRegistry
  89.                                         .getRegistry(remoteHost).lookup("TextAnalyzer");
  90.                 } catch (NotBoundException e) {
  91.                         System.err.println("TextAnalyzer is not bound on " + remoteHost);
  92.                 }
  93.         }
  94.  
  95.         /**
  96.          * Checks a word for its language. If the word is already in the local
  97.          * foreign word set the method returns as this word is already known.
  98.          *
  99.          * If the word is in the dictionary it does not have to be added and thus
  100.          * the method returns.
  101.          *
  102.          * If the word is in the foreign dictionary the word is newly added to the
  103.          * global foreign word set of the TextAnalyzer. Further every word of the
  104.          * global foreign word set is copied to the local one.
  105.          *
  106.          * If none of these cases matches the method returns also and thus the word
  107.          * is ignored.
  108.          *
  109.          */
  110.         private void checkWord(String word) throws RemoteException {
  111.  
  112.                 for (String localForeignWord : localForeignWordSet) {
  113.                         if (word.equals(localForeignWord)) {
  114.                                 return;
  115.                         }
  116.                 }
  117.  
  118.                 for (String dictionaryWord : dictionary) {
  119.                         if (word.equals(dictionaryWord)) {
  120.                                 return;
  121.                         }
  122.                 }
  123.  
  124.                 for (String foreignDictionaryWord : foreignDictionary) {
  125.                         if (word.equals(foreignDictionaryWord)) {
  126.                                 remoteTextAnalyzer.addForeignWord(word);
  127.                                 remoteTextAnalyzer.getGlobalForeignWordSet().addAll(
  128.                                                 localForeignWordSet);
  129.                         }
  130.                 }
  131.         }
  132.  
  133. }
clone this paste RAW Paste Data