]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-561: Implement property to reindex all records in ES.
authorRay Lee <rhlee@berkeley.edu>
Thu, 3 Jan 2019 23:33:31 +0000 (15:33 -0800)
committerRay Lee <rhlee@berkeley.edu>
Thu, 3 Jan 2019 23:38:22 +0000 (15:38 -0800)
services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CSpaceResteasyBootstrap.java
services/config/src/main/resources/service.xsd

index 09873f6c8595138573a6250c957659dc67b612d2..2d0ab8e85f9ce704f1ab48a976155cfa9332e9ca 100644 (file)
@@ -16,6 +16,7 @@ import org.collectionspace.services.common.CSWebApplicationException;
 import org.collectionspace.services.common.ResourceMap;
 import org.collectionspace.services.common.ServiceMain;
 import org.collectionspace.services.common.api.RefName;
+import org.collectionspace.services.common.config.ConfigUtils;
 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
 import org.collectionspace.services.common.vocabulary.AuthorityResource;
 
@@ -25,6 +26,11 @@ import org.collectionspace.services.config.service.ServiceBindingType.AuthorityI
 import org.collectionspace.services.config.service.Term;
 import org.collectionspace.services.config.service.TermList;
 import org.collectionspace.services.config.tenant.TenantBindingType;
+import org.collectionspace.services.nuxeo.util.NuxeoUtils;
+
+import org.nuxeo.elasticsearch.ElasticSearchComponent;
+import org.nuxeo.elasticsearch.api.ElasticSearchService;
+import org.nuxeo.runtime.api.Framework;
 
 import java.lang.reflect.Constructor;
 import java.util.Date;
@@ -33,11 +39,12 @@ import java.util.List;
 import java.util.logging.Level;
 
 public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
-       
+
        java.util.logging.Logger logger = java.util.logging.Logger.getAnonymousLogger();
        static final String RESET_AUTHORITIES_PROPERTY = "org.collectionspace.services.authorities.reset";
+       private static final String RESET_ELASTICSEARCH_INDEX_PROPERTY = "org.collectionspace.services.elasticsearch.reset";
        private static final String QUICK_BOOT_PROPERTY = "org.collectionspace.services.quickboot";
-       
+
        @Override
        public void  contextInitialized(ServletContextEvent event) {
                try {
@@ -50,27 +57,67 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                        CollectionSpaceJaxRsApplication app = (CollectionSpaceJaxRsApplication)deployment.getApplication();
                        Dispatcher disp = deployment.getDispatcher();
                        disp.getDefaultContextObjects().put(ResourceMap.class, app.getResourceMap());
-                       
+
                        String quickBoot = System.getProperty(QUICK_BOOT_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
                        if (Boolean.valueOf(quickBoot) == false) {
                                String resetAuthsString = System.getProperty(RESET_AUTHORITIES_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
                                initializeAuthorities(app.getResourceMap(), Boolean.valueOf(resetAuthsString));
+
+                               String resetElasticsearchIndexString = System.getProperty(RESET_ELASTICSEARCH_INDEX_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
+
+                               if (Boolean.valueOf(resetElasticsearchIndexString) == true) {
+                                       resetElasticSearchIndex();
+                               }
                        }
-                       
+
                        logger.log(Level.INFO, String.format("%tc [INFO] CollectionSpace Services' JAX-RS application started.", new Date()));
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }
-       
-       
-    @Override
-    public void contextDestroyed(ServletContextEvent event) {
-       logger.log(Level.INFO, "[INFO] Shutting down the CollectionSpace Services' JAX-RS application.");
-       //Do something if needed.
-       logger.log(Level.INFO, "[INFO] CollectionSpace Services' JAX-RS application stopped.");
-    }  
+
+
+       @Override
+       public void contextDestroyed(ServletContextEvent event) {
+               logger.log(Level.INFO, "[INFO] Shutting down the CollectionSpace Services' JAX-RS application.");
+               //Do something if needed.
+               logger.log(Level.INFO, "[INFO] CollectionSpace Services' JAX-RS application stopped.");
+       }
+
+       public void resetElasticSearchIndex() throws Exception {
+               ElasticSearchComponent es = (ElasticSearchComponent) Framework.getService(ElasticSearchService.class);
+
+               for (String repositoryName : es.getRepositoryNames()) {
+                       logger.log(Level.INFO, String.format("%tc [INFO] Rebuilding Elasticsearch index for repository %s", new Date(), repositoryName));
+
+                       es.dropAndInitRepositoryIndex(repositoryName);
+               }
+
+               TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
+               Hashtable<String, TenantBindingType> tenantBindingsTable = tenantBindingConfigReader.getTenantBindings(false);
+
+               for (TenantBindingType tenantBinding : tenantBindingsTable.values()) {
+                       CSpaceTenant tenant = new CSpaceTenant(tenantBinding.getId(), tenantBinding.getName());
+
+                       AuthZ.get().login(tenant);
+
+                       for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
+                               Boolean isElasticsearchIndexed = serviceBinding.isElasticsearchIndexed();
+                               String servicesRepoDomainName = serviceBinding.getRepositoryDomain();
+
+                               if (isElasticsearchIndexed && servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
+                                       String repositoryName = ConfigUtils.getRepositoryName(tenantBinding, servicesRepoDomainName);
+                                       String docType = serviceBinding.getObject().getName();
+                                       String tenantQualifiedDocType = NuxeoUtils.getTenantQualifiedDocType(tenantBinding.getId(), docType);
+
+                                       logger.log(Level.INFO, String.format("%tc [INFO] Starting Elasticsearch reindexing for docType %s in repository %s", new Date(), tenantQualifiedDocType, repositoryName));
+
+                                       es.runReindexingWorker(repositoryName, String.format("SELECT ecm:uuid FROM %s", tenantQualifiedDocType));
+                               }
+                       }
+               }
+       }
 
     /**
      * Initialize all authorities and vocabularies defined in the service bindings.
@@ -78,7 +125,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
      * @throws Exception
      */
     public void initializeAuthorities(ResourceMap resourceMap, boolean reset) throws Exception {
-       TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();     
+       TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
        Hashtable<String, TenantBindingType> tenantBindingsTable = tenantBindingConfigReader.getTenantBindings(false);
        for (TenantBindingType tenantBindings : tenantBindingsTable.values()) {
                        CSpaceTenant tenant = new CSpaceTenant(tenantBindings.getId(), tenantBindings.getName());
@@ -107,7 +154,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                        }
        }
        }
-    
+
     @SuppressWarnings("rawtypes")
        private AuthorityClient getAuthorityClient(String classname) throws Exception {
         Class clazz = Class.forName(classname.trim());
@@ -115,7 +162,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
         Object classInstance = co.newInstance(null);
         return (AuthorityClient) classInstance;
     }
-    
+
     private boolean shouldInitializeAuthorities(CSpaceTenant cspaceTenant, boolean reset) {
                AuthZ.get().login(); // login as super admin
                TenantResource tenantResource = new TenantResource();
@@ -128,7 +175,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                //
                return tenantState.isAuthoritiesInitialized() == false || reset == true;
     }
-    
+
     private void setAuthoritiesInitialized(CSpaceTenant cspaceTenant, boolean initState) {
                AuthZ.get().login(); // login as super admin
                TenantResource tenantResource = new TenantResource();
@@ -142,15 +189,15 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
     /*
      * Check to see if an an authority instance and its corresponding terms exist.  If not, try to create them.
      */
-    private void initializeAuthorityInstance(ResourceMap resourceMap, 
-               AuthorityInstanceType authorityInstance, 
-               ServiceBindingType serviceBinding, 
+    private void initializeAuthorityInstance(ResourceMap resourceMap,
+               AuthorityInstanceType authorityInstance,
+               ServiceBindingType serviceBinding,
                CSpaceTenant cspaceTenant,
                boolean reset) throws Exception {
        int status = -1;
        Response response = null;
                String serviceName = serviceBinding.getName();
-               
+
                AuthZ.get().login(cspaceTenant);
                String clientClassName = serviceBinding.getClientHandler();
                AuthorityClient client = getAuthorityClient(clientClassName);
@@ -165,7 +212,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                } catch (CSWebApplicationException e) {
                        response = e.getResponse();  // If the authority doesn't exist, we expect a 404 error
                }
-               
+
                //
                // If it doesn't exist (status is not 200), then try to create the authority instance
                //
@@ -175,10 +222,10 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                        response = authorityResource.createAuthority(xmlPayload);
                        status = response.getStatus();
                        if (status != Response.Status.CREATED.getStatusCode()) {
-                               throw new CSWebApplicationException(response); 
+                               throw new CSWebApplicationException(response);
                        }
                }
-               
+
                if (status == Response.Status.OK.getStatusCode()) {
                        logger.log(Level.FINE, String.format("Authority of type '%s' with the short ID of '%s' existed already.",
                                        serviceName, authorityInstance.getTitleRef()));
@@ -188,23 +235,23 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                } else {
                        logger.log(Level.WARNING, String.format("Unknown status '%d' encountered when creating or fetching authority of type '%s' with the short ID of '%s'.",
                                        serviceName, authorityInstance.getTitleRef()));
-               }               
-               
+               }
+
                //
                // Next, try to create or verify the authority terms.
                //
-               initializeAuthorityInstanceTerms(authorityResource, client, authoritySpecifier, resourceMap, authorityInstance, serviceName, cspaceTenant);             
+               initializeAuthorityInstanceTerms(authorityResource, client, authoritySpecifier, resourceMap, authorityInstance, serviceName, cspaceTenant);
        }
-    
+
     private void initializeAuthorityInstanceTerms(
                AuthorityResource authorityResource,
-               AuthorityClient client, 
-               String authoritySpecifier, 
-               ResourceMap resourceMap, 
-               AuthorityInstanceType authorityInstance, 
+               AuthorityClient client,
+               String authoritySpecifier,
+               ResourceMap resourceMap,
+               AuthorityInstanceType authorityInstance,
                String serviceName,
                CSpaceTenant tenant) throws Exception {
-       
+
        int status = -1;
        Response response = null;
 
@@ -212,20 +259,20 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
        if (termListElement == null) {
                return;
        }
-       
+
        for (Term term : termListElement.getTerm()) {
                //
                // Check to see if the term already exists
                //
                try {
-                       String termSpecifier = RefName.shortIdToPath(term.getId());                     
+                       String termSpecifier = RefName.shortIdToPath(term.getId());
                        authorityResource.getAuthorityItem(null, null, resourceMap, authoritySpecifier, termSpecifier);
                        status = Response.Status.OK.getStatusCode();
                } catch (CSWebApplicationException e) {
                        response = e.getResponse();  // If the authority doesn't exist, we expect a 404 error
                        status = response.getStatus();
                }
-               
+
                //
                // If the term doesn't exist, create it.
                //
@@ -241,7 +288,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
                                response = e.getResponse();
                                status = response.getStatus();
                                if (status != Response.Status.CREATED.getStatusCode()) {
-                                       throw new CSWebApplicationException(response); 
+                                       throw new CSWebApplicationException(response);
                                }
                        }
                }
index dbe3103f68a2eb633399576d83efe46359a7ee36..7093ea3f62466e7878bbebed21bb4608573f4b7a 100644 (file)
     >
 
     <xs:import namespace="http://collectionspace.org/services/config/types" schemaLocation="types.xsd" />
-    
-    
+
+
     <xs:element name="root" type="ServiceBindingType"></xs:element>
-    
+
 
     <xs:complexType name="ServiceBindingType">
         <xs:sequence>
             <!-- other URI paths using which this service binding could be accessed -->
             <xs:element name="uriPath" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
-            
+
             <!-- object representation served by the service -->
             <xs:element name="object" type="ServiceObjectType" minOccurs="1" maxOccurs="1"/>
-            
+
             <!-- document handler to be used to process the content (need to be in classpath) -->
             <xs:element name="documentHandler" type="xs:string" minOccurs="1" maxOccurs="1"/>
             <xs:element name="DocHandlerParams" type="DocHandlerParams" minOccurs="0" maxOccurs="1"/>
-            
+
             <!-- optional instances definitions -->
             <xs:element name="AuthorityInstanceList">
                 <xs:complexType>
             <xs:element name="repositoryWorkspaceId" type="xs:string" minOccurs="0" maxOccurs="1"/>
             <xs:element name="properties" type="types:PropertyType" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence>
-        
+
         <!-- name of the service, this is also the default URI path to access this service binding -->
         <xs:attribute name="name" type="xs:string" use="required"/>
         <xs:attribute name="type" type="xs:string" use="optional"/>
         <xs:attribute name="version" type="types:VersionType" use="required"/>
+        <xs:attribute name="elasticsearchIndexed" type="xs:boolean" default="false"/>
         <xs:attribute name="requiresDocumentHandler" type="xs:boolean" default="true"/>
         <xs:attribute name="supportsReplicating" type="xs:boolean" default="false"/>
         <xs:attribute name="remoteClientConfigName" type="xs:string"/>
@@ -90,7 +91,7 @@
             <xs:element ref="termList"/>
         </xs:sequence>
     </xs:complexType>
-        
+
     <xs:element name="termList">
         <xs:complexType>
             <xs:sequence>
             <xs:attribute name="id" use="required" type="xs:NCName"/>
         </xs:complexType>
     </xs:element>
-    
+
     <!--
         ServiceObjectType defines the manifest for a collectionspace
         object.  includes properties of the object as well as manifests
             <xs:element name="public" type="xs:boolean" minOccurs="0" maxOccurs="1" default="true"/>                   <!-- any entity in the chain can cache this -->
             <xs:element name="noCache" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>                 <!-- should not be cached in any way/method -->
             <xs:element name="mustRevalidate" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>  <!-- See http://bit.ly/2mnCwIi -->
-            <xs:element name="proxyRevalidate" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/> 
+            <xs:element name="proxyRevalidate" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>
             <xs:element name="noStore" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>                 <!-- can be cached but should not be stored on disk (most browsers will hold the resources in memory until they will be quit) -->
             <xs:element name="noTransform" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>             <!-- the resource should not be modified (for example shrink image by proxy) -->
             <xs:element name="maxAge" type="xs:integer" minOccurs="0" maxOccurs="1" default="86400"/>                  <!-- how long the resource is valid (measured in seconds) -->