View Javadoc
1   /*
2    * Copyright (C) 2009 Jayway AB
3    * Copyright (C) 2007-2008 JVending Masa
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package com.simpligility.maven.plugins.android.phase01generatesources;
18  
19  import java.io.File;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.net.URLClassLoader;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Creates a ClassLoader from a Collection of classpath elements.
28   *
29   * @author William Ferguson - william.ferguson@xandar.com.au
30   */
31  final class ClassLoaderFactory
32  {
33      private final List<String> classpathElements;
34  
35      ClassLoaderFactory( List<String> classpathElements )
36      {
37          this.classpathElements = classpathElements;
38      }
39  
40      /**
41       * @return ClassLoader containing the classpaths.
42       */
43      ClassLoader create()
44      {
45          final List<URL> urls = new ArrayList<>();
46          for ( final String element : classpathElements )
47          {
48              try
49              {
50                  urls.add( new File( element ).toURI().toURL() );
51              }
52              catch ( MalformedURLException e )
53              {
54                  throw new IllegalArgumentException( "Could not resolve dependency : " + element, e );
55              }
56          }
57          return new URLClassLoader(
58                  urls.toArray( new URL[urls.size()] ),
59                  Thread.currentThread().getContextClassLoader()
60          );
61      }
62  }