1 |
package appl.parallel.services; |
2 |
|
3 |
import java.util.Iterator; |
4 |
import java.util.Vector; |
5 |
|
6 |
import org.apache.log4j.LogManager; |
7 |
import org.apache.log4j.Logger; |
8 |
|
9 |
import appl.ext.XuluConfig; |
10 |
import appl.parallel.ComputingResource; |
11 |
import appl.parallel.ComputingResourceContainer; |
12 |
|
13 |
/** |
14 |
* This class is responsible for discovering Resources. For this it first starts |
15 |
* all known {@link DiscoveryService DiscoveryServices} and queries them for |
16 |
* known {@link ComputingResource Computing Resources}. <br> |
17 |
* <br> |
18 |
* This class starts all {@link DiscoveryService}s registered with the following |
19 |
* property in the {@link XuluConfig}: <br> |
20 |
* <br> |
21 |
* DiscoveryServices.activeServices<br> |
22 |
* <br> |
23 |
* Register {@link DiscoveryService DiscoveryServices} with their fully |
24 |
* qualified java name because |
25 |
* {@link java.lang.Class#forName(String) Class.forName} will be called on that |
26 |
* String. |
27 |
* |
28 |
* @author Dominik Appl |
29 |
*/ |
30 |
public class GlobalDiscoveryService implements Service { |
31 |
|
32 |
private final Logger LOG = LogManager.getLogger(this.getClass().getName()); |
33 |
|
34 |
private static GlobalDiscoveryService instance; |
35 |
|
36 |
Vector<DiscoveryService> services = new Vector<DiscoveryService>(); |
37 |
|
38 |
private boolean isRunning; |
39 |
|
40 |
/** |
41 |
* @return all active {@link ComputingResource RemoteResources} in a |
42 |
* container class with meta in the a ComputingResourceContainer |
43 |
* Object. They are queried from all known Discovery Services. Do |
44 |
* not query more than necessary! |
45 |
*/ |
46 |
public Vector<ComputingResourceContainer> getRemoteResources() { |
47 |
Vector<ComputingResourceContainer> discResources = new Vector<ComputingResourceContainer>(); |
48 |
// query all services for RemoteResources |
49 |
for (DiscoveryService disc : services) { |
50 |
Vector<ComputingResourceContainer> newResources = disc |
51 |
.getResources(); |
52 |
// do not add double entrys |
53 |
for (ComputingResourceContainer container : newResources) { |
54 |
if (!(checkAlreadyInside(container, discResources))) |
55 |
discResources.add(container); |
56 |
} |
57 |
} |
58 |
return discResources; |
59 |
} |
60 |
|
61 |
/** |
62 |
* checks if the a remoteResource of a given container is in a Vector of |
63 |
* RemoteResourceContainers |
64 |
* |
65 |
* @param container |
66 |
* @return |
67 |
*/ |
68 |
private boolean checkAlreadyInside(ComputingResourceContainer container, |
69 |
Vector<ComputingResourceContainer> containers) { |
70 |
for (ComputingResourceContainer originalcontainer : containers) { |
71 |
if (originalcontainer.getResource().equals(container.getResource())) |
72 |
return true; |
73 |
} |
74 |
return false; |
75 |
} |
76 |
|
77 |
/** |
78 |
* Looks up all services from the config file, loads them and also starts |
79 |
* them. |
80 |
*/ |
81 |
public GlobalDiscoveryService() { |
82 |
String[] classnames = XuluConfig.getXuluConfig().getMultiProperty( |
83 |
"DiscoveryServices.activeServices"); |
84 |
for (String classname : classnames) { |
85 |
Class serviceclass; |
86 |
try { |
87 |
serviceclass = Class.forName(classname); |
88 |
DiscoveryService service = (DiscoveryService) serviceclass |
89 |
.newInstance(); |
90 |
services.add(service); |
91 |
service.startService(); |
92 |
|
93 |
} catch (ClassNotFoundException e) { |
94 |
LOG.error("Class was not found for string" + classname); |
95 |
LOG.error(e.getMessage()); |
96 |
} catch (InstantiationException e) { |
97 |
LOG.error("Could not load class " + classname); |
98 |
LOG.error(e.getMessage()); |
99 |
} catch (IllegalAccessException e) { |
100 |
LOG.error("Could not load class " + classname); |
101 |
LOG.error(e.getMessage()); |
102 |
} |
103 |
} |
104 |
// allow some time for the services to discover Resources |
105 |
try { |
106 |
Thread.currentThread().sleep(200); |
107 |
} catch (InterruptedException e) { |
108 |
// TODO Auto-generated catch block |
109 |
e.printStackTrace(); |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
/* |
115 |
* (non-Javadoc) |
116 |
* |
117 |
* @see appl.parallel.services.Service#isRunning() |
118 |
*/ |
119 |
public boolean isRunning() { |
120 |
return isRunning; |
121 |
} |
122 |
|
123 |
/* |
124 |
* (non-Javadoc) |
125 |
* |
126 |
* @see appl.parallel.services.Service#startService() |
127 |
*/ |
128 |
public void startService() { |
129 |
isRunning = true; |
130 |
|
131 |
} |
132 |
|
133 |
/* |
134 |
* (non-Javadoc) |
135 |
* |
136 |
* @see appl.parallel.services.Service#stopService() |
137 |
*/ |
138 |
public void stopService() { |
139 |
for (DiscoveryService s : services) { |
140 |
s.stopService(); |
141 |
} |
142 |
isRunning = false; |
143 |
|
144 |
} |
145 |
|
146 |
} |