2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.Arrays
;
20 import java
.util
.List
;
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
;
30 import org
.argeo
.jcr
.JcrUtils
;
31 import org
.argeo
.slc
.SlcException
;
33 /** Repository backend, maintain the JCR repository, mainly through listeners */
34 public class RepoImpl
{
36 private Repository jcrRepository
;
37 private Session adminSession
;
39 private ArtifactListener artifactListener
;
41 /** order may be important */
42 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
46 adminSession
= jcrRepository
.login();
47 artifactListener
= new ArtifactListener();
50 .getObservationManager()
51 .addEventListener(artifactListener
, Event
.NODE_ADDED
,
52 RepoConstants
.ARTIFACTS_BASE_PATH
, true, null,
55 String localrepoWorkspace
= "localrepo";
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
);
65 public void destroy() {
66 JcrUtils
.logoutQuietly(adminSession
);
69 public void setJcrRepository(Repository jcrRepository
) {
70 this.jcrRepository
= jcrRepository
;
73 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
74 this.nodeIndexers
= nodeIndexers
;
77 class ArtifactListener
implements EventListener
{
79 public void onEvent(EventIterator events
) {
80 while (events
.hasNext()) {
81 Event event
= events
.nextEvent();
83 String newNodePath
= event
.getPath();
85 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
87 if (nodeIndexer
.support(newNodePath
)) {
89 newNode
= adminSession
.getNode(newNodePath
);
90 nodeIndexer
.index(newNode
);
92 } catch (RuntimeException e
) {
99 } catch (RepositoryException e
) {
100 throw new SlcException("Cannot process event " + event
, e
);
102 JcrUtils
.discardQuietly(adminSession
);