001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.cPlanner.transfer.implementation;
016:
017: import org.griphyn.cPlanner.common.PegasusProperties;
018:
019: import org.griphyn.cPlanner.classes.PlannerOptions;
020:
021: import org.griphyn.cPlanner.transfer.Implementation;
022:
023: import org.griphyn.common.util.DynamicLoader;
024:
025: import java.io.IOException;
026:
027: import java.lang.reflect.InvocationTargetException;
028:
029: /**
030: * The factory class that loads an appropriate Transfer Immplementation class,
031: * as specified by the properties.
032: *
033: * @author Karan Vahi
034: * @version $Revision: 50 $
035: */
036: public class ImplementationFactory {
037:
038: /**
039: * The default package where the implementations reside, which this factory
040: * loads.
041: */
042: public static final String DEFAULT_PACKAGE_NAME = "org.griphyn.cPlanner.transfer.implementation";
043:
044: /**
045: * The constant designating the implementation be loaded for stage in jobs.
046: */
047: public static final int TYPE_STAGE_IN = 0;
048:
049: /**
050: * The constant designating the implementation be loaded for inter pool jobs.
051: */
052: public static final int TYPE_STAGE_INTER = 1;
053:
054: /**
055: * The constant designating the implementation be loaded for stage out jobs.
056: */
057: public static final int TYPE_STAGE_OUT = 2;
058:
059: /**
060: * Loads the implementing class corresponding to the type specified by the user.
061: * The type is used to determine what property to be picked up from the
062: * properties file. The properties object passed should not be null.
063: *
064: * @param properties the <code>PegasusProperties</code> object containing all
065: * the properties required by Pegasus.
066: * @param options the options passed to the planner at runtime.
067: * @param type the type for which implementation needs to be loaded.
068: *
069: * @return the instance of the class implementing this interface.
070: *
071: * @exception TransferImplementationFactoryException that nests any error that
072: * might occur during the instantiation.
073: *
074: * @see #DEFAULT_PACKAGE_NAME
075: */
076: public static Implementation loadInstance(
077: PegasusProperties properties, PlannerOptions options,
078: int type) throws TransferImplementationFactoryException {
079:
080: return loadInstance(properties
081: .getTransferImplementation(getProperty(type)),
082: properties, options);
083: }
084:
085: /**
086: * Loads the implementing class corresponding to the mode specified by the user
087: * at runtime in the properties file. The properties object passed should not
088: * be null.
089: *
090: * @param properties the <code>PegasusProperties</code> object containing all
091: * the properties required by Pegasus.
092: * @param options the options passed to the planner at runtime.
093: *
094: * @return the instance of the class implementing this interface.
095: *
096: * @exception TransferImplementationFactoryException that nests any error that
097: * might occur during the instantiation.
098: *
099: * @see #DEFAULT_PACKAGE_NAME
100: */
101: public static Implementation loadInstance(
102: PegasusProperties properties, PlannerOptions options)
103: throws TransferImplementationFactoryException {
104:
105: return loadInstance(properties.getTransferImplementation(),
106: properties, options);
107: }
108:
109: /**
110: * Loads the implementing class corresponding to the class. If the package
111: * name is not specified with the class, then class is assumed to be
112: * in the DEFAULT_PACKAGE. The properties object passed should not be null.
113: *
114: * @param className the name of the class that implements the mode.It can or
115: * cannot be with the package name.
116: * @param properties the <code>PegasusProperties</code> object containing all
117: * the properties required by Pegasus.
118: * @param options the options passed to the planner at runtime.
119: *
120: * @return the instance of the class implementing this interface.
121: *
122: * @exception TransferImplementationFactoryException that nests any error that
123: * might occur during the instantiation.
124: *
125: * @see #DEFAULT_PACKAGE_NAME
126: */
127: private static Implementation loadInstance(String className,
128: PegasusProperties properties, PlannerOptions options)
129: throws TransferImplementationFactoryException {
130:
131: Implementation implementation = null;
132: try {
133: //sanity check
134: if (properties == null) {
135: throw new RuntimeException("Invalid properties passed");
136: }
137:
138: //prepend the package name
139: className = (className.indexOf('.') == -1) ?
140: //pick up from the default package
141: DEFAULT_PACKAGE_NAME + "." + className
142: :
143: //load directly
144: className;
145:
146: //try loading the class dynamically
147: DynamicLoader dl = new DynamicLoader(className);
148: Object argList[] = new Object[2];
149: argList[0] = properties;
150: argList[1] = options;
151: implementation = (Implementation) dl.instantiate(argList);
152: } catch (Exception e) {
153: throw new TransferImplementationFactoryException(
154: "Instantiating Transfer Impelmentation ",
155: className, e);
156: }
157: return implementation;
158:
159: }
160:
161: /**
162: * Returns the name of the property that needs to be loaded for a particular
163: * type.
164: *
165: * @param type the type of implementation to be loaded.
166: *
167: * @return the name of the property
168: * @throws IllegalArgumentException
169: */
170: private static String getProperty(int type)
171: throws IllegalArgumentException {
172: String property;
173: if (type == TYPE_STAGE_IN) {
174: property = "pegasus.transfer.stagein.impl";
175: } else if (type == TYPE_STAGE_INTER) {
176: property = "pegasus.transfer.inter.impl";
177: } else if (type == TYPE_STAGE_OUT) {
178: property = "pegasus.transfer.stageout.impl";
179: } else {
180: throw new java.lang.IllegalArgumentException(
181: "Invalid implementation type passed to factory "
182: + type);
183: }
184: return property;
185: }
186:
187: }
|