KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > PluginBootstrap2


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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
18 package org.apache.geronimo.deployment;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.jar.JarOutputStream JavaDoc;
25
26 import org.apache.geronimo.deployment.service.ServiceConfigBuilder;
27 import org.apache.geronimo.deployment.service.GBeanBuilder;
28 import org.apache.geronimo.deployment.xbeans.ModuleDocument;
29 import org.apache.geronimo.deployment.xbeans.ModuleType;
30 import org.apache.geronimo.kernel.Jsr77Naming;
31 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
32 import org.apache.geronimo.kernel.config.ConfigurationData;
33 import org.apache.geronimo.kernel.config.ConfigurationStore;
34 import org.apache.geronimo.kernel.config.NullConfigurationStore;
35 import org.apache.geronimo.kernel.repository.Artifact;
36 import org.apache.geronimo.kernel.repository.ArtifactManager;
37 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
38 import org.apache.geronimo.kernel.repository.ArtifactResolver;
39 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
40 import org.apache.geronimo.system.configuration.ExecutableConfigurationUtil;
41 import org.apache.geronimo.system.repository.Maven2Repository;
42
43 /**
44  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
45  */

46 public class PluginBootstrap2 {
47     private File JavaDoc localRepo;
48     private File JavaDoc plan;
49     private File JavaDoc buildDir;
50     private File JavaDoc carFile;
51     private boolean expanded;
52
53     public void setLocalRepo(File JavaDoc localRepo) {
54         this.localRepo = localRepo;
55     }
56
57     public void setPlan(File JavaDoc plan) {
58         this.plan = plan;
59     }
60
61     public void setBuildDir(File JavaDoc buildDir) {
62         this.buildDir = buildDir;
63     }
64
65     public void setCarFile(File JavaDoc carFile) {
66         this.carFile = carFile;
67     }
68
69     public void setExpanded(final boolean expanded) {
70         this.expanded = expanded;
71     }
72
73     public void bootstrap() throws Exception JavaDoc {
74         System.out.println("Packaging module configuration: " + plan);
75
76         ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
77
78         Maven2Repository repository = new Maven2Repository(localRepo);
79         GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
80         ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
81         ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
82             public File JavaDoc createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
83                 return buildDir;
84             }
85         };
86
87         ArtifactManager artifactManager = new DefaultArtifactManager();
88         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
89         DeploymentContext context = builder.buildConfiguration(
90                 false,
91                 builder.getConfigurationID(config, null, new ModuleIDBuilder()),
92                 config,
93                 null,
94                 Collections.singleton(targetConfigurationStore),
95                 artifactResolver,
96                 targetConfigurationStore);
97
98         ConfigurationData configurationData = context.getConfigurationData();
99
100         try {
101             writeConfiguration(configurationData);
102         }
103         finally {
104             context.close();
105         }
106     }
107
108     private void writeConfiguration(final ConfigurationData configurationData) throws IOException JavaDoc {
109         if (expanded) {
110             ExecutableConfigurationUtil.writeConfiguration(configurationData, carFile);
111         }
112         else {
113             JarOutputStream JavaDoc out = null;
114             try {
115                 out = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc ignored) {
125                         // ignored
126
}
127                 }
128             }
129         }
130     }
131 }
132
Popular Tags