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