001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.service;
017:
018: import java.beans.PropertyEditorSupport;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.LinkedHashSet;
024: import java.util.List;
025: import java.util.Set;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.geronimo.common.DeploymentException;
030: import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
031: import org.apache.geronimo.deployment.xbeans.ArtifactType;
032: import org.apache.geronimo.deployment.xbeans.ClassFilterType;
033: import org.apache.geronimo.deployment.xbeans.DependenciesType;
034: import org.apache.geronimo.deployment.xbeans.EnvironmentDocument;
035: import org.apache.geronimo.deployment.xbeans.EnvironmentType;
036: import org.apache.geronimo.deployment.xbeans.ImportType;
037: import org.apache.geronimo.deployment.xbeans.DependencyType;
038: import org.apache.geronimo.kernel.repository.Artifact;
039: import org.apache.geronimo.kernel.repository.Dependency;
040: import org.apache.geronimo.kernel.repository.Environment;
041: import org.apache.xmlbeans.XmlException;
042: import org.apache.xmlbeans.XmlObject;
043: import org.apache.xmlbeans.XmlOptions;
044:
045: /**
046: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
047: */
048: public class EnvironmentBuilder extends PropertyEditorSupport implements
049: XmlAttributeBuilder {
050: private final static QName QNAME = EnvironmentDocument.type
051: .getDocumentElementName();
052: private final static String NAMESPACE = QNAME.getNamespaceURI();
053:
054: public static Environment buildEnvironment(
055: EnvironmentType environmentType) {
056: Environment environment = new Environment();
057: if (environmentType != null) {
058: if (environmentType.isSetModuleId()) {
059: environment.setConfigId(toArtifact(environmentType
060: .getModuleId(), null));
061: }
062:
063: if (environmentType.isSetDependencies()) {
064: DependencyType[] dependencyArray = environmentType
065: .getDependencies().getDependencyArray();
066: LinkedHashSet dependencies = toDependencies(dependencyArray);
067: environment.setDependencies(dependencies);
068: }
069: environment.setInverseClassLoading(environmentType
070: .isSetInverseClassloading());
071: environment.setSuppressDefaultEnvironment(environmentType
072: .isSetSuppressDefaultEnvironment());
073: environment.setHiddenClasses(toFilters(environmentType
074: .getHiddenClasses()));
075: environment
076: .setNonOverrideableClasses(toFilters(environmentType
077: .getNonOverridableClasses()));
078: }
079:
080: return environment;
081: }
082:
083: public static void mergeEnvironments(Environment environment,
084: Environment additionalEnvironment) {
085: if (additionalEnvironment != null) {
086: //TODO merge configIds??
087: if (environment.getConfigId() == null) {
088: environment.setConfigId(additionalEnvironment
089: .getConfigId());
090: }
091: environment.addDependencies(additionalEnvironment
092: .getDependencies());
093: environment.setInverseClassLoading(environment
094: .isInverseClassLoading()
095: || additionalEnvironment.isInverseClassLoading());
096: environment.setSuppressDefaultEnvironment(environment
097: .isSuppressDefaultEnvironment()
098: || additionalEnvironment
099: .isSuppressDefaultEnvironment());
100: environment.addHiddenClasses(additionalEnvironment
101: .getHiddenClasses());
102: environment.addNonOverrideableClasses(additionalEnvironment
103: .getNonOverrideableClasses());
104: }
105: }
106:
107: public static Environment buildEnvironment(
108: EnvironmentType environmentType,
109: Environment defaultEnvironment) {
110: Environment environment = buildEnvironment(environmentType);
111: if (!environment.isSuppressDefaultEnvironment()) {
112: mergeEnvironments(environment, defaultEnvironment);
113: }
114: return environment;
115: }
116:
117: public static EnvironmentType buildEnvironmentType(
118: Environment environment) {
119: EnvironmentType environmentType = EnvironmentType.Factory
120: .newInstance();
121: if (environment.getConfigId() != null) {
122: ArtifactType configId = toArtifactType(environment
123: .getConfigId());
124: environmentType.setModuleId(configId);
125: }
126:
127: List dependencies = toDependencyTypes(environment
128: .getDependencies());
129: DependencyType[] dependencyTypes = (DependencyType[]) dependencies
130: .toArray(new DependencyType[dependencies.size()]);
131: DependenciesType dependenciesType = environmentType
132: .addNewDependencies();
133: dependenciesType.setDependencyArray(dependencyTypes);
134: if (environment.isInverseClassLoading()) {
135: environmentType.addNewInverseClassloading();
136: }
137: if (environment.isSuppressDefaultEnvironment()) {
138: environmentType.addNewSuppressDefaultEnvironment();
139: }
140: environmentType.setHiddenClasses(toFilterType(environment
141: .getHiddenClasses()));
142: environmentType
143: .setNonOverridableClasses(toFilterType(environment
144: .getNonOverrideableClasses()));
145: return environmentType;
146: }
147:
148: private static ClassFilterType toFilterType(Set filters) {
149: String[] classFilters = (String[]) filters
150: .toArray(new String[filters.size()]);
151: ClassFilterType classFilter = ClassFilterType.Factory
152: .newInstance();
153: classFilter.setFilterArray(classFilters);
154: return classFilter;
155: }
156:
157: private static List toDependencyTypes(Collection artifacts) {
158: List dependencies = new ArrayList();
159: for (Iterator iterator = artifacts.iterator(); iterator
160: .hasNext();) {
161: Dependency dependency = (Dependency) iterator.next();
162: ArtifactType artifactType = toDependencyType(dependency);
163: dependencies.add(artifactType);
164: }
165: return dependencies;
166: }
167:
168: private static ArtifactType toArtifactType(Artifact artifact) {
169: ArtifactType artifactType = ArtifactType.Factory.newInstance();
170: fillArtifactType(artifact, artifactType);
171: return artifactType;
172: }
173:
174: private static void fillArtifactType(Artifact artifact,
175: ArtifactType artifactType) {
176: if (artifact.getGroupId() != null) {
177: artifactType.setGroupId(artifact.getGroupId());
178: }
179: if (artifact.getArtifactId() != null) {
180: artifactType.setArtifactId(artifact.getArtifactId());
181: }
182: if (artifact.getVersion() != null) {
183: artifactType.setVersion(artifact.getVersion().toString());
184: }
185: if (artifact.getType() != null) {
186: artifactType.setType(artifact.getType());
187: }
188: }
189:
190: private static DependencyType toDependencyType(Dependency dependency) {
191: DependencyType dependencyType = DependencyType.Factory
192: .newInstance();
193: fillArtifactType(dependency.getArtifact(), dependencyType);
194:
195: org.apache.geronimo.kernel.repository.ImportType importType = dependency
196: .getImportType();
197: if (importType == org.apache.geronimo.kernel.repository.ImportType.CLASSES) {
198: dependencyType.setImport(ImportType.CLASSES);
199: } else if (importType == org.apache.geronimo.kernel.repository.ImportType.SERVICES) {
200: dependencyType.setImport(ImportType.SERVICES);
201: }
202:
203: return dependencyType;
204: }
205:
206: private static Set toFilters(ClassFilterType filterType) {
207: Set filters = new HashSet();
208: if (filterType != null) {
209: String[] filterArray = filterType.getFilterArray();
210: for (int i = 0; i < filterArray.length; i++) {
211: String filter = filterArray[i].trim();
212: filters.add(filter);
213: }
214: }
215: return filters;
216: }
217:
218: //package level for testing
219: static LinkedHashSet toArtifacts(ArtifactType[] artifactTypes) {
220: LinkedHashSet artifacts = new LinkedHashSet();
221: for (int i = 0; i < artifactTypes.length; i++) {
222: ArtifactType artifactType = artifactTypes[i];
223: Artifact artifact = toArtifact(artifactType, "jar");
224: artifacts.add(artifact);
225: }
226: return artifacts;
227: }
228:
229: private static LinkedHashSet toDependencies(
230: DependencyType[] dependencyArray) {
231: LinkedHashSet dependencies = new LinkedHashSet();
232: for (int i = 0; i < dependencyArray.length; i++) {
233: DependencyType artifactType = dependencyArray[i];
234: Dependency dependency = toDependency(artifactType);
235: dependencies.add(dependency);
236: }
237: return dependencies;
238: }
239:
240: private static Dependency toDependency(DependencyType dependencyType) {
241: Artifact artifact = toArtifact(dependencyType, null);
242: if (ImportType.CLASSES.equals(dependencyType.getImport())) {
243: return new Dependency(
244: artifact,
245: org.apache.geronimo.kernel.repository.ImportType.CLASSES);
246: } else if (ImportType.SERVICES.equals(dependencyType
247: .getImport())) {
248: return new Dependency(
249: artifact,
250: org.apache.geronimo.kernel.repository.ImportType.SERVICES);
251: } else if (dependencyType.getImport() == null) {
252: return new Dependency(
253: artifact,
254: org.apache.geronimo.kernel.repository.ImportType.ALL);
255: } else {
256: throw new IllegalArgumentException("Unknown import type: "
257: + dependencyType.getImport());
258: }
259: }
260:
261: private static Artifact toArtifact(ArtifactType artifactType,
262: String defaultType) {
263: String groupId = artifactType.isSetGroupId() ? trim(artifactType
264: .getGroupId())
265: : null;
266: String type = artifactType.isSetType() ? trim(artifactType
267: .getType()) : defaultType;
268: String artifactId = trim(artifactType.getArtifactId());
269: String version = artifactType.isSetVersion() ? trim(artifactType
270: .getVersion())
271: : null;
272: return new Artifact(groupId, artifactId, version, type);
273: }
274:
275: private static String trim(String string) {
276: if (string == null || string.length() == 0) {
277: return null;
278: }
279: return string.trim();
280: }
281:
282: public String getNamespace() {
283: return NAMESPACE;
284: }
285:
286: public Object getValue(XmlObject xmlObject, String type,
287: ClassLoader cl) throws DeploymentException {
288:
289: EnvironmentType environmentType;
290: if (xmlObject instanceof EnvironmentType) {
291: environmentType = (EnvironmentType) xmlObject;
292: } else {
293: environmentType = (EnvironmentType) xmlObject.copy()
294: .changeType(EnvironmentType.type);
295: }
296: try {
297: XmlOptions xmlOptions = new XmlOptions();
298: xmlOptions.setLoadLineNumbers();
299: Collection errors = new ArrayList();
300: xmlOptions.setErrorListener(errors);
301: if (!environmentType.validate(xmlOptions)) {
302: throw new XmlException(
303: "Invalid deployment descriptor: " + errors
304: + "\nDescriptor: "
305: + environmentType.toString(), null,
306: errors);
307: }
308: } catch (XmlException e) {
309: throw new DeploymentException(e);
310: }
311:
312: return buildEnvironment(environmentType);
313: }
314:
315: public String getAsText() {
316: Environment environment = (Environment) getValue();
317: EnvironmentType environmentType = buildEnvironmentType(environment);
318: XmlOptions xmlOptions = new XmlOptions();
319: xmlOptions.setSaveSyntheticDocumentElement(QNAME);
320: xmlOptions.setSavePrettyPrint();
321: return environmentType.xmlText(xmlOptions);
322: }
323:
324: public void setAsText(String text) {
325: try {
326: EnvironmentDocument environmentDocument = EnvironmentDocument.Factory
327: .parse(text);
328: EnvironmentType environmentType = environmentDocument
329: .getEnvironment();
330: Environment environment = (Environment) getValue(
331: environmentType, null, null);
332: setValue(environment);
333:
334: } catch (XmlException e) {
335: throw new PropertyEditorException(e);
336: } catch (DeploymentException e) {
337: throw new PropertyEditorException(e);
338: }
339: }
340:
341: //This is added by hand to the xmlAttributeBuilders since it is needed to bootstrap the ServiceConfigBuilder.
342: }
|