View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap.mojo;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.InputStream;
23  
24  /**
25   * The abstract base class for DSML or LDIF source files located on the class path or file system.
26   *
27   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
28   * @since 1.2.0
29   */
30  public abstract class AbstractSource implements Source {
31  
32      /**
33       * The prefix for class path resources.
34       */
35      private static final String CLASSPATH_PREFIX = "classpath:";
36      /**
37       * The length of the prefix for class path resources.
38       */
39      private static final int CLASSPATH_PREFIX_LENGTH = 10;
40      /**
41       * The location of the source on the class path or file system.
42       */
43      private String path;
44  
45      /**
46       * Initialise the source setting the location of the source on the class path or file system.
47       *
48       * @param path The source location.
49       */
50      protected AbstractSource(final String path) {
51          this.path = path;
52      }
53  
54      /**
55       * Method call by plexus to inject path after construction of the class.
56       *
57       * @param path The path of the source file.
58       */
59      public void set(String path) {
60          this.path = path;
61      }
62  
63      /**
64       * Open the resource or file and return the input stream. If the path is prefixed with classpath: then it is treated
65       * as a classpath resource. Otherwise, it is assumed to be a file system path.
66       *
67       * @return The input stream or {@code null} if the file or resource cannot be found.
68       */
69      public InputStream open() {
70          if (path.startsWith(CLASSPATH_PREFIX)) {
71              final ClassLoader loader = Thread.currentThread().getContextClassLoader();
72              return loader.getResourceAsStream(path.substring(CLASSPATH_PREFIX_LENGTH));
73          } else {
74              final File file = new File(path);
75              try {
76                  return new FileInputStream(file);
77              } catch (final FileNotFoundException e) {
78                  return null;
79              }
80          }
81      }
82  
83      /**
84       * Convert the source to a string.
85       *
86       * @return Returns the value of {@link #path}.
87       */
88      @Override
89      public final String toString() {
90          return path;
91      }
92  }