/**
 *
 * 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.deployment.plugin.jmx;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import javax.enterprise.deploy.model.DeployableObject;
import javax.enterprise.deploy.shared.DConfigBeanVersionType;
import javax.enterprise.deploy.shared.ModuleType;
import javax.enterprise.deploy.spi.DeploymentConfiguration;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.Target;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException;
import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
import javax.enterprise.deploy.spi.exceptions.TargetException;
import javax.enterprise.deploy.spi.status.ProgressObject;
import javax.management.remote.JMXConnector;
import javax.management.MBeanServerConnection;

import org.apache.geronimo.kernel.KernelMBean;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.jmx.MBeanProxyFactory;
import org.apache.geronimo.deployment.plugin.local.StartCommand;
import org.apache.geronimo.deployment.plugin.local.StopCommand;
import org.apache.geronimo.deployment.plugin.TargetImpl;

/**
 * 
 * 
 * @version $Revision: 1.1 $ $Date: 2004/06/02 06:50:41 $
 */
public class JMXDeploymentManager implements DeploymentManager {
    private JMXConnector jmxConnector;
    private MBeanServerConnection mbServerConnection;
    private KernelMBean kernel;

    public JMXDeploymentManager(JMXConnector jmxConnector) throws IOException {
        this.jmxConnector = jmxConnector;
        mbServerConnection = jmxConnector.getMBeanServerConnection();
        kernel = (KernelMBean) MBeanProxyFactory.getProxy(KernelMBean.class, mbServerConnection, Kernel.KERNEL);
    }

    public void release() {
        try {
            jmxConnector.close();
        } catch (IOException e) {
            // can't do much with this
        } finally {
            mbServerConnection = null;
            kernel = null;
        }
    }

    public Target[] getTargets() {
        Target target = new TargetImpl("default", null);
        return new Target[]{target};
    }

    public TargetModuleID[] getAvailableModules(ModuleType moduleType, Target[] targetList) throws TargetException {
        throw new UnsupportedOperationException();
    }

    public TargetModuleID[] getNonRunningModules(ModuleType moduleType, Target[] targetList) throws TargetException {
        throw new UnsupportedOperationException();
    }

    public TargetModuleID[] getRunningModules(ModuleType moduleType, Target[] targetList) throws TargetException{
        throw new UnsupportedOperationException();
    }

    public ProgressObject distribute(Target[] targetList, File moduleArchive, File deploymentPlan) {
        throw new UnsupportedOperationException();
    }

    public ProgressObject distribute(Target[] targetList, InputStream moduleArchive, InputStream deploymentPlan) {
        throw new UnsupportedOperationException();
    }

    public ProgressObject start(TargetModuleID[] moduleIDList) {
        StartCommand command = new StartCommand(kernel, moduleIDList);
        new Thread(command).start();
        return command;
    }

    public ProgressObject stop(TargetModuleID[] moduleIDList) {
        StopCommand command = new StopCommand(kernel, moduleIDList);
        new Thread(command).start();
        return command;
    }

    public ProgressObject undeploy(TargetModuleID[] moduleIDList) {
        throw new UnsupportedOperationException();
    }

    public boolean isRedeploySupported() {
        throw new UnsupportedOperationException();
    }

    public ProgressObject redeploy(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
        throw new UnsupportedOperationException();
    }

    public ProgressObject redeploy(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
        throw new UnsupportedOperationException();
    }

    public Locale[] getSupportedLocales() {
        throw new UnsupportedOperationException();
    }

    public Locale getCurrentLocale() {
        throw new UnsupportedOperationException();
    }

    public Locale getDefaultLocale() {
        throw new UnsupportedOperationException();
    }

    public boolean isLocaleSupported(Locale locale) {
        throw new UnsupportedOperationException();
    }

    public void setLocale(Locale locale) {
        throw new UnsupportedOperationException();
    }

    public DConfigBeanVersionType getDConfigBeanVersion() {
        throw new UnsupportedOperationException();
    }

    public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType version) {
        throw new UnsupportedOperationException();
    }

    public void setDConfigBeanVersion(DConfigBeanVersionType version) throws DConfigBeanVersionUnsupportedException {
        throw new UnsupportedOperationException();
    }

    public DeploymentConfiguration createConfiguration(DeployableObject dObj) throws InvalidModuleException {
        throw new UnsupportedOperationException();
    }
}

@@ -1857,21 +1857,19 @@
 %0A%0A/**%0A *
- 
 %0A *
- 
 %0A * @ver
@@ -1886,17 +1886,17 @@
 sion: 1.
-1
+2
  $ $Date
@@ -1913,15 +1913,15 @@
 02 0
-6:50:41
+7:05:30
  $%0A 
@@ -2709,24 +2709,125 @@
 Targets() %7B%0A
+        if (kernel == null) %7B%0A            throw new IllegalStateException(%22Disconnected%22);%0A        %7D%0A
         Targ
@@ -3378,16 +3378,17 @@
 xception
+ 
 %7B%0A      
@@ -3828,32 +3828,133 @@
 moduleIDList) %7B%0A
+        if (kernel == null) %7B%0A            throw new IllegalStateException(%22Disconnected%22);%0A        %7D%0A
         StartCom
@@ -4132,32 +4132,133 @@
 moduleIDList) %7B%0A
+        if (kernel == null) %7B%0A            throw new IllegalStateException(%22Disconnected%22);%0A        %7D%0A
         StopComm
@@ -4559,49 +4559,20 @@
     
-throw new UnsupportedOperationException()
+return false
 ;%0A  

Check for disconnection