This project has retired. For details please refer to its Attic page.
IndexCreationFeature xref
View Javadoc
1   package org.apache.archiva.repository.features;
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  
23  import org.apache.archiva.repository.Repository;
24  import org.apache.archiva.repository.event.RepositoryIndexEvent;
25  import org.apache.archiva.event.EventHandler;
26  import org.apache.archiva.repository.storage.StorageAsset;
27  import org.apache.commons.lang3.StringUtils;
28  
29  import java.net.URI;
30  import java.net.URISyntaxException;
31  
32  import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
33  import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_PACKED_INDEX_PATH;
34  
35  /**
36   *
37   * This feature provides information about index creation.
38   *
39   * Repositories that support this feature are able to create indexes and download them from remote repositories.
40   *
41   * Repositories may have a normal and packed index. A normal index is used by repository search utilities, the packed
42   * index is for downloading purpose.
43   *
44   * A index may have a remote and a local representation. The remote representation is used for downloading and
45   * updating the local representation.
46   *
47   * The feature is throwing a {@link RepositoryIndexEvent}, if the URI of the index has been changed.
48   *
49   */
50  public class IndexCreationFeature extends AbstractFeature implements RepositoryFeature<IndexCreationFeature>{
51  
52  
53      private boolean skipPackedIndexCreation = false;
54  
55      private URI indexPath;
56  
57      private URI packedIndexPath;
58  
59      private StorageAsset localIndexPath;
60  
61      private StorageAsset localPackedIndexPath;
62  
63      private Repository repo;
64  
65      public IndexCreationFeature(Repository repository, EventHandler listener) {
66          super(listener);
67          this.repo = repository;
68          try {
69              this.indexPath = new URI(DEFAULT_INDEX_PATH);
70              this.packedIndexPath = new URI(DEFAULT_PACKED_INDEX_PATH);
71          } catch (URISyntaxException e) {
72              // Does not happen
73              e.printStackTrace();
74          }
75      }
76  
77      public IndexCreationFeature(boolean skipPackedIndexCreation) {
78          this.skipPackedIndexCreation = skipPackedIndexCreation;
79          try {
80              this.indexPath = new URI(DEFAULT_INDEX_PATH);
81              this.packedIndexPath = new URI(DEFAULT_PACKED_INDEX_PATH);
82          } catch (URISyntaxException e) {
83              // Does not happen
84              e.printStackTrace();
85          }
86      }
87  
88      @Override
89      public IndexCreationFeature get() {
90          return this;
91      }
92  
93      /**
94       * Returns true, if no packed index files should be created.
95       * @return True, if no packed index files are created, otherwise false.
96       */
97      public boolean isSkipPackedIndexCreation() {
98          return skipPackedIndexCreation;
99      }
100 
101     /**
102      * Sets the flag for packed index creation.
103      *
104      * @param skipPackedIndexCreation
105      */
106     public void setSkipPackedIndexCreation(boolean skipPackedIndexCreation) {
107         this.skipPackedIndexCreation = skipPackedIndexCreation;
108     }
109 
110     /**
111      * Returns the path that is used to store the index. The path may be a absolute URI or relative to the
112      * base URI of the repository.
113      *
114      * @return the uri (may be relative or absolute)
115      */
116     public URI getIndexPath( )
117     {
118         return indexPath;
119     }
120 
121     /**
122      * Sets the path that is used to store the index. The path may be either absolute or a
123      * path that is relative to the repository storage path (either a local or remote path).
124      *
125      * @param indexPath the uri to the index path (may be relative)
126      */
127     public void setIndexPath( URI indexPath )
128     {
129         if ((this.indexPath==null && indexPath!=null) || !this.indexPath.equals(indexPath)) {
130             URI oldVal = this.indexPath;
131             this.indexPath = indexPath;
132             pushEvent(RepositoryIndexEvent.indexUriChange(this, repo, oldVal, this.indexPath));
133         }
134 
135     }
136 
137     /**
138      * Returns true, if this repository has a index defined.
139      *
140      * @return <code>true</code>, if a index path is set, otherwise <code>false</code>
141      */
142     public boolean hasIndex() {
143         return this.indexPath!=null && !StringUtils.isEmpty( this.indexPath.getPath() );
144     }
145 
146     /**
147      * Returns the path where the index is stored physically.
148      *
149      * @return
150      */
151     public StorageAsset getLocalIndexPath() {
152         return localIndexPath;
153     }
154 
155     /**
156      * Sets the path where the index is stored locally.
157      *
158      * @param localIndexPath
159      */
160     public void setLocalIndexPath(StorageAsset localIndexPath) {
161         this.localIndexPath = localIndexPath;
162     }
163 
164 
165     /**
166      * Returns the path of the packed index.
167      * @return
168      */
169     public URI getPackedIndexPath() {
170         return packedIndexPath;
171     }
172 
173     /**
174      * Sets the path (relative or absolute) of the packed index.
175      *
176      * Throws a {@link RepositoryIndexEvent.Index#PACKED_INDEX_URI_CHANGE}, if the value changes.
177      *
178      * @param packedIndexPath the new path uri for the packed index
179      */
180     public void setPackedIndexPath(URI packedIndexPath) {
181         URI oldVal = this.packedIndexPath;
182         this.packedIndexPath = packedIndexPath;
183         pushEvent(RepositoryIndexEvent.packedIndexUriChange(this, repo, oldVal, this.packedIndexPath));
184     }
185 
186     /**
187      * Returns the directory where the packed index is stored.
188      * @return
189      */
190     public StorageAsset getLocalPackedIndexPath() {
191         return localPackedIndexPath;
192     }
193 
194     /**
195      * Sets the path where the packed index is stored physically. This method should only be used by the
196      * MavenIndexProvider implementations.
197      *
198      * @param localPackedIndexPath
199      */
200     public void setLocalPackedIndexPath(StorageAsset localPackedIndexPath) {
201         this.localPackedIndexPath = localPackedIndexPath;
202     }
203 
204     @Override
205     public String toString() {
206         StringBuilder sb = new StringBuilder();
207         sb.append("IndexCreationFeature:{").append("skipPackedIndexCreation=").append(skipPackedIndexCreation)
208                 .append(",indexPath=").append(indexPath).append(",packedIndexPath=").append(packedIndexPath).append("}");
209         return sb.toString();
210     }
211 }