/[xulu]/branches/1.8-gt2-2.6/src/appl/parallel/util/Helper.java
ViewVC logotype

Contents of /branches/1.8-gt2-2.6/src/appl/parallel/util/Helper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (show annotations)
Sun Oct 4 16:54:52 2009 UTC (15 years, 2 months ago) by alfonx
File size: 7187 byte(s)
* organized imports
1 package appl.parallel.util;
2
3 import java.rmi.AccessException;
4 import java.rmi.NotBoundException;
5 import java.rmi.Remote;
6 import java.rmi.RemoteException;
7 import java.rmi.registry.LocateRegistry;
8 import java.rmi.registry.Registry;
9 import java.util.Vector;
10 import java.util.concurrent.Callable;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
15
16 import org.apache.log4j.LogManager;
17 import org.apache.log4j.Logger;
18
19 import appl.parallel.ComputingResource;
20 import appl.parallel.ComputingResourceContainer;
21 import appl.parallel.ComputingResourceProperties;
22
23 /**
24 * See method description for details.
25 *
26 * @author Dominik Appl
27 */
28 public class Helper {
29 private static final Logger LOG = LogManager.getLogger("appl.parallel.util.Helper");
30
31 /**
32 * Bind the remote object's stub in the registry. Creates a registry if no running registry
33 * is found.
34 *
35 * @param bindingName the name to be used for binding (with port specified, if necessary)
36 * @param bindingInstance an instance of the type to bind to the registry
37 * @param registryPort the default registry port
38 * @throws RemoteException if something goes wrong
39 */
40 public static void bind(String bindingName, Remote bindingInstance, int registryPort) throws RemoteException{
41 boolean failed=true;
42 Registry registry;
43 try {
44 registry = LocateRegistry.getRegistry(registryPort);
45 registry.rebind(bindingName, bindingInstance);
46 failed = false;
47 } catch (AccessException e) {
48 System.err.println("Not enough permissions to bind" + bindingName +
49 "to the registry! Adjust your security permission file!");
50 LOG.error("Not enough permissions to bind" + bindingName +
51 "to the registry! Adjust your security permission file!",e);
52 e.printStackTrace();
53 } catch (RemoteException e) {
54 if(e instanceof java.rmi.ConnectException){
55 System.out.println("Could not connect to registry! Trying to create new one...");
56 try {
57 registry = LocateRegistry.createRegistry(registryPort);
58 registry.rebind(bindingName, bindingInstance);
59 System.out.println("Registry successfully created at port " + registryPort);
60 failed = false;
61 } catch (RemoteException e1) {
62 // TODO Auto-generated catch block
63 e1.printStackTrace();
64 }
65 }
66 else
67 e.printStackTrace();
68 }
69 if(failed)
70 throw new RemoteException("Could not find or create registry!");
71 }
72
73 /**
74 * Unbinds a a stub from the registry!
75 * @param bindingName
76 */
77 public static void unbind(String bindingName){
78 Registry registry;
79 try {
80 registry = LocateRegistry.getRegistry();
81 registry.unbind(bindingName);
82 LOG.debug("Sucessfully removed " + bindingName + " from reggie");
83 } catch (RemoteException e) {
84 LOG.debug("tried to unbind " + bindingName
85 + " from reggie, but an exception occured: "
86 + e.getMessage());
87 } catch (NotBoundException e) {
88 LOG.debug("tried to unbind " + bindingName
89 + " from reggie, but the name was not bound "
90 + e.getMessage());
91 }
92 }
93
94 /**
95 * Gets the remote resources from all given {@link ComputingResource} objects.
96 * For this all resources are queried. If a ressouce does not respond the
97 * Resource is removed form the Vector(!!!!!) and a warning is given to the
98 * logger.
99 *
100 * @param res
101 */
102 public static Vector<ComputingResourceContainer> getResourceContainersForVector(
103 Vector<ComputingResource> res) {
104
105 final Vector<ComputingResourceContainer> containers = new Vector<ComputingResourceContainer>();
106 ExecutorService executor = Executors.newCachedThreadPool();
107
108 //this is not a for each loop to avoid ConcurrentModificationException form threads
109
110 Future[] results = new Future[res.size()];
111 int i = res.size() - 1;
112 while (i >= 0) {
113 results[i] = executor.submit(new Helper().new SimpleConnectionThread(res,i));
114 i--;
115 }
116 //wait for the threads to finish /
117 //add the responding containers to a new Vector and return it
118 for (int j = 0; j < results.length; j++) {
119 Object result;
120 try {
121 result = results[j].get();
122 if(result!=null)
123 containers.add((ComputingResourceContainer) result);
124 } catch (InterruptedException e) {
125 // TODO Auto-generated catch block
126 e.printStackTrace();
127 } catch (ExecutionException e) {
128 // TODO Auto-generated catch block
129 e.printStackTrace();
130 }
131
132 }
133 return containers;
134 }
135
136 /**
137 * if a connection to the given resource is possible the ResourceProperties of this object
138 * are returned. Else NULL is returned.
139 *
140 * @author Dominik Appl
141 */
142 public class SimpleConnectionThread implements Callable {
143
144 private final Vector<ComputingResource> containers;
145
146 private final int position;
147
148 public SimpleConnectionThread(
149 Vector<ComputingResource> containers, int position) {
150 this.containers = containers;
151 this.position = position;
152 }
153
154 /*
155 * (non-Javadoc)
156 *
157 * @see java.util.concurrent.Callable#call()
158 */
159 public Object call() throws Exception {
160 // will remain true, if there is a connection error
161 boolean error = true;
162 ComputingResource r = (ComputingResource) containers.get(position);
163 try {
164 // throw a exeception in case of a connection error:
165 ComputingResourceProperties rp = r.getResourceInformation();
166 error = false;
167 if (LOG.isDebugEnabled())
168 LOG.debug("Getting of resInfo of <"
169 + rp.getProperty("Name") + "> was successfull!");
170 return new ComputingResourceContainer(r, rp);
171 } catch (Exception e) {
172 LOG.warn("A server was not responding .. removing ressouce!");
173 if (LOG.isDebugEnabled())
174 LOG.debug("Reason was: " + e.getMessage());
175 }
176 return null;
177 }
178 }
179
180
181 /**
182 * Calculates weights out of ratings. A rating is a value >=1. If the rating
183 * is 0 an average rating is assumed. This method weights the ratings
184 * relative to each other so that a weight is a double value between 0 and
185 * 1 and the sum of all calculated weights is 1.
186 *
187 * @param ratings
188 */
189 public static double[] calculateWeights(int ratings[]){
190 // values with 0 are weightes average. Add all values and count the
191 // values ==0
192 double sum = 0;
193 double nulls = 0;
194 for (int i : ratings) {
195 sum += i;
196 if (i == 0)
197 nulls++;
198 }
199 // for each null add the average
200 double average = (sum / (ratings.length - nulls));
201 // if there are ONLY nulls define the average seperately
202 if (nulls == ratings.length)
203 average = 1000.0 / ratings.length;
204 sum += average * nulls;
205
206 // calculate weights
207 double[] weights = new double[ratings.length];
208 for (int i = 0; i < weights.length; i++) {
209 if (ratings[i] == 0)
210 ratings[i] = (int) average;
211 weights[i] = ratings[i] / sum;
212 }
213
214 if (true == true) {
215 String weightString = "Calculated weights: ";
216 for (double d : weights) {
217 weightString += (d + " ");
218 }
219 LOG.debug("Calculated weights: + " + weightString);
220 System.out.println(weightString);
221 }
222 return weights;
223
224
225
226 }
227
228 }
229
230

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26