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(Files.getFileExtension(c.getFinalname()).equals(CONFIG_FILE_SUFFIX)) {
60 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(c);
61 if(featureConfigSnapshotHolder.isPresent()) {
62 snapShotHolders.add(featureConfigSnapshotHolder.get());
66 return snapShotHolders;
69 protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
71 return Optional.of(new FeatureConfigSnapshotHolder(c, this));
72 } catch (final JAXBException e) {
73 LOG.warn("Unable to parse configuration snapshot. Config from '{}' will be IGNORED. " +
74 "Note that subsequent config files may fail due to this problem. " +
75 "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
77 } catch (final XMLStreamException e) {
78 // Files that cannot be loaded are ignored as non config subsystem files e.g. jetty.xml
79 LOG.debug("Unable to read configuration file '{}'. Not a configuration snapshot",
82 return Optional.absent();
86 public int hashCode() {
89 result = prime * result + ((feature == null) ? 0 : feature.hashCode());
94 public boolean equals(final Object obj) {
101 if (getClass() != obj.getClass()) {
104 final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
105 if (feature == null) {
106 if (other.feature != null) {
109 } else if (!feature.equals(other.feature)) {
116 public String toString() {
117 return feature.getName();
122 * @see org.apache.karaf.features.Feature#getId()
125 public String getId() {
126 return feature.getId();
131 * @see org.apache.karaf.features.Feature#getName()
134 public String getName() {
135 return feature.getName();
140 * @see org.apache.karaf.features.Feature#getDescription()
143 public String getDescription() {
144 return feature.getDescription();
149 * @see org.apache.karaf.features.Feature#getDetails()
152 public String getDetails() {
153 return feature.getDetails();
158 * @see org.apache.karaf.features.Feature#getVersion()
161 public String getVersion() {
162 return feature.getVersion();
167 * @see org.apache.karaf.features.Feature#hasVersion()
170 public boolean hasVersion() {
171 return feature.hasVersion();
176 * @see org.apache.karaf.features.Feature#getResolver()
179 public String getResolver() {
180 return feature.getResolver();
185 * @see org.apache.karaf.features.Feature#getInstall()
188 public String getInstall() {
189 return feature.getInstall();
194 * @see org.apache.karaf.features.Feature#getDependencies()
197 public List<Dependency> getDependencies() {
198 return feature.getDependencies();
203 * @see org.apache.karaf.features.Feature#getBundles()
206 public List<BundleInfo> getBundles() {
207 return feature.getBundles();
212 * @see org.apache.karaf.features.Feature#getConfigurations()
215 public List<ConfigInfo> getConfigurations() {
216 return feature.getConfigurations();
221 * @see org.apache.karaf.features.Feature#getConfigurationFiles()
224 public List<ConfigFileInfo> getConfigurationFiles() {
225 return feature.getConfigurationFiles();
230 * @see org.apache.karaf.features.Feature#getConditional()
233 public List<? extends Conditional> getConditional() {
234 return feature.getConditional();
239 * @see org.apache.karaf.features.Feature#getStartLevel()
242 public int getStartLevel() {
243 return feature.getStartLevel();
248 * @see org.apache.karaf.features.Feature#getRegion()
251 public String getRegion() {
252 return feature.getRegion();