/[xulu]/trunk.bakup/src/appl/parallel/util/Helper.java
ViewVC logotype

Annotation of /trunk.bakup/src/appl/parallel/util/Helper.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26