/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.as.arquillian.protocol.jmx;

import java.util.concurrent.atomic.AtomicBoolean;

import org.jboss.arquillian.container.spi.Container;
import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.container.spi.context.annotation.ContainerScoped;
import org.jboss.arquillian.container.spi.event.container.BeforeDeploy;
import org.jboss.arquillian.container.spi.event.container.BeforeStop;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.annotation.SuiteScoped;
import org.jboss.as.arquillian.protocol.jmx.JMXProtocolAS7.ServiceArchiveHolder;
import org.jboss.logging.Logger;

/**
 * A deployer for the Arquillian JMXProtocol endpoint.
 *
 * @see JMXProtocolPackager
 *
 * @author thomas.diesler@jboss.com
 * @since 31-May-2011
 */
public class ArquillianServiceDeployer {

    private static final Logger log = Logger.getLogger(ArquillianServiceDeployer.class);

    @Inject
    @SuiteScoped
    private Instance<ServiceArchiveHolder> archiveHolderInst;

    @Inject
    @ContainerScoped
    private Instance<Container> containerInst;

    private AtomicBoolean serviceArchiveDeployed = new AtomicBoolean();

    public void doServiceDeploy(@Observes BeforeDeploy event) {
        ServiceArchiveHolder archiveHolder = archiveHolderInst.get();
        if (archiveHolder != null && !serviceArchiveDeployed.get()) {
            try {
                DeployableContainer<?> deployableContainer = containerInst.get().getDeployableContainer();
                deployableContainer.deploy(archiveHolder.getArchive());
                serviceArchiveDeployed.set(true);
            } catch (Throwable th) {
                log.error("Cannot deploy arquillian service", th);
            }
        }
    }

    public void undeploy(@Observes BeforeStop event) {
        if (serviceArchiveDeployed.get()) {
            try {
                DeployableContainer<?> deployableContainer = containerInst.get().getDeployableContainer();
                ServiceArchiveHolder archiveHolder = archiveHolderInst.get();
                deployableContainer.undeploy(archiveHolder.getArchive());
                serviceArchiveDeployed.set(false);
            } catch (Throwable th) {
                log.error("Cannot undeploy arquillian service", th);
            }
        }
    }
}

@@ -1606,16 +1606,57 @@
 .Logger;
+%0Aimport org.jboss.shrinkwrap.api.Archive;
 %0A%0A/**%0A *
@@ -2189,24 +2189,37 @@
 %0A    public 
+synchronized 
 void doServi
@@ -2366,17 +2366,16 @@
 null && 
-!
 serviceA
@@ -2386,32 +2386,41 @@
 veDeployed.get()
+ == false
 ) %7B%0A            
@@ -2417,32 +2417,166 @@
           try %7B%0A
+                Archive%3C?%3E archive = archiveHolder.getArchive();%0A                log.infof(%22Deploy arquillian service: %25s%22, archive);%0A
                 
@@ -2716,35 +2716,16 @@
 (archive
-Holder.getArchive()
 );%0A     
@@ -2915,16 +2915,29 @@
  public 
+synchronized 
 void und
@@ -2980,20 +2980,115 @@
         
-if (
+ServiceArchiveHolder archiveHolder = archiveHolderInst.get();%0A        if (archiveHolder != null && 
 serviceA
@@ -3107,16 +3107,24 @@
 ed.get()
+ == true
 ) %7B%0A    
@@ -3129,32 +3129,168 @@
           try %7B%0A
+                Archive%3C?%3E archive = archiveHolder.getArchive();%0A                log.infof(%22Undeploy arquillian service: %25s%22, archive);%0A
                 
@@ -3384,86 +3384,8 @@
 ();%0A
-                ServiceArchiveHolder archiveHolder = archiveHolderInst.get();%0A
     
@@ -3432,35 +3432,16 @@
 (archive
-Holder.getArchive()
 );%0A     

Synchronize arquillian-service deployment/undeployment