001/* 002 * Copyright 2015 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.indexing.solr; 017 018import org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020import org.apache.solr.client.solrj.SolrClient; 021 022import org.ametys.cms.content.indexing.solr.SolrFieldNames; 023import org.ametys.cms.indexing.IndexingException; 024import org.ametys.cms.indexing.solr.SolrWorkspaceIndexer; 025import org.ametys.plugins.repository.AmetysObject; 026import org.ametys.plugins.repository.AmetysObjectIterable; 027import org.ametys.plugins.repository.AmetysObjectResolver; 028import org.ametys.plugins.repository.RepositoryConstants; 029import org.ametys.plugins.repository.TraversableAmetysObject; 030import org.ametys.plugins.repository.UnknownAmetysObjectException; 031import org.ametys.runtime.config.Config; 032import org.ametys.web.explorer.ResourceHelper; 033import org.ametys.web.indexing.SiteIndexer; 034import org.ametys.web.repository.site.Site; 035import org.ametys.web.repository.site.SiteManager; 036 037/** 038 * Web-specific component indexing a workspace in a Solr server. 039 */ 040public class SolrWebWorkspaceIndexer extends SolrWorkspaceIndexer 041{ 042 /** The site manager. */ 043 protected SiteManager _siteManager; 044 045 /** The site indexer. */ 046 protected SiteIndexer _siteIndexer; 047 048 /** The Ametys object resolver. */ 049 protected AmetysObjectResolver _resolver; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 super.service(manager); 055 056 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 057 _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE); 058 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 059 } 060 061 @Override 062 protected void doIndex(String workspaceName) throws IndexingException 063 { 064 SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, false); 065 066 try (AmetysObjectIterable<Site> sites = _siteManager.getSites()) 067 { 068 // First, unindex all documents. 069 _solrIndexer.unindexAllDocuments(workspaceName, solrClient); 070 071 // Index each site. 072 for (Site site : sites) 073 { 074 _siteIndexer.indexSite(site.getName(), workspaceName, solrClient); 075 } 076 077 // Index contents in the out-of-site content root. 078 indexContentRoot(workspaceName, solrClient); 079 indexPluginsContentRoot(workspaceName, solrClient); 080 081 // Index shared resources. 082 indexSharedResources(workspaceName, solrClient); 083 084 // Optionally index additional documents. 085 indexAdditionalDocuments(workspaceName, solrClient); 086 087 _solrIndexer.commit(workspaceName, solrClient); 088 // Optimize once when done. 089 _solrIndexer.optimize(workspaceName, solrClient); 090 } 091 catch (Exception e) 092 { 093 getLogger().error("Error indexing the workspace '" + workspaceName + "'.", e); 094 throw new IndexingException("Error indexing the workspace '" + workspaceName + "'.", e); 095 } 096 } 097 098 /** 099 * Index the out-of-site contents. 100 * @param workspaceName The workspace name 101 * @param solrClient The solr client to use 102 * @throws Exception If an error occurs. 103 */ 104 protected void indexContentRoot(String workspaceName, SolrClient solrClient) throws Exception 105 { 106 try 107 { 108 TraversableAmetysObject contentRoot = _resolver.resolveByPath("/" + RepositoryConstants.NAMESPACE_PREFIX + ":contents"); 109 _solrIndexer.indexContents(contentRoot.getChildren(), workspaceName, true, solrClient); 110 } 111 catch (UnknownAmetysObjectException e) 112 { 113 // Ignore if the content root doesn't exist in a workspace. 114 } 115 } 116 117 /** 118 * Index the contents stored by plugins 119 * @param workspaceName The workspace name 120 * @param solrClient The solr client to use 121 * @throws Exception If an error occurs. 122 */ 123 protected void indexPluginsContentRoot(String workspaceName, SolrClient solrClient) throws Exception 124 { 125 try 126 { 127 TraversableAmetysObject pluginsNode = _resolver.resolveByPath("/" + RepositoryConstants.NAMESPACE_PREFIX + ":plugins"); 128 129 for (AmetysObject pluginNode : pluginsNode.getChildren()) 130 { 131 if (pluginNode instanceof TraversableAmetysObject) 132 { 133 try 134 { 135 AmetysObject rootContents = ((TraversableAmetysObject) pluginNode).getChild(RepositoryConstants.NAMESPACE_PREFIX + ":contents"); 136 if (rootContents instanceof TraversableAmetysObject) 137 { 138 _solrIndexer.indexContents(((TraversableAmetysObject) rootContents).getChildren(), workspaceName, true, solrClient); 139 } 140 } 141 catch (UnknownAmetysObjectException e) 142 { 143 // Ignore if the content root doesn't exist in a plugin's node. 144 } 145 146 } 147 } 148 } 149 catch (UnknownAmetysObjectException e) 150 { 151 // Ignore if the plugin's node doesn't exist in a workspace. 152 } 153 } 154 155 156 /** 157 * Index the shared resources. 158 * @param workspaceName The workspace name 159 * @param solrClient The solr client to use 160 * @throws Exception If an error occurs. 161 */ 162 protected void indexSharedResources(String workspaceName, SolrClient solrClient) throws Exception 163 { 164 boolean useShared = Config.getInstance().getValue("resources.shared.folder"); 165 if (useShared) 166 { 167 try 168 { 169 TraversableAmetysObject sharedResources = _resolver.resolveByPath(ResourceHelper.SHARED_RESOURCE_PATH + "/all"); 170 _solrIndexer.indexResources(sharedResources.getChildren(), SolrFieldNames.TYPE_RESOURCE, sharedResources, workspaceName, solrClient); 171 } 172 catch (UnknownAmetysObjectException e) 173 { 174 // Ignore if the shared resource root doesn't exist in a workspace. 175 } 176 } 177 } 178 179}