-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
CommunityBundleAddonHandler.java
81 lines (71 loc) · 3.11 KB
/
CommunityBundleAddonHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.addon.marketplace.internal.community;
import static org.openhab.core.addon.marketplace.internal.community.CommunityMarketplaceAddonService.JAR_CONTENT_TYPE;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.addon.Addon;
import org.openhab.core.addon.marketplace.MarketplaceAddonHandler;
import org.openhab.core.addon.marketplace.MarketplaceBundleInstaller;
import org.openhab.core.addon.marketplace.MarketplaceHandlerException;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
/**
* A {@link MarketplaceAddonHandler} implementation, which handles add-ons as jar files (specifically, OSGi
* bundles) and installs them through the standard OSGi bundle installation mechanism.
* The bundles installed this way have their location set to the add-on ID to identify them and determine their
* installation status.
*
* @author Kai Kreuzer - Initial contribution and API
* @author Yannick Schaus - refactoring
*
*/
@Component(immediate = true)
@NonNullByDefault
public class CommunityBundleAddonHandler extends MarketplaceBundleInstaller implements MarketplaceAddonHandler {
private static final List<String> SUPPORTED_EXT_TYPES = List.of("automation", "binding", "io", "persistence",
"transformation", "ui", "voice");
private static final String JAR_DOWNLOAD_URL_PROPERTY = "jar_download_url";
private final BundleContext bundleContext;
@Activate
public CommunityBundleAddonHandler(BundleContext bundleContext) {
this.bundleContext = bundleContext;
ensureCachedBundlesAreInstalled(bundleContext);
}
@Override
public boolean supports(String type, String contentType) {
// we support only certain extension types, and only as pure OSGi bundles
return SUPPORTED_EXT_TYPES.contains(type) && contentType.equals(JAR_CONTENT_TYPE);
}
@Override
public boolean isInstalled(String id) {
return isBundleInstalled(bundleContext, id);
}
@Override
public void install(Addon addon) throws MarketplaceHandlerException {
try {
URL sourceUrl = new URL((String) addon.getProperties().get(JAR_DOWNLOAD_URL_PROPERTY));
addBundleToCache(addon.getId(), sourceUrl);
installFromCache(bundleContext, addon.getId());
} catch (MalformedURLException e) {
throw new MarketplaceHandlerException("Malformed source URL: " + e.getMessage(), e);
}
}
@Override
public void uninstall(Addon addon) throws MarketplaceHandlerException {
uninstallBundle(bundleContext, addon.getId());
}
}