Coverage Report - sk.baka.webvm.SearchResults
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchResults
0%
0/24
0%
0/8
1.846
SearchResults$CLResult
0%
0/23
0%
0/6
1.846
SearchResults$ResultsListView
0%
0/5
N/A
1.846
SearchResults$ResultsListView$DownloadableResultLink
0%
0/11
0%
0/2
1.846
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.Serializable;
 23  
 import java.net.URL;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.List;
 27  
 import org.apache.wicket.markup.ComponentTag;
 28  
 import org.apache.wicket.markup.MarkupStream;
 29  
 import org.apache.wicket.markup.html.basic.Label;
 30  
 import org.apache.wicket.markup.html.link.Link;
 31  
 import org.apache.wicket.markup.html.list.ListItem;
 32  
 import org.apache.wicket.markup.html.list.ListView;
 33  
 import org.apache.wicket.model.IModel;
 34  
 import sk.baka.webvm.analyzer.classloader.ClassLoaderUtils;
 35  
 import sk.baka.webvm.analyzer.classloader.ResourceLink;
 36  
 import sk.baka.webvm.wicket.WicketUtils;
 37  
 
 38  
 /**
 39  
  * Performs a search and posts search results.
 40  
  * @author Martin Vysny
 41  
  */
 42  
 public final class SearchResults extends WebVMPage {
 43  
 
 44  
     private static final long serialVersionUID = 1L;
 45  
 
 46  
     /**
 47  
      * Creates the search page and performs the search.
 48  
      * @param searchQuery the query to use. Used as substring in classloader resources names search.
 49  
      */
 50  
     public SearchResults(final String searchQuery) {
 51  0
         super();
 52  0
         border.add(new Label("searchQuery", searchQuery));
 53  0
         final List<CLResult> results = performSearch(searchQuery);
 54  0
         border.add(new ResultsListView("classpathItems", results));
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Performs a search in URLs of all known class loaders.
 59  
      * @param searchQuery a substring to search for, must not be null
 60  
      * @return a mixed list of {@link ResourceLink}s (if search went OKay) and {@link String} (if exception occured in the search process).
 61  
      */
 62  
     private List<CLResult> performSearch(String searchQuery) {
 63  0
         final List<CLResult> result = new ArrayList<CLResult>();
 64  0
         int clIndex = 0;
 65  0
         for (ClassLoader loader = Thread.currentThread().getContextClassLoader(); loader != null; loader = loader.getParent()) {
 66  0
             clIndex++;
 67  0
             final URL[] urls = ClassLoaderUtils.getURLs(loader);
 68  0
             for (final URL url : urls) {
 69  0
                 final File file = toFile(url);
 70  0
                 if (file == null) {
 71  0
                     continue;
 72  
                 }
 73  0
                 final ResourceLink root = ResourceLink.newFor(file);
 74  
                 try {
 75  0
                     final List<ResourceLink> search = root.search(searchQuery);
 76  
                     // if the root file name itself matches the search query, add it to the result list
 77  0
                     if (root.getContainer().getName().toLowerCase().contains(searchQuery.toLowerCase())) {
 78  0
                         search.add(0, root);
 79  
                     }
 80  0
                     result.addAll(CLResult.from(search, loader, clIndex));
 81  0
                 } catch (Exception ex) {
 82  0
                     result.add(CLResult.from(ex, loader, clIndex));
 83  0
                 }
 84  
             }
 85  
         }
 86  
         // sort the list
 87  0
         Collections.sort(result);
 88  0
         return result;
 89  
     }
 90  
 
 91  
     /**
 92  
      * The classloader search result item, contains either a resource link or an error message.
 93  
      */
 94  0
     private static class CLResult implements Comparable<CLResult>, Serializable {
 95  
 
 96  
         private static final long serialVersionUID = 1L;
 97  
         /**
 98  
          * A link to the resource item.
 99  
          */
 100  
         public ResourceLink res;
 101  
         /**
 102  
          * If we were unable to obtain a resource link then this is the error message.
 103  
          */
 104  
         public String error;
 105  
         /**
 106  
          * Classloader number, 1 is the context classloader, 2 is its parent etc.
 107  
          */
 108  
         public int clIndex;
 109  
 
 110  
         /**
 111  
          * Converts a list of resource links.
 112  
          * @param source the source list, must not be null
 113  
          * @param cl originating class laoder.
 114  
          * @param clIndex Classloader number, 1 is the context classloader, 2 is its parent etc.
 115  
          * @return non-null converted list.
 116  
          */
 117  
         public static List<CLResult> from(final List<? extends ResourceLink> source, final ClassLoader cl, final int clIndex) {
 118  0
             final List<CLResult> result = new ArrayList<CLResult>(source.size());
 119  0
             for (final ResourceLink res : source) {
 120  0
                 final CLResult cresult = new CLResult();
 121  0
                 cresult.res = res;
 122  0
                 cresult.clIndex = clIndex;
 123  0
                 result.add(cresult);
 124  0
             }
 125  0
             return result;
 126  
         }
 127  
 
 128  
         /**
 129  
          * Converts a throwable.
 130  
          * @param t the throwable.
 131  
          * @param cl originating class laoder.
 132  
          * @param clIndex Classloader number, 1 is the context classloader, 2 is its parent etc.
 133  
          * @return non-null result.
 134  
          */
 135  
         public static CLResult from(final Throwable t, final ClassLoader cl, final int clIndex) {
 136  0
             final CLResult result = new CLResult();
 137  0
             result.error = t.toString();
 138  0
             result.clIndex = clIndex;
 139  0
             return result;
 140  
         }
 141  
 
 142  
         public int compareTo(CLResult o) {
 143  0
             final String s1 = toString();
 144  0
             final String s2 = o.toString();
 145  0
             return s1.compareToIgnoreCase(s2);
 146  
         }
 147  
 
 148  
         @Override
 149  
         public String toString() {
 150  0
             return res != null ? "[" + clIndex + "] " + res.getFullName() : error;
 151  
         }
 152  
 
 153  
         @Override
 154  
         public boolean equals(Object obj) {
 155  0
             if (!(obj instanceof CLResult)) {
 156  0
                 return false;
 157  
             }
 158  0
             final String s1 = toString();
 159  0
             final String s2 = obj.toString();
 160  0
             return s1.equals(s2);
 161  
         }
 162  
 
 163  
         @Override
 164  
         public int hashCode() {
 165  0
             return toString().hashCode();
 166  
         }
 167  
     }
 168  
 
 169  
     /**
 170  
      * Shows search result list.
 171  
      */
 172  
     private static class ResultsListView extends ListView<CLResult> {
 173  
 
 174  
         public ResultsListView(String id, List<? extends CLResult> list) {
 175  0
             super(id, list);
 176  0
         }
 177  
         private static final long serialVersionUID = 1L;
 178  
 
 179  
         @Override
 180  
         protected void populateItem(ListItem<CLResult> item) {
 181  0
             final CLResult resLink = item.getModelObject();
 182  0
             item.add(new DownloadableResultLink("classpathItem", item.getModel(), resLink));
 183  0
         }
 184  
 
 185  
         private static class DownloadableResultLink extends Link<CLResult> {
 186  
 
 187  
             private final CLResult resLink;
 188  
 
 189  
             public DownloadableResultLink(String id, IModel<CLResult> model, CLResult resLink) {
 190  0
                 super(id, model);
 191  0
                 this.resLink = resLink;
 192  0
             }
 193  
             private static final long serialVersionUID = 1L;
 194  
 
 195  
             @Override
 196  
             protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
 197  0
                 final CLResult model = getModelObject();
 198  0
                 final String caption = model.toString();
 199  0
                 replaceComponentTagBody(markupStream, openTag, caption);
 200  0
             }
 201  
 
 202  
             @Override
 203  
             public void onClick() {
 204  0
                 if (resLink.res == null) {
 205  0
                     return;
 206  
                 }
 207  0
                 WicketUtils.redirectTo(resLink.res);
 208  0
             }
 209  
         }
 210  
     }
 211  
 }