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.cms.content.indexing.solr;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.runtime.plugin.Init;
031
032/**
033 * Creates one or several Solr cores at initialization.
034 */
035public class SolrCoreInit extends AbstractLogEnabled implements Init, Serviceable, Configurable
036{
037    
038    /** The Solr indexer. */
039    protected SolrIndexer _solr;
040    
041    /** The name of the cores to create. */
042    protected Set<String> _coreNames;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _solr = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
048    }
049    
050    @Override
051    public void configure(Configuration configuration) throws ConfigurationException
052    {
053        _coreNames = new HashSet<>();
054        for (Configuration coreConf : configuration.getChildren("core"))
055        {
056            String name = coreConf.getAttribute("name");
057            if (StringUtils.isBlank(name))
058            {
059                throw new ConfigurationException("The core name can't be blank.");
060            }
061            _coreNames.add(name.trim());
062        }
063    }
064    
065    @Override
066    public void init() throws Exception
067    {
068        boolean error = createCores(_coreNames);
069        if (!error)
070        {
071            updateAmetysUrlCoreProperty();
072        }
073    }
074    
075    /**
076     * Create the cores.
077     * @param coreNames the names of the cores to create.
078     * @return true if an exception occured. The exception was caught and not re-thrown (just logged)
079     */
080    protected boolean createCores(Set<String> coreNames)
081    {
082        try
083        {
084            Set<String> cores = _solr.getCoreNames();
085            
086            for (String coreName : coreNames)
087            {
088                if (!cores.contains(coreName))
089                {
090                    _solr.createCore(coreName);
091                }
092                else
093                {
094                    if (getLogger().isDebugEnabled())
095                    {
096                        getLogger().debug("Core '" + coreName + "' already exists, skipping it.");
097                    }
098                }
099            }
100        }
101        catch (Exception e)
102        {
103            // Don't re-throw the exception: we don't want to prevent the server from starting.
104            getLogger().warn("Error checking if some cores exist.", e);
105            return true;
106        }
107        return false;
108    }
109    
110    /**
111     * Updates the Ametys URL on Solr server
112     * @return true if an exception occured. The exception was caught and not re-thrown (just logged)
113     */
114    protected boolean updateAmetysUrlCoreProperty()
115    {
116        try
117        {
118            _solr.updateAmetysUrlCoreProperty();
119        }
120        catch (Exception e)
121        {
122            // Don't re-throw the exception: we don't want to prevent the server from starting.
123            getLogger().warn("Error when trying to update Ametys URL core property on Solr server.", e);
124            return true;
125        }
126        return false;
127    }
128    
129}