org/objectweb/proactive/core/process/AbstractListProcessDecorator.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.process;
00032 
00033 import org.objectweb.proactive.core.process.filetransfer.FileTransferWorkShop;
00034 import org.objectweb.proactive.core.util.RemoteProcessMessageLogger;
00035 
00036 import java.io.IOException;
00037 
00038 import java.util.ArrayList;
00039 
00040 
00049 public abstract class AbstractListProcessDecorator
00050     implements ExternalProcessDecorator {
00051     //Array of processes
00052     protected ArrayList<ExternalProcessDecorator> processes;
00053 
00054     //baseProcess is used to get the common configuration
00055     protected ExternalProcessDecorator baseProcess;
00056 
00057     //represent the fixed part of the hostName
00058     protected String fixedName;
00059     protected String domain;
00060     protected String list;
00061     private String tmp;
00062     protected String beginIndex;
00063     protected String endIndex;
00064     protected int padding = 1;
00065     protected int repeat = 1;
00066     protected int step = 1;
00067     protected ArrayList<String> excludeArray = new ArrayList<String>();
00068     protected int nodeNumber = 0;
00069 
00070     public AbstractListProcessDecorator() {
00071         processes = new ArrayList<ExternalProcessDecorator>();
00072     }
00073 
00074     public void setHostConfig(String fixedName, String list, String domain,
00075         int padding, int repeat) {
00076         this.fixedName = fixedName;
00077         this.domain = domain;
00078         this.padding = padding;
00079         this.repeat = repeat;
00080         this.list = list.replaceAll("\\s", "");
00081         setAllIndex(this.list);
00082         for (int i = Integer.parseInt(beginIndex);
00083                 i <= Integer.parseInt(endIndex); i = i + step) {
00084             tmp = "" + i; //we change as String to check the array
00085             if (!excludeArray.contains(tmp)) {
00086                 for (int count=0; count<repeat; count++) {
00087                         setHostname(handlePadding(tmp));
00088                 }
00089             }
00090         }
00091         baseProcess = processes.get(0);
00092     }
00093 
00094     public void setHostList(String hostlistattr, String domain) {
00095         String[] hostlist = hostlistattr.split("\\s");
00096         if (checkNonEmpty(domain)) {
00097             if (!domain.startsWith(".")) {
00098                 domain = "." + domain;
00099             }
00100             for (int i = 0; i < hostlist.length; i++) {
00101                 String hostname = hostlist[i] + domain;
00102                 setHostname(hostname);
00103             }
00104         } else {
00105             for (int i = 0; i < hostlist.length; i++) {
00106                 setHostname(hostlist[i]);
00107             }
00108         }
00109         baseProcess =  processes.get(0);
00110     }
00111 
00112     //
00113     //--------------------------Implements ExternalProcessDecorator----------------------
00114 
00118     public ExternalProcess getTargetProcess() {
00119         return baseProcess.getTargetProcess();
00120     }
00121 
00125     public void setTargetProcess(ExternalProcess targetProcess) {
00126         for (int i = 0; i < processes.size(); i++) {
00127             ( processes.get(i)).setTargetProcess(targetProcess);
00128         }
00129     }
00130 
00134     public int getCompositionType() {
00135         return baseProcess.getCompositionType();
00136     }
00137 
00141     public void setCompositionType(int compositionType) {
00142         for (int i = 0; i < processes.size(); i++) {
00143             ( processes.get(i)).setCompositionType(compositionType);
00144         }
00145     }
00146 
00147     public FileTransferWorkShop getFileTransferWorkShopRetrieve() {
00148 
00149         /* TODO Check if this is the correct place
00150          * implement this. Then implement it
00151          */
00152         return null;
00153     }
00154 
00155     public FileTransferWorkShop getFileTransferWorkShopDeploy() {
00156 
00157         /* TODO Check if this is the correct place
00158          * implement this. Then implement it
00159          */
00160         return null;
00161     }
00162 
00163     public void startFileTransfer() {
00164 
00165         /* TODO Check if this is the correct place
00166          * implement this. Then implement it
00167          */
00168     }
00169 
00170     public boolean isRequiredFileTransferDeployOnNodeCreation() {
00171 
00172         /* TODO Check if this is the correct place
00173          * implement this. Then implement it
00174          */
00175         return false;
00176     }
00177 
00181     public void closeStream() {
00182         for (int i = 0; i < processes.size(); i++) {
00183             ( processes.get(i)).closeStream();
00184         }
00185     }
00186 
00190     public RemoteProcessMessageLogger getInputMessageLogger() {
00191         return baseProcess.getInputMessageLogger();
00192     }
00193 
00197     public RemoteProcessMessageLogger getErrorMessageLogger() {
00198         return baseProcess.getErrorMessageLogger();
00199     }
00200 
00204     public MessageSink getOutputMessageSink() {
00205         return baseProcess.getOutputMessageSink();
00206     }
00207 
00211     public void setInputMessageLogger(
00212         RemoteProcessMessageLogger inputMessageLogger) {
00213         for (int i = 0; i < processes.size(); i++) {
00214             ( processes.get(i)).setInputMessageLogger(inputMessageLogger);
00215         }
00216     }
00217 
00221     public void setErrorMessageLogger(
00222         RemoteProcessMessageLogger errorMessageLogger) {
00223         for (int i = 0; i < processes.size(); i++) {
00224             ( processes.get(i)).setErrorMessageLogger(errorMessageLogger);
00225         }
00226     }
00227 
00231     public void setOutputMessageSink(MessageSink outputMessageSink) {
00232         for (int i = 0; i < processes.size(); i++) {
00233             ( processes.get(i)).setOutputMessageSink(outputMessageSink);
00234         }
00235     }
00236 
00240     public String[] getEnvironment() {
00241         return baseProcess.getEnvironment();
00242     }
00243 
00247     public void setEnvironment(String[] environment) {
00248         for (int i = 0; i < processes.size(); i++) {
00249             ( processes.get(i)).setEnvironment(environment);
00250         }
00251     }
00252 
00256     public String getHostname() {
00257         return fixedName;
00258     }
00259 
00263     public void setHostname(String hostname) {
00264         ExternalProcessDecorator process = createProcess();
00265         process.setHostname(hostname);
00266         processes.add(process);
00267     }
00268 
00272     public String getUsername() {
00273         return baseProcess.getUsername();
00274     }
00275 
00279     public void setUsername(String username) {
00280         for (int i = 0; i < processes.size(); i++) {
00281             ( processes.get(i)).setUsername(username);
00282         }
00283     }
00284 
00288     public String getCommand() {
00289         return baseProcess.getCommandPath() + " " + fixedName + " " + list +
00290         " " + domain;
00291     }
00292 
00296     public String getProcessId() {
00297         return baseProcess.getProcessId();
00298     }
00299 
00303     public int getNodeNumber() {
00304         if (nodeNumber == 0) {
00305             for (int i = 0; i < processes.size(); i++) {
00306                 nodeNumber = nodeNumber +
00307                     ( processes.get(i)).getNodeNumber();
00308             }
00309         }
00310         return nodeNumber;
00311     }
00312 
00316     public UniversalProcess getFinalProcess() {
00317         return baseProcess.getFinalProcess();
00318     }
00319 
00323     public void startProcess() throws IOException {
00324         for (int i = 0; i < processes.size(); i++) {
00325             ( processes.get(i)).startProcess();
00326         }
00327     }
00328 
00332     public void stopProcess() {
00333         for (int i = 0; i < processes.size(); i++) {
00334             ( processes.get(i)).stopProcess();
00335         }
00336     }
00337 
00341     public int waitFor() throws InterruptedException {
00342         int status = 0;
00343         for (int i = 0; i < processes.size(); i++) {
00344             status = ( processes.get(i)).waitFor();
00345             if (status != 0) {
00346                 return status;
00347             }
00348         }
00349         return status;
00350     }
00351 
00355     public boolean isStarted() {
00356         boolean started = true;
00357         for (int i = 0; i < processes.size(); i++) {
00358             started = ( processes.get(i)).isStarted();
00359             if (!started) {
00360                 return started;
00361             }
00362         }
00363         return started;
00364     }
00365 
00369     public boolean isFinished() {
00370         boolean finished = true;
00371         for (int i = 0; i < processes.size(); i++) {
00372             finished = ( processes.get(i)).isFinished();
00373             if (!finished) {
00374                 return finished;
00375             }
00376         }
00377         return finished;
00378     }
00379 
00380     /* Return true if this process is hierarchical, false otherwise  */
00381     public boolean isHierarchical() {
00382         return false;
00383     }
00384 
00385     /* Return true if this process is dependent, false otherwise */
00386     public boolean isDependent() {
00387         return false;
00388     }
00389 
00390     /* Return true if this process is sequential, false otherwise */
00391     public boolean isSequential() {
00392         return false;
00393     }
00394 
00398     public void setCommandPath(String path) {
00399         for (int i = 0; i < processes.size(); i++) {
00400             ( processes.get(i)).setCommandPath(path);
00401         }
00402     }
00403 
00407     public String getCommandPath() {
00408         return baseProcess.getCommandPath();
00409     }
00410 
00411     //
00412     //-----------------protected methods----------------------------------------
00413     //
00414     protected abstract ExternalProcessDecorator createProcess();
00415 
00416     protected void setAllIndex(String list) {
00417         int index1 = list.indexOf(';');
00418         int index2 = list.indexOf(']');
00419         int index3 = list.indexOf('^');
00420         beginIndex = list.substring(1, list.indexOf('-'));
00421         if (index1 == -1) {
00422             endIndex = list.substring(list.indexOf('-') + 1, index2);
00423         } else {
00424             endIndex = list.substring(list.indexOf('-') + 1, index1);
00425             String scount = list.substring(index1 + 1, index2);
00426             step = new Integer(scount).intValue();
00427         }
00428         if (index3 != -1) {
00429             String[] listexclude = list.substring(list.lastIndexOf('[') + 1,
00430                     list.lastIndexOf(']')).split(",");
00431             for (int i = 0; i < listexclude.length; i++) {
00432                 if (listexclude[i].indexOf("-") < 0) {
00433                     excludeArray.add(listexclude[i]);
00434                 } else {
00435                     String init = listexclude[i].substring(0,
00436                             listexclude[i].indexOf("-"));
00437                     String end = listexclude[i].substring(listexclude[i].indexOf(
00438                                 "-") + 1, listexclude[i].length());
00439                     for (int j = Integer.parseInt(init);
00440                             j <= Integer.parseInt(end); j++) {
00441                         excludeArray.add(new Integer(j).toString());
00442                     }
00443                 }
00444             }
00445         }
00446     }
00447 
00448     protected String handlePadding(String word) {
00449         int wlength = word.length();
00450         if (wlength < padding) {
00451             for (int i = 0; i < (padding - wlength); i++) {
00452                 word = "0" + word;
00453             }
00454         }
00455         if (domain.length() > 0) {
00456             if (!domain.startsWith(".")) {
00457                 domain = "." + domain;
00458             }
00459         }
00460         return fixedName + word + domain;
00461     }
00462 
00463     protected boolean checkNonEmpty(String s) {
00464         return (s != null) && (s.length() > 0);
00465     }
00466 
00467     public int exitValue() throws IllegalThreadStateException {
00468         // TODO Auto-generated method stub
00469         return 0;
00470     }
00471 
00472     public void setStarted(boolean isStarted) {
00473         // TODO Auto-generated method stub
00474     }
00475 
00476     public void setFinished(boolean isFinished) {
00477         // TODO Auto-generated method stub
00478     }
00479 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1