2 * Copyright (C) 2007-2012 Argeo GmbH
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
.net
.InetAddress
;
19 import java
.net
.UnknownHostException
;
20 import java
.util
.UUID
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.Session
;
26 import javax
.jcr
.security
.Privilege
;
28 import org
.argeo
.jcr
.JcrUtils
;
29 import org
.argeo
.slc
.SlcConstants
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.core
.execution
.DefaultAgent
;
32 import org
.argeo
.slc
.core
.execution
.ProcessThread
;
33 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
34 import org
.argeo
.slc
.execution
.ExecutionProcess
;
35 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
36 import org
.argeo
.slc
.jcr
.SlcNames
;
37 import org
.argeo
.slc
.jcr
.SlcTypes
;
39 /** SLC VM agent synchronizing with a JCR repository. */
40 public class JcrAgent
extends DefaultAgent
implements SlcNames
{
41 // final static String ROLE_REMOTE = "ROLE_REMOTE";
42 final static String NODE_REPO_URI
= "argeo.node.repo.uri";
44 private Repository repository
;
46 private String agentNodeName
= "default";
51 protected String
initAgentUuid() {
52 Session session
= null;
54 session
= repository
.login();
56 String agentFactoryPath
= getAgentFactoryPath();
57 Node vmAgentFactoryNode
= JcrUtils
.mkdirsSafe(session
, agentFactoryPath
, SlcTypes
.SLC_AGENT_FACTORY
);
58 JcrUtils
.addPrivilege(session
, SlcJcrConstants
.SLC_BASE_PATH
, SlcConstants
.ROLE_SLC
, Privilege
.JCR_ALL
);
59 if (!vmAgentFactoryNode
.hasNode(agentNodeName
)) {
60 String uuid
= UUID
.randomUUID().toString();
61 Node agentNode
= vmAgentFactoryNode
.addNode(agentNodeName
, SlcTypes
.SLC_AGENT
);
62 agentNode
.setProperty(SLC_UUID
, uuid
);
65 return vmAgentFactoryNode
.getNode(agentNodeName
).getProperty(SLC_UUID
).getString();
66 } catch (RepositoryException e
) {
67 JcrUtils
.discardQuietly(session
);
68 throw new SlcException("Cannot find JCR agent UUID", e
);
70 JcrUtils
.logoutQuietly(session
);
75 public void destroy() {
83 protected ProcessThread
createProcessThread(ThreadGroup processesThreadGroup
,
84 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
85 if (process
instanceof JcrExecutionProcess
)
86 return new JcrProcessThread(processesThreadGroup
, modulesManager
, (JcrExecutionProcess
) process
);
88 return super.createProcessThread(processesThreadGroup
, modulesManager
, process
);
94 public String
getNodePath() {
95 return getAgentFactoryPath() + '/' + getAgentNodeName();
98 public String
getAgentFactoryPath() {
100 Boolean isRemote
= System
.getProperty(NODE_REPO_URI
) != null;
101 String agentFactoryPath
;
103 InetAddress localhost
= InetAddress
.getLocalHost();
104 agentFactoryPath
= SlcJcrConstants
.AGENTS_BASE_PATH
+ "/" + localhost
.getCanonicalHostName();
106 if (agentFactoryPath
.equals(SlcJcrConstants
.VM_AGENT_FACTORY_PATH
))
107 throw new SlcException("Unsupported hostname " + localhost
.getCanonicalHostName());
109 agentFactoryPath
= SlcJcrConstants
.VM_AGENT_FACTORY_PATH
;
111 return agentFactoryPath
;
112 } catch (UnknownHostException e
) {
113 throw new SlcException("Cannot find agent factory base path", e
);
120 public String
getAgentNodeName() {
121 return agentNodeName
;
124 public void setRepository(Repository repository
) {
125 this.repository
= repository
;
128 public void setAgentNodeName(String agentNodeName
) {
129 this.agentNodeName
= agentNodeName
;