mev-4.0.01/source/org/tigr/microarray/mev/TMEVAlgorithmFactory.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: TMEVAlgorithmFactory.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.4 $
2 26 Feb 07 jari 8  * $Date: 2006/02/23 20:59:41 $
2 26 Feb 07 jari 9  * $Author: caliente $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.microarray.mev;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import java.util.StringTokenizer;
2 26 Feb 07 jari 15
2 26 Feb 07 jari 16 import org.tigr.microarray.mev.cluster.algorithm.Algorithm;
2 26 Feb 07 jari 17 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException;
2 26 Feb 07 jari 18 import org.tigr.microarray.mev.cluster.algorithm.AlgorithmFactory;
2 26 Feb 07 jari 19 import org.tigr.remote.RemoteAlgorithm;
2 26 Feb 07 jari 20 import org.tigr.remote.communication.CommunicatorFactory;
2 26 Feb 07 jari 21 import org.tigr.util.ConfMap;
2 26 Feb 07 jari 22
2 26 Feb 07 jari 23 public class TMEVAlgorithmFactory implements AlgorithmFactory {
2 26 Feb 07 jari 24     
2 26 Feb 07 jari 25     private ConfMap cfg;
2 26 Feb 07 jari 26     private AlgorithmFactory localFactory;
2 26 Feb 07 jari 27     private boolean isRemoteEnabled = false;
2 26 Feb 07 jari 28     
2 26 Feb 07 jari 29     /**
2 26 Feb 07 jari 30      * Constructs a <code>TMEVAlgorithmFactory</code> using specified
2 26 Feb 07 jari 31      * configuration.
2 26 Feb 07 jari 32      * @see ConfMap
2 26 Feb 07 jari 33      */
2 26 Feb 07 jari 34     public TMEVAlgorithmFactory(ConfMap cfg) {
2 26 Feb 07 jari 35   this.cfg = cfg;
2 26 Feb 07 jari 36   // local
2 26 Feb 07 jari 37   String className = cfg.getString("algorithm.factory.class");
2 26 Feb 07 jari 38   if (className != null && !className.equals("null")) {
2 26 Feb 07 jari 39       try {
2 26 Feb 07 jari 40     Class clazz = Class.forName(className);
2 26 Feb 07 jari 41     localFactory = (AlgorithmFactory)clazz.newInstance();
2 26 Feb 07 jari 42       } catch (Exception e) {
2 26 Feb 07 jari 43     System.out.println("Local factory not available, check the 'algorithm.factory.class' key in cfg file.");
2 26 Feb 07 jari 44     e.printStackTrace();
2 26 Feb 07 jari 45       }
2 26 Feb 07 jari 46   } else {
2 26 Feb 07 jari 47       System.out.println("Local factory not available, check the 'algorithm.factory.class' key in cfg file.");
2 26 Feb 07 jari 48   }
2 26 Feb 07 jari 49   // remote
2 26 Feb 07 jari 50   try {
2 26 Feb 07 jari 51       CommunicatorFactory.init(cfg);
2 26 Feb 07 jari 52       this.isRemoteEnabled = true;
2 26 Feb 07 jari 53   } catch (Exception e) {
2 26 Feb 07 jari 54       System.out.println("Failed to configure remote execution.");
2 26 Feb 07 jari 55       e.printStackTrace();
2 26 Feb 07 jari 56   }
2 26 Feb 07 jari 57     }
2 26 Feb 07 jari 58     
2 26 Feb 07 jari 59     /**
2 26 Feb 07 jari 60      * Returns an instance of algorithm by its name.
2 26 Feb 07 jari 61      * @throws AlgorithmException if specified algo is not accessible.
2 26 Feb 07 jari 62      */
2 26 Feb 07 jari 63     public Algorithm getAlgorithm(String name) throws AlgorithmException {
2 26 Feb 07 jari 64   if (name == null) {
2 26 Feb 07 jari 65       throw new AlgorithmException("Algorithm name expected.");
2 26 Feb 07 jari 66   }
2 26 Feb 07 jari 67   if (isRemote(name)) {
2 26 Feb 07 jari 68       if (this.isRemoteEnabled) {
2 26 Feb 07 jari 69     return new RemoteAlgorithm(name);
2 26 Feb 07 jari 70       }
2 26 Feb 07 jari 71       throw new AlgorithmException("Remote execution not available.");
2 26 Feb 07 jari 72   }
2 26 Feb 07 jari 73   if (this.localFactory == null) {
2 26 Feb 07 jari 74       throw new AlgorithmException("Local execution not available.");
2 26 Feb 07 jari 75   }
2 26 Feb 07 jari 76   return this.localFactory.getAlgorithm(name);
2 26 Feb 07 jari 77     }
2 26 Feb 07 jari 78     
2 26 Feb 07 jari 79     /**
2 26 Feb 07 jari 80      * Returns true if algorithm with a specified name was configured
2 26 Feb 07 jari 81      * as remote.
2 26 Feb 07 jari 82      */
2 26 Feb 07 jari 83     private boolean isRemote(String name) {
2 26 Feb 07 jari 84   String remoute = this.cfg.getString("algorithm.remote");
2 26 Feb 07 jari 85   if (remoute == null) {
2 26 Feb 07 jari 86       return false;
2 26 Feb 07 jari 87   }
2 26 Feb 07 jari 88   StringTokenizer tokenizer = new StringTokenizer(remoute, ":");
2 26 Feb 07 jari 89   String str;
2 26 Feb 07 jari 90   while (tokenizer.hasMoreTokens()) {
2 26 Feb 07 jari 91       str = tokenizer.nextToken();
2 26 Feb 07 jari 92       if (str.trim().toUpperCase().equals(name.toUpperCase())) {
2 26 Feb 07 jari 93     return true;
2 26 Feb 07 jari 94       }
2 26 Feb 07 jari 95   }
2 26 Feb 07 jari 96   return false;
2 26 Feb 07 jari 97     }
2 26 Feb 07 jari 98 }