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.InputStream;
22 import java.net.URL;
23 import java.util.Iterator;
24 import javax.faces.application.ResourceVisitOption;
25 import javax.faces.context.FacesContext;
26 import org.apache.myfaces.shared.resource.ClassLoaderResourceLoaderIterator;
27 import org.apache.myfaces.shared.resource.ContractResourceLoader;
28 import org.apache.myfaces.shared.resource.ResourceMeta;
29 import org.apache.myfaces.shared.resource.ResourceMetaImpl;
30 import org.apache.myfaces.shared.util.ClassUtils;
31
32
33
34
35
36 public class ClassLoaderContractResourceLoader extends ContractResourceLoader
37 {
38
39 public ClassLoaderContractResourceLoader(String prefix)
40 {
41 super(prefix);
42 }
43
44 @Override
45 public String getLibraryVersion(String path, String contractName)
46 {
47 return null;
48 }
49
50 @Override
51 public InputStream getResourceInputStream(ResourceMeta resourceMeta)
52 {
53 InputStream is = null;
54 if (getPrefix() != null && !"".equals(getPrefix()))
55 {
56 String name = getPrefix() + '/' + resourceMeta.getContractName()
57 + '/' + resourceMeta.getResourceIdentifier();
58 is = getClassLoader().getResourceAsStream(name);
59 if (is == null)
60 {
61 is = this.getClass().getClassLoader().getResourceAsStream(name);
62 }
63 return is;
64 }
65 else
66 {
67 String name = resourceMeta.getContractName()
68 + '/' + resourceMeta.getResourceIdentifier();
69 is = getClassLoader().getResourceAsStream(name);
70 if (is == null)
71 {
72 is = this.getClass().getClassLoader().getResourceAsStream(name);
73 }
74 return is;
75 }
76 }
77
78 @Override
79 public URL getResourceURL(ResourceMeta resourceMeta)
80 {
81 URL url = null;
82 if (getPrefix() != null && !"".equals(getPrefix()))
83 {
84 String name = getPrefix() + '/' + resourceMeta.getContractName() +
85 '/' + resourceMeta.getResourceIdentifier();
86 url = getClassLoader().getResource(name);
87 if (url == null)
88 {
89 url = this.getClass().getClassLoader().getResource(name);
90 }
91 return url;
92 }
93 else
94 {
95 String name = resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier();
96 url = getClassLoader().getResource(name);
97 if (url == null)
98 {
99 url = this.getClass().getClassLoader().getResource(name);
100 }
101 return url;
102 }
103 }
104
105 @Override
106 public String getResourceVersion(String path, String contractName)
107 {
108 return null;
109 }
110
111 @Override
112 public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
113 String resourceName, String resourceVersion,
114 String contractName)
115 {
116 return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName,
117 resourceVersion, contractName);
118 }
119
120
121
122
123
124
125
126 protected ClassLoader getClassLoader()
127 {
128 return ClassUtils.getContextClassLoader();
129 }
130
131 @Override
132 public boolean libraryExists(String libraryName, String contractName)
133 {
134 if (getPrefix() != null && !"".equals(getPrefix()))
135 {
136 String name = getPrefix() + '/' +
137 contractName + '/' + libraryName;
138 URL url = getClassLoader().getResource(name);
139 if (url == null)
140 {
141 url = this.getClass().getClassLoader().getResource(name);
142 }
143 if (url != null)
144 {
145 return true;
146 }
147 }
148 else
149 {
150 String name = contractName + '/' + libraryName;
151 URL url = getClassLoader().getResource(name);
152 if (url == null)
153 {
154 url = this.getClass().getClassLoader().getResource(name);
155 }
156 if (url != null)
157 {
158 return true;
159 }
160 }
161 return false;
162 }
163
164 @Override
165 public Iterator<String> iterator(FacesContext facesContext, String path,
166 int maxDepth, ResourceVisitOption... options)
167 {
168 String basePath = path;
169
170 if (getPrefix() != null)
171 {
172 basePath = getPrefix() + '/' + (path.startsWith("/") ? path.substring(1) : path);
173 }
174
175 URL url = getClassLoader().getResource(basePath);
176
177 return new ClassLoaderResourceLoaderIterator(url, basePath, maxDepth, options);
178 }
179
180 }