]> 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
Refactor JCR utils and home usage
[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 Mathieu Baudier
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.util.List;
19 import java.util.UUID;
20
21 import javax.jcr.Node;
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25
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;
37
38 /** SLC VM agent synchronizing with a JCR repository. */
39 public class JcrAgent extends DefaultAgent implements SlcAgentFactory, SlcNames {
40 private Repository repository;
41
42 /** only one agent per VM is currently supported */
43 private final String agentNodeName = "default";
44
45 /*
46 * LIFECYCLE
47 */
48 protected String initAgentUuid() {
49 Session session = null;
50 try {
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,
58 SlcTypes.SLC_AGENT);
59 agentNode.setProperty(SLC_UUID, uuid);
60 }
61 session.save();
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);
67 } finally {
68 JcrUtils.logoutQuietly(session);
69 }
70 }
71
72 @Override
73 public void destroy() {
74 super.destroy();
75 }
76
77 /*
78 * SLC AGENT
79 */
80 @Override
81 protected ProcessThread createProcessThread(
82 ThreadGroup processesThreadGroup,
83 ExecutionModulesManager modulesManager, ExecutionProcess process) {
84 return new JcrProcessThread(processesThreadGroup, modulesManager,
85 (JcrExecutionProcess) process);
86 }
87
88 /*
89 * SLC AGENT FACTORY
90 */
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);
95 return this;
96 }
97
98 public void pingAll(List<String> activeAgentIds) {
99 ping();
100 }
101
102 /*
103 * UTILITIES
104 */
105 public String getNodePath() {
106 return SlcJcrConstants.VM_AGENT_FACTORY_PATH + '/' + getAgentNodeName();
107 }
108
109 /*
110 * BEAN
111 */
112 public String getAgentNodeName() {
113 return agentNodeName;
114 }
115
116 public void setRepository(Repository repository) {
117 this.repository = repository;
118 }
119
120 }