geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r1350782 - in /geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main: java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java webapp/WEB-INF/view/obrmanager/OBRManager.jsp
Date Fri, 15 Jun 2012 20:24:00 GMT
Author: gawor
Date: Fri Jun 15 20:23:59 2012
New Revision: 1350782

URL: http://svn.apache.org/viewvc?rev=1350782&view=rev
Log:
GERONIMO-6360: Additional OBR portlet improvmenets: 1) improve searching, 2) support 'package
capability' search, 3) sort results

Modified:
    geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java
    geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/webapp/WEB-INF/view/obrmanager/OBRManager.jsp

Modified: geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java?rev=1350782&r1=1350781&r2=1350782&view=diff
==============================================================================
--- geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java
(original)
+++ geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/java/org/apache/geronimo/console/obrmanager/OBRManagerPortlet.java
Fri Jun 15 20:23:59 2012
@@ -20,6 +20,9 @@ package org.apache.geronimo.console.obrm
 import java.io.IOException;
 import java.net.URI;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 import javax.portlet.ActionRequest;
@@ -32,11 +35,13 @@ import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import javax.portlet.WindowState;
 
+import org.apache.felix.bundlerepository.Capability;
 import org.apache.felix.bundlerepository.Repository;
 import org.apache.felix.bundlerepository.RepositoryAdmin;
 import org.apache.felix.bundlerepository.Resource;
 import org.apache.geronimo.console.BasePortlet;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -61,8 +66,6 @@ public class OBRManagerPortlet extends B
 
     private static final String SEARCH_TYPE_ALL = "ALL";
 
-    private static final String SEARCH_TYPE_SEARCH = "SEARCH";
-
     public void init(PortletConfig portletConfig) throws PortletException {
         super.init(portletConfig);
         helpView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/obrmanager/OBRManager.jsp");
@@ -119,10 +122,10 @@ public class OBRManagerPortlet extends B
             actionResponse.setRenderParameter("searchType", searchType);
         }
         else if (SERACH_ACTION.equals(action)) {
-            String searchType = SEARCH_TYPE_SEARCH;
             String searchString = actionRequest.getParameter("searchString");
+            actionResponse.setRenderParameter("searchString", searchString);        
+            String searchType = actionRequest.getParameter("searchType");
             actionResponse.setRenderParameter("searchType", searchType);
-            actionResponse.setRenderParameter("searchString", searchString);            
         }
         
     }
@@ -142,50 +145,162 @@ public class OBRManagerPortlet extends B
             Repository[] repos = repositoryAdmin.listRepositories();
             renderRequest.setAttribute("repos", repos);
 
-            String searchType = renderRequest.getParameter("searchType");
-            if (searchType != null && !"".equals(searchType)) {
-                if (SEARCH_TYPE_ALL.equals(searchType)) {
-                    Repository[] repoAll = repositoryAdmin.listRepositories();
-                    List<Resource> resourcesResult = new ArrayList<Resource>();
-                    
-                    for (Repository repo : repoAll) {
-                        Resource[] resources = repo.getResources();
-                        for (Resource reso : resources) {
-                            resourcesResult.add(reso);
+            try {
+                String searchType = renderRequest.getParameter("searchType");
+                if (searchType != null && !"".equals(searchType)) {
+                    if (SEARCH_TYPE_ALL.equals(searchType)) {
+                        Resource[] resources = getAllResources(repositoryAdmin);
+                        Arrays.sort(resources, ResourceComparator.INSTANCE);
+                        renderRequest.setAttribute("resources", resources);
+                    } else {
+                        String searchString = renderRequest.getParameter("searchString");
+                        if (searchString == null || searchString.length() == 0) {
+                            Resource[] resources = getAllResources(repositoryAdmin);
+                            Arrays.sort(resources, ResourceComparator.INSTANCE);
+                            renderRequest.setAttribute("resources", resources);
+                        } else {
+                            ResourceMatcher resourceMatcher = getResourceMatcher(searchType,
searchString);
+                            List<Resource> resourcesResult = queryResources(repositoryAdmin,
resourceMatcher);
+                            renderRequest.setAttribute("resources", resourcesResult);
                         }
                     }
-                    renderRequest.setAttribute("resources", resourcesResult);
-                    
+                }                
+            } catch (Exception e) {
+                addErrorMessage(renderRequest, getLocalizedString(renderRequest, "consolebase.obrmanager.err.actionError"),
e.getMessage());
+                logger.error("Exception", e);
+            } finally {
+                bundleContext.ungetService(reference);
+            }
+
+            obrManagerView.include(renderRequest, renderResponse);
+        }
+
+    }
+    
+    private List<Resource> queryResources(RepositoryAdmin repositoryAdmin, ResourceMatcher
resourceMatcher) {
+        List<Resource> resourcesResult = null;
+        Resource[] resources = getAllResources(repositoryAdmin);
+        if (resources != null) {
+            resourcesResult = new ArrayList<Resource>();
+            for (Resource resource : resources) {
+                if (resourceMatcher.match(resource)) {
+                    resourcesResult.add(resource);
                 }
-                else if (SEARCH_TYPE_SEARCH.equals(searchType)) {
-                    try {
-                        String searchString = renderRequest.getParameter("searchString");
-                        StringBuffer sb = new StringBuffer();
-                        
-                        if (searchString==null||"".equals(searchString)) {
-                            sb.append("(|(presentationname=*)(symbolicname=*))");
-                        }
-                        else {
-                            sb.append("(|(presentationname=*");
-                            sb.append(searchString);
-                            sb.append("*)(symbolicname=*");
-                            sb.append(searchString);
-                            sb.append("*))");
+            }       
+            Collections.sort(resourcesResult, ResourceComparator.INSTANCE);
+        } else {
+            resourcesResult = new ArrayList<Resource>(0);
+        }
+        return resourcesResult;
+    }
+    
+    private Resource[] getAllResources(RepositoryAdmin repositoryAdmin) {
+        try {
+            return repositoryAdmin.discoverResources("(symbolicname=*)");
+        } catch (InvalidSyntaxException e) {
+            throw new RuntimeException("Unexpected error", e);
+        }
+    }
+
+    private ResourceMatcher getResourceMatcher(String searchType, String searchString) {
+        if (searchType.equalsIgnoreCase("symbolic-name")) {
+            return new SymbolicNameMatcher(searchString);
+        } else if (searchType.equalsIgnoreCase("bundle-name")) {
+            return new BundleNameMatcher(searchString);
+        } else if (searchType.equalsIgnoreCase("package-capability")) {
+            return new PackageCapabilityMatcher(searchString);
+        } else {
+            throw new RuntimeException("Unsupported search type: " + searchType);
+        }
+    }
+    
+    private abstract static class ResourceMatcher {
+        
+        private String query;
+        
+        public ResourceMatcher(String query) {
+            this.query = query;
+        }
+        
+        abstract boolean match(Resource resource);
+        
+        protected boolean matchQuery(String name) {
+            if (name != null) {
+                return name.toLowerCase().contains(query);
+            } else {
+                return false;
+            }
+        }
+    }
+    
+    private static class SymbolicNameMatcher extends ResourceMatcher {
+
+        public SymbolicNameMatcher(String query) {
+            super(query);
+        }
+
+        public boolean match(Resource resource) {
+            return matchQuery(resource.getSymbolicName());
+        }
+        
+    }
+    
+    private static class BundleNameMatcher extends ResourceMatcher {
+
+        public BundleNameMatcher(String query) {
+            super(query);
+        }
+
+        public boolean match(Resource resource) {
+            return matchQuery(resource.getPresentationName());
+        }
+        
+    }
+    
+    private static class PackageCapabilityMatcher extends ResourceMatcher {
+
+        public PackageCapabilityMatcher(String query) {
+            super(query);
+        }
+
+        public boolean match(Resource resource) {
+            Capability[] capabilities = resource.getCapabilities();
+            if (capabilities != null) {
+                for (Capability capability : capabilities) {
+                    if (Capability.PACKAGE.equals(capability.getName())) {
+                        String packageName = (String) capability.getPropertiesAsMap().get(Capability.PACKAGE);
+                        if (matchQuery(packageName)) {
+                            return true;
                         }
-                        
-                        Resource[] resources = repositoryAdmin.discoverResources(sb.toString());
-                        renderRequest.setAttribute("resources", resources);
-                    } catch (Exception e) {
-                        addErrorMessage(renderRequest, getLocalizedString(renderRequest,
"consolebase.obrmanager.err.actionError"), e.getMessage());
-                        logger.error("Exception", e);
-                     } finally {
-                         bundleContext.ungetService(reference);
-                     }
+                    }
                 }
             }
-            obrManagerView.include(renderRequest, renderResponse);
+            return false;
         }
+        
+    }
+    
+    private static class ResourceComparator implements Comparator<Resource> {
 
+        public static final ResourceComparator INSTANCE = new ResourceComparator();
+        
+        @Override
+        public int compare(Resource resource1, Resource resource2) {
+            String name1 = resource1.getSymbolicName();
+            String name2 = resource2.getSymbolicName();
+            if (name1 == null) {
+                if (name2 == null) {
+                    return 0;
+                } else {
+                    return 1;
+                }
+            } else if (name2 == null) {
+                return -1;
+            } else {
+                return name1.compareTo(name2);
+            }
+        }
+        
     }
 
     private BundleContext getBundleContext(PortletRequest request) {

Modified: geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/webapp/WEB-INF/view/obrmanager/OBRManager.jsp
URL: http://svn.apache.org/viewvc/geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/webapp/WEB-INF/view/obrmanager/OBRManager.jsp?rev=1350782&r1=1350781&r2=1350782&view=diff
==============================================================================
--- geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/webapp/WEB-INF/view/obrmanager/OBRManager.jsp
(original)
+++ geronimo/server/branches/3.0-beta/plugins/console/console-base-portlets/src/main/webapp/WEB-INF/view/obrmanager/OBRManager.jsp
Fri Jun 15 20:23:59 2012
@@ -90,7 +90,14 @@
         </td>
         <td align="right">
             <form id="searchForm" method="POST" action="<portlet:actionURL><portlet:param
name='action' value='search'/></portlet:actionURL>">
-                Search by Symbolic Name:
+                Search type:
+                
+                <select name="searchType">
+                    <option id="symbolic-name" value="symbolic-name">Symbolic name</option>
+                    <option id="bundle-name" value="bundle-name">Bundle name</option>
+                    <option id="package-capability" value="package-capability">Package
capability</option>
+                </select>
+                
                 <input type="text" id="searchString" name="searchString" value="${searchString}"/>&nbsp;
                 <input type="submit" value="Go" />
                 <input type="button" value="Reset" onclick="resetSearchForm()" />
@@ -100,6 +107,10 @@
                     document.getElementById("searchString").value = "";
                     document.getElementById("searchForm").submit();
                 }
+                var searchOption = document.getElementById("${param.searchType}");
+                if (searchOption) {
+                	searchOption.selected = true;
+                }
             </script>
         </td>
     </tr>



Mime
View raw message