BundleInfoImpl.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2019 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.wcm.commons.bundleinfo.impl;

  21. import java.util.Date;
  22. import java.util.Dictionary;

  23. import org.apache.commons.lang3.StringUtils;
  24. import org.jetbrains.annotations.NotNull;
  25. import org.jetbrains.annotations.Nullable;
  26. import org.osgi.framework.Bundle;
  27. import org.osgi.framework.Constants;

  28. import io.wcm.wcm.commons.bundleinfo.BundleInfo;
  29. import io.wcm.wcm.commons.bundleinfo.BundleState;

  30. /**
  31.  * Provides meta-information about a installed bundle.
  32.  */
  33. class BundleInfoImpl implements BundleInfo {

  34.   private final String symbolicName;
  35.   private final Bundle bundle;
  36.   private final Dictionary<String, String> headers;
  37.   private final BundleState state;

  38.   BundleInfoImpl(Bundle bundle) {
  39.     this.symbolicName = bundle.getSymbolicName();
  40.     this.bundle = bundle;
  41.     this.headers = bundle.getHeaders();
  42.     if (isFragment()) {
  43.       this.state = BundleState.FRAGMENT;
  44.     }
  45.     else {
  46.       this.state = BundleState.valueOf(bundle.getState());
  47.     }
  48.   }

  49.   @Override
  50.   public @NotNull Bundle getBundle() {
  51.     return bundle;
  52.   }

  53.   @Override
  54.   public @NotNull String getSymbolicName() {
  55.     return symbolicName;
  56.   }

  57.   @Override
  58.   public @NotNull String getName() {
  59.     return StringUtils.defaultString(headers.get(Constants.BUNDLE_NAME), getSymbolicName());
  60.   }

  61.   @Override
  62.   public @NotNull String getVersion() {
  63.     return StringUtils.defaultString(headers.get(Constants.BUNDLE_VERSION));
  64.   }

  65.   @Override
  66.   public @NotNull BundleState getState() {
  67.     return state;
  68.   }

  69.   @Override
  70.   public @Nullable Date getLastModified() {
  71.     if (bundle.getLastModified() == 0) {
  72.       return null;
  73.     }
  74.     return new Date(bundle.getLastModified());
  75.   }

  76.   @Override
  77.   public boolean isFragment() {
  78.     String fragmentHost = headers.get(Constants.FRAGMENT_HOST);
  79.     return StringUtils.isNotBlank(fragmentHost);
  80.   }

  81.   @Override
  82.   public String toString() {
  83.     return symbolicName;
  84.   }

  85.   @Override
  86.   public int hashCode() {
  87.     return symbolicName.hashCode();
  88.   }

  89.   @Override
  90.   public boolean equals(Object obj) {
  91.     if (!(obj instanceof BundleInfoImpl)) {
  92.       return false;
  93.     }
  94.     BundleInfoImpl other = (BundleInfoImpl)obj;
  95.     return symbolicName.equals(other.symbolicName);
  96.   }

  97.   @Override
  98.   public int compareTo(BundleInfo obj) {
  99.     return getSymbolicName().compareTo(obj.getSymbolicName());
  100.   }

  101. }