001package org.apache.archiva.metadata.repository.jcr; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.repository.MetadataResolver; 023import org.apache.archiva.metadata.repository.MetadataSessionException; 024import org.apache.archiva.metadata.repository.RepositorySession; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import javax.jcr.RepositoryException; 029import javax.jcr.Session; 030 031/** 032 * 033 * Session implementation for a JCR repository. 034 * 035 * @author Martin Stockhammer <martin_s@apache.org> 036 */ 037public class JcrRepositorySession extends RepositorySession implements AutoCloseable 038{ 039 040 private static final Logger log = LoggerFactory.getLogger( JcrRepositorySession.class ); 041 042 private Session jcrSession; 043 private JcrMetadataRepository repository; 044 045 public JcrRepositorySession( JcrMetadataRepository metadataRepository, MetadataResolver resolver) throws RepositoryException 046 { 047 super( metadataRepository, resolver ); 048 this.repository = metadataRepository; 049 this.jcrSession = metadataRepository.login(); 050 } 051 052 public Session getJcrSession() { 053 return jcrSession; 054 } 055 056 public JcrMetadataRepository getJcrRepository() { 057 return repository; 058 } 059 060 @Override 061 public void close( ) 062 { 063 super.close( ); 064 jcrSession.logout(); 065 } 066 067 @Override 068 protected boolean isDirty( ) 069 { 070 if (super.isDirty()) { 071 return true; 072 } 073 try 074 { 075 return jcrSession.hasPendingChanges( ); 076 } 077 catch ( RepositoryException e ) 078 { 079 log.error( "Could not check pending changes {}", e.getMessage( ) ); 080 return true; 081 } 082 } 083 084 @Override 085 public void save( ) throws MetadataSessionException 086 { 087 super.save( ); 088 try 089 { 090 jcrSession.save(); 091 } 092 catch ( RepositoryException e ) 093 { 094 throw new MetadataSessionException( e.getMessage( ), e ); 095 } 096 } 097 098 @Override 099 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}