]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/AgentServiceImpl.java
remove a warning
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / services / impl / AgentServiceImpl.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.services.impl;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.security.ArgeoSecurityService;
25 import org.argeo.slc.dao.runtime.SlcAgentDescriptorDao;
26 import org.argeo.slc.runtime.SlcAgent;
27 import org.argeo.slc.runtime.SlcAgentDescriptor;
28 import org.argeo.slc.runtime.SlcAgentFactory;
29 import org.argeo.slc.services.AgentService;
30 import org.springframework.beans.factory.DisposableBean;
31 import org.springframework.beans.factory.InitializingBean;
32
33 public class AgentServiceImpl implements AgentService, InitializingBean,
34 DisposableBean {
35 private final static Log log = LogFactory.getLog(AgentServiceImpl.class);
36
37 private final SlcAgentDescriptorDao slcAgentDescriptorDao;
38 private final SlcAgentFactory agentFactory;
39
40 private ArgeoSecurityService securityService;
41
42 private Long pingCycle = 20000l;
43
44 private Boolean pingThreadActive = true;
45
46 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao,
47 SlcAgentFactory agentFactory) {
48 this.slcAgentDescriptorDao = slcAgentDescriptorDao;
49 this.agentFactory = agentFactory;
50 }
51
52 public void register(SlcAgentDescriptor slcAgentDescriptor) {
53 if (slcAgentDescriptorDao.getAgentDescriptor(slcAgentDescriptor
54 .getUuid()) == null)
55 slcAgentDescriptorDao.create(slcAgentDescriptor);
56 log.info("Registered agent #" + slcAgentDescriptor.getUuid());
57 }
58
59 public void unregister(SlcAgentDescriptor slcAgentDescriptor) {
60 slcAgentDescriptorDao.delete(slcAgentDescriptor);
61 log.info("Unregistered agent #" + slcAgentDescriptor.getUuid());
62 }
63
64 public void afterPropertiesSet() throws Exception {
65 // if (pingCycle > 0)
66 // new PingThread().start();
67 if (pingCycle > 0) {
68 Thread authenticatedThread = new Thread(securityService
69 .wrapWithSystemAuthentication(new AgentsPing()),
70 "SLC Agents Ping");
71 authenticatedThread.start();
72
73 }
74 }
75
76 public synchronized void destroy() throws Exception {
77 pingThreadActive = false;
78 notifyAll();
79 }
80
81 public void setPingCycle(Long pingCycle) {
82 this.pingCycle = pingCycle;
83 }
84
85 public void setSecurityService(ArgeoSecurityService securityService) {
86 this.securityService = securityService;
87 }
88
89 protected class AgentsPing implements Runnable {
90 public void run() {
91
92 // FIXME: temporary hack so that the ping starts after the server
93 // has been properly started.
94 try {
95 Thread.sleep(10 * 1000);
96 } catch (InterruptedException e1) {
97 // silent
98 }
99
100 log.info("Start pinging agents.");
101 while (pingThreadActive) {
102 List<SlcAgentDescriptor> lst = slcAgentDescriptorDao
103 .listSlcAgentDescriptors();
104 List<String> agentIds = new ArrayList<String>();
105 for (SlcAgentDescriptor ad : lst)
106 agentIds.add(ad.getUuid());
107
108 if (log.isTraceEnabled())
109 log.debug("Ping " + agentIds.size() + " agent.");
110 for (String agentId : agentIds) {
111 SlcAgent agent = agentFactory.getAgent(agentId);
112 if (!agent.ping()) {
113 log.info("Agent " + agentId + " did not reply to ping,"
114 + " removing its descriptor...");
115 slcAgentDescriptorDao.delete(agentId);
116 }
117 }
118
119 lst = slcAgentDescriptorDao.listSlcAgentDescriptors();
120 agentIds = new ArrayList<String>();
121 for (SlcAgentDescriptor ad : lst)
122 agentIds.add(ad.getUuid());
123 agentFactory.pingAll(agentIds);
124
125 synchronized (AgentServiceImpl.this) {
126 try {
127 AgentServiceImpl.this.wait(pingCycle);
128 } catch (InterruptedException e) {
129 // silent
130 }
131 }
132 }
133 log.info("Stopped pinging agents.");
134 }
135
136 }
137
138 }