View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.deployment;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.jar.JarOutputStream;
24  
25  import org.apache.geronimo.deployment.service.ServiceConfigBuilder;
26  import org.apache.geronimo.deployment.service.GBeanBuilder;
27  import org.apache.geronimo.deployment.xbeans.ModuleDocument;
28  import org.apache.geronimo.deployment.xbeans.ModuleType;
29  import org.apache.geronimo.kernel.Jsr77Naming;
30  import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
31  import org.apache.geronimo.kernel.config.ConfigurationData;
32  import org.apache.geronimo.kernel.config.ConfigurationStore;
33  import org.apache.geronimo.kernel.config.NullConfigurationStore;
34  import org.apache.geronimo.kernel.repository.Artifact;
35  import org.apache.geronimo.kernel.repository.ArtifactManager;
36  import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
37  import org.apache.geronimo.kernel.repository.ArtifactResolver;
38  import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
39  import org.apache.geronimo.system.configuration.ExecutableConfigurationUtil;
40  import org.apache.geronimo.system.repository.Maven1Repository;
41  
42  /**
43   * @version $Rev: 432510 $ $Date: 2006-08-18 00:44:04 -0700 (Fri, 18 Aug 2006) $
44   */
45  public class PluginBootstrap {
46      private File localRepo;
47      private File plan;
48      private File buildDir;
49      private File carFile;
50  
51      public void setLocalRepo(File localRepo) {
52          this.localRepo = localRepo;
53      }
54  
55      public void setPlan(File plan) {
56          this.plan = plan;
57      }
58  
59      public void setBuildDir(File buildDir) {
60          this.buildDir = buildDir;
61      }
62  
63      public void setCarFile(File carFile) {
64          this.carFile = carFile;
65      }
66  
67      public void bootstrap() throws Exception {
68          System.out.println();
69          System.out.println("    Packaging configuration " + plan);
70          System.out.println();
71  
72          ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
73  
74          Maven1Repository repository = new Maven1Repository(localRepo);
75          GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
76          ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
77          ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
78              public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
79                  return buildDir;
80              }
81          };
82  
83          ArtifactManager artifactManager = new DefaultArtifactManager();
84          ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
85          DeploymentContext context = builder.buildConfiguration(false, builder.getConfigurationID(config, null, new ModuleIDBuilder()), config, null, Collections.singleton(targetConfigurationStore), artifactResolver, targetConfigurationStore);
86          JarOutputStream out = null;
87          try {
88              ConfigurationData configurationData = context.getConfigurationData();
89              out = new JarOutputStream(new FileOutputStream(carFile));
90              ExecutableConfigurationUtil.writeConfiguration(configurationData, out);
91              out.flush();
92          } finally {
93              if (out != null)
94              {
95                  try {
96                      out.close();
97                  } catch (IOException ignored) {
98                      // ignored
99                  }
100             }
101             if (context != null)
102                 context.close();
103         }
104     }
105 }