001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment;
019    
020    import java.io.File;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.util.Collections;
024    import java.util.jar.JarOutputStream;
025    
026    import org.apache.geronimo.deployment.service.ServiceConfigBuilder;
027    import org.apache.geronimo.deployment.service.GBeanBuilder;
028    import org.apache.geronimo.deployment.xbeans.ModuleDocument;
029    import org.apache.geronimo.deployment.xbeans.ModuleType;
030    import org.apache.geronimo.kernel.Jsr77Naming;
031    import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
032    import org.apache.geronimo.kernel.config.ConfigurationData;
033    import org.apache.geronimo.kernel.config.ConfigurationStore;
034    import org.apache.geronimo.kernel.config.NullConfigurationStore;
035    import org.apache.geronimo.kernel.repository.Artifact;
036    import org.apache.geronimo.kernel.repository.ArtifactManager;
037    import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
038    import org.apache.geronimo.kernel.repository.ArtifactResolver;
039    import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
040    import org.apache.geronimo.system.configuration.ExecutableConfigurationUtil;
041    import org.apache.geronimo.system.repository.Maven2Repository;
042    
043    /**
044     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
045     */
046    public class PluginBootstrap2 {
047        private File localRepo;
048        private File plan;
049        private File buildDir;
050        private File carFile;
051        private boolean expanded;
052    
053        public void setLocalRepo(File localRepo) {
054            this.localRepo = localRepo;
055        }
056    
057        public void setPlan(File plan) {
058            this.plan = plan;
059        }
060    
061        public void setBuildDir(File buildDir) {
062            this.buildDir = buildDir;
063        }
064    
065        public void setCarFile(File carFile) {
066            this.carFile = carFile;
067        }
068    
069        public void setExpanded(final boolean expanded) {
070            this.expanded = expanded;
071        }
072    
073        public void bootstrap() throws Exception {
074            System.out.println("Packaging module configuration: " + plan);
075    
076            ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
077    
078            Maven2Repository repository = new Maven2Repository(localRepo);
079            GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
080            ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
081            ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
082                public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
083                    return buildDir;
084                }
085            };
086    
087            ArtifactManager artifactManager = new DefaultArtifactManager();
088            ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
089            DeploymentContext context = builder.buildConfiguration(
090                    false,
091                    builder.getConfigurationID(config, null, new ModuleIDBuilder()),
092                    config,
093                    null,
094                    Collections.singleton(targetConfigurationStore),
095                    artifactResolver,
096                    targetConfigurationStore);
097    
098            ConfigurationData configurationData = context.getConfigurationData();
099    
100            try {
101                writeConfiguration(configurationData);
102            }
103            finally {
104                context.close();
105            }
106        }
107    
108        private void writeConfiguration(final ConfigurationData configurationData) throws IOException {
109            if (expanded) {
110                ExecutableConfigurationUtil.writeConfiguration(configurationData, carFile);
111            }
112            else {
113                JarOutputStream out = null;
114                try {
115                    out = new JarOutputStream(new FileOutputStream(carFile));
116                    ExecutableConfigurationUtil.writeConfiguration(configurationData, out);
117                    out.flush();
118                }
119                finally {
120                    if (out != null) {
121                        try {
122                            out.close();
123                        }
124                        catch (IOException ignored) {
125                            // ignored
126                        }
127                    }
128                }
129            }
130        }
131    }