/* 
 * JBoss, Home of Professional Open Source 
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved. 
 * See the copyright.txt in the distribution for a 
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use, 
 * modify, copy, or redistribute it subject to the terms and conditions 
 * of the GNU Lesser General Public License, v. 2.1. 
 * This program is distributed in the hope that it will be useful, but WITHOUT A 
 * 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, 
 * v.2.1 along with this distribution; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */
package org.switchyard.as7.extension.deployment;

import java.io.IOException;
import java.io.InputStream;

import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.logging.Logger;
import org.jboss.modules.Module;
import org.switchyard.as7.extension.SwitchYardDeploymentMarker;
import org.switchyard.config.model.ModelPuller;
import org.switchyard.config.model.switchyard.SwitchYardModel;

/**
 * @author Magesh Kumar B <mageshbk@jboss.com> (C) 2011 Red Hat Inc.
 */
public class SwitchYardConfigProcessor implements DeploymentUnitProcessor {

    private static final Logger LOG = Logger.getLogger("org.switchyard");

    /* (non-Javadoc)
     * @see org.jboss.as.server.deployment.DeploymentUnitProcessor#deploy(org.jboss.as.server.deployment.DeploymentPhaseContext)
     */
    @Override
    public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();

        if (!SwitchYardDeploymentMarker.isSwitchYardDeployment(deploymentUnit)) {
            LOG.debug("Ignoring deployment unit '" + deploymentUnit.getName() + "' as it is not recognized as being a SwitchYard Application.");
            return;
        }
        SwitchYardMetaData switchYardMetaData = deploymentUnit.getAttachment(SwitchYardMetaData.ATTACHMENT_KEY);

        ClassLoader origCL = Thread.currentThread().getContextClassLoader();
        InputStream is = null;
        try {
            final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
            Thread.currentThread().setContextClassLoader(module.getClassLoader());
            is = switchYardMetaData.getSwitchYardFile().openStream();
            SwitchYardModel switchyardModel = new ModelPuller<SwitchYardModel>().pull(is);
            switchYardMetaData.setSwitchYardModel(switchyardModel);
            LOG.debug("Successfully parsed SwitchYard configuration for deployment unit '" + deploymentUnit.getName() + "'.");
        } catch (IOException ioe) {
            throw new DeploymentUnitProcessingException(ioe);
        } finally {
            Thread.currentThread().setContextClassLoader(origCL);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                    LOG.error(ioe);
                }
            }
        }

    }

    /* (non-Javadoc)
     * @see org.jboss.as.server.deployment.DeploymentUnitProcessor#undeploy(org.jboss.as.server.deployment.DeploymentUnit)
     */
    @Override
    public void undeploy(DeploymentUnit context) {
        // TODO Auto-generated method stub

    }

}

@@ -1507,24 +1507,188 @@
 mentMarker;%0A
+import org.switchyard.common.property.CompoundPropertyResolver;%0Aimport org.switchyard.common.property.PropertyResolver;%0Aimport org.switchyard.config.Configuration;%0A
 import org.s
@@ -1719,24 +1719,24 @@
 odelPuller;%0A
-
 import org.s
@@ -3193,16 +3193,88 @@
 ll(is);%0A
+            JBossPropertyResolver.set(deploymentUnit, switchyardModel);%0A
         
@@ -4071,24 +4071,24 @@
  context) %7B%0A
-
         // T
@@ -4126,10 +4126,1053 @@
 %0A    %7D%0A%0A
+    private static class JBossPropertyResolver implements PropertyResolver %7B%0A%0A        private final org.jboss.metadata.property.PropertyResolver _wrapped;%0A%0A        private JBossPropertyResolver(org.jboss.metadata.property.PropertyResolver wrapped) %7B%0A            _wrapped = wrapped;%0A        %7D%0A%0A        /**%0A         * %7B@inheritDoc%7D%0A         */%0A        @Override%0A        public Object resolveProperty(String key) %7B%0A            return key != null ? _wrapped.resolve(key) : null;%0A        %7D%0A%0A        private static void set(DeploymentUnit deploymentUnit, SwitchYardModel switchyardModel) %7B%0A            org.jboss.metadata.property.PropertyResolver wrapped = deploymentUnit.getAttachment(org.jboss.as.ee.metadata.property.Attachments.FINAL_PROPERTY_RESOLVER);%0A            if (wrapped != null) %7B%0A                Configuration config = switchyardModel.getModelConfiguration();%0A                config.setPropertyResolver(CompoundPropertyResolver.compact(config.getPropertyResolver(), new JBossPropertyResolver(wrapped)));%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A
 %7D%0A

support obfuscation/encryption for password details