This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.repository.features;
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
022
023import org.apache.archiva.repository.Repository;
024import org.apache.archiva.repository.event.RepositoryIndexEvent;
025import org.apache.archiva.event.EventHandler;
026import org.apache.archiva.repository.storage.StorageAsset;
027import org.apache.commons.lang3.StringUtils;
028
029import java.net.URI;
030import java.net.URISyntaxException;
031
032import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_INDEX_PATH;
033import static org.apache.archiva.indexer.ArchivaIndexManager.DEFAULT_PACKED_INDEX_PATH;
034
035/**
036 *
037 * This feature provides information about index creation.
038 *
039 * Repositories that support this feature are able to create indexes and download them from remote repositories.
040 *
041 * Repositories may have a normal and packed index. A normal index is used by repository search utilities, the packed
042 * index is for downloading purpose.
043 *
044 * A index may have a remote and a local representation. The remote representation is used for downloading and
045 * updating the local representation.
046 *
047 * The feature is throwing a {@link RepositoryIndexEvent}, if the URI of the index has been changed.
048 *
049 */
050public class IndexCreationFeature extends AbstractFeature implements RepositoryFeature<IndexCreationFeature>{
051
052
053    private boolean skipPackedIndexCreation = false;
054
055    private URI indexPath;
056
057    private URI packedIndexPath;
058
059    private StorageAsset localIndexPath;
060
061    private StorageAsset localPackedIndexPath;
062
063    private Repository repo;
064
065    public IndexCreationFeature(Repository repository, EventHandler listener) {
066        super(listener);
067        this.repo = repository;
068        try {
069            this.indexPath = new URI(DEFAULT_INDEX_PATH);
070            this.packedIndexPath = new URI(DEFAULT_PACKED_INDEX_PATH);
071        } catch (URISyntaxException e) {
072            // Does not happen
073            e.printStackTrace();
074        }
075    }
076
077    public IndexCreationFeature(boolean skipPackedIndexCreation) {
078        this.skipPackedIndexCreation = skipPackedIndexCreation;
079        try {
080            this.indexPath = new URI(DEFAULT_INDEX_PATH);
081            this.packedIndexPath = new URI(DEFAULT_PACKED_INDEX_PATH);
082        } catch (URISyntaxException e) {
083            // Does not happen
084            e.printStackTrace();
085        }
086    }
087
088    @Override
089    public IndexCreationFeature get() {
090        return this;
091    }
092
093    /**
094     * Returns true, if no packed index files should be created.
095     * @return True, if no packed index files are created, otherwise false.
096     */
097    public boolean isSkipPackedIndexCreation() {
098        return skipPackedIndexCreation;
099    }
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}