gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / MutableURLClassLoader.java @ 1099
History | View | Annotate | Download (2.93 KB)
1 |
|
---|---|
2 |
package org.gvsig.scripting.impl; |
3 |
|
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.net.URL; |
8 |
import java.net.URLClassLoader; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Arrays; |
11 |
import java.util.Enumeration; |
12 |
import java.util.List; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.slf4j.Logger; |
15 |
import org.slf4j.LoggerFactory; |
16 |
|
17 |
|
18 |
public class MutableURLClassLoader extends URLClassLoader { |
19 |
protected static final Logger logger = LoggerFactory.getLogger(MutableURLClassLoader.class); |
20 |
|
21 |
public MutableURLClassLoader(List<URL> urls, ClassLoader parent) { |
22 |
super(urls.toArray(new URL[urls.size()]),parent); |
23 |
} |
24 |
|
25 |
public void addUrls(List<URL> urls) { |
26 |
List<URL> currentsURLs = new ArrayList<>(); |
27 |
currentsURLs.addAll(Arrays.asList(this.getURLs())); |
28 |
|
29 |
for (URL url : urls) { |
30 |
if( ! currentsURLs.contains(url) ) {
|
31 |
this.addURL(url);
|
32 |
currentsURLs.add(url); |
33 |
} |
34 |
} |
35 |
} |
36 |
|
37 |
public boolean needRecompile(String name, URL url) { |
38 |
if( url!=null && name.endsWith("$py.class") ) { |
39 |
if( StringUtils.equalsIgnoreCase(url.getProtocol(),"file") ) { |
40 |
String path = url.getPath();
|
41 |
File binary = new File(path); |
42 |
File source = new File(StringUtils.left(path, path.length()-9)+".py"); |
43 |
if( !binary.exists() || org.apache.commons.io.FileUtils.isFileNewer(source, binary) ) {
|
44 |
return true; |
45 |
} |
46 |
} |
47 |
} |
48 |
return false; |
49 |
} |
50 |
|
51 |
@Override
|
52 |
public URL getResource(String name) { |
53 |
URL x = super.getResource(name); |
54 |
if( needRecompile(name, x) ) {
|
55 |
logger.trace("getResoure("+name+") -> null (need recompile)."); |
56 |
return null; |
57 |
} |
58 |
logger.trace("getResoure("+name+") -> "+x); |
59 |
return x;
|
60 |
} |
61 |
|
62 |
@Override
|
63 |
public InputStream getResourceAsStream(String name) { |
64 |
URL url = super.getResource(name); |
65 |
if( needRecompile(name, url) ) {
|
66 |
logger.trace("getResourceAsStream("+name+") -> null (need recompile)."); |
67 |
return null; |
68 |
} |
69 |
InputStream x = super.getResourceAsStream(name); |
70 |
logger.trace("getResourceAsStream("+name+") -> "+(x==null? "null":"(stream)")); |
71 |
return x;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public Enumeration<URL> getResources(String name) throws IOException { |
76 |
Enumeration<URL> urls = super.getResources(name); |
77 |
logger.info("getResources("+name+") -> "+(urls==null? "null":urls)); |
78 |
return urls;
|
79 |
} |
80 |
|
81 |
public String getResourcePath(String name) { |
82 |
if( name.startsWith("__pyclasspath__/") ) { |
83 |
name = name.substring(16);
|
84 |
} |
85 |
URL url = this.getResource(name); |
86 |
if( url == null ) { |
87 |
return name;
|
88 |
} |
89 |
File f = org.apache.commons.io.FileUtils.toFile(url);
|
90 |
return f.getAbsolutePath();
|
91 |
} |
92 |
} |