2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.configpusherfeature.internal;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.Files;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import javax.xml.bind.JAXBException;
16 import javax.xml.stream.XMLStreamException;
17 import org.apache.karaf.features.BundleInfo;
18 import org.apache.karaf.features.Conditional;
19 import org.apache.karaf.features.ConfigFileInfo;
20 import org.apache.karaf.features.ConfigInfo;
21 import org.apache.karaf.features.Dependency;
22 import org.apache.karaf.features.Feature;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
28 * its underlying ConfigFileInfo's
30 * Delegates the the contained feature and provides additional methods.
32 public class AbstractFeatureWrapper implements Feature {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureWrapper.class);
35 protected static final String CONFIG_FILE_SUFFIX = "xml";
37 protected Feature feature = null;
39 protected AbstractFeatureWrapper() {
40 // prevent instantiation without Feature
44 * @param f Feature to wrap
46 public AbstractFeatureWrapper(final Feature f) {
47 Preconditions.checkNotNull(f,"FeatureWrapper requires non-null Feature in constructor");
52 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
53 * from the underlying Feature Config files
55 public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
56 final LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
57 for(final ConfigFileInfo c: getConfigurationFiles()) {
59 if(isConfigXMLFile(c.getFinalname())) {
60 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(c);
61 if(featureConfigSnapshotHolder.isPresent()) {
62 snapShotHolders.add(featureConfigSnapshotHolder.get());
66 return snapShotHolders;
69 private static boolean isConfigXMLFile(String fullName) {
70 return Files.getFileExtension(fullName).equals(CONFIG_FILE_SUFFIX);
73 protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
75 return Optional.of(new FeatureConfigSnapshotHolder(c, this));
76 } catch (final JAXBException e) {
77 LOG.warn("Unable to parse configuration snapshot. Config from '{}' will be IGNORED. " +
78 "Note that subsequent config files may fail due to this problem. " +
79 "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
81 } catch (final XMLStreamException e) {
82 // Files that cannot be loaded are ignored as non config subsystem files e.g. jetty.xml
83 LOG.debug("Unable to read configuration file '{}'. Not a configuration snapshot",
86 return Optional.absent();
90 public int hashCode() {
93 result = prime * result + (feature == null ? 0 : feature.hashCode());
98 public boolean equals(final Object obj) {
105 if (getClass() != obj.getClass()) {
108 final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
109 if (feature == null) {
110 if (other.feature != null) {
113 } else if (!feature.equals(other.feature)) {
120 public String toString() {
121 return feature.getName();
126 * @see org.apache.karaf.features.Feature#getId()
129 public String getId() {
130 return feature.getId();
135 * @see org.apache.karaf.features.Feature#getName()
138 public String getName() {
139 return feature.getName();
144 * @see org.apache.karaf.features.Feature#getDescription()
147 public String getDescription() {
148 return feature.getDescription();
153 * @see org.apache.karaf.features.Feature#getDetails()
156 public String getDetails() {
157 return feature.getDetails();
162 * @see org.apache.karaf.features.Feature#getVersion()
165 public String getVersion() {
166 return feature.getVersion();
171 * @see org.apache.karaf.features.Feature#hasVersion()
174 public boolean hasVersion() {
175 return feature.hasVersion();
180 * @see org.apache.karaf.features.Feature#getResolver()
183 public String getResolver() {
184 return feature.getResolver();
189 * @see org.apache.karaf.features.Feature#getInstall()
192 public String getInstall() {
193 return feature.getInstall();
198 * @see org.apache.karaf.features.Feature#getDependencies()
201 public List<Dependency> getDependencies() {
202 return feature.getDependencies();
207 * @see org.apache.karaf.features.Feature#getBundles()
210 public List<BundleInfo> getBundles() {
211 return feature.getBundles();
216 * @see org.apache.karaf.features.Feature#getConfigurations()
219 public List<ConfigInfo> getConfigurations() {
220 return feature.getConfigurations();
225 * @see org.apache.karaf.features.Feature#getConfigurationFiles()
228 public List<ConfigFileInfo> getConfigurationFiles() {
229 return feature.getConfigurationFiles();
234 * @see org.apache.karaf.features.Feature#getConditional()
237 public List<? extends Conditional> getConditional() {
238 return feature.getConditional();
243 * @see org.apache.karaf.features.Feature#getStartLevel()
246 public int getStartLevel() {
247 return feature.getStartLevel();
252 * @see org.apache.karaf.features.Feature#getRegion()
255 public String getRegion() {
256 return feature.getRegion();