[Yanel-dev] Reindexer resource

Cedric Staub cedric.staub at wyona.com
Wed Jan 5 16:00:29 CET 2011


Hello there

I now have a simple re-indexer resource which works like this:

- The re-indexer can re-index the realm's default data repository
  and arbitraty extra repositories. I'm currently working on making
  it re-index ac-policies, ac-identities and res-configs as well.

- You can specify which repositories should be re-indexable in the
  resource configuration. If the repository isn't in the resource
  config, it can't be re-indexed.

I attached screenshot(s) and a patch so you can look at it.

This is just to confirm that I'm going in the right direction with this
considering the implementation of the whole thing. If you don't like it,
please tell me ;-). Otherwise I'll continue with the implementation in
the direction it's heading now.

Cheers,
Cedric
-------------- next part --------------
A non-text attachment was scrubbed...
Name: re-indexer-1.png
Type: image/png
Size: 55248 bytes
Desc: not available
Url : http://lists.wyona.org/pipermail/yanel-development/attachments/20110105/2f5bbf6e/re-indexer-1-0001.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: re-indexer-2.png
Type: image/png
Size: 31509 bytes
Desc: not available
Url : http://lists.wyona.org/pipermail/yanel-development/attachments/20110105/2f5bbf6e/re-indexer-2-0001.png
-------------- next part --------------
Index: src/contributions/resources/search/htdocs/reindexer.xsl
===================================================================
--- src/contributions/resources/search/htdocs/reindexer.xsl	(revision 0)
+++ src/contributions/resources/search/htdocs/reindexer.xsl	(revision 0)
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<xsl:stylesheet version="1.0"
+  xmlns="http://www.w3.org/1999/xhtml"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  xmlns:xhtml="http://www.w3.org/1999/xhtml"
+  xmlns:y="http://www.wyona.org/yanel/reindex/1.0">
+
+  <xsl:template match="/y:reindex">
+    <html>
+      <head>
+        <title>Re-indexer</title>
+      </head>
+      <body>
+        <h1>Re-indexer</h1>
+        <xsl:apply-templates select="y:exception"/>
+        <xsl:apply-templates select="y:message"/>
+
+        <xsl:if test="not(y:exception or y:message)">
+          <p>Choose which repository you would like to re-index:</p>
+          <ul>
+            <xsl:for-each select="y:repository">
+              <li>
+                <a href="?repository={@id}"><xsl:value-of select="."/></a></li>
+            </xsl:for-each>
+          </ul>
+        </xsl:if>
+      </body>
+    </html>
+  </xsl:template>
+
+  <xsl:template match="y:exception">
+    <p style="color:red;"><xsl:value-of select="."/></p>
+  </xsl:template>
+
+  <xsl:template match="y:message">
+    <p style="color:green;"><xsl:value-of select="."/></p>
+  </xsl:template>
+
+</xsl:stylesheet>
Index: src/contributions/resources/search/src/java/org/wyona/yanel/impl/resources/reindex/ReindexResource.java
===================================================================
--- src/contributions/resources/search/src/java/org/wyona/yanel/impl/resources/reindex/ReindexResource.java	(revision 0)
+++ src/contributions/resources/search/src/java/org/wyona/yanel/impl/resources/reindex/ReindexResource.java	(revision 0)
@@ -0,0 +1,131 @@
+/*-
+ * Copyright 2011 Wyona
+ */
+
+package org.wyona.yanel.impl.resources.reindexer;
+
+import org.wyona.yarep.util.YarepUtil;
+import org.wyona.yarep.core.Repository;
+
+import org.wyona.yanel.core.map.Realm;
+import org.wyona.yanel.core.attributes.viewable.View;
+import org.wyona.yanel.impl.resources.BasicXMLResource;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.apache.log4j.Logger;
+
+import org.wyona.meguni.parser.Parser;
+import org.wyona.meguni.util.ResultSet;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationUtil;
+import org.apache.commons.lang.StringEscapeUtils;
+
+/**
+ * Re-indexing resource. This resource can be used to start the re-indexing
+ * process of an arbitrary repository from within a browser session. If you
+ * configure this resource in your realm, make sure to protect it because you
+ * most likely don't want your users to start re-indexing processes.
+ */
+public class ReindexResource extends BasicXMLResource {
+    private static Logger log = Logger.getLogger(ReindexResource.class);
+
+    private static String REPO_NAME = "repository";
+    private static String REINDEX_XMLNS = "http://www.wyona.org/yanel/reindex/1.0";
+    
+    /**
+     * @see org.wyona.yanel.impl.resources.BasicXMLResource#getContentXML(String)
+     */
+    protected InputStream getContentXML(String viewId) throws Exception {
+        // Build output document
+        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>");
+        sb.append("<r:reindex xmlns:r=\"");
+        sb.append(REINDEX_XMLNS);
+        sb.append("\">");
+
+        // Which repo needs to be re-indexed?
+        Repository repo = null;
+        String reindexRepo = getEnvironment().getRequest().getParameter("repository"); 
+
+        // Are we allowed to re-index this repository?
+        // Only repositories in the res-config are allowed to be re-indexed
+        boolean allowed = false;
+
+        // List default repositories
+        // Only show them if we're allowed to re-index them
+        String defaultRepos = getResourceConfigProperty("allow-default-repos");
+        if("true".equals(defaultRepos)) {
+            sb.append("<r:repository id=\"yanel_data\">Realm's data repository</r:repository>");
+            // sb.append("<r:repository id=\"yanel_ac-identities\">Realm's ac-identities repository</r:repository>");
+            // sb.append("<r:repository id=\"yanel_ac-policies\">Realm's ac-policies repository</r:repository>");
+            // sb.append("<r:repository id=\"yanel_res-configs\">Realm's res-configs repository</r:repository>");
+        }
+
+        // List extra repositories from repo configuration
+        String[] repoIds = getResourceConfigProperties("repository-id");
+        for(String repoId : repoIds) {
+            sb.append("<r:repository id=\"");
+            sb.append(repoId);
+            sb.append("\">Configured repository with id '");
+            sb.append(repoId);
+            sb.append("'</r:repository>");
+            // Check  if repo that should be re-indexed is in res-config
+            if(repoId.equals(reindexRepo)) allowed = true;
+        }
+
+        // Check if repo that should be re-indexed is default repo
+        // This is because default repos have to be instantiated diffrently
+        if("true".equals(defaultRepos) && !allowed) {
+            try {
+                if("yanel_data".equals(reindexRepo)) {
+                    repo = getRealm().getRepository();
+                }
+                // TODO: Also make it possible to re-index
+                // ac-policies, ac-identities and res-configs repo
+            } catch(Exception e) {
+                repo = null;
+                sb.append("<r:exception>Opening repository failed with exception: ");
+                sb.append(e.getMessage());
+                sb.append("</r:exception>");
+            }
+        }
+
+        // If it's an extra repo, allowed needs to be set to true
+        if(allowed) {
+            try {
+                repo = getRealm().getRepository(reindexRepo);
+            } catch(Exception e) {
+                sb.append("<r:exception>Opening repo failed with exception: ");
+                sb.append(e.getMessage());
+                sb.append("</r:exception>");
+            }
+        }
+
+        if(repo != null) {
+            YarepUtil yu = new YarepUtil();
+
+            try {
+                yu.indexRepository(repo);
+                sb.append("<r:message>Re-indexing was successful.</r:message>");
+            } catch(Exception e) {
+                sb.append("<r:exception>Re-indexing failed with exception: ");
+                sb.append(e.getMessage());
+                sb.append("</r:exception>");
+            }
+        } else if(reindexRepo != null) {
+            sb.append("<r:exception>Repository does not exist.</r:exception>");
+        }
+
+        sb.append("</r:reindex>");
+        return new ByteArrayInputStream(sb.toString().getBytes());
+    }
+
+    /**
+     * @see org.wyona.yanel.core.api.attributes.ViewableV2#exists()
+     */
+    public boolean exists() throws Exception {
+        return true;
+    }
+}
Index: src/contributions/resources/search/resource-reindexer.xml
===================================================================
--- src/contributions/resources/search/resource-reindexer.xml	(revision 0)
+++ src/contributions/resources/search/resource-reindexer.xml	(revision 0)
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+
+<resource xmlns="http://www.wyona.org/yanel/1.0"
+  name="reindexer"
+  namespace="http://www.wyona.org/yanel/resource/1.0"
+  class="org.wyona.yanel.impl.resources.reindexer.ReindexResource"
+  >
+<description>
+A reindexer resource.
+</description>
+
+<rtd>
+  <!-- No properties -->
+</rtd>
+</resource>


More information about the Yanel-development mailing list