00001 package org.gridlab.gat.engine;
00002
00003 import java.util.Hashtable;
00004 import java.util.Iterator;
00005 import java.util.StringTokenizer;
00006
00007 import org.gridlab.gat.GATContext;
00008 import org.gridlab.gat.Preferences;
00009 import java.io.File;
00010 import java.io.IOException;
00011 import java.util.List;
00012 import java.util.Vector;
00013 import java.util.jar.Attributes;
00014 import java.util.jar.JarFile;
00015 import java.util.jar.Manifest;
00016
00017 import org.gridlab.gat.io.cpi.FileCpi;
00018 import org.gridlab.gat.io.cpi.FileInputStreamCpi;
00019 import org.gridlab.gat.io.cpi.FileOutputStreamCpi;
00020 import org.gridlab.gat.io.cpi.LogicalFileCpi;
00021 import org.gridlab.gat.io.cpi.RandomAccessFileCpi;
00022 import org.gridlab.gat.resources.cpi.JobCpi;
00023 import org.gridlab.gat.resources.cpi.ResourceBrokerCpi;
00024
00025
00026
00027
00028
00029
00030
00031 public class GATEngine {
00032
00033 protected static final boolean DEBUG = true;
00034
00035
00036
00037
00038 private static GATEngine gatEngine = null;
00039
00040
00041 private Hashtable adaptors;
00042
00043
00044
00045
00046 protected GATEngine() {
00047 adaptors = new Hashtable();
00048 readJarFiles();
00049 }
00050
00051
00052
00053
00054
00055
00056 public static synchronized GATEngine getGATEngine() {
00057 if (gatEngine == null) gatEngine = new GATEngine();
00058 return gatEngine;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 public Object getAdaptor(Class cpiClass, GATContext gatContext,
00075 Preferences preferences, Object[] tmpParams) {
00076
00077 if(DEBUG) {
00078 System.err.println("getAdaptor: trying to get an adaptor for type " +
00079 cpiClass.getName());
00080 }
00081
00082 if (tmpParams == null) {
00083 tmpParams = new Object[0];
00084 }
00085
00086 if (preferences == null) {
00087 preferences = new Preferences(new Hashtable());
00088 }
00089
00090
00091 Object[] parameters = new Object[tmpParams.length + 2];
00092 parameters[0] = gatContext;
00093 parameters[1] = preferences;
00094 for (int i = 0; i < tmpParams.length; i++) {
00095 parameters[i + 2] = tmpParams[i];
00096 }
00097
00098
00099 Class[] parameterTypes = new Class[parameters.length];
00100 for (int count = 0; count < parameterTypes.length; count++) {
00101 parameterTypes[count] = parameters[count].getClass();
00102 }
00103
00104
00105 AdaptorList l = (AdaptorList) adaptors.get(cpiClass.getName());
00106 if (l == null) {
00107 if(DEBUG) {
00108 System.err.println("No adaptors loaded for type " + cpiClass.getName());
00109 }
00110 return null;
00111 }
00112
00113 for (int i = 0; i < l.size(); i++) {
00114 Adaptor a = l.get(i);
00115 if (a.satisfies(preferences)) {
00116 Object o = a.newInstance(parameterTypes, parameters);
00117 if(o != null) {
00118 if(DEBUG) {
00119 System.err.println("Created adaptor instance of type " + a.getName());
00120 }
00121 return o;
00122 } else {
00123 if (DEBUG) {
00124 System.err.println("Could not create an instance of Adaptor" + a.getName());
00125 }
00126 }
00127 }
00128 }
00129
00130 if(DEBUG) {
00131 System.err.println("None of the loaded adaptors satisfies the requested user preferences.");
00132 }
00133
00134 return null;
00135 }
00136
00137
00138
00139
00140
00141 protected void readJarFiles() {
00142
00143 List jarFiles = getJarFiles(getOptionalPkgDirectory());
00144
00145 String adaptorPath = System.getProperty("gat.adaptor.path");
00146 if(adaptorPath != null) {
00147 StringTokenizer st = new StringTokenizer(adaptorPath, File.pathSeparator);
00148 for(int i=0; i<st.countTokens(); i++) {
00149 String dir = st.nextToken();
00150 List l = getJarFiles(dir);
00151 jarFiles.addAll(l);
00152 }
00153 }
00154
00155 if(DEBUG) {
00156 System.err.println("List of GAT jar files is: ");
00157 printJars(jarFiles);
00158 }
00159
00160
00161 loadJarFiles(jarFiles);
00162 }
00163
00164 protected void printJars(List l) {
00165 for(int i=0; i<l.size(); i++) {
00166 JarFile f = (JarFile) l.get(i);
00167 System.err.println(" " + f.getName());
00168 }
00169 }
00170
00171
00172
00173
00174 protected String getOptionalPkgDirectory() {
00175 return System.getProperty("java.home")
00176 + File.separator + "lib" + File.separator + "ext";
00177 }
00178
00179
00180
00181
00182 protected List getFiles(File optionalPkgDirectory) {
00183 Vector vector = new Vector();
00184 File[] files = optionalPkgDirectory.listFiles();
00185
00186 for (int count = 0; count < files.length; count++)
00187 vector.add(files[count]);
00188
00189 return vector;
00190 }
00191
00192
00193
00194
00195 protected List getJarFiles(String dir) {
00196 File nextFile = null;
00197 JarFile jarFile = null;
00198 Manifest manifest = null;
00199 Attributes attributes = null;
00200
00201
00202 List files = getFiles(new File(dir));
00203
00204 Iterator iterator = files.iterator();
00205
00206 Vector jarFiles = new Vector();
00207
00208 while (iterator.hasNext()) {
00209 nextFile = (File) iterator.next();
00210
00211 if (nextFile.isFile()) {
00212 try {
00213 jarFile = new JarFile(nextFile, true);
00214 manifest = jarFile.getManifest();
00215 if (null != manifest) {
00216 attributes = manifest.getMainAttributes();
00217
00218
00219 if ("true".equals(attributes.getValue("GATAdaptor"))) {
00220 jarFiles.add(jarFile);
00221 }
00222 }
00223 } catch (IOException ioException) {
00224
00225 }
00226 }
00227 }
00228
00229 return jarFiles;
00230 }
00231
00232 protected void loadCpiClass(Manifest manifest, Attributes attributes,
00233 String className, Class cpiClazz) {
00234
00235 if(DEBUG) {
00236 System.err.println("Trying to load adaptor for " + className);
00237 }
00238
00239 String attributeName = className + "Cpi-class";
00240
00241
00242 String clazzString = attributes.getValue(attributeName);
00243
00244 if (clazzString == null) {
00245 if(DEBUG) {
00246 System.err.println("Adaptor for " + className + " not found in Manifest");
00247 }
00248 return;
00249 }
00250
00251 if(DEBUG) {
00252 System.err.println("Adaptor for " + className + " found in Manifest, loading");
00253 }
00254
00255 Class clazz = null;
00256 try {
00257 clazz = Class.forName(clazzString);
00258 } catch (ClassNotFoundException e) {
00259 if(DEBUG) {
00260 System.err.println("Could not load Adaptor for " + className + ": " + e);
00261 }
00262 return;
00263 }
00264
00265 if(DEBUG) {
00266 System.err.println("Adaptor for " + className + " loaded");
00267 }
00268
00269 Preferences preferences = new Preferences(attributes);
00270
00271 Adaptor a = new Adaptor(cpiClazz, clazz, preferences);
00272 AdaptorList s = (AdaptorList) adaptors.get(cpiClazz.getName());
00273 if (s == null) {
00274 s = new AdaptorList(cpiClazz);
00275 adaptors.put(cpiClazz.getName(), s);
00276 }
00277
00278 s.addAdaptor(a);
00279 }
00280
00281 protected void loadCPIClassesFromJar(JarFile jarFile) {
00282 Manifest manifest = null;
00283 Attributes attributes = null;
00284
00285
00286 try {
00287 manifest = jarFile.getManifest();
00288 } catch (IOException e) {
00289 return;
00290 }
00291
00292 attributes = manifest.getMainAttributes();
00293
00294 loadCpiClass(manifest, attributes, "File", FileCpi.class);
00295 loadCpiClass(manifest, attributes, "LogicalFile", LogicalFileCpi.class);
00296 loadCpiClass(manifest, attributes, "RandomAccessFile",
00297 RandomAccessFileCpi.class);
00298 loadCpiClass(manifest, attributes, "FileInputStream",
00299 FileInputStreamCpi.class);
00300 loadCpiClass(manifest, attributes, "FileOutputStream",
00301 FileOutputStreamCpi.class);
00302 loadCpiClass(manifest, attributes, "Job", JobCpi.class);
00303 loadCpiClass(manifest, attributes, "ResourceBroker",
00304 ResourceBrokerCpi.class);
00305 }
00306
00307
00308
00309
00310 protected void loadJarFiles(List jarFiles) {
00311 JarFile jarFile = null;
00312
00313 Iterator iterator = jarFiles.iterator();
00314
00315
00316 while (iterator.hasNext()) {
00317 jarFile = (JarFile) iterator.next();
00318 if(DEBUG) {
00319 System.err.println("loading adaptors from " + jarFile.getName());
00320 }
00321 loadCPIClassesFromJar(jarFile);
00322 }
00323 }
00324 }