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      public void addArchivedFileSet(final @Nonnull File archiveFile, final String prefix) throws ArchiverException {
68          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public void addArchivedFileSet(final File archiveFile, final String[] includes, final String[] excludes)
76              throws ArchiverException {
77          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void addArchivedFileSet(final @Nonnull File archiveFile) throws ArchiverException {
85          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public void addDirectory(
93              final @Nonnull File directory, final String prefix, final String[] includes, final String[] excludes)
94              throws ArchiverException {
95          debug("DRY RUN: Skipping delegated call to: " + getMethodName());
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void addSymlink(String symlinkName, String symlinkDestination) throws ArchiverException {
103         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void addSymlink(String symlinkName, int permissions, String symlinkDestination) throws ArchiverException {
111         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void addDirectory(final @Nonnull File directory, final String prefix) throws ArchiverException {
119         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public void addDirectory(final @Nonnull File directory, final String[] includes, final String[] excludes)
127             throws ArchiverException {
128         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public void addDirectory(final @Nonnull File directory) throws ArchiverException {
136         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName, final int permissions)
144             throws ArchiverException {
145         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName) throws ArchiverException {
153         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public void createArchive() throws ArchiverException, IOException {
161         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public void setDotFileDirectory(final File dotFileDirectory) {
169         throw new UnsupportedOperationException("Undocumented feature of plexus-archiver; this is not yet supported.");
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public void addArchivedFileSet(final ArchivedFileSet fileSet) throws ArchiverException {
177 
178         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
179     }
180 
181     @Override
182     public void addArchivedFileSet(ArchivedFileSet fileSet, Charset charset) throws ArchiverException {
183         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void addFileSet(final @Nonnull FileSet fileSet) throws ArchiverException {
191         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
192     }
193 
194     @Override
195     public void addResource(PlexusIoResource resource, String destFileName, int permissions) throws ArchiverException {
196         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
197     }
198 
199     @Override
200     public void addResources(PlexusIoResourceCollection resources) throws ArchiverException {
201         debug("DRY RUN: Skipping delegated call to: " + getMethodName());
202     }
203 
204     private String getMethodName() {
205         final NullPointerException npe = new NullPointerException();
206         final StackTraceElement[] trace = npe.getStackTrace();
207         final StackTraceElement methodElement = trace[1];
208         return methodElement.getMethodName() + " (archiver line: " + methodElement.getLineNumber() + ")";
209     }
210 }