001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    package org.apache.geronimo.deployment;
018    
019    import java.io.File;
020    import java.io.FileOutputStream;
021    import java.io.IOException;
022    import java.util.Collections;
023    import java.util.jar.JarOutputStream;
024    
025    import org.apache.geronimo.deployment.service.ServiceConfigBuilder;
026    import org.apache.geronimo.deployment.service.GBeanBuilder;
027    import org.apache.geronimo.deployment.xbeans.ModuleDocument;
028    import org.apache.geronimo.deployment.xbeans.ModuleType;
029    import org.apache.geronimo.kernel.Jsr77Naming;
030    import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
031    import org.apache.geronimo.kernel.config.ConfigurationData;
032    import org.apache.geronimo.kernel.config.ConfigurationStore;
033    import org.apache.geronimo.kernel.config.NullConfigurationStore;
034    import org.apache.geronimo.kernel.repository.Artifact;
035    import org.apache.geronimo.kernel.repository.ArtifactManager;
036    import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
037    import org.apache.geronimo.kernel.repository.ArtifactResolver;
038    import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
039    import org.apache.geronimo.system.configuration.ExecutableConfigurationUtil;
040    import org.apache.geronimo.system.repository.Maven1Repository;
041    
042    /**
043     * @version $Rev: 432510 $ $Date: 2006-08-18 00:44:04 -0700 (Fri, 18 Aug 2006) $
044     */
045    public class PluginBootstrap {
046        private File localRepo;
047        private File plan;
048        private File buildDir;
049        private File carFile;
050    
051        public void setLocalRepo(File localRepo) {
052            this.localRepo = localRepo;
053        }
054    
055        public void setPlan(File plan) {
056            this.plan = plan;
057        }
058    
059        public void setBuildDir(File buildDir) {
060            this.buildDir = buildDir;
061        }
062    
063        public void setCarFile(File carFile) {
064            this.carFile = carFile;
065        }
066    
067        public void bootstrap() throws Exception {
068            System.out.println();
069            System.out.println("    Packaging configuration " + plan);
070            System.out.println();
071    
072            ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
073    
074            Maven1Repository repository = new Maven1Repository(localRepo);
075            GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
076            ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
077            ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
078                public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
079                    return buildDir;
080                }
081            };
082    
083            ArtifactManager artifactManager = new DefaultArtifactManager();
084            ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
085            DeploymentContext context = builder.buildConfiguration(false, builder.getConfigurationID(config, null, new ModuleIDBuilder()), config, null, Collections.singleton(targetConfigurationStore), artifactResolver, targetConfigurationStore);
086            JarOutputStream out = null;
087            try {
088                ConfigurationData configurationData = context.getConfigurationData();
089                out = new JarOutputStream(new FileOutputStream(carFile));
090                ExecutableConfigurationUtil.writeConfiguration(configurationData, out);
091                out.flush();
092            } finally {
093                if (out != null)
094                {
095                    try {
096                        out.close();
097                    } catch (IOException ignored) {
098                        // ignored
099                    }
100                }
101                if (context != null)
102                    context.close();
103            }
104        }
105    }