]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoImpl.java
Make import more robust
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / RepoImpl.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.repo;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
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.observation.Event;
27 import javax.jcr.observation.EventIterator;
28 import javax.jcr.observation.EventListener;
29
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.slc.SlcException;
32
33 /** Repository backend, maintain the JCR repository, mainly through listeners */
34 public class RepoImpl {
35
36 private Repository jcrRepository;
37 private Session adminSession;
38
39 private ArtifactListener artifactListener;
40
41 /** order may be important */
42 private List<NodeIndexer> nodeIndexers = new ArrayList<NodeIndexer>();
43
44 public void init() {
45 try {
46 adminSession = jcrRepository.login();
47 artifactListener = new ArtifactListener();
48 adminSession
49 .getWorkspace()
50 .getObservationManager()
51 .addEventListener(artifactListener, Event.NODE_ADDED,
52 RepoConstants.ARTIFACTS_BASE_PATH, true, null,
53 null, true);
54 // localrepo
55 String localrepoWorkspace = "localrepo";
56 if (!Arrays.asList(
57 adminSession.getWorkspace().getAccessibleWorkspaceNames())
58 .contains(localrepoWorkspace))
59 adminSession.getWorkspace().createWorkspace(localrepoWorkspace);
60 } catch (RepositoryException e) {
61 throw new SlcException("Cannot initialize repository backend", e);
62 }
63 }
64
65 public void destroy() {
66 JcrUtils.logoutQuietly(adminSession);
67 }
68
69 public void setJcrRepository(Repository jcrRepository) {
70 this.jcrRepository = jcrRepository;
71 }
72
73 public void setNodeIndexers(List<NodeIndexer> nodeIndexers) {
74 this.nodeIndexers = nodeIndexers;
75 }
76
77 class ArtifactListener implements EventListener {
78
79 public void onEvent(EventIterator events) {
80 while (events.hasNext()) {
81 Event event = events.nextEvent();
82 try {
83 String newNodePath = event.getPath();
84 Node newNode = null;
85 for (NodeIndexer nodeIndexer : nodeIndexers) {
86 try {
87 if (nodeIndexer.support(newNodePath)) {
88 if (newNode == null)
89 newNode = adminSession.getNode(newNodePath);
90 nodeIndexer.index(newNode);
91 }
92 } catch (RuntimeException e) {
93 e.printStackTrace();
94 throw e;
95 }
96 }
97 if (newNode != null)
98 adminSession.save();
99 } catch (RepositoryException e) {
100 throw new SlcException("Cannot process event " + event, e);
101 } finally {
102 JcrUtils.discardQuietly(adminSession);
103 }
104 }
105
106 }
107
108 }
109
110 }