/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id:  $
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.webapp.jonasadmin.jonasserver;

import java.util.ArrayList;

import javax.management.ObjectName;

import org.objectweb.jonas.jmx.JonasManagementRepr;
/**
 * Helper class which manipulates CMI MBean for registry content
 * @author Adriana Danes
 *
 */
public class CmiRegistryResource {

    public static final String PREFIX = "REG_";
    /**
     * local node
     */
    public static final String LOCAL = "local";

    public static ArrayList getCmiRegistry(ObjectName cmiOn, String serverName) {
        ArrayList result = new ArrayList();

        // distributed registry
        String[] regList = (String[]) JonasManagementRepr.getAttribute(cmiOn, "registry", serverName);
        for (int i = 0; i < regList.length; i++) {
            if (regList[i].startsWith(PREFIX)) {
                regList[i] = regList[i].substring(PREFIX.length());
                String[] tab = regList[i].split("->");
                ArrayList l = new ArrayList(2);
                l.add(tab[0]);
                l.add(tab[1]);
                result.add(l);
            }
        }
        // local registry
        String[] localRegList = (String[]) JonasManagementRepr.getAttribute(cmiOn, "localRegistry", serverName);
        for (int i = 0; i < localRegList.length; i++) {
            ArrayList l = new ArrayList(2);
            l.add(localRegList[i]);
            l.add(LOCAL);
            result.add(l);
        }

        return result;
    }
}