View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.codehaus.plexus.archiver.diags;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.charset.Charset;
26  
27  import org.codehaus.plexus.archiver.ArchivedFileSet;
28  import org.codehaus.plexus.archiver.Archiver;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.FileSet;
31  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
32  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
33  import org.slf4j.Logger;
34  
35  /**
36   * A dry run archiver that does nothing. Some methods fall through to the underlying
37   * archiver, but no actions are executed.
38   */
39  public class DryRunArchiver extends DelgatingArchiver {
40  
41      private final Logger logger;
42  
43      public DryRunArchiver(final Archiver target, final Logger logger) {
44          super(target);
45          this.logger = logger;
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public void addArchivedFileSet(
53              final @Nonnull File archiveFile, final String prefix, final String[] includes, final String[] excludes) {
54          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
55      }
56  
57      private void debug(final String message) {
58          if ((logger != null) && logger.isDebugEnabled()) {
59              logger.debug(message);
60          }
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      @Deprecated
68      public void addArchivedFileSet(final @Nonnull File archiveFile, final String prefix) throws ArchiverException {
69          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void addArchivedFileSet(final File archiveFile, final String[] includes, final String[] excludes)
77              throws ArchiverException {
78          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void addArchivedFileSet(final @Nonnull File archiveFile) throws ArchiverException {
86          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void addDirectory(
94              final @Nonnull File directory, final String prefix, final String[] includes, final String[] excludes)
95              throws ArchiverException {
96          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public void addSymlink(String symlinkName, String symlinkDestination) throws ArchiverException {
104         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void addSymlink(String symlinkName, int permissions, String symlinkDestination) throws ArchiverException {
112         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     @Deprecated
120     public void addDirectory(final @Nonnull File directory, final String prefix) throws ArchiverException {
121         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     @Deprecated
129     public void addDirectory(final @Nonnull File directory, final String[] includes, final String[] excludes)
130             throws ArchiverException {
131         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     @Deprecated
139     public void addDirectory(final @Nonnull File directory) throws ArchiverException {
140         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName, final int permissions)
148             throws ArchiverException {
149         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName) throws ArchiverException {
157         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public void createArchive() throws ArchiverException, IOException {
165         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public void setDotFileDirectory(final File dotFileDirectory) {
173         throw new UnsupportedOperationException("Undocumented feature of plexus-archiver; this is not yet supported.");
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public void addArchivedFileSet(final ArchivedFileSet fileSet) throws ArchiverException {
181 
182         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
183     }
184 
185     @Override
186     public void addArchivedFileSet(ArchivedFileSet fileSet, Charset charset) throws ArchiverException {
187         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
188     }
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public void addFileSet(final @Nonnull FileSet fileSet) throws ArchiverException {
195         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
196     }
197 
198     @Override
199     public void addResource(PlexusIoResource resource, String destFileName, int permissions) throws ArchiverException {
200         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
201     }
202 
203     @Override
204     public void addResources(PlexusIoResourceCollection resources) throws ArchiverException {
205         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
206     }
207 
208     private String getMethodName() {
209         final NullPointerException npe = new NullPointerException();
210         final StackTraceElement[] trace = npe.getStackTrace();
211         final StackTraceElement methodElement = trace[1];
212         return methodElement.getMethodName() + " (archiver line: " + methodElement.getLineNumber() + ")";
213     }
214 }