/*
 * ALSO SEE: build/lenya/src-tools/org/apache/lenya/cms/ant/CopyJavaSourcesTask.java
 */
package org.wyona.yanel.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;

import java.io.File;

import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.core.map.RealmManager;

import org.apache.log4j.Logger;

/**
 * Merge resource-types.xml config files of the various realms and core
 */
public class MergeResourceTypesConfigsTask extends Task {

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

    private Path defaultRealmsConfigDir;
    private Path localRealmsConfigDir;

    /**
     *
     */
    public void execute() throws BuildException {
        log.info("Merge ...");
        log("INFO: Default realms config directory: " + defaultRealmsConfigDir);
        log("INFO: Local realms config directory: " + localRealmsConfigDir);
        File defaultRealmsConfig = new File(defaultRealmsConfigDir.toString(), "realms.xml");
        File localRealmsConfig = new File(localRealmsConfigDir.toString(), "local.realms.xml");
        RealmManager realmManager;
        try {
            if (localRealmsConfig.isFile()) {
                log("INFO: Local realms config exists: " + localRealmsConfig.getAbsolutePath());
                // TODO: Unfortunately the class RealmManager consumes at the moment a yanel configuration rather than a realms configuration ...
                realmManager = new RealmManager("yanel.xml");
                //realmManager = new RealmManager("local.realms.xml");
                //realmManager = new RealmManager(localRealmsConfig.getAbsolutePath());
            } else {
                log("WARN: No local realms config '" + localRealmsConfig.getAbsolutePath() + "' exists, hence use default one '" + defaultRealmsConfig.getAbsolutePath() + "'");
                // TODO: ...
                realmManager = new RealmManager("yanel.xml");
                //realmManager = new RealmManager(defaultRealmsConfig.getAbsolutePath());
            }
        } catch (Exception e) {
            log("ERROR: " + e.getMessage());
            throw new BuildException(e.getMessage(), e);
        }

        Realm[] realms = realmManager.getRealms();
        log("Number of realms: " + realms.length);
        for (int i = 0; i < realms.length; i++) {
            log("Realm config: " + realms[i].getConfigFile());
        }
        log("HUGO");
    }

    /**
     *
     */
    public void setDefaultRealmsConfigDir(Path defaultRealmsConfigDir) {
        this.defaultRealmsConfigDir = defaultRealmsConfigDir;
    }

    /**
     *
     */
    public void setLocalRealmsConfigDir(Path localRealmsConfigDir) {
        this.localRealmsConfigDir = localRealmsConfigDir;
    }
}

@@ -295,16 +295,68 @@
 .Realm;%0A
+import org.wyona.yanel.core.map.RealmContextConfig;%0A
 import o
@@ -391,16 +391,22 @@
 mManager
+Config
 ;%0A%0Aimpor
@@ -1213,16 +1213,22 @@
 mManager
+Config
  realmMa
@@ -1232,16 +1232,99 @@
 mManager
+Config = new RealmManagerConfig();%0A        RealmContextConfig%5B%5D realmContextConfigs
 ;%0A      
@@ -1628,32 +1628,34 @@
                 
+//
 realmManager = n
@@ -1704,148 +1704,89 @@
     
-//
 realm
-Manager = new RealmManager(%22local.realms.xml%22);%0A                //realmManager = new RealmManager(localRealmsConfig.getAbsolutePath()
+ContextConfigs = realmManagerConfig.getRealmContextConfigs(localRealmsConfig
 );%0A 
@@ -2023,24 +2023,26 @@
             
+//
 realmManager
@@ -2095,41 +2095,71 @@
     
-//
 realm
-Manager = new RealmManager
+ContextConfigs = realmManagerConfig.getRealmContextConfigs
 (def
@@ -2174,34 +2174,16 @@
 msConfig
-.getAbsolutePath()
 );%0A     
@@ -2340,59 +2340,8 @@
  %7D%0A%0A
-        Realm%5B%5D realms = realmManager.getRealms();%0A
     
@@ -2372,24 +2372,37 @@
 s: %22 + realm
+ContextConfig
 s.length);%0A 
@@ -2433,16 +2433,29 @@
  %3C realm
+ContextConfig
 s.length
@@ -2486,16 +2486,24 @@
 (%22Realm 
+context 
 config: 
@@ -2511,16 +2511,29 @@
  + realm
+ContextConfig
 s%5Bi%5D.get
@@ -2538,16 +2538,23 @@
 etConfig
+uration
 File());
@@ -2568,29 +2568,8 @@
   %7D%0A
-        log(%22HUGO%22);%0A
     

use new configuration classes