2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.jcr
.execution
;
18 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.Repository
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.core
.execution
.ProcessThread
;
29 import org
.argeo
.slc
.core
.runtime
.DefaultAgent
;
30 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
31 import org
.argeo
.slc
.execution
.ExecutionProcess
;
32 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
33 import org
.argeo
.slc
.jcr
.SlcNames
;
34 import org
.argeo
.slc
.jcr
.SlcTypes
;
35 import org
.argeo
.slc
.runtime
.SlcAgent
;
36 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
38 /** SLC VM agent synchronizing with a JCR repository. */
39 public class JcrAgent
extends DefaultAgent
implements SlcAgentFactory
, SlcNames
{
40 private Repository repository
;
42 /** only one agent per VM is currently supported */
43 private final String agentNodeName
= "default";
48 protected String
initAgentUuid() {
49 Session session
= null;
51 session
= repository
.login();
52 Node vmAgentFactoryNode
= JcrUtils
.mkdirsSafe(session
,
53 SlcJcrConstants
.VM_AGENT_FACTORY_PATH
,
54 SlcTypes
.SLC_AGENT_FACTORY
);
55 if (!vmAgentFactoryNode
.hasNode(agentNodeName
)) {
56 String uuid
= UUID
.randomUUID().toString();
57 Node agentNode
= vmAgentFactoryNode
.addNode(agentNodeName
,
59 agentNode
.setProperty(SLC_UUID
, uuid
);
62 return vmAgentFactoryNode
.getNode(agentNodeName
)
63 .getProperty(SLC_UUID
).getString();
64 } catch (RepositoryException e
) {
65 JcrUtils
.discardQuietly(session
);
66 throw new SlcException("Cannot find JCR agent UUID", e
);
68 JcrUtils
.logoutQuietly(session
);
73 public void destroy() {
81 protected ProcessThread
createProcessThread(
82 ThreadGroup processesThreadGroup
,
83 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
84 return new JcrProcessThread(processesThreadGroup
, modulesManager
,
85 (JcrExecutionProcess
) process
);
91 public SlcAgent
getAgent(String uuid
) {
92 if (!uuid
.equals(getAgentUuid()))
93 throw new SlcException("Internal UUID " + getAgentUuid()
94 + " is different from argument UUID " + uuid
);
98 public void pingAll(List
<String
> activeAgentIds
) {
105 public String
getNodePath() {
106 return SlcJcrConstants
.VM_AGENT_FACTORY_PATH
+ '/' + getAgentNodeName();
112 public String
getAgentNodeName() {
113 return agentNodeName
;
116 public void setRepository(Repository repository
) {
117 this.repository
= repository
;