1 /**
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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.Maven2Repository;
41
42 /**
43 * @version $Rev: 437254 $ $Date: 2006-08-26 17:07:55 -0700 (Sat, 26 Aug 2006) $
44 */
45 public class PluginBootstrap2 {
46 private File localRepo;
47 private File plan;
48 private File buildDir;
49 private File carFile;
50 private boolean expanded;
51
52 public void setLocalRepo(File localRepo) {
53 this.localRepo = localRepo;
54 }
55
56 public void setPlan(File plan) {
57 this.plan = plan;
58 }
59
60 public void setBuildDir(File buildDir) {
61 this.buildDir = buildDir;
62 }
63
64 public void setCarFile(File carFile) {
65 this.carFile = carFile;
66 }
67
68 public void setExpanded(final boolean expanded) {
69 this.expanded = expanded;
70 }
71
72 public void bootstrap() throws Exception {
73 System.out.println("Packaging module configuration: " + plan);
74
75 ModuleType config = ModuleDocument.Factory.parse(plan).getModule();
76
77 Maven2Repository repository = new Maven2Repository(localRepo);
78 GBeanBuilder gBeanBuilder = new GBeanBuilder(null, null);
79 ServiceConfigBuilder builder = new ServiceConfigBuilder(null, Collections.singleton(repository), Collections.singleton(gBeanBuilder), new Jsr77Naming());
80 ConfigurationStore targetConfigurationStore = new NullConfigurationStore() {
81 public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
82 return buildDir;
83 }
84 };
85
86 ArtifactManager artifactManager = new DefaultArtifactManager();
87 ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.singleton(repository), null);
88 DeploymentContext context = builder.buildConfiguration(
89 false,
90 builder.getConfigurationID(config, null, new ModuleIDBuilder()),
91 config,
92 null,
93 Collections.singleton(targetConfigurationStore),
94 artifactResolver,
95 targetConfigurationStore);
96
97 ConfigurationData configurationData = context.getConfigurationData();
98
99 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
125 }
126 }
127 }
128 }
129 }
130 }