]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.jcr/src/org/argeo/slc/jcr/execution/JcrAgent.java
Adapt to changes in Argeo Commons.
[gpl/argeo-slc.git] / org.argeo.slc.jcr / src / org / argeo / slc / jcr / execution / JcrAgent.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.jcr.execution;
17
18 import java.net.InetAddress;
19 import java.net.UnknownHostException;
20 import java.util.UUID;
21
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;
27
28 import org.argeo.jcr.JcrUtils;
29 import org.argeo.slc.SlcConstants;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.SlcNames;
32 import org.argeo.slc.SlcTypes;
33 import org.argeo.slc.runtime.DefaultAgent;
34 import org.argeo.slc.execution.ExecutionModulesManager;
35 import org.argeo.slc.execution.ExecutionProcess;
36 import org.argeo.slc.jcr.SlcJcrConstants;
37 import org.argeo.slc.runtime.ProcessThread;
38
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";
43
44 private Repository repository;
45
46 private String agentNodeName = "default";
47
48 /*
49 * LIFECYCLE
50 */
51 protected String initAgentUuid() {
52 Session session = null;
53 try {
54 session = repository.login();
55
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);
63 }
64 session.save();
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);
69 } finally {
70 JcrUtils.logoutQuietly(session);
71 }
72 }
73
74 @Override
75 public void destroy() {
76 super.destroy();
77 }
78
79 /*
80 * SLC AGENT
81 */
82 @Override
83 protected ProcessThread createProcessThread(ThreadGroup processesThreadGroup,
84 ExecutionModulesManager modulesManager, ExecutionProcess process) {
85 if (process instanceof JcrExecutionProcess)
86 return new JcrProcessThread(processesThreadGroup, modulesManager, (JcrExecutionProcess) process);
87 else
88 return super.createProcessThread(processesThreadGroup, modulesManager, process);
89 }
90
91 /*
92 * UTILITIES
93 */
94 public String getNodePath() {
95 return getAgentFactoryPath() + '/' + getAgentNodeName();
96 }
97
98 public String getAgentFactoryPath() {
99 try {
100 Boolean isRemote = System.getProperty(NODE_REPO_URI) != null;
101 String agentFactoryPath;
102 if (isRemote) {
103 InetAddress localhost = InetAddress.getLocalHost();
104 agentFactoryPath = SlcJcrConstants.AGENTS_BASE_PATH + "/" + localhost.getCanonicalHostName();
105
106 if (agentFactoryPath.equals(SlcJcrConstants.VM_AGENT_FACTORY_PATH))
107 throw new SlcException("Unsupported hostname " + localhost.getCanonicalHostName());
108 } else {// local
109 agentFactoryPath = SlcJcrConstants.VM_AGENT_FACTORY_PATH;
110 }
111 return agentFactoryPath;
112 } catch (UnknownHostException e) {
113 throw new SlcException("Cannot find agent factory base path", e);
114 }
115 }
116
117 /*
118 * BEAN
119 */
120 public String getAgentNodeName() {
121 return agentNodeName;
122 }
123
124 public void setRepository(Repository repository) {
125 this.repository = repository;
126 }
127
128 public void setAgentNodeName(String agentNodeName) {
129 this.agentNodeName = agentNodeName;
130 }
131 }