]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrAgent.java
Work in progress - work on modular distributions.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / 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
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;
36
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";
41
42 private Repository repository;
43
44 private String agentNodeName = "default";
45
46 /*
47 * LIFECYCLE
48 */
49 protected String initAgentUuid() {
50 Session session = null;
51 try {
52 session = repository.login();
53
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,
60 SlcTypes.SLC_AGENT);
61 agentNode.setProperty(SLC_UUID, uuid);
62 }
63 session.save();
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);
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(
84 ThreadGroup processesThreadGroup,
85 ExecutionModulesManager modulesManager, ExecutionProcess process) {
86 if (process instanceof JcrExecutionProcess)
87 return new JcrProcessThread(processesThreadGroup, modulesManager,
88 (JcrExecutionProcess) process);
89 else
90 return super.createProcessThread(processesThreadGroup,
91 modulesManager, process);
92 }
93
94 /*
95 * UTILITIES
96 */
97 public String getNodePath() {
98 return getAgentFactoryPath() + '/' + getAgentNodeName();
99 }
100
101 public String getAgentFactoryPath() {
102 try {
103 Boolean isRemote = System.getProperty(NODE_REPO_URI) != null;
104 String agentFactoryPath;
105 if (isRemote) {
106 InetAddress localhost = InetAddress.getLocalHost();
107 agentFactoryPath = SlcJcrConstants.AGENTS_BASE_PATH + "/"
108 + localhost.getCanonicalHostName();
109
110 if (agentFactoryPath
111 .equals(SlcJcrConstants.VM_AGENT_FACTORY_PATH))
112 throw new SlcException("Unsupported hostname "
113 + localhost.getCanonicalHostName());
114 } else {// local
115 agentFactoryPath = SlcJcrConstants.VM_AGENT_FACTORY_PATH;
116 }
117 return agentFactoryPath;
118 } catch (UnknownHostException e) {
119 throw new SlcException("Cannot find agent factory base path", e);
120 }
121 }
122
123 /*
124 * BEAN
125 */
126 public String getAgentNodeName() {
127 return agentNodeName;
128 }
129
130 public void setRepository(Repository repository) {
131 this.repository = repository;
132 }
133
134 public void setAgentNodeName(String agentNodeName) {
135 this.agentNodeName = agentNodeName;
136 }
137 }