2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.repo
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
;
29 import org
.argeo
.jcr
.JcrUtils
;
30 import org
.argeo
.slc
.SlcException
;
32 /** Repository backend, maintain the JCR repository, mainly through listeners */
33 public class RepoIndexer
{
35 private Repository jcrRepository
;
36 private String workspace
= null;
39 private Session adminSession
;
40 private FilesListener artifactListener
;
42 /** order may be important */
43 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
47 adminSession
= jcrRepository
.login(workspace
);
48 artifactListener
= new FilesListener();
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
);
59 public void destroy() {
60 JcrUtils
.logoutQuietly(adminSession
);
63 public void setJcrRepository(Repository jcrRepository
) {
64 this.jcrRepository
= jcrRepository
;
67 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
68 this.nodeIndexers
= nodeIndexers
;
71 public void setWorkspace(String workspace
) {
72 this.workspace
= workspace
;
75 class FilesListener
implements EventListener
{
77 public void onEvent(EventIterator events
) {
78 while (events
.hasNext()) {
79 Event event
= events
.nextEvent();
81 String newNodePath
= event
.getPath();
83 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
85 if (nodeIndexer
.support(newNodePath
)) {
87 newNode
= adminSession
.getNode(newNodePath
);
88 nodeIndexer
.index(newNode
);
90 } catch (RuntimeException e
) {
97 } catch (RepositoryException e
) {
98 throw new SlcException("Cannot process event " + event
, e
);
100 JcrUtils
.discardQuietly(adminSession
);