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
;
21 import java
.util
.concurrent
.Executor
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.security
.ArgeoSecurityService
;
26 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
27 import org
.argeo
.slc
.runtime
.SlcAgent
;
28 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
29 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
30 import org
.argeo
.slc
.services
.AgentService
;
31 import org
.springframework
.beans
.factory
.DisposableBean
;
32 import org
.springframework
.beans
.factory
.InitializingBean
;
34 public class AgentServiceImpl
implements AgentService
, InitializingBean
,
36 private final static Log log
= LogFactory
.getLog(AgentServiceImpl
.class);
38 private final SlcAgentDescriptorDao slcAgentDescriptorDao
;
39 private final SlcAgentFactory agentFactory
;
41 private Executor securityService
;
43 private Long pingCycle
= 20000l;
45 private Boolean pingThreadActive
= true;
47 public AgentServiceImpl(SlcAgentDescriptorDao slcAgentDescriptorDao
,
48 SlcAgentFactory agentFactory
) {
49 this.slcAgentDescriptorDao
= slcAgentDescriptorDao
;
50 this.agentFactory
= agentFactory
;
53 public void register(SlcAgentDescriptor slcAgentDescriptor
) {
54 if (slcAgentDescriptorDao
.getAgentDescriptor(slcAgentDescriptor
56 slcAgentDescriptorDao
.create(slcAgentDescriptor
);
57 log
.info("Registered agent #" + slcAgentDescriptor
.getUuid());
60 public void unregister(SlcAgentDescriptor slcAgentDescriptor
) {
61 slcAgentDescriptorDao
.delete(slcAgentDescriptor
);
62 log
.info("Unregistered agent #" + slcAgentDescriptor
.getUuid());
65 public void afterPropertiesSet() throws Exception
{
67 // new PingThread().start();
69 Thread authenticatedThread
= new Thread("SLC Agents Ping") {
71 securityService
.execute(new AgentsPing());
74 authenticatedThread
.start();
79 public synchronized void destroy() throws Exception
{
80 pingThreadActive
= false;
84 public void setPingCycle(Long pingCycle
) {
85 this.pingCycle
= pingCycle
;
88 public void setSecurityService(Executor securityService
) {
89 this.securityService
= securityService
;
92 protected class AgentsPing
implements Runnable
{
95 // FIXME: temporary hack so that the ping starts after the server
96 // has been properly started.
98 Thread
.sleep(10 * 1000);
99 } catch (InterruptedException e1
) {
103 log
.info("Start pinging agents.");
104 while (pingThreadActive
) {
105 List
<SlcAgentDescriptor
> lst
= slcAgentDescriptorDao
106 .listSlcAgentDescriptors();
107 List
<String
> agentIds
= new ArrayList
<String
>();
108 for (SlcAgentDescriptor ad
: lst
)
109 agentIds
.add(ad
.getUuid());
111 if (log
.isTraceEnabled())
112 log
.trace("Ping " + agentIds
.size() + " agent(s).");
113 for (String agentId
: agentIds
) {
114 SlcAgent agent
= agentFactory
.getAgent(agentId
);
116 log
.info("Agent " + agentId
+ " did not reply to ping,"
117 + " removing its descriptor...");
118 slcAgentDescriptorDao
.delete(agentId
);
122 lst
= slcAgentDescriptorDao
.listSlcAgentDescriptors();
123 agentIds
= new ArrayList
<String
>();
124 for (SlcAgentDescriptor ad
: lst
)
125 agentIds
.add(ad
.getUuid());
126 agentFactory
.pingAll(agentIds
);
128 synchronized (AgentServiceImpl
.this) {
130 AgentServiceImpl
.this.wait(pingCycle
);
131 } catch (InterruptedException e
) {
136 log
.info("Stopped pinging agents.");