/**
 *
 * Copyright 2004 The Apache Software Foundation
 *
 *  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.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.apache.geronimo.j2ee.management.impl;

import java.util.Hashtable;
import javax.management.ObjectName;
import javax.naming.Context;

import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.j2ee.management.J2EEApplication;
import org.apache.geronimo.j2ee.management.J2EEServer;
import org.apache.geronimo.kernel.jmx.JMXUtil;

/**
 * @version $Revision$ $Date$
 */
public class J2EEAppClientModuleImpl {
    private final String deploymentDescriptor;
    private final J2EEServer server;
    private final J2EEApplication application;
    private final Context componentContext;
    private final ClassLoader classLoader;

    public J2EEAppClientModuleImpl(Context componentContext, String objectName, J2EEServer server, J2EEApplication application, String deploymentDescriptor, ClassLoader classLoader) {
        this.componentContext = componentContext;
        ObjectName myObjectName = JMXUtil.getObjectName(objectName);
        verifyObjectName(myObjectName);

        this.server = server;
        this.application = application;
        this.deploymentDescriptor = deploymentDescriptor;
        this.classLoader = classLoader;
    }

    /**
     * ObjectName must match this pattern:
     * <p/>
     * domain:j2eeType=AppClientModule,name=MyName,J2EEServer=MyServer,J2EEApplication=MyApplication
     */
    private void verifyObjectName(ObjectName objectName) {
        if (objectName.isPattern()) {
            throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
        }
        Hashtable keyPropertyList = objectName.getKeyPropertyList();
        if (!"AppClientModule".equals(keyPropertyList.get("j2eeType"))) {
            throw new InvalidObjectNameException("AppClientModule object name j2eeType property must be 'AppClientModule'", objectName);
        }
        if (!keyPropertyList.containsKey("name")) {
            throw new InvalidObjectNameException("AppClientModule object must contain a name property", objectName);
        }
        if (!keyPropertyList.containsKey("J2EEServer")) {
            throw new InvalidObjectNameException("AppClientModule object name must contain a J2EEServer property", objectName);
        }
        if (!keyPropertyList.containsKey("J2EEApplication")) {
            throw new InvalidObjectNameException("AppClientModule object name must contain a J2EEApplication property", objectName);
        }
        if (keyPropertyList.size() != 4) {
            throw new InvalidObjectNameException("AppClientModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties", objectName);
        }
    }

    public String getDeploymentDescriptor() {
        return deploymentDescriptor;
    }

    public String getServer() {
        return server.getObjectName();
    }

    public String getApplication() {
        if (application == null) {
            return null;
        }
        return application.getObjectName();
    }

    public String[] getJavaVMs() {
        return server.getJavaVMs();
    }

    public Context getComponentContext() {
        return componentContext;
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(J2EEAppClientModuleImpl.class);
        infoFactory.addReference("J2EEServer", J2EEServer.class);
        infoFactory.addReference("J2EEApplication", J2EEApplication.class);

        infoFactory.addAttribute("componentContext", Context.class, true);
        infoFactory.addAttribute("deploymentDescriptor", String.class, true);

        infoFactory.addAttribute("objectName", String.class, false);
        infoFactory.addAttribute("server", String.class, false);
        infoFactory.addAttribute("application", String.class, false);
        infoFactory.addAttribute("javaVMs", String[].class, false);
        infoFactory.addAttribute("classLoader", ClassLoader.class, false);


        infoFactory.setConstructor(new String[]{
            "componentContext",
            "objectName",
            "J2EEServer",
            "J2EEApplication",
            "deploymentDescriptor",
            "classLoader"});

        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}

@@ -1234,52 +1234,8 @@
 on;%0A
-    private final Context componentContext;%0A
     
@@ -1313,34 +1313,8 @@
 mpl(
-Context componentContext, 
 Stri
@@ -1436,58 +1436,8 @@
 ) %7B%0A
-        this.componentContext = componentContext;%0A
     
@@ -3598,91 +3598,8 @@
  %7D%0A%0A
-    public Context getComponentContext() %7B%0A        return componentContext;%0A    %7D%0A%0A
     
@@ -3632,24 +3632,24 @@
 sLoader() %7B%0A
+
         retu
@@ -3970,83 +3970,8 @@
 );%0A%0A
-        infoFactory.addAttribute(%22componentContext%22, Context.class, true);%0A
     
@@ -4394,16 +4394,16 @@
 lse);%0A%0A%0A
+
         
@@ -4447,40 +4447,8 @@
 %5B%5D%7B%0A
-            %22componentContext%22,%0A
     

remove component context from server side gbean, it can't be deserialized. GERONIMO-545 (requires some changes in AppClientModuleBuilder, in subsequent commit)