/*
 * Copyright 2009 Wyona
 *
 *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.map;

import java.io.File;

import org.apache.log4j.Logger;

import org.wyona.yanel.core.ConfigurationException;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

/**
 * Class providing access to realms configuration
 */
public class RealmManagerConfig {

    private static Logger log = Logger.getLogger(RealmManagerConfig.class);

    private File realmsConfigFile;

    /**
     * Get realms configuration file
     */
    public File getConfigurationFile() {
        return realmsConfigFile;
    }

    /**
     * Read realms configuration
     * @param realmsConfigFile Realms configuration file, where several realms (location, etc.) are configured
     */
    public RealmContextConfig[] getRealmContextConfigs(File realmsConfigFile) throws ConfigurationException {

        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
        Configuration config;
        java.util.Vector realmContextConfigsVec = new java.util.Vector();
        try {
            log.info("Read realms configuration: " + realmsConfigFile);
            config = builder.buildFromFile(realmsConfigFile);
            Configuration[] realmElements = config.getChildren("realm");
            for (int i = 0;i < realmElements.length; i++) {
                String realmId = realmElements[i].getAttribute("id", null);

                String mountPoint = realmElements[i].getAttribute("mount-point", null);

                String rootFlag = realmElements[i].getAttribute("root", "false");

                Configuration configElement = realmElements[i].getChild("config", false);
                if (configElement == null) {
                    throw new ConfigurationException("Missing <config src=\"...\"/> child element for realm " + realmId);
                }
                String configSrc = configElement.getAttribute("src", null);
                if (configSrc != null) {
                    configSrc = configSrc.trim(); // INFO: Trim in case somebody added accidentally leading or trailing spaces
                }

                RealmContextConfig rcc = new RealmContextConfig(realmId, mountPoint, new Boolean(rootFlag).booleanValue(), new File(configSrc));

                Configuration nameConfig = realmElements[i].getChild("name", false);
                if (nameConfig != null) rcc.setLabel(nameConfig.getValue());

                String boostDomain = realmElements[i].getAttribute("user-tracking-domain-id", null);
                if (boostDomain != null) {
                    rcc.setUserTrackingDomain(boostDomain);
                } else {
                    log.warn("No user tracking domain configured inside realms configuration, hence use realm ID...");
                    rcc.setUserTrackingDomain(realmId);
                }

                Configuration proxy = realmElements[i].getChild("reverse-proxy", false);
                if (proxy != null) {
                    int proxyPort = new Integer(proxy.getChild("port").getValue("-1")).intValue();
                    int proxySSLPort = new Integer(proxy.getChild("ssl-port").getValue("-1")).intValue();
                    String prefixValue = proxy.getChild("prefix").getValue("");
                    if (prefixValue.length() == 0) prefixValue = null;
                    log.debug("Prefix value: " + prefixValue);
                    String hostName = proxy.getChild("host-name").getValue();
                    rcc.setReverseProxyConfig(new ReverseProxyConfig(hostName, proxyPort, proxySSLPort, prefixValue));
                }

                realmContextConfigsVec.add(rcc);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ConfigurationException("Error reading realms config from file " + 
                    realmsConfigFile + ": " + e, e);
        }

        RealmContextConfig[] realmContextConfigs = new RealmContextConfig[realmContextConfigsVec.size()];
        for (int i = 0;i < realmContextConfigs.length; i++) {
            realmContextConfigs[i] = (RealmContextConfig) realmContextConfigsVec.elementAt(i);
        }
        return realmContextConfigs;
    }
}

@@ -680,14 +680,66 @@
 .log
-4j.Log
+ging.log4j.Logger;%0Aimport org.apache.logging.log4j.LogMana
 ger;
@@ -1064,16 +1064,20 @@
 og = Log
+Mana
 ger.getL
@@ -3753,32 +3753,33 @@
 %22)).intValue();%0A
+%0A
                 
@@ -3860,32 +3860,33 @@
 %22)).intValue();%0A
+%0A
                 
@@ -4079,24 +4079,284 @@
 efixValue);%0A
+%0A                    String reversePrefixValue = proxy.getChild(%22reverse-prefix%22).getValue(%22%22);%0A                    if (reversePrefixValue.length() == 0) reversePrefixValue = null;%0A                    log.debug(%22Reverse prefix value: %22 + reversePrefixValue);%0A%0A
             
@@ -4417,24 +4417,25 @@
 getValue();%0A
+%0A
             
@@ -4442,23 +4442,16 @@
         
-rcc.set
 ReverseP
@@ -4460,17 +4460,23 @@
 xyConfig
-(
+ rpc = 
 new Reve
@@ -4537,16 +4537,129 @@
 ixValue)
+;%0A                    rpc.setReversePrefix(reversePrefixValue);%0A                    rcc.setReverseProxyConfig(rpc
 );%0A     

set reverse prefix if exists