Updated Branches: refs/heads/wicket-6.x 7d8313f7a -> c47a15c47
WICKET-5322 "Source code" link doesn't work at wicket-library.com Simplify SourcesPage. Add log4j as compile dependency because the examples failed at start with ClassNotFoundException org/apache/log4j/Level Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c47a15c4 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c47a15c4 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c47a15c4 Branch: refs/heads/wicket-6.x Commit: c47a15c47ed74d842f6bf0d987346de92fab02d2 Parents: 7d8313f Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon Aug 19 22:17:05 2013 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Mon Aug 19 22:17:05 2013 +0300 ---------------------------------------------------------------------- wicket-examples/pom.xml | 5 + .../wicket/examples/source/SourcesPage.java | 210 +++++-------------- 2 files changed, 58 insertions(+), 157 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/c47a15c4/wicket-examples/pom.xml ---------------------------------------------------------------------- diff --git a/wicket-examples/pom.xml b/wicket-examples/pom.xml index c1cb2de..db6da87 100644 --- a/wicket-examples/pom.xml +++ b/wicket-examples/pom.xml @@ -117,6 +117,11 @@ <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.17</version> + </dependency> + <dependency> <groupId>org.jboss.weld</groupId> <artifactId>weld-core</artifactId> <version>1.1.9.Final</version> http://git-wip-us.apache.org/repos/asf/wicket/blob/c47a15c4/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java index c839c24..6b0d1dc 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/source/SourcesPage.java @@ -43,19 +43,19 @@ import org.apache.wicket.ajax.attributes.AjaxCallListener; import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; import org.apache.wicket.ajax.markup.html.AjaxFallbackLink; import org.apache.wicket.authorization.UnauthorizedInstantiationException; +import org.apache.wicket.core.util.lang.WicketObjects; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.model.AbstractReadOnlyModel; +import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.request.http.handler.ErrorCodeRequestHandler; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.io.IOUtils; -import org.apache.wicket.util.io.Streams; import org.apache.wicket.util.lang.PackageName; -import org.apache.wicket.util.string.AppendingStringBuffer; -import org.apache.wicket.util.string.Strings; +import org.apache.wicket.util.string.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,16 +71,9 @@ public class SourcesPage extends WebPage /** * Model for retrieving the source code from the classpath of a packaged resource. */ - public class SourceModel extends AbstractReadOnlyModel<String> + private class SourceModel extends AbstractReadOnlyModel<String> { /** - * Constructor. - */ - public SourceModel() - { - } - - /** * Returns the contents of the file loaded from the classpath. * * @return the contents of the file identified by name @@ -89,23 +82,18 @@ public class SourcesPage extends WebPage public String getObject() { // name contains the name of the selected file - if (Strings.isEmpty(name) && - Strings.isEmpty(getPage().getRequest() - .getRequestParameters() - .getParameterValue(SOURCE) - .toOptionalString())) + StringValue sourceParam = getPageParameters().get(SOURCE); + if (Strings.isEmpty(name) && sourceParam.isEmpty()) { return ""; } + BufferedReader br = null; String source = null; try { - StringBuilder sb = new StringBuilder(); - source = (name != null) ? name : getPage().getRequest() - .getRequestParameters() - .getParameterValue(SOURCE) - .toOptionalString(); + StringBuilder sb = new StringBuilder(); + source = (name != null) ? name : sourceParam.toString(); InputStream resourceAsStream = getPageTargetClass().getResourceAsStream(source); if (resourceAsStream == null) { @@ -116,7 +104,7 @@ public class SourcesPage extends WebPage while (br.ready()) { sb.append(br.readLine()); - sb.append("\n"); + sb.append('\n'); } int lastDot = source.lastIndexOf('.'); if (lastDot != -1) @@ -128,9 +116,8 @@ public class SourcesPage extends WebPage return renderer.highlight(source, sb.toString(), "UTF-8", true); } } - return Strings.escapeMarkup(sb.toString(), false, true) - .toString() - .replaceAll("\n", "<br />"); + CharSequence escaped = Strings.escapeMarkup(sb.toString(), false, true); + return Strings.replaceAll(escaped, "\n", "<br />").toString(); } catch (IOException e) { @@ -149,119 +136,32 @@ public class SourcesPage extends WebPage /** * Model for retrieving the contents of a package directory from the class path. */ - public class PackagedResourcesModel extends AbstractReadOnlyModel<List<String>> + public class PackagedResourcesModel extends LoadableDetachableModel<List<String>> { - private final List<String> resources = new ArrayList<String>(); - - /** - * Constructor. - */ - public PackagedResourcesModel() - { - } - - /** - * Clears the list to save space. - */ - protected void onDetach() - { - resources.clear(); - } - /** * Returns the list of resources found in the package of the page. * * @return the list of resources found in the package of the page. */ @Override - public List<String> getObject() + protected List<String> load() { - if (resources.isEmpty()) - { - get(getPageTargetClass()); -// PackageName name = PackageName.forClass(page); -// ClassLoader loader = page.getClassLoader(); -// String path = Strings.replaceAll(name.getName(), ".", "/").toString(); -// try -// { -// // gives the urls for each place where the package -// // path could be found. There could be multiple -// // jar files containing the same package, so each -// // jar file has its own url. -// -// Enumeration<URL> urls = loader.getResources(path); -// while (urls.hasMoreElements()) -// { -// URL url = urls.nextElement(); -// -// // the url points to the directory structure -// // embedded in the classpath. -// -// getPackageContents(url); -// } -// } -// catch (IOException e) -// { -// log.error("Unable to read resource for: " + path, e); -// } - } - return resources; + return get(getPageTargetClass()); } - /** - * Retrieves the package contents for the given URL. - * - * @param packageListing - * the url to list. - */ - private void getPackageContents(URL packageListing) - { - BufferedReader br = null; - try - { - InputStream openStream = Streams.readNonCaching(packageListing); - if (openStream == null) - { - return; - } - br = new BufferedReader(new InputStreamReader(openStream)); - - while (br.ready()) - { - String listing = br.readLine(); - String extension = Strings.afterLast(listing, '.'); - if (!listing.endsWith("class")) - { - resources.add(listing); - } - } - } - catch (IOException e) - { - log.error("Unable to get package content: " + packageListing.toString(), e); - } - finally - { - IOUtils.closeQuietly(br); - } - } - - private final void addResources(final Class<?> scope, - final AppendingStringBuffer relativePath, final File dir) + private final void addResources(final AppendingStringBuffer relativePath, final File dir, List<String> resources) { File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { - addResources(scope, - new AppendingStringBuffer(relativePath).append(file.getName()).append('/'), - file); + addResources(new AppendingStringBuffer(relativePath).append(file.getName()).append('/'), + file, resources); } else { String name = file.getName(); - String extension = Strings.afterLast(name, '.'); if (!name.endsWith("class")) { resources.add(relativePath + name); @@ -270,8 +170,10 @@ public class SourcesPage extends WebPage } } - private void get(Class<?> scope) + private List<String> get(Class<?> scope) { + List<String> resources = new ArrayList<String>(); + String packageRef = Strings.replaceAll(PackageName.forClass(scope).getName(), ".", "/") .toString(); ClassLoader loader = scope.getClassLoader(); @@ -286,7 +188,7 @@ public class SourcesPage extends WebPage if (connection instanceof JarURLConnection) { JarFile jf = ((JarURLConnection)connection).getJarFile(); - scanJarFile(scope, packageRef, jf); + scanJarFile(packageRef, jf, resources); } else { @@ -328,15 +230,15 @@ public class SourcesPage extends WebPage // ".zip" log.debug("trying the filename: " + filename + " to load as a zip/jar."); JarFile jarFile = new JarFile(filename, false); - scanJarFile(scope, packageRef, jarFile); - return; + scanJarFile(packageRef, jarFile, resources); + return resources; } if (!basedir.isDirectory()) { throw new IllegalStateException( "unable to read resources from directory " + basedir); } - addResources(scope, new AppendingStringBuffer(), basedir); + addResources(new AppendingStringBuffer(), basedir, resources); } } } @@ -345,9 +247,11 @@ public class SourcesPage extends WebPage throw new WicketRuntimeException(e); } Collections.sort(resources); + + return resources; } - private void scanJarFile(Class<?> scope, String packageRef, JarFile jf) + private void scanJarFile(String packageRef, JarFile jf, List<String> resources) { Enumeration<JarEntry> enumeration = jf.entries(); while (enumeration.hasMoreElements()) @@ -357,7 +261,6 @@ public class SourcesPage extends WebPage if (name.startsWith(packageRef)) { name = name.substring(packageRef.length() + 1); - String extension = Strings.afterLast(name, '.'); if (!name.endsWith("class")) { resources.add(name); @@ -504,11 +407,12 @@ public class SourcesPage extends WebPage * * Construct. * - * @param <C> * @param params */ - public <C extends Page> SourcesPage(final PageParameters params) + public SourcesPage(final PageParameters params) { + super(params); + filename = new Label("filename", new AbstractReadOnlyModel<String>() { @@ -532,7 +436,7 @@ public class SourcesPage extends WebPage /** * * @param page - * @return PageParamets for reconstructing the bookmarkable page. + * @return PageParameters for reconstructing the bookmarkable page. */ public static PageParameters generatePageParameters(Page page) { @@ -550,51 +454,43 @@ public class SourcesPage extends WebPage PageParameters p = new PageParameters(); p.set(PAGE_CLASS, clazz.getName()); if (fileName != null) + { p.set(SOURCE, fileName); + } return p; } - private String getPageParam() - { - return getPage().getRequest() - .getRequestParameters() - .getParameterValue(PAGE_CLASS) - .toOptionalString(); - } - private Class<? extends Page> getPageTargetClass() { if (page == null) { - try + String pageParam = getPageParameters().get(PAGE_CLASS).toOptionalString(); + if (pageParam == null) { - String pageParam = getPageParam(); - if (pageParam == null) + if (log.isErrorEnabled()) { - if (log.isErrorEnabled()) - { - log.error("key: " + PAGE_CLASS + " is null."); - } - getRequestCycle().replaceAllRequestHandlers( - new ErrorCodeRequestHandler(404, - "Could not find sources for the page you requested")); + log.error("key: " + PAGE_CLASS + " is null."); } - if (!pageParam.startsWith("org.apache.wicket.examples")) + getRequestCycle().replaceAllRequestHandlers( + new ErrorCodeRequestHandler(404, + "Could not find sources for the page you requested")); + } + else if (!pageParam.startsWith("org.apache.wicket.examples")) + { + if (log.isErrorEnabled()) { - if (log.isErrorEnabled()) - { - log.error("user is trying to access class: " + pageParam + - " which is not in the scope of org.apache.wicket.examples"); - } - throw new UnauthorizedInstantiationException(getClass()); + log.error("user is trying to access class: " + pageParam + + " which is not in the scope of org.apache.wicket.examples"); } - page = (Class<? extends Page>)Class.forName(getPageParam()); + throw new UnauthorizedInstantiationException(getClass()); } - catch (ClassNotFoundException e) + page = WicketObjects.resolveClass(pageParam); + + if (page == null) { getRequestCycle().replaceAllRequestHandlers( - new ErrorCodeRequestHandler(404, - "Could not find sources for the page you requested")); + new ErrorCodeRequestHandler(404, + "Could not find sources for the page you requested")); } } return page;
