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