2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.services
.impl
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
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
;
33 public class AgentServiceImpl
implements AgentService
, InitializingBean
,
35 private final static Log log
= LogFactory
.getLog(AgentServiceImpl
.class);
37 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
38 private final SlcAgentFactory agentFactory
;
40 private ArgeoSecurityService securityService
;
42 private Long pingCycle
= 20000l;
44 private Boolean pingThreadActive
= true;
46 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao
,
47 SlcAgentFactory agentFactory
) {
48 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
49 this.agentFactory
= agentFactory
;
52 public void register(SlcAgentDescriptor slcAgentDescriptor
) {
53 if (slcAgentDescriptorDao
.getAgentDescriptor(slcAgentDescriptor
55 slcAgentDescriptorDao
.create(slcAgentDescriptor
);
56 log
.info("Registered agent #" + slcAgentDescriptor
.getUuid());
59 public void unregister(SlcAgentDescriptor slcAgentDescriptor
) {
60 slcAgentDescriptorDao
.delete(slcAgentDescriptor
);
61 log
.info("Unregistered agent #" + slcAgentDescriptor
.getUuid());
64 public void afterPropertiesSet() throws Exception
{
66 // new PingThread().start();
68 Thread authenticatedThread
= new Thread(securityService
69 .wrapWithSystemAuthentication(new AgentsPing()),
71 authenticatedThread
.start();
76 public synchronized void destroy() throws Exception
{
77 pingThreadActive
= false;
81 public void setPingCycle(Long pingCycle
) {
82 this.pingCycle
= pingCycle
;
85 public void setSecurityService(ArgeoSecurityService securityService
) {
86 this.securityService
= securityService
;
89 protected class AgentsPing
implements Runnable
{
92 // FIXME: temporary hack so that the ping starts after the server
93 // has been properly started.
95 Thread
.sleep(10 * 1000);
96 } catch (InterruptedException e1
) {
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());
108 if (log
.isTraceEnabled())
109 log
.debug("Ping " + agentIds
.size() + " agent.");
110 for (String agentId
: agentIds
) {
111 SlcAgent agent
= agentFactory
.getAgent(agentId
);
113 log
.info("Agent " + agentId
+ " did not reply to ping,"
114 + " removing its descriptor...");
115 slcAgentDescriptorDao
.delete(agentId
);
119 lst
= slcAgentDescriptorDao
.listSlcAgentDescriptors();
120 agentIds
= new ArrayList
<String
>();
121 for (SlcAgentDescriptor ad
: lst
)
122 agentIds
.add(ad
.getUuid());
123 agentFactory
.pingAll(agentIds
);
125 synchronized (AgentServiceImpl
.this) {
127 AgentServiceImpl
.this.wait(pingCycle
);
128 } catch (InterruptedException e
) {
133 log
.info("Stopped pinging agents.");