/*
 *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

package org.wso2.carbon.registry.indexing;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.context.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonException;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.config.RegistryConfigurationProcessor;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.indexing.internal.IndexingServiceComponent;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.WaitBeforeShutdownObserver;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;

public class Utils {
    private static final Log log = LogFactory.getLog(Utils.class);

    private static RegistryService registryService;
    private static List<WaitBeforeShutdownObserver> waitBeforeShutdownObserver =
            new LinkedList<WaitBeforeShutdownObserver>();

    private static String defaultEventingServiceURL;

    private static String remoteTopicHeaderName;
    
    private static String remoteTopicHeaderNS;

    private static String remoteSubscriptionStoreContext;

    public static void setRegistryService(RegistryService service) {
        registryService = service;
    }

    public static RegistryService getRegistryService() {
        return registryService;
    }

    public static WaitBeforeShutdownObserver[] getWaitBeforeShutdownObservers() {
        return waitBeforeShutdownObserver.toArray(
                new WaitBeforeShutdownObserver[waitBeforeShutdownObserver.size()]);
    }

    public static void setWaitBeforeShutdownObserver(WaitBeforeShutdownObserver service) {
        CarbonUtils.checkSecurity();
        waitBeforeShutdownObserver.add(service);
    }

    public static void clearWaitBeforeShutdownObserver() {
        CarbonUtils.checkSecurity();
        waitBeforeShutdownObserver = new LinkedList<WaitBeforeShutdownObserver>();
    }

    public static Registry getRegistry() throws RegistryException {

        MessageContext messageContext = MessageContext.getCurrentMessageContext();
        if (messageContext == null) {
            String msg = "Could not get the user's Registry session. Message context not found.";
            log.error(msg);
            throw new RegistryException(msg);
        }

        HttpServletRequest request =
                (HttpServletRequest) messageContext.getProperty("transport.http.servletRequest");

        Registry registry =
                (Registry) request.getSession().getAttribute(RegistryConstants.USER_REGISTRY);

        if (registry == null) {
            String msg = "User's Registry instance is not found. " +
                    "Creating a anonymous Registry instance for the user.";
            if(log.isDebugEnabled()) {
                log.debug(msg);
            }
            if (registryService == null) {
                msg = "Unable to create anonymous Registry instance for user. " +
                        "Registry Service was not found.";
                log.error(msg);
                throw new RegistryException(msg);
            }
            registry = registryService.getUserRegistry();
            request.getSession().setAttribute(RegistryConstants.USER_REGISTRY, registry);
        }

        return registry;
    }
    public static String getDefaultEventingServiceURL() {
        return defaultEventingServiceURL;
    }

    public static void setDefaultEventingServiceURL(String defaultEventingServiceURL) {
        Utils.defaultEventingServiceURL = defaultEventingServiceURL;
    }

    public static String getRemoteTopicHeaderName() {
        return remoteTopicHeaderName;
    }

    public static void setRemoteTopicHeaderName(String remoteTopicHeaderName) {
        Utils.remoteTopicHeaderName = remoteTopicHeaderName;
    }

    public static String getRemoteTopicHeaderNS() {
        return remoteTopicHeaderNS;
    }

    public static void setRemoteTopicHeaderNS(String remoteTopicHeaderNS) {
        Utils.remoteTopicHeaderNS = remoteTopicHeaderNS;
    }

    public static String getRemoteSubscriptionStoreContext() {
        return remoteSubscriptionStoreContext;
    }

    public static void setRemoteSubscriptionStoreContext(String remoteSubscriptionStoreContext) {
        Utils.remoteSubscriptionStoreContext = remoteSubscriptionStoreContext;
    }

    public static boolean isIndexingConfigAvailable() throws RegistryException {

        String configPath = CarbonUtils.getRegistryXMLPath();
        File registryXML = new File(configPath);
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(registryXML);
        } catch (FileNotFoundException e) {
            String msg = "Registry configuration file (registry.xml) file doesn't exist in the path " + configPath;
            log.error(msg, e);
            throw new RegistryException(msg);
        }
        StAXOMBuilder builder;
        try {
            builder = new StAXOMBuilder(
                    CarbonUtils.replaceSystemVariablesInXml(fileInputStream));
            OMElement configElement = builder.getDocumentElement();
            if (configElement != null) {
                OMElement indexingConfig = configElement.getFirstChildWithName(
                        new QName("indexingConfiguration"));
                return indexingConfig != null;
            }

        } catch (XMLStreamException e) {
            String msg = "Failed to read <indexingConfiguration/> from registry.xml";
            log.error(msg, e);
            throw new RegistryException(msg);
        } catch (CarbonException e) {
            String msg = "Failed to read <indexingConfiguration/> from registry.xml";
            log.error(msg, e);
            throw new RegistryException(msg);
        }
        return false;
    }
    
    /**
     * Returns indexing configuration in the registry.xml
     * @return
     * @throws RegistryException
     */
    public static OMElement getIndexingConfiguration() throws RegistryException {
    	OMElement resultElement= null;
 
        String configPath = CarbonUtils.getRegistryXMLPath();
        File registryXML = new File(configPath);
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(registryXML);
        } catch (FileNotFoundException e) {
            String msg = "Registry configuration file (registry.xml) file doesn't exist in the path " + configPath;
            throw new RegistryException(msg,e);
        }
        StAXOMBuilder builder;
        try {
            builder = new StAXOMBuilder(
                    CarbonUtils.replaceSystemVariablesInXml(fileInputStream));
            OMElement configElement = builder.getDocumentElement();
            if (configElement != null) {
                resultElement = configElement.getFirstChildWithName(
                        new QName("indexingConfiguration"));
            }
        } catch (XMLStreamException e) {
            String msg = "Failed to read <indexingConfiguration/> from registry.xml";
            throw new RegistryException(msg,e);
        } catch (CarbonException e) {
            String msg = "Failed to read <indexingConfiguration/> from registry.xml";
            throw new RegistryException(msg,e);
        }
        
    	return resultElement;
    }
}

@@ -6946,1518 +6946,6 @@
   %7D%0A
-    %0A    /**%0A     * Returns indexing configuration in the registry.xml%0A     * @return%0A     * @throws RegistryException%0A     */%0A    public static OMElement getIndexingConfiguration() throws RegistryException %7B%0A    %09OMElement resultElement= null;%0A %0A        String configPath = CarbonUtils.getRegistryXMLPath();%0A        File registryXML = new File(configPath);%0A        FileInputStream fileInputStream;%0A        try %7B%0A            fileInputStream = new FileInputStream(registryXML);%0A        %7D catch (FileNotFoundException e) %7B%0A            String msg = %22Registry configuration file (registry.xml) file doesn't exist in the path %22 + configPath;%0A            throw new RegistryException(msg,e);%0A        %7D%0A        StAXOMBuilder builder;%0A        try %7B%0A            builder = new StAXOMBuilder(%0A                    CarbonUtils.replaceSystemVariablesInXml(fileInputStream));%0A            OMElement configElement = builder.getDocumentElement();%0A            if (configElement != null) %7B%0A                resultElement = configElement.getFirstChildWithName(%0A                        new QName(%22indexingConfiguration%22));%0A            %7D%0A        %7D catch (XMLStreamException e) %7B%0A            String msg = %22Failed to read %3CindexingConfiguration/%3E from registry.xml%22;%0A            throw new RegistryException(msg,e);%0A        %7D catch (CarbonException e) %7B%0A            String msg = %22Failed to read %3CindexingConfiguration/%3E from registry.xml%22;%0A            throw new RegistryException(msg,e);%0A        %7D%0A        %0A    %09return resultElement;%0A    %7D%0A
 %7D%0A

remove unwanted method