diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/CompositeFileSystem.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/CompositeFileSystem.java deleted file mode 100644 index 69eda54e..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/CompositeFileSystem.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.Map; -import java.util.Objects; -import java.util.TreeMap; - -import org.codehaus.mojo.mrm.api.DefaultDirectoryEntry; -import org.codehaus.mojo.mrm.api.DirectoryEntry; -import org.codehaus.mojo.mrm.api.Entry; -import org.codehaus.mojo.mrm.api.FileEntry; -import org.codehaus.mojo.mrm.api.FileSystem; - -/** - * A file system that is a composite of multiple file systems. - * - * @since 1.0 - */ -public class CompositeFileSystem implements FileSystem { - - /** - * Ensure consistent serialization. - * - * @since 1.0 - */ - private static final long serialVersionUID = 1L; - - /** - * The file systems to delegate to (in order of preference). - * - * @since 1.0 - */ - private final FileSystem[] delegates; - - /** - * The root entry. - * - * @since 1.0 - */ - private final DirectoryEntry root = new DefaultDirectoryEntry(this, null, ""); - - /** - * Creates a new {@link FileSystem} that will delegate to each of the supplied delegate {@link FileSystem} in turn - * until a matching entry is found. - * - * @param delegates the delegate {@link FileSystem}s (in order of preference). - */ - public CompositeFileSystem(FileSystem[] delegates) { - this.delegates = delegates; - } - - /** - * {@inheritDoc} - */ - public Entry[] listEntries(DirectoryEntry directory) { - Map result = new TreeMap<>(); - for (FileSystem delegate : delegates) { - Entry[] entries = delegate.listEntries(DefaultDirectoryEntry.equivalent(delegate, directory)); - if (entries == null) { - continue; - } - for (Entry entry : entries) { - if (result.containsKey(entry.getName())) { - continue; - } - if (entry instanceof DirectoryEntry) { - result.put(entry.getName(), new DefaultDirectoryEntry(this, directory, entry.getName())); - } else if (entry instanceof FileEntry) { - result.put(entry.getName(), new LinkFileEntry(this, directory, (FileEntry) entry)); - } - } - } - return result.values().toArray(new Entry[0]); - } - - /** - * {@inheritDoc} - */ - public Entry get(String path) { - return Arrays.stream(delegates) - .map(fileSystem -> fileSystem.get(path)) - .filter(Objects::nonNull) - .filter(entry -> entry instanceof DirectoryEntry) - .map(entry -> DefaultDirectoryEntry.equivalent(this, (DirectoryEntry) entry)) - .findFirst() - .orElse(null); - } - - /** - * {@inheritDoc} - */ - public long getLastModified(DirectoryEntry entry) throws IOException { - long lastModified = 0; - for (FileSystem delegate : delegates) { - try { - lastModified = Math.max(lastModified, delegate.getLastModified(entry)); - } catch (IOException e) { - // ignore - } - } - return lastModified; - } - - /** - * {@inheritDoc} - */ - public DirectoryEntry getRoot() { - return root; - } - - /** - * {@inheritDoc} - */ - public DirectoryEntry mkdir(DirectoryEntry parent, String name) { - throw new UnsupportedOperationException(); - } - - /** - * {@inheritDoc} - */ - public FileEntry put(DirectoryEntry parent, String name, InputStream content) throws IOException { - throw new UnsupportedOperationException(); - } - - /** - * {@inheritDoc} - */ - public FileEntry put(DirectoryEntry parent, String name, byte[] content) throws IOException { - throw new UnsupportedOperationException(); - } - - /** - * {@inheritDoc} - */ - public void remove(Entry entry) { - throw new UnsupportedOperationException(); - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileEntry.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileEntry.java deleted file mode 100644 index 6350cbcf..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileEntry.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -import org.codehaus.mojo.mrm.api.BaseFileEntry; -import org.codehaus.mojo.mrm.api.DirectoryEntry; -import org.codehaus.mojo.mrm.api.FileSystem; - -/** - * A file entry backed by a {@link File} on a local disk. - * - * @since 1.0 - */ -public class DiskFileEntry extends BaseFileEntry { - - /** - * Ensure consistent serialization. - * - * @since 1.0 - */ - private static final long serialVersionUID = 1L; - - /** - * The backing file. - * - * @since 1.0 - */ - private final File file; - - /** - * Creates a new instance in the specified parent directory of the specified file system that backs the supplied - * file named with {@link java.io.File#getName()}. - * - * @param fileSystem the file system. - * @param parent the parent directory. - * @param file the backing file. - * @since 1.0 - */ - public DiskFileEntry(FileSystem fileSystem, DirectoryEntry parent, File file) { - this(fileSystem, parent, file.getName(), file); - } - - /** - * Creates a new instance in the specified parent directory of the specified file system that backs the supplied - * file named with the supplied name. - * - * @param fileSystem the file system. - * @param parent the parent directory. - * @param name the name of the entry. - * @param file the backing file. - * @since 1.0 - */ - public DiskFileEntry(FileSystem fileSystem, DirectoryEntry parent, String name, File file) { - super(fileSystem, parent, name); - this.file = file; - } - - /** - * {@inheritDoc} - */ - public long getLastModified() { - return file.lastModified(); - } - - /** - * {@inheritDoc} - */ - public long getSize() { - return file.length(); - } - - /** - * {@inheritDoc} - */ - public InputStream getInputStream() throws IOException { - return new FileInputStream(file); - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileSystem.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileSystem.java deleted file mode 100644 index b7953168..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/DiskFileSystem.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Stack; - -import org.apache.commons.io.FileUtils; -import org.codehaus.mojo.mrm.api.BaseFileSystem; -import org.codehaus.mojo.mrm.api.DefaultDirectoryEntry; -import org.codehaus.mojo.mrm.api.DirectoryEntry; -import org.codehaus.mojo.mrm.api.Entry; -import org.codehaus.mojo.mrm.api.FileEntry; - -/** - * A file system hosted from a local directory. - * - * @since 1.0 - */ -public class DiskFileSystem extends BaseFileSystem { - - /** - * The root directory of the file system. - * - * @since 1.0 - */ - private final File root; - - /** - * Whether the file system can support modifications. - * - * @since 1.0 - */ - private final boolean readOnly; - - /** - * Creates a new instance. - * - * @param root the root of the file system. - * @param readOnly true if the file system is to be read-only - * @since 1.0 - */ - public DiskFileSystem(File root, boolean readOnly) { - this.root = root; - this.readOnly = readOnly; - } - - /** - * Creates a new read-only instance. - * - * @param root the root of the file system. - * @since 1.0 - */ - public DiskFileSystem(File root) { - this(root, true); - } - - /** - * {@inheritDoc} - */ - public Entry[] listEntries(DirectoryEntry directory) { - File file = toFile(directory); - if (!file.isDirectory()) { - return null; - } - File[] files = file.listFiles(); - - Entry[] result = new Entry[files.length]; - for (int i = 0; i < files.length; i++) { - if (files[i].isFile()) { - result[i] = new DiskFileEntry(this, directory, files[i]); - } else { - result[i] = new DefaultDirectoryEntry(this, directory, files[i].getName()); - } - } - return result; - } - - /** - * {@inheritDoc} - */ - public long getLastModified(DirectoryEntry entry) throws IOException { - return toFile(entry).lastModified(); - } - - /** - * Convert an entry into the corresponding file path. - * - * @param entry the entry. - * @return the corresponding file. - * @since 1.0 - */ - private File toFile(Entry entry) { - Stack stack = new Stack<>(); - Entry entryRoot = entry.getFileSystem().getRoot(); - while (entry != null && !entryRoot.equals(entry)) { - String name = entry.getName(); - if ("..".equals(name)) { - if (!stack.isEmpty()) { - stack.pop(); - } - } else if (!".".equals(name)) { - stack.push(name); - } - entry = entry.getParent(); - } - File file = this.root; - while (!stack.empty()) { - file = new File(file, stack.pop()); - } - return file; - } - - /** - * {@inheritDoc} - */ - public DirectoryEntry mkdir(DirectoryEntry parent, String name) { - if (readOnly) { - throw new UnsupportedOperationException(); - } - File file = new File(toFile(parent), name); - file.mkdirs(); - return new DefaultDirectoryEntry(this, parent, name); - } - - /** - * {@inheritDoc} - */ - public FileEntry put(DirectoryEntry parent, String name, InputStream content) throws IOException { - if (readOnly) { - throw new UnsupportedOperationException(); - } - File parentFile = toFile(parent); - parentFile.mkdirs(); - File file = new File(parentFile, name); - FileUtils.copyInputStreamToFile(content, file); - return new DiskFileEntry(this, parent, file); - } - - /** - * {@inheritDoc} - */ - public void remove(Entry entry) { - if (readOnly) { - throw new UnsupportedOperationException(); - } - File file = toFile(entry); - file.delete(); - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileEntry.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileEntry.java deleted file mode 100644 index a3820723..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileEntry.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - -import org.codehaus.mojo.mrm.api.BaseFileEntry; -import org.codehaus.mojo.mrm.api.DirectoryEntry; -import org.codehaus.mojo.mrm.api.FileSystem; - -/** - * A {@link org.codehaus.mojo.mrm.api.FileEntry} who's contents are held in memory. - */ -public class MemoryFileEntry extends BaseFileEntry { - - /** - * Ensure consistent serialization. - * - * @since 1.0 - */ - private static final long serialVersionUID = 1L; - - /** - * Lazy null surrogate. - * - * @since 1.0 - */ - private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; - - /** - * The content to serve. - * - * @since 1.0 - */ - private final byte[] content; - - /** - * The timestamp of the content. - * - * @since 1.0 - */ - private final long lastModified; - - /** - * Creates a new entry for the specified parent directory of the specified file system with the specified name - * having the supplied content. - * - * @param fileSystem the file system. - * @param parent the parent directory - * @param name the name. - * @param content the content. - * @since 1.0 - */ - public MemoryFileEntry(FileSystem fileSystem, DirectoryEntry parent, String name, byte[] content) { - super(fileSystem, parent, name); - this.content = content == null ? EMPTY_BYTE_ARRAY : content; - this.lastModified = System.currentTimeMillis(); - } - - /** - * {@inheritDoc} - */ - public long getLastModified() { - return lastModified; - } - - /** - * {@inheritDoc} - */ - public long getSize() { - return content.length; - } - - /** - * {@inheritDoc} - */ - public InputStream getInputStream() throws IOException { - return new ByteArrayInputStream(content); - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileSystem.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileSystem.java deleted file mode 100644 index 1c8ebc0c..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/MemoryFileSystem.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.apache.commons.io.IOUtils; -import org.codehaus.mojo.mrm.api.BaseFileSystem; -import org.codehaus.mojo.mrm.api.DefaultDirectoryEntry; -import org.codehaus.mojo.mrm.api.DirectoryEntry; -import org.codehaus.mojo.mrm.api.Entry; -import org.codehaus.mojo.mrm.api.FileEntry; - -/** - * A file system who's structure is entirely stored in memory. - * - * @since 1.0 - */ -public class MemoryFileSystem extends BaseFileSystem { - - /** - * The file system content. - * - * @since 1.0 - */ - private final Map> contents = new HashMap<>(); - - /** - * Create a new empty file system. - * - * @since 1.0 - */ - public MemoryFileSystem() { - contents.put(getRoot(), new ArrayList<>()); - } - - /** - * {@inheritDoc} - */ - public synchronized Entry[] listEntries(DirectoryEntry directory) { - List entries = contents.get(directory == null ? getRoot() : directory); - if (entries == null) { - return null; - } - return entries.toArray(new Entry[0]); - } - - /** - * {@inheritDoc} - */ - public long getLastModified(DirectoryEntry directoryEntry) throws IOException { - long lastModified = 0; - Entry[] entries = listEntries(directoryEntry); - if (entries != null) { - for (Entry entry : entries) { - lastModified = Math.max(lastModified, entry.getLastModified()); - } - } - - return lastModified; - } - - /** - * {@inheritDoc} - */ - protected synchronized Entry get(DirectoryEntry parent, String name) { - List parentEntries = contents.get(parent); - return parentEntries == null - ? null - : parentEntries.stream() - .filter(entry -> name.equals(entry.getName())) - .findFirst() - .orElse(null); - } - - /** - * {@inheritDoc} - */ - public synchronized DirectoryEntry mkdir(DirectoryEntry parent, String name) { - parent = getNormalizedParent(parent); - List entries = getEntriesList(parent); - for (Entry entry : entries) { - if (name.equals(entry.getName())) { - if (entry instanceof DirectoryEntry) { - return (DirectoryEntry) entry; - } - return null; - } - } - DirectoryEntry entry = new DefaultDirectoryEntry(this, parent, name); - entries.add(entry); - return entry; - } - - /** - * {@inheritDoc} - */ - public synchronized FileEntry put(DirectoryEntry parent, String name, InputStream content) throws IOException { - parent = getNormalizedParent(parent); - List entries = getEntriesList(parent); - for (Iterator i = entries.iterator(); i.hasNext(); ) { - Entry entry = i.next(); - if (name.equals(entry.getName())) { - if (entry instanceof FileEntry) { - i.remove(); - } else { - return null; - } - } - } - FileEntry entry = new MemoryFileEntry(this, parent, name, IOUtils.toByteArray(content)); - entries.add(entry); - return entry; - } - - /** - * Gets the parent directory entry (ensuring it exists). - * - * @param parent the parent entry to get. - * @return the actual directory entry instance used as the key in {@link #contents}. - * @since 1.0 - */ - private DirectoryEntry getNormalizedParent(DirectoryEntry parent) { - if (parent.getParent() == null) { - return getRoot(); - } else { - return mkdir(parent.getParent(), parent.getName()); - } - } - - /** - * Gets the list of entries in the specified directory. - * - * @param directory the directory to get the entries of. - * @return the list of entries (never null). - * @since 1.0 - */ - private synchronized List getEntriesList(DirectoryEntry directory) { - return contents.computeIfAbsent(directory, k -> new ArrayList<>()); - } - - /** - * {@inheritDoc} - */ - public synchronized void remove(Entry entry) { - List entries; - if (entry == null) { - return; - } - DirectoryEntry parent = entry.getParent(); - if (parent == null) { - return; - } else { - entries = contents.get(parent); - if (entries == null) { - return; - } - } - for (Iterator i = entries.iterator(); i.hasNext(); ) { - Entry e = i.next(); - if (entry.equals(e)) { - if (e instanceof DirectoryEntry) { - Entry[] children = listEntries((DirectoryEntry) e); - for (int j = children.length - 1; j >= 0; j--) { - remove(children[j]); - } - contents.remove(e); - } - i.remove(); - return; - } - } - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/maven/MemoryArtifactStore.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/maven/MemoryArtifactStore.java deleted file mode 100644 index 0a4161cb..00000000 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/maven/MemoryArtifactStore.java +++ /dev/null @@ -1,636 +0,0 @@ -/* - * Copyright 2011 Stephen Connolly - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.codehaus.mojo.mrm.impl.maven; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.Serializable; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import java.util.TreeSet; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.maven.archetype.catalog.ArchetypeCatalog; -import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.Plugin; -import org.apache.maven.artifact.repository.metadata.Snapshot; -import org.apache.maven.artifact.repository.metadata.SnapshotVersion; -import org.apache.maven.artifact.repository.metadata.Versioning; -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; -import org.apache.maven.model.Build; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.codehaus.mojo.mrm.api.maven.ArchetypeCatalogNotFoundException; -import org.codehaus.mojo.mrm.api.maven.Artifact; -import org.codehaus.mojo.mrm.api.maven.ArtifactNotFoundException; -import org.codehaus.mojo.mrm.api.maven.BaseArtifactStore; -import org.codehaus.mojo.mrm.api.maven.MetadataNotFoundException; -import org.codehaus.plexus.util.xml.XmlStreamReader; -import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -/** - * An artifact store that keeps all its artifacts in memory. - * - * @since 1.0 - */ -public class MemoryArtifactStore extends BaseArtifactStore implements Serializable { - /** - * Ensure consistent serialization. - * - * @since 1.0 - */ - private static final long serialVersionUID = 1L; - - /** - * The contents of this artifact store. - * - * @since 1.0 - */ - private Map>>> contents = new HashMap<>(); - - private Content archetypeCatalog; - - /** - * {@inheritDoc} - */ - public synchronized Set getGroupIds(String parentGroupId) { - Set result = new TreeSet<>(); - if (StringUtils.isEmpty(parentGroupId)) { - for (String groupId : contents.keySet()) { - int index = groupId.indexOf('.'); - result.add(index == -1 ? groupId : groupId.substring(0, index)); - } - } else { - String prefix = parentGroupId + '.'; - int start = prefix.length(); - for (String groupId : contents.keySet()) { - if (groupId.startsWith(prefix)) { - int index = groupId.indexOf('.', start); - result.add(index == -1 ? groupId.substring(start) : groupId.substring(start, index)); - } - } - } - return result; - } - - /** - * {@inheritDoc} - */ - public synchronized Set getArtifactIds(String groupId) { - Map>> artifactMap = contents.get(groupId); - return artifactMap == null ? Collections.emptySet() : new TreeSet<>(artifactMap.keySet()); - } - - /** - * {@inheritDoc} - */ - public synchronized Set getVersions(String groupId, String artifactId) { - Map>> artifactMap = contents.get(groupId); - Map> versionMap = (artifactMap == null ? null : artifactMap.get(artifactId)); - return versionMap == null ? Collections.emptySet() : new TreeSet<>(versionMap.keySet()); - } - - /** - * {@inheritDoc} - */ - public synchronized Set getArtifacts(String groupId, String artifactId, String version) { - Map>> artifactMap = contents.get(groupId); - Map> versionMap = (artifactMap == null ? null : artifactMap.get(artifactId)); - Map filesMap = (versionMap == null ? null : versionMap.get(version)); - - return filesMap == null ? Collections.emptySet() : new HashSet<>(filesMap.keySet()); - } - - /** - * {@inheritDoc} - */ - public synchronized long getLastModified(Artifact artifact) throws IOException, ArtifactNotFoundException { - Map>> artifactMap = contents.get(artifact.getGroupId()); - Map> versionMap = - (artifactMap == null ? null : artifactMap.get(artifact.getArtifactId())); - Map filesMap = (versionMap == null ? null : versionMap.get(artifact.getVersion())); - Content content = (filesMap == null ? null : filesMap.get(artifact)); - if (content == null) { - if (artifact.isSnapshot() && artifact.getTimestamp() == null && filesMap != null) { - Artifact best = null; - for (Map.Entry entry : filesMap.entrySet()) { - Artifact a = entry.getKey(); - if (artifact.equalSnapshots(a) && (best == null || best.compareTo(a) < 0)) { - best = a; - content = entry.getValue(); - } - } - if (content == null) { - throw new ArtifactNotFoundException(artifact); - } - } else { - throw new ArtifactNotFoundException(artifact); - } - } - return content.getLastModified(); - } - - /** - * {@inheritDoc} - */ - public synchronized long getSize(Artifact artifact) throws IOException, ArtifactNotFoundException { - Map>> artifactMap = contents.get(artifact.getGroupId()); - Map> versionMap = - (artifactMap == null ? null : artifactMap.get(artifact.getArtifactId())); - Map filesMap = (versionMap == null ? null : versionMap.get(artifact.getVersion())); - Content content = (filesMap == null ? null : filesMap.get(artifact)); - if (content == null) { - if (artifact.isSnapshot() && artifact.getTimestamp() == null && filesMap != null) { - Artifact best = null; - for (Map.Entry entry : filesMap.entrySet()) { - Artifact a = entry.getKey(); - if (artifact.equalSnapshots(a) && (best == null || best.compareTo(a) < 0)) { - best = a; - content = entry.getValue(); - } - } - if (content == null) { - throw new ArtifactNotFoundException(artifact); - } - } else { - throw new ArtifactNotFoundException(artifact); - } - } - return content.getBytes().length; - } - - /** - * {@inheritDoc} - */ - public synchronized InputStream get(Artifact artifact) throws IOException, ArtifactNotFoundException { - Map>> artifactMap = contents.get(artifact.getGroupId()); - Map> versionMap = - (artifactMap == null ? null : artifactMap.get(artifact.getArtifactId())); - Map filesMap = (versionMap == null ? null : versionMap.get(artifact.getVersion())); - Content content = (filesMap == null ? null : filesMap.get(artifact)); - if (content == null) { - if (artifact.isSnapshot() && artifact.getTimestamp() == null && filesMap != null) { - Artifact best = null; - for (Map.Entry entry : filesMap.entrySet()) { - Artifact a = entry.getKey(); - if (artifact.equalSnapshots(a) && (best == null || best.compareTo(a) < 0)) { - best = a; - content = entry.getValue(); - } - } - if (content == null) { - throw new ArtifactNotFoundException(artifact); - } - } else { - throw new ArtifactNotFoundException(artifact); - } - } - return new ByteArrayInputStream(content.getBytes()); - } - - /** - * {@inheritDoc} - */ - public synchronized void set(Artifact artifact, InputStream content) throws IOException { - Map>> artifactMap = - contents.computeIfAbsent(artifact.getGroupId(), k -> new HashMap<>()); - Map> versionMap = - artifactMap.computeIfAbsent(artifact.getArtifactId(), k -> new HashMap<>()); - Map filesMap = versionMap.computeIfAbsent(artifact.getVersion(), k -> new HashMap<>()); - try { - filesMap.put(artifact, new Content(IOUtils.toByteArray(content))); - } finally { - IOUtils.closeQuietly(content); - } - } - - @SuppressWarnings("checkstyle:MethodLength") - public synchronized Metadata getMetadata(String path) throws IOException, MetadataNotFoundException { - Metadata metadata = new Metadata(); - boolean foundMetadata = false; - path = StringUtils.stripEnd(StringUtils.stripStart(path, "/"), "/"); - String groupId = path.replace('/', '.'); - Set pluginArtifactIds = getArtifactIds(groupId); - if (pluginArtifactIds != null) { - List plugins = new ArrayList<>(); - for (String artifactId : pluginArtifactIds) { - Set pluginVersions = getVersions(groupId, artifactId); - if (pluginVersions == null || pluginVersions.isEmpty()) { - continue; - } - String[] versions = pluginVersions.toArray(new String[0]); - Arrays.sort(versions, INSTANCE); - MavenXpp3Reader reader = new MavenXpp3Reader(); - for (int j = versions.length - 1; j >= 0; j--) { - try (InputStream inputStream = get(new Artifact(groupId, artifactId, versions[j], "pom")); - XmlStreamReader xmlStreamReader = new XmlStreamReader(inputStream)) { - Model model = reader.read(xmlStreamReader); - if (model == null || !"maven-plugin".equals(model.getPackaging())) { - continue; - } - Plugin plugin = new Plugin(); - plugin.setArtifactId(artifactId); - plugin.setName(model.getName()); - // TODO proper goal-prefix determination - // ugh! this is incredibly hacky and does not handle some fool that sets the goal prefix in - // a parent pom... ok unlikely, but stupid is as stupid does - boolean havePrefix = false; - final Build build = model.getBuild(); - if (build != null && build.getPlugins() != null) { - havePrefix = setPluginGoalPrefixFromConfiguration(plugin, build.getPlugins()); - } - if (!havePrefix - && build != null - && build.getPluginManagement() != null - && build.getPluginManagement().getPlugins() != null) { - havePrefix = setPluginGoalPrefixFromConfiguration( - plugin, build.getPluginManagement().getPlugins()); - } - if (!havePrefix && artifactId.startsWith("maven-") && artifactId.endsWith("-plugin")) { - plugin.setPrefix( - StringUtils.removeStart(StringUtils.removeEnd(artifactId, "-plugin"), "maven-")); - havePrefix = true; - } - if (!havePrefix && artifactId.endsWith("-maven-plugin")) { - plugin.setPrefix(StringUtils.removeEnd(artifactId, "-maven-plugin")); - havePrefix = true; - } - if (!havePrefix) { - plugin.setPrefix(artifactId); - } - plugins.add(plugin); - foundMetadata = true; - break; - } catch (ArtifactNotFoundException | XmlPullParserException e) { - // ignore - } - } - } - if (!plugins.isEmpty()) { - metadata.setPlugins(plugins); - } - } - int index = path.lastIndexOf('/'); - groupId = (index == -1 ? groupId : groupId.substring(0, index)).replace('/', '.'); - String artifactId = (index == -1 ? null : path.substring(index + 1)); - if (artifactId != null) { - Set artifactVersions = getVersions(groupId, artifactId); - if (artifactVersions != null && !artifactVersions.isEmpty()) { - metadata.setGroupId(groupId); - metadata.setArtifactId(artifactId); - Versioning versioning = new Versioning(); - List versions = new ArrayList<>(artifactVersions); - versions.sort(INSTANCE); // sort the Maven way - long lastUpdated = 0; - for (String version : versions) { - try { - long lastModified = getLastModified(new Artifact(groupId, artifactId, version, "pom")); - versioning.addVersion(version); - if (lastModified >= lastUpdated) { - lastUpdated = lastModified; - versioning.setLastUpdatedTimestamp(new Date(lastModified)); - versioning.setLatest(version); - if (!version.endsWith("-SNAPSHOT")) { - versioning.setRelease(version); - } - } - } catch (ArtifactNotFoundException e) { - // ignore - } - } - metadata.setVersioning(versioning); - foundMetadata = true; - } - } - - int index2 = index == -1 ? -1 : path.lastIndexOf('/', index - 1); - groupId = index2 == -1 ? groupId : groupId.substring(0, index2).replace('/', '.'); - artifactId = index2 == -1 ? artifactId : path.substring(index2 + 1, index); - String version = index2 == -1 ? null : path.substring(index + 1); - if (version != null && version.endsWith("-SNAPSHOT")) { - Map>> artifactMap = contents.get(groupId); - Map> versionMap = (artifactMap == null ? null : artifactMap.get(artifactId)); - Map filesMap = (versionMap == null ? null : versionMap.get(version)); - if (filesMap != null) { - List snapshotVersions = new ArrayList<>(); - int maxBuildNumber = 0; - long lastUpdated = 0; - String timestamp = null; - boolean found = false; - for (Map.Entry entry : filesMap.entrySet()) { - Artifact artifact = entry.getKey(); - Content content = entry.getValue(); - SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss"); - fmt.setTimeZone(TimeZone.getTimeZone("GMT")); - String lastUpdatedTime = fmt.format(new Date(content.getLastModified())); - try { - Maven3.addSnapshotVersion(snapshotVersions, artifact, lastUpdatedTime); - } catch (LinkageError e) { - // Maven 2 - } - if ("pom".equals(artifact.getType())) { - if (artifact.getBuildNumber() != null && maxBuildNumber < artifact.getBuildNumber()) { - maxBuildNumber = artifact.getBuildNumber(); - timestamp = artifact.getTimestampString(); - } else { - maxBuildNumber = Math.max(1, maxBuildNumber); - } - lastUpdated = Math.max(lastUpdated, content.getLastModified()); - found = true; - } - } - - if (!snapshotVersions.isEmpty() || found) { - Versioning versioning = metadata.getVersioning(); - if (versioning == null) { - versioning = new Versioning(); - } - metadata.setGroupId(groupId); - metadata.setArtifactId(artifactId); - metadata.setVersion(version); - try { - Maven3.addSnapshotVersions(versioning, snapshotVersions); - } catch (LinkageError e) { - // Maven 2 - } - if (maxBuildNumber > 0) { - Snapshot snapshot = new Snapshot(); - snapshot.setBuildNumber(maxBuildNumber); - snapshot.setTimestamp(timestamp); - versioning.setSnapshot(snapshot); - } - versioning.setLastUpdatedTimestamp(new Date(lastUpdated)); - metadata.setVersioning(versioning); - foundMetadata = true; - } - } - } - if (!foundMetadata) { - throw new MetadataNotFoundException(path); - } - return metadata; - } - - /** - * {@inheritDoc} - */ - public synchronized long getMetadataLastModified(String path) throws IOException, MetadataNotFoundException { - boolean haveResult = false; - long result = 0; - path = StringUtils.stripEnd(StringUtils.stripStart(path, "/"), "/"); - String groupId = path.replace('/', '.'); - Map>> artifactMap = contents.get(groupId); - if (artifactMap != null) { - for (Map> versionMap : artifactMap.values()) { - for (Map filesMap : versionMap.values()) { - for (Content content : filesMap.values()) { - haveResult = true; - result = Math.max(result, content.getLastModified()); - } - } - } - } - int index = path.lastIndexOf('/'); - groupId = index == -1 ? groupId : groupId.substring(0, index).replace('/', '.'); - String artifactId = (index == -1 ? null : path.substring(index + 1)); - if (artifactId != null) { - artifactMap = contents.get(groupId); - Map> versionMap = (artifactMap == null ? null : artifactMap.get(artifactId)); - if (versionMap != null) { - for (Map filesMap : versionMap.values()) { - for (Content content : filesMap.values()) { - haveResult = true; - result = Math.max(result, content.getLastModified()); - } - } - } - } - int index2 = index == -1 ? -1 : path.lastIndexOf('/', index - 1); - groupId = index2 == -1 ? groupId : groupId.substring(0, index2).replace('/', '.'); - artifactId = index2 == -1 ? artifactId : path.substring(index2 + 1, index); - String version = index2 == -1 ? null : path.substring(index + 1); - if (version != null && version.endsWith("-SNAPSHOT")) { - artifactMap = contents.get(groupId); - Map> versionMap = (artifactMap == null ? null : artifactMap.get(artifactId)); - Map filesMap = (versionMap == null ? null : versionMap.get(version)); - if (filesMap != null) { - for (Content content : filesMap.values()) { - haveResult = true; - result = Math.max(result, content.getLastModified()); - } - } - } - if (haveResult) { - return result; - } - throw new MetadataNotFoundException(path); - } - - @Override - public synchronized void setArchetypeCatalog(InputStream content) throws IOException { - archetypeCatalog = new Content(IOUtils.toByteArray(content)); - } - - public synchronized ArchetypeCatalog getArchetypeCatalog() throws IOException, ArchetypeCatalogNotFoundException { - if (archetypeCatalog != null) { - try (InputStream inputStream = new ByteArrayInputStream(archetypeCatalog.getBytes())) { - return new ArchetypeCatalogXpp3Reader().read(inputStream); - } catch (XmlPullParserException e) { - throw new ArchetypeCatalogNotFoundException(e.getMessage(), e); - } - } else { - throw new ArchetypeCatalogNotFoundException(); - } - } - - public synchronized long getArchetypeCatalogLastModified() throws ArchetypeCatalogNotFoundException { - if (archetypeCatalog != null) { - return archetypeCatalog.getLastModified(); - } else { - throw new ArchetypeCatalogNotFoundException(); - } - } - - /** - * If the plugin configurations contain a reference to the maven-plugin-plugin and that contains - * configuration of the goalPrefix, update the supplied plugin with that prefix. - * - * @param plugin the plugin to update. - * @param pluginConfigs the configurations of {@link org.apache.maven.model.Plugin} to search. - * @return true if the prefix has been set. - * @since 1.0 - */ - private boolean setPluginGoalPrefixFromConfiguration( - Plugin plugin, List pluginConfigs) { - for (org.apache.maven.model.Plugin def : pluginConfigs) { - if ((def.getGroupId() == null || StringUtils.equals("org.apache.maven.plugins", def.getGroupId())) - && StringUtils.equals("maven-plugin-plugin", def.getArtifactId())) { - Xpp3Dom configuration = (Xpp3Dom) def.getConfiguration(); - if (configuration != null) { - final Xpp3Dom goalPrefix = configuration.getChild("goalPrefix"); - if (goalPrefix != null) { - plugin.setPrefix(goalPrefix.getValue()); - return true; - } - } - break; - } - } - return false; - } - - private static final Comparator INSTANCE = new VersionComparator(); - - /** - * Compares two versions using Maven's version comparison rules. - * - * @since 1.0 - */ - private static class VersionComparator implements Comparator { - /** - * {@inheritDoc} - */ - public int compare(String o1, String o2) { - ArtifactVersion v1 = new DefaultArtifactVersion(o1); - ArtifactVersion v2 = new DefaultArtifactVersion(o2); - return v1.compareTo(v2); - } - } - - /** - * Holds the contents of an artifact. - * - * @since 1.0 - */ - private static class Content implements Serializable { - - /** - * Ensure consistent serialization. - * - * @since 1.0 - */ - private static final long serialVersionUID = 1L; - - /** - * The last modified timestamp. - * - * @since 1.0 - */ - private final long lastModified; - - /** - * The actual content. - * - * @since 1.0 - */ - private final byte[] bytes; - - /** - * Creates new content from the supplied content. - * - * @param bytes the content. - * @since 1.0 - */ - private Content(byte[] bytes) { - this.lastModified = System.currentTimeMillis(); - this.bytes = bytes; - } - - /** - * Returns the last modified timestamp. - * - * @return the last modified timestamp. - * @since 1.0 - */ - public long getLastModified() { - return lastModified; - } - - /** - * Returns the content. - * - * @return the content. - * @since 1.0 - */ - public byte[] getBytes() { - return bytes; - } - } - - /** - * In order to allow the use of Maven 3 methods from a plugin running in Maven 2, we need to encapsulate all the - * Maven 3 method signatures in a separate class so that we can catch the {@link LinkageError} that will be thrown - * when the class is attempted to load. If we didn't do it this way then our class could not load either. - * - * @since 1.0 - */ - private static class Maven3 { - /** - * Adds a snapshot version to the list of snapshot versions. - * - * @param snapshotVersions the list of snapshot versions. - * @param artifact the artifact to add details of. - * @param lastUpdatedTime the time to flag for last updated. - * @since 1.0 - */ - private static void addSnapshotVersion( - List snapshotVersions, Artifact artifact, String lastUpdatedTime) { - try { - SnapshotVersion snapshotVersion = new SnapshotVersion(); - snapshotVersion.setExtension(artifact.getType()); - snapshotVersion.setClassifier(artifact.getClassifier() == null ? "" : artifact.getClassifier()); - snapshotVersion.setVersion(artifact.getTimestampVersion()); - snapshotVersion.setUpdated(lastUpdatedTime); - snapshotVersions.add(snapshotVersion); - } catch (NoClassDefFoundError e) { - // Maven 2 - } - } - - /** - * Add the list of {@link SnapshotVersion}s to the {@link Versioning}. - * - * @param versioning the versionioning to add to. - * @param snapshotVersions the snapshot versions to add. - * @since 1.0 - */ - private static void addSnapshotVersions(Versioning versioning, List snapshotVersions) { - try { - for (SnapshotVersion snapshotVersion : snapshotVersions) { - versioning.addSnapshotVersion(snapshotVersion); - } - } catch (NoClassDefFoundError e) { - // Maven 2 - } - } - } -} diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/servlet/FileSystemServlet.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/servlet/FileSystemServlet.java index 0b415f2f..e67a0758 100644 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/servlet/FileSystemServlet.java +++ b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/servlet/FileSystemServlet.java @@ -44,7 +44,6 @@ import org.codehaus.mojo.mrm.api.Entry; import org.codehaus.mojo.mrm.api.FileEntry; import org.codehaus.mojo.mrm.api.FileSystem; -import org.codehaus.mojo.mrm.impl.MemoryFileSystem; import org.codehaus.mojo.mrm.impl.Utils; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.InterpolationFilterReader; @@ -82,15 +81,6 @@ public class FileSystemServlet extends HttpServlet { */ private String settingsServletPath; - /** - * Default constructor. - * - * @since 1.0 - */ - public FileSystemServlet() { - this.fileSystem = new MemoryFileSystem(); - } - /** * Constructor that takes a specific file system instance. *