1 package org.apache.archiva.metadata.repository.jcr;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.archiva.metadata.repository.MetadataResolver;
23 import org.apache.archiva.metadata.repository.MetadataSessionException;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Session;
30
31 /**
32 *
33 * Session implementation for a JCR repository.
34 *
35 * @author Martin Stockhammer <martin_s@apache.org>
36 */
37 public class JcrRepositorySession extends RepositorySession implements AutoCloseable
38 {
39
40 private static final Logger log = LoggerFactory.getLogger( JcrRepositorySession.class );
41
42 private Session jcrSession;
43 private JcrMetadataRepository repository;
44
45 public JcrRepositorySession( JcrMetadataRepository metadataRepository, MetadataResolver resolver) throws RepositoryException
46 {
47 super( metadataRepository, resolver );
48 this.repository = metadataRepository;
49 this.jcrSession = metadataRepository.login();
50 }
51
52 public Session getJcrSession() {
53 return jcrSession;
54 }
55
56 public JcrMetadataRepository getJcrRepository() {
57 return repository;
58 }
59
60 @Override
61 public void close( )
62 {
63 super.close( );
64 jcrSession.logout();
65 }
66
67 @Override
68 protected boolean isDirty( )
69 {
70 if (super.isDirty()) {
71 return true;
72 }
73 try
74 {
75 return jcrSession.hasPendingChanges( );
76 }
77 catch ( RepositoryException e )
78 {
79 log.error( "Could not check pending changes {}", e.getMessage( ) );
80 return true;
81 }
82 }
83
84 @Override
85 public void save( ) throws MetadataSessionException
86 {
87 super.save( );
88 try
89 {
90 jcrSession.save();
91 }
92 catch ( RepositoryException e )
93 {
94 throw new MetadataSessionException( e.getMessage( ), e );
95 }
96 }
97
98 @Override
99 public void revert( ) throws MetadataSessionException
100 {
101 super.revert( );
102 try
103 {
104 jcrSession.refresh( false );
105 }
106 catch ( RepositoryException e )
107 {
108 throw new MetadataSessionException( e.getMessage( ), e );
109 }
110 }
111
112 @Override
113 public void refresh() throws MetadataSessionException {
114 try {
115 jcrSession.refresh(true);
116 } catch (RepositoryException e) {
117 throw new MetadataSessionException(e.getMessage(), e);
118 }
119 }
120
121 @Override
122 public void refreshAndDiscard() throws MetadataSessionException {
123 try {
124 jcrSession.refresh(false);
125 } catch (RepositoryException e) {
126 throw new MetadataSessionException(e.getMessage(), e);
127 }
128 }
129 }