KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > EnvironmentBuilder


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.service;
19
20 import java.beans.PropertyEditorSupport JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedHashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
33 import org.apache.geronimo.deployment.xbeans.ArtifactType;
34 import org.apache.geronimo.deployment.xbeans.ClassFilterType;
35 import org.apache.geronimo.deployment.xbeans.DependenciesType;
36 import org.apache.geronimo.deployment.xbeans.EnvironmentDocument;
37 import org.apache.geronimo.deployment.xbeans.EnvironmentType;
38 import org.apache.geronimo.deployment.xbeans.ImportType;
39 import org.apache.geronimo.deployment.xbeans.DependencyType;
40 import org.apache.geronimo.kernel.repository.Artifact;
41 import org.apache.geronimo.kernel.repository.Dependency;
42 import org.apache.geronimo.kernel.repository.Environment;
43 import org.apache.xmlbeans.XmlException;
44 import org.apache.xmlbeans.XmlObject;
45 import org.apache.xmlbeans.XmlOptions;
46
47 /**
48  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
49  */

50 public class EnvironmentBuilder extends PropertyEditorSupport JavaDoc implements XmlAttributeBuilder {
51     private final static QName JavaDoc QNAME = EnvironmentDocument.type.getDocumentElementName();
52     private final static String JavaDoc NAMESPACE = QNAME.getNamespaceURI();
53
54     public static Environment buildEnvironment(EnvironmentType environmentType) {
55         Environment environment = new Environment();
56         if (environmentType != null) {
57             if (environmentType.isSetModuleId()) {
58                 environment.setConfigId(toArtifact(environmentType.getModuleId(), null));
59             }
60
61             if (environmentType.isSetDependencies()) {
62                 DependencyType[] dependencyArray = environmentType.getDependencies().getDependencyArray();
63                 LinkedHashSet JavaDoc dependencies = toDependencies(dependencyArray);
64                 environment.setDependencies(dependencies);
65             }
66             environment.setInverseClassLoading(environmentType.isSetInverseClassloading());
67             environment.setSuppressDefaultEnvironment(environmentType.isSetSuppressDefaultEnvironment());
68             environment.setHiddenClasses(toFilters(environmentType.getHiddenClasses()));
69             environment.setNonOverrideableClasses(toFilters(environmentType.getNonOverridableClasses()));
70         }
71
72         return environment;
73     }
74
75     public static void mergeEnvironments(Environment environment, Environment additionalEnvironment) {
76         if (additionalEnvironment != null) {
77             //TODO merge configIds??
78
if (environment.getConfigId() == null) {
79                 environment.setConfigId(additionalEnvironment.getConfigId());
80             }
81             environment.addDependencies(additionalEnvironment.getDependencies());
82             environment.setInverseClassLoading(environment.isInverseClassLoading() || additionalEnvironment.isInverseClassLoading());
83             environment.setSuppressDefaultEnvironment(environment.isSuppressDefaultEnvironment() || additionalEnvironment.isSuppressDefaultEnvironment());
84             environment.addHiddenClasses(additionalEnvironment.getHiddenClasses());
85             environment.addNonOverrideableClasses(additionalEnvironment.getNonOverrideableClasses());
86         }
87     }
88
89     public static Environment buildEnvironment(EnvironmentType environmentType, Environment defaultEnvironment) {
90         Environment environment = buildEnvironment(environmentType);
91         if (!environment.isSuppressDefaultEnvironment()) {
92             mergeEnvironments(environment, defaultEnvironment);
93         }
94         return environment;
95     }
96
97     public static EnvironmentType buildEnvironmentType(Environment environment) {
98         EnvironmentType environmentType = EnvironmentType.Factory.newInstance();
99         if (environment.getConfigId() != null) {
100             ArtifactType configId = toArtifactType(environment.getConfigId());
101             environmentType.setModuleId(configId);
102         }
103
104         List JavaDoc dependencies = toDependencyTypes(environment.getDependencies());
105         DependencyType[] dependencyTypes = (DependencyType[]) dependencies.toArray(new DependencyType[dependencies.size()]);
106         DependenciesType dependenciesType = environmentType.addNewDependencies();
107         dependenciesType.setDependencyArray(dependencyTypes);
108         if (environment.isInverseClassLoading()) {
109             environmentType.addNewInverseClassloading();
110         }
111         if (environment.isSuppressDefaultEnvironment()) {
112             environmentType.addNewSuppressDefaultEnvironment();
113         }
114         environmentType.setHiddenClasses(toFilterType(environment.getHiddenClasses()));
115         environmentType.setNonOverridableClasses(toFilterType(environment.getNonOverrideableClasses()));
116         return environmentType;
117     }
118
119     private static ClassFilterType toFilterType(Set JavaDoc filters) {
120         String JavaDoc[] classFilters = (String JavaDoc[]) filters.toArray(new String JavaDoc[filters.size()]);
121         ClassFilterType classFilter = ClassFilterType.Factory.newInstance();
122         classFilter.setFilterArray(classFilters);
123         return classFilter;
124     }
125
126     private static List JavaDoc toDependencyTypes(Collection JavaDoc artifacts) {
127         List JavaDoc dependencies = new ArrayList JavaDoc();
128         for (Iterator JavaDoc iterator = artifacts.iterator(); iterator.hasNext();) {
129             Dependency dependency = (Dependency) iterator.next();
130             ArtifactType artifactType = toDependencyType(dependency);
131             dependencies.add(artifactType);
132         }
133         return dependencies;
134     }
135
136     private static ArtifactType toArtifactType(Artifact artifact) {
137         ArtifactType artifactType = ArtifactType.Factory.newInstance();
138         fillArtifactType(artifact, artifactType);
139         return artifactType;
140     }
141
142     private static void fillArtifactType(Artifact artifact, ArtifactType artifactType) {
143         if (artifact.getGroupId() != null) {
144             artifactType.setGroupId(artifact.getGroupId());
145         }
146         if (artifact.getArtifactId() != null) {
147             artifactType.setArtifactId(artifact.getArtifactId());
148         }
149         if (artifact.getVersion() != null) {
150             artifactType.setVersion(artifact.getVersion().toString());
151         }
152         if (artifact.getType() != null) {
153             artifactType.setType(artifact.getType());
154         }
155     }
156
157     private static DependencyType toDependencyType(Dependency dependency) {
158         DependencyType dependencyType = DependencyType.Factory.newInstance();
159         fillArtifactType(dependency.getArtifact(), dependencyType);
160
161         org.apache.geronimo.kernel.repository.ImportType importType = dependency.getImportType();
162         if (importType == org.apache.geronimo.kernel.repository.ImportType.CLASSES) {
163             dependencyType.setImport(ImportType.CLASSES);
164         } else if (importType == org.apache.geronimo.kernel.repository.ImportType.SERVICES) {
165             dependencyType.setImport(ImportType.SERVICES);
166         }
167
168         return dependencyType;
169     }
170
171     private static Set JavaDoc toFilters(ClassFilterType filterType) {
172         Set JavaDoc filters = new HashSet JavaDoc();
173         if (filterType != null) {
174             String JavaDoc[] filterArray = filterType.getFilterArray();
175             for (int i = 0; i < filterArray.length; i++) {
176                 String JavaDoc filter = filterArray[i].trim();
177                 filters.add(filter);
178             }
179         }
180         return filters;
181     }
182
183     //package level for testing
184
static LinkedHashSet JavaDoc toArtifacts(ArtifactType[] artifactTypes) {
185         LinkedHashSet JavaDoc artifacts = new LinkedHashSet JavaDoc();
186         for (int i = 0; i < artifactTypes.length; i++) {
187             ArtifactType artifactType = artifactTypes[i];
188             Artifact artifact = toArtifact(artifactType, "jar");
189             artifacts.add(artifact);
190         }
191         return artifacts;
192     }
193
194     private static LinkedHashSet JavaDoc toDependencies(DependencyType[] dependencyArray) {
195         LinkedHashSet JavaDoc dependencies = new LinkedHashSet JavaDoc();
196         for (int i = 0; i < dependencyArray.length; i++) {
197             DependencyType artifactType = dependencyArray[i];
198             Dependency dependency = toDependency(artifactType);
199             dependencies.add(dependency);
200         }
201         return dependencies;
202     }
203
204     private static Dependency toDependency(DependencyType dependencyType) {
205         Artifact artifact = toArtifact(dependencyType, null);
206         if (ImportType.CLASSES.equals(dependencyType.getImport())) {
207             return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.CLASSES);
208         } else if (ImportType.SERVICES.equals(dependencyType.getImport())) {
209             return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.SERVICES);
210         } else if (dependencyType.getImport() == null) {
211             return new Dependency(artifact, org.apache.geronimo.kernel.repository.ImportType.ALL);
212         } else {
213             throw new IllegalArgumentException JavaDoc("Unknown import type: " + dependencyType.getImport());
214         }
215     }
216
217     private static Artifact toArtifact(ArtifactType artifactType, String JavaDoc defaultType) {
218         String JavaDoc groupId = artifactType.isSetGroupId() ? trim(artifactType.getGroupId()) : null;
219         String JavaDoc type = artifactType.isSetType() ? trim(artifactType.getType()) : defaultType;
220         String JavaDoc artifactId = trim(artifactType.getArtifactId());
221         String JavaDoc version = artifactType.isSetVersion() ? trim(artifactType.getVersion()) : null;
222         return new Artifact(groupId, artifactId, version, type);
223     }
224
225     private static String JavaDoc trim(String JavaDoc string) {
226         if (string == null || string.length() == 0) {
227         return null;
228         }
229         return string.trim();
230     }
231
232
233     public String JavaDoc getNamespace() {
234         return NAMESPACE;
235     }
236
237     public Object JavaDoc getValue(XmlObject xmlObject, String JavaDoc type, ClassLoader JavaDoc cl) throws DeploymentException {
238
239         EnvironmentType environmentType;
240         if (xmlObject instanceof EnvironmentType) {
241             environmentType = (EnvironmentType) xmlObject;
242         } else {
243             environmentType = (EnvironmentType) xmlObject.copy().changeType(EnvironmentType.type);
244         }
245         try {
246             XmlOptions xmlOptions = new XmlOptions();
247             xmlOptions.setLoadLineNumbers();
248             Collection JavaDoc errors = new ArrayList JavaDoc();
249             xmlOptions.setErrorListener(errors);
250             if (!environmentType.validate(xmlOptions)) {
251                 throw new XmlException("Invalid deployment descriptor: " + errors + "\nDescriptor: " + environmentType.toString(), null, errors);
252             }
253         } catch (XmlException e) {
254             throw new DeploymentException(e);
255         }
256
257         return buildEnvironment(environmentType);
258     }
259
260     public String JavaDoc getAsText() {
261         Environment environment = (Environment) getValue();
262         EnvironmentType environmentType = buildEnvironmentType(environment);
263         XmlOptions xmlOptions = new XmlOptions();
264         xmlOptions.setSaveSyntheticDocumentElement(QNAME);
265         xmlOptions.setSavePrettyPrint();
266         return environmentType.xmlText(xmlOptions);
267     }
268
269     public void setAsText(String JavaDoc text) {
270         try {
271             EnvironmentDocument environmentDocument = EnvironmentDocument.Factory.parse(text);
272             EnvironmentType environmentType = environmentDocument.getEnvironment();
273             Environment environment = (Environment) getValue(environmentType, null, null);
274             setValue(environment);
275
276         } catch (XmlException e) {
277             throw new PropertyEditorException(e);
278         } catch (DeploymentException e) {
279             throw new PropertyEditorException(e);
280         }
281     }
282
283
284     //This is added by hand to the xmlAttributeBuilders since it is needed to bootstrap the ServiceConfigBuilder.
285
}
286
Popular Tags