1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.resource; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.net.URL; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Set; |
25 | |
import javax.faces.application.ResourceHandler; |
26 | |
import javax.faces.context.ExternalContext; |
27 | |
import org.apache.myfaces.shared.util.ClassUtils; |
28 | |
import org.apache.myfaces.shared.util.WebConfigParamUtils; |
29 | |
import org.apache.myfaces.spi.ResourceLibraryContractsProvider; |
30 | |
import org.apache.myfaces.view.facelets.util.Classpath; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class DefaultResourceLibraryContractsProvider extends ResourceLibraryContractsProvider |
37 | |
{ |
38 | |
private static final String META_INF_CONTRACTS_PREFIX = "META-INF/contracts/"; |
39 | |
private static final String META_INF_CONTRACTS_SUFFIX = "javax.faces.contract.xml"; |
40 | |
private static final String META_INF_CONTRACTS_FILE = "/javax.faces.contract.xml"; |
41 | |
private static final String CONTRACTS = "contracts"; |
42 | |
|
43 | |
@Override |
44 | |
public Set<String> getExternalContextResourceLibraryContracts(ExternalContext context) |
45 | |
throws IOException |
46 | |
{ |
47 | 0 | String directory = WebConfigParamUtils.getStringInitParameter(context, |
48 | |
ResourceHandler.WEBAPP_CONTRACTS_DIRECTORY_PARAM_NAME, CONTRACTS); |
49 | |
|
50 | 0 | if (directory.startsWith("/")) |
51 | |
{ |
52 | 0 | throw new IllegalStateException("javax.faces.WEBAPP_CONTRACTS_DIRECTORY cannot start with '/"); |
53 | |
} |
54 | 0 | if (directory.endsWith("/")) |
55 | |
{ |
56 | 0 | directory = directory.substring(0, directory.length()-1); |
57 | |
} |
58 | |
|
59 | 0 | directory = "/"+directory+"/"; |
60 | 0 | Set<String> contracts = new HashSet<String>(); |
61 | 0 | Set<String> paths = context.getResourcePaths(directory); |
62 | 0 | if (paths != null) |
63 | |
{ |
64 | 0 | for (String path : paths) |
65 | |
{ |
66 | 0 | if (path.endsWith("/")) |
67 | |
{ |
68 | |
|
69 | 0 | contracts.add(path.substring(directory.length(), path.length()-1)); |
70 | |
} |
71 | 0 | } |
72 | |
} |
73 | 0 | return contracts; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public Set<String> getClassloaderResourceLibraryContracts(ExternalContext context) |
78 | |
throws IOException |
79 | |
{ |
80 | 0 | Set<String> contracts = new HashSet<String>(); |
81 | 0 | URL[] urls = Classpath.search(getClassLoader(), |
82 | |
META_INF_CONTRACTS_PREFIX, META_INF_CONTRACTS_SUFFIX); |
83 | 0 | for (int i = 0; i < urls.length; i++) |
84 | |
{ |
85 | 0 | String urlString = urls[i].toExternalForm(); |
86 | 0 | int suffixPos = urlString.lastIndexOf(META_INF_CONTRACTS_FILE); |
87 | 0 | int slashPos = urlString.lastIndexOf('/', suffixPos-1); |
88 | 0 | if (suffixPos > 0 && slashPos > 0) |
89 | |
{ |
90 | 0 | contracts.add(urlString.substring(slashPos+1, suffixPos)); |
91 | |
} |
92 | |
} |
93 | 0 | return contracts; |
94 | |
} |
95 | |
|
96 | |
private ClassLoader getClassLoader() |
97 | |
{ |
98 | 0 | ClassLoader loader = ClassUtils.getContextClassLoader(); |
99 | 0 | if (loader == null) |
100 | |
{ |
101 | 0 | loader = this.getClass().getClassLoader(); |
102 | |
} |
103 | 0 | return loader; |
104 | |
} |
105 | |
} |