1 package org.apache.archiva.metadata.repository;
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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26 * The repository session provides a single interface to accessing Archiva repositories. It provides access to three
27 * resources:
28 * <ul>
29 * <li>{@link MetadataRepository} - the metadata content repository for read/write access, in its current state (no
30 * remote resources will be retrieved in the process</li>
31 * <li>{@link MetadataResolver} - access to resolve metadata content, accommodating metadata not yet stored or up to
32 * date in the content repository (i.e. virtualised repositories, remote proxied content, or metadata in a different
33 * model format in the repository storage)</li>
34 * <li>{@link org.apache.archiva.metadata.repository.storage.RepositoryStorage} - access to the physical storage of a
35 * repository and the source artifacts and project models</li>
36 * </ul>
37 */
38 public class RepositorySession
39 {
40 private final MetadataRepository repository;
41
42 private final MetadataResolver resolver;
43
44 private boolean dirty;
45
46 private Logger log = LoggerFactory.getLogger( getClass() );
47
48 // FIXME: include storage here too - perhaps a factory based on repository ID, or one per type to retrieve and
49 // operate on a given repo within the storage API
50
51 public RepositorySession( MetadataRepository metadataRepository, MetadataResolver resolver )
52 {
53 this.repository = metadataRepository;
54 this.resolver = resolver;
55 }
56
57 public MetadataRepository getRepository()
58 {
59 return repository;
60 }
61
62 public MetadataResolver getResolver()
63 {
64 return resolver;
65 }
66
67 public void save()
68 {
69 repository.save();
70
71 dirty = false;
72 }
73
74 public void revert()
75 {
76 repository.revert();
77 dirty = false;
78 }
79
80 /**
81 * Close the session. Required to be called for all open sessions to ensure resources are properly released.
82 * If the session has been marked as dirty, it will be saved. This may save partial changes in the case of a typical
83 * <code>try { ... } finally { ... }</code> approach - if this is a problem, ensure you revert changes when an
84 * exception occurs.
85 * <b>can throw RuntimeException</b>
86 */
87 public void close()
88 {
89 try
90 {
91 if ( dirty )
92 {
93 save();
94 }
95 }
96 finally
97 {
98 try
99 {
100 repository.close();
101 }
102 catch ( MetadataRepositoryException e )
103 {
104 throw new RuntimeException( e.getMessage(), e );
105 }
106 }
107 }
108
109 /**
110 * ignore RuntimeException when closing repository
111 * @since 1.4-M4
112 */
113 public void closeQuietly()
114 {
115 try
116 {
117 this.close();
118 }
119 catch ( RuntimeException e )
120 {
121 log.warn( "ignore Runtime exception while closing: {}", e.getMessage(), e );
122 }
123 }
124
125
126 public void markDirty()
127 {
128 this.dirty = true;
129 }
130 }