001    /**
002     *  Copyright 2005 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
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.Maven2Repository;
041    
042    /**
043     * @version $Rev: 437254 $ $Date: 2006-08-26 17:07:55 -0700 (Sat, 26 Aug 2006) $
044     */
045    public class PluginBootstrap2 {
046        private File localRepo;
047        private File plan;
048        private File buildDir;
049        private File carFile;
050        private boolean expanded;
051    
052        public void setLocalRepo(File localRepo) {
053            this.localRepo = localRepo;
054        }
055    
056        public void setPlan(File plan) {
057            this.plan = plan;
058        }
059    
060        public void setBuildDir(File buildDir) {
061            this.buildDir = buildDir;
062        }
063    
064        public void setCarFile(File carFile) {
065            this.carFile = carFile;
066        }
067    
068        public void setExpanded(final boolean expanded) {
069            this.expanded = expanded;
070        }
071    
072        public void bootstrap() throws Exception {
073            System.out.println("Packaging module configuration: " + plan);
074    
075            ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
076    
077            Maven2Repository repository = new Maven2Repository(localRepo);
078            GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
079            ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
080            ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
081                public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
082                    return buildDir;
083                }
084            };
085    
086            ArtifactManager artifactManager = new DefaultArtifactManager();
087            ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
088            DeploymentContext context = builder.buildConfiguration(
089                    false,
090                    builder.getConfigurationID(config, null, new ModuleIDBuilder()),
091                    config,
092                    null,
093                    Collections.singleton(targetConfigurationStore),
094                    artifactResolver,
095                    targetConfigurationStore);
096    
097            ConfigurationData configurationData = context.getConfigurationData();
098    
099            try {
100                writeConfiguration(configurationData);
101            }
102            finally {
103                context.close();
104            }
105        }
106    
107        private void writeConfiguration(final ConfigurationData configurationData) throws IOException {
108            if (expanded) {
109                ExecutableConfigurationUtil.writeConfiguration(configurationData, carFile);
110            }
111            else {
112                JarOutputStream out = null;
113                try {
114                    out = new JarOutputStream(new FileOutputStream(carFile));
115                    ExecutableConfigurationUtil.writeConfiguration(configurationData, out);
116                    out.flush();
117                }
118                finally {
119                    if (out != null) {
120                        try {
121                            out.close();
122                        }
123                        catch (IOException ignored) {
124                            // ignored
125                        }
126                    }
127                }
128            }
129        }
130    }