View Javadoc
1   package org.apache.maven.shared.io.location;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.net.URL;
23  
24  import org.apache.maven.shared.io.logging.MessageHolder;
25  
26  /**
27   * classpath resource locator strategy.
28   *
29   */
30  public class ClasspathResourceLocatorStrategy
31      implements LocatorStrategy
32  {
33  
34      private String tempFilePrefix = "location.";
35  
36      private String tempFileSuffix = ".cpurl";
37  
38      private boolean tempFileDeleteOnExit = true;
39  
40      /**
41       * Create instance.
42       */
43      public ClasspathResourceLocatorStrategy()
44      {
45      }
46  
47      /**
48       * @param tempFilePrefix Prefix.
49       * @param tempFileSuffix Suffix.
50       * @param tempFileDeleteOnExit delete on exit.
51       */
52      public ClasspathResourceLocatorStrategy( String tempFilePrefix, String tempFileSuffix,
53                                               boolean tempFileDeleteOnExit )
54      {
55          this.tempFilePrefix = tempFilePrefix;
56          this.tempFileSuffix = tempFileSuffix;
57          this.tempFileDeleteOnExit = tempFileDeleteOnExit;
58      }
59  
60      /** {@inheritDoc} */
61      public Location resolve( String locationSpecification, MessageHolder messageHolder )
62      {
63          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
64  
65          URL resource = cloader.getResource( locationSpecification );
66  
67          Location location = null;
68  
69          if ( resource != null )
70          {
71              location = new URLLocation( resource, locationSpecification, tempFilePrefix, tempFileSuffix,
72                                          tempFileDeleteOnExit );
73          }
74          else
75          {
76              messageHolder.addMessage( "Failed to resolve classpath resource: " + locationSpecification
77                  + " from classloader: " + cloader );
78          }
79  
80          return location;
81      }
82  
83  }