Skip to content

Commit 67ef7e2

Browse files
committed
Fixed annotation scanning to correctly work with the jetty-maven-plugin and the 'jetty:run' goal
1 parent 44acd23 commit 67ef7e2

3 files changed

Lines changed: 71 additions & 69 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/config/annotation/AbstractClassFinder.java‎

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -78,83 +78,45 @@ public AbstractClassFinder(ServletContext servletContext, ClassLoader classLoade
7878
}
7979

8080
/**
81-
* Creates a webapp relative name from an URL. This method requires the
82-
* caller do supply a known prefix of the resulting name. Class files for
83-
* example would have a prefix like <code>/WEB-INF/classes</code>
81+
* Strip everything up to and including a given prefix from a string.
8482
*
83+
* @param str
84+
* The string to process
8585
* @param prefix
86-
* The first part of the resulting name.
87-
* @param url
88-
* An {@link URL} pointing to a webapp resources
89-
* @return the webapp relative name
86+
* The prefix
87+
* @return the stripped string or <code>null</code> if the prefix has not
88+
* been found
9089
*/
91-
protected static String getWebappRelativeName(URL url, String prefix)
90+
protected String stripKnownPrefix(String str, String prefix)
9291
{
9392

94-
// we are only interested in the path
95-
String directoryPath = url.getPath();
96-
97-
// we remove everything up to the prefix
98-
// (Example for the prefix: "/WEB-INF/classes/")
99-
int startIndex = directoryPath.lastIndexOf(prefix);
100-
101-
// prefix not found?
102-
if (startIndex == -1)
93+
int startIndex = str.lastIndexOf(prefix);
94+
if (startIndex != -1)
10395
{
104-
throw new IllegalArgumentException("Cannot find prefix '" + prefix + "' in URL: " + url.toString());
96+
return str.substring(startIndex + prefix.length());
10597
}
10698

107-
return directoryPath.substring(startIndex);
108-
99+
return null;
109100
}
110-
101+
111102
/**
112103
* <p>
113104
* Creates a FQCN from an {@link URL} representing a <code>.class</code>
114105
* file.
115106
* </p>
116-
* <p>
117-
* You may optionally supply a prefix. In this case all characters up to the
118-
* prefix and the prefix itself will be removed from the file name. For
119-
* classes in a web application you may for example use
120-
* <code>/WEB-INF/lib/</code> as a prefix.
121-
* </p>
122-
*
107+
*
123108
* @param url
124109
* The path of the class file
125-
* @param prefix
126-
* optional prefix
127110
* @return the FQCN of the class
128111
*/
129-
protected static String getClassName(String filename, String prefix)
112+
protected static String getClassName(String filename)
130113
{
131114

132-
// start index
133-
int startIndex = 0;
134-
135-
// optional: remove prefix
136-
if (prefix != null && prefix.length() > 0)
137-
{
138-
139-
// find prefix position
140-
int prefixLastIndex = filename.lastIndexOf(prefix);
141-
142-
// prefix not found?
143-
if (prefixLastIndex == -1)
144-
{
145-
throw new IllegalArgumentException("Cannot find prefix '" + prefix + "' in filename: " + filename);
146-
}
147-
148-
// set correct startIndex
149-
startIndex = prefixLastIndex + prefix.length();
150-
151-
}
152-
153115
// end index is just before ".class"
154116
int endIndex = filename.length() - ".class".length();
155117

156118
// extract relevant part of the path
157-
String relativePath = filename.substring(startIndex, endIndex);
119+
String relativePath = filename.substring(0, endIndex);
158120

159121
// replace / by . to create FQCN
160122
return relativePath.replace('/', '.');

‎core/src/main/java/com/ocpsoft/pretty/faces/config/annotation/WebClassesFinder.java‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void findClasses(PrettyAnnotationHandler handler)
6565
}
6666

6767
// call recursive directory processing method
68-
processDirectory(classesFolderUrl, handler);
68+
processDirectory(classesFolderUrl, classesFolderUrl, handler);
6969

7070
}
7171
catch (MalformedURLException e)
@@ -88,20 +88,23 @@ public void findClasses(PrettyAnnotationHandler handler)
8888
* @throws MalformedURLException
8989
* for invalid URLs
9090
*/
91-
@SuppressWarnings("unchecked")
92-
protected void processDirectory(URL directoryUrl, PrettyAnnotationHandler handler) throws MalformedURLException
91+
protected void processDirectory(URL classesFolderUrl, URL directoryUrl, PrettyAnnotationHandler handler) throws MalformedURLException
9392
{
93+
94+
// only the path of the classes folder URL is required in this method
95+
String classesFolderPath = classesFolderUrl.getPath();
96+
9497
// log directory name on trace level
9598
if (log.isTraceEnabled())
9699
{
97100
log.trace("Processing directory: " + directoryUrl.toString());
98101
}
99102

100-
// build directory name relative to webapp root
101-
String relativeName = getWebappRelativeName(directoryUrl, CLASSES_FOLDER);
102-
103+
// get the directory name relative to the '/WEB-INF/classes/' folder
104+
String relativeDirectoryName = getPathRelativeToClassesFolder(directoryUrl.getPath(), classesFolderPath);
105+
103106
// call getResourcePaths to get directory entries
104-
Set paths = servletContext.getResourcePaths(relativeName);
107+
Set<?> paths = servletContext.getResourcePaths(CLASSES_FOLDER+relativeDirectoryName);
105108

106109
// loop over all entries of the directory
107110
for (Object relativePath : paths)
@@ -114,8 +117,11 @@ protected void processDirectory(URL directoryUrl, PrettyAnnotationHandler handle
114117
if (entryUrl.getPath().endsWith(".class"))
115118
{
116119

117-
// get class name
118-
String className = getClassName(entryUrl.toString(), CLASSES_FOLDER);
120+
// the name of the entry relative to the '/WEB-INF/classes/' folder
121+
String entryRelativeName = getPathRelativeToClassesFolder(entryUrl.getPath(), classesFolderPath);
122+
123+
// build class name from relative name
124+
String className = getClassName(entryRelativeName);
119125

120126
// check filter
121127
if (mustProcessClass(className))
@@ -174,10 +180,48 @@ protected void processDirectory(URL directoryUrl, PrettyAnnotationHandler handle
174180
{
175181

176182
// walk down the directory
177-
processDirectory(entryUrl, handler);
183+
processDirectory(classesFolderUrl, entryUrl, handler);
178184

179185
}
180186
}
181187
}
182188

189+
/**
190+
* This method will create a path relative to the '/WEB-INF/classes/' folder
191+
* for the given path. It will first try to find the '/WEB-INF/classes/'
192+
* suffix in the path to do so. If this suffix cannot be found (can happen
193+
* when using the jetty-maven-plugin with 'jetty:run' goal), the method will
194+
* try to build the relative name by stripping the path of the
195+
* '/WEB-INF/classes/' folder which must be supplied to the method. The
196+
* method will throw an {@link IllegalArgumentException} if the relative path
197+
* could not be build.
198+
*
199+
* @param path
200+
* The path to build the relative path for
201+
* @param classesFolderPath
202+
* the known path of the '/WEB-INF/classes/' folder.
203+
* @return the relative name of the path
204+
*/
205+
private String getPathRelativeToClassesFolder(String path, String classesFolderPath)
206+
{
207+
208+
// first try to find the '/WEB-INF/classes' suffix
209+
String result = stripKnownPrefix(path, CLASSES_FOLDER);
210+
211+
// alternative: try to strip the full path of the '/WEB-INF/classes/' folder
212+
if (result == null)
213+
{
214+
result = stripKnownPrefix(path, classesFolderPath);
215+
}
216+
217+
// none of the two methods worked?
218+
if (result == null)
219+
{
220+
throw new IllegalArgumentException("Unable to build path relative to '/WEB-INF/classes/' from: "+path);
221+
}
222+
223+
return result;
224+
225+
}
226+
183227
}

‎core/src/main/java/com/ocpsoft/pretty/faces/config/annotation/WebLibFinder.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public WebLibFinder(ServletContext servletContext, ClassLoader classLoader, Pack
5252
* com.ocpsoft.pretty.faces.config.annotation.ClassFinder#findClasses(com
5353
* .ocpsoft.pretty.faces.config.annotation.PrettyAnnotationHandler)
5454
*/
55-
@SuppressWarnings("unchecked")
5655
public void findClasses(PrettyAnnotationHandler classHandler)
5756
{
5857

@@ -70,11 +69,8 @@ public void findClasses(PrettyAnnotationHandler classHandler)
7069
return;
7170
}
7271

73-
// build directory name relative to webapp root
74-
String relativeName = getWebappRelativeName(libFolderUrl, LIB_FOLDER);
75-
7672
// call getResourcePaths to get directory entries
77-
Set paths = servletContext.getResourcePaths(relativeName);
73+
Set<?> paths = servletContext.getResourcePaths(LIB_FOLDER);
7874

7975
// loop over all entries of the directory
8076
for (Object relativePath : paths)
@@ -135,7 +131,7 @@ private void processJarFile(URL jarUrl, PrettyAnnotationHandler classHandler)
135131
{
136132

137133
// generate FQCN from entry
138-
String className = getClassName(jarEntry.getName(), null);
134+
String className = getClassName(jarEntry.getName());
139135

140136
// check name against PackageFilter
141137
if (mustProcessClass(className))

0 commit comments

Comments
 (0)