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
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.core
.execution
.DefaultAgent
;
30 import org
.argeo
.slc
.core
.execution
.ProcessThread
;
31 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
32 import org
.argeo
.slc
.execution
.ExecutionProcess
;
33 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
34 import org
.argeo
.slc
.jcr
.SlcNames
;
35 import org
.argeo
.slc
.jcr
.SlcTypes
;
37 /** SLC VM agent synchronizing with a JCR repository. */
38 public class JcrAgent
extends DefaultAgent
implements SlcNames
{
39 // final static String ROLE_REMOTE = "ROLE_REMOTE";
40 final static String NODE_REPO_URI
= "argeo.node.repo.uri";
42 private Repository repository
;
44 private String agentNodeName
= "default";
49 protected String
initAgentUuid() {
50 Session session
= null;
52 session
= repository
.login();
54 String agentFactoryPath
= getAgentFactoryPath();
55 Node vmAgentFactoryNode
= JcrUtils
.mkdirsSafe(session
,
56 agentFactoryPath
, SlcTypes
.SLC_AGENT_FACTORY
);
57 if (!vmAgentFactoryNode
.hasNode(agentNodeName
)) {
58 String uuid
= UUID
.randomUUID().toString();
59 Node agentNode
= vmAgentFactoryNode
.addNode(agentNodeName
,
61 agentNode
.setProperty(SLC_UUID
, uuid
);
64 return vmAgentFactoryNode
.getNode(agentNodeName
)
65 .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(
84 ThreadGroup processesThreadGroup
,
85 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
86 if (process
instanceof JcrExecutionProcess
)
87 return new JcrProcessThread(processesThreadGroup
, modulesManager
,
88 (JcrExecutionProcess
) process
);
90 return super.createProcessThread(processesThreadGroup
,
91 modulesManager
, process
);
97 public String
getNodePath() {
98 return getAgentFactoryPath() + '/' + getAgentNodeName();
101 public String
getAgentFactoryPath() {
103 Boolean isRemote
= System
.getProperty(NODE_REPO_URI
) != null;
104 String agentFactoryPath
;
106 InetAddress localhost
= InetAddress
.getLocalHost();
107 agentFactoryPath
= SlcJcrConstants
.AGENTS_BASE_PATH
+ "/"
108 + localhost
.getCanonicalHostName();
111 .equals(SlcJcrConstants
.VM_AGENT_FACTORY_PATH
))
112 throw new SlcException("Unsupported hostname "
113 + localhost
.getCanonicalHostName());
115 agentFactoryPath
= SlcJcrConstants
.VM_AGENT_FACTORY_PATH
;
117 return agentFactoryPath
;
118 } catch (UnknownHostException e
) {
119 throw new SlcException("Cannot find agent factory base path", e
);
126 public String
getAgentNodeName() {
127 return agentNodeName
;
130 public void setRepository(Repository repository
) {
131 this.repository
= repository
;
134 public void setAgentNodeName(String agentNodeName
) {
135 this.agentNodeName
= agentNodeName
;