use of io.fabric8.support.api.Resource in project fabric8 by jboss-fuse.
the class ProfileEditAction method editProfile.
private void editProfile(Profile profile) throws Exception {
boolean editInLine = false;
ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
if (delete || remove) {
editInLine = true;
}
if (features != null && features.length > 0) {
editInLine = true;
handleFeatures(builder, features, profile);
}
if (repositories != null && repositories.length > 0) {
editInLine = true;
handleFeatureRepositories(builder, repositories, profile);
}
if (libs != null && libs.length > 0) {
editInLine = true;
handleLibraries(builder, libs, profile, "lib", LIB_PREFIX);
}
if (endorsed != null && endorsed.length > 0) {
editInLine = true;
handleLibraries(builder, endorsed, profile, "endorsed lib", ENDORSED_PREFIX);
}
if (extension != null && extension.length > 0) {
editInLine = true;
handleLibraries(builder, extension, profile, "extension lib", EXT_PREFIX);
}
if (bundles != null && bundles.length > 0) {
editInLine = true;
handleBundles(builder, bundles, profile);
}
if (fabs != null && fabs.length > 0) {
editInLine = true;
handleFabs(builder, fabs, profile);
}
if (overrides != null && overrides.length > 0) {
editInLine = true;
handleOverrides(builder, overrides, profile);
}
if (pidProperties != null && pidProperties.length > 0) {
editInLine = handlePid(builder, pidProperties, profile);
}
if (systemProperties != null && systemProperties.length > 0) {
editInLine = true;
handleSystemProperties(builder, systemProperties, profile);
}
if (configProperties != null && configProperties.length > 0) {
editInLine = true;
handleConfigProperties(builder, configProperties, profile);
}
if (resource != null && delete) {
editInLine = true;
builder.deleteFileConfiguration(resource);
}
profileService.updateProfile(builder.getProfile());
if (!editInLine) {
resource = resource != null ? resource : "io.fabric8.agent.properties";
// then open the resource in the editor.
if (pidProperties != null && pidProperties.length == 1) {
resource = pidProperties[0] + ".properties";
}
openInEditor(profile, resource);
}
}
use of io.fabric8.support.api.Resource in project fabric8 by jboss-fuse.
the class FabricResource method container.
/**
* Accesses a container resource
*/
@Path("container/{containerId}")
public ContainerResource container(@PathParam("containerId") String containerId) {
FabricService fabricService = getFabricService();
if (fabricService != null && Strings.isNotBlank(containerId)) {
Container container = fabricService.getContainer(containerId);
if (container != null) {
return new ContainerResource(this, container);
}
LOG.warn("No container found for: {}", container);
}
return null;
}
use of io.fabric8.support.api.Resource in project fabric8 by jboss-fuse.
the class Agent method provision.
public void provision(Map<String, Feature> allFeatures, Set<String> features, Set<String> bundles, Set<String> reqs, Set<String> overrides, Set<String> optionals, Map<String, Map<VersionRange, Map<String, String>>> metadata) throws Exception {
Callable<Map<String, Resource>> res = loadResources(manager, metadata, optionals);
// TODO: requirements should be able to be assigned to a region
Map<String, Set<String>> requirements = new HashMap<>();
for (String feature : features) {
addToMapSet(requirements, ROOT_REGION, "feature:" + feature);
}
for (String bundle : bundles) {
addToMapSet(requirements, ROOT_REGION, "bundle:" + bundle);
}
for (String req : reqs) {
addToMapSet(requirements, ROOT_REGION, "req:" + req);
}
Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
request.updateSnaphots = updateSnaphots;
request.bundleUpdateRange = bundleUpdateRange;
request.featureResolutionRange = featureResolutionRange;
request.globalRepository = new StaticRepository(res.call().values());
request.overrides = overrides;
request.requirements = requirements;
request.stateChanges = Collections.emptyMap();
request.options = options;
request.metadata = metadata;
request.bundleStartTimeout = bundleStartTimeout;
Deployer.DeploymentState dstate = new Deployer.DeploymentState();
// Service bundle
dstate.serviceBundle = serviceBundle;
// Start level
FrameworkStartLevel fsl = systemBundleContext.getBundle().adapt(FrameworkStartLevel.class);
dstate.initialBundleStartLevel = fsl.getInitialBundleStartLevel();
dstate.currentStartLevel = fsl.getStartLevel();
// Bundles
dstate.bundles = new HashMap<>();
for (Bundle bundle : systemBundleContext.getBundles()) {
dstate.bundles.put(bundle.getBundleId(), bundle);
}
// Features
dstate.features = allFeatures;
// Region -> bundles mapping
// Region -> policy mapping
dstate.bundlesPerRegion = new HashMap<>();
dstate.filtersPerRegion = new HashMap<>();
if (digraph == null) {
for (Long id : dstate.bundles.keySet()) {
addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, id);
}
} else {
RegionDigraph clone = digraph.copy();
for (Region region : clone.getRegions()) {
// Get bundles
dstate.bundlesPerRegion.put(region.getName(), new HashSet<>(region.getBundleIds()));
// Get policies
Map<String, Map<String, Set<String>>> edges = new HashMap<>();
for (RegionDigraph.FilteredRegion fr : clone.getEdges(region)) {
Map<String, Set<String>> policy = new HashMap<>();
Map<String, Collection<String>> current = fr.getFilter().getSharingPolicy();
for (String ns : current.keySet()) {
for (String f : current.get(ns)) {
addToMapSet(policy, ns, f);
}
}
edges.put(fr.getRegion().getName(), policy);
}
dstate.filtersPerRegion.put(region.getName(), edges);
}
}
final State state = new State();
try {
storage.load(state);
} catch (IOException e) {
LOGGER.warn("Error loading agent state", e);
}
if (state.managedBundles.isEmpty()) {
for (Bundle b : systemBundleContext.getBundles()) {
if (b.getBundleId() != 0) {
addToMapSet(state.managedBundles, ROOT_REGION, b.getBundleId());
}
}
}
// corresponding jar url and use that one to compute the checksum of the bundle.
for (Map.Entry<Long, Bundle> entry : dstate.bundles.entrySet()) {
long id = entry.getKey();
Bundle bundle = entry.getValue();
if (id > 0 && isUpdateable(bundle) && !state.bundleChecksums.containsKey(id)) {
try {
URL url = bundle.getResource("META-INF/MANIFEST.MF");
URLConnection con = url.openConnection();
Method method = con.getClass().getDeclaredMethod("getLocalURL");
method.setAccessible(true);
String jarUrl = ((URL) method.invoke(con)).toExternalForm();
if (jarUrl.startsWith("jar:")) {
String jar = jarUrl.substring("jar:".length(), jarUrl.indexOf("!/"));
jar = new URL(jar).getFile();
long checksum = ChecksumUtils.checksumFile(new File(jar));
state.bundleChecksums.put(id, checksum);
}
} catch (Throwable t) {
LOGGER.debug("Error calculating checksum for bundle: %s", bundle, t);
}
}
}
dstate.state = state;
Set<String> prereqs = new HashSet<>();
while (true) {
try {
Deployer.DeployCallback callback = new BaseDeployCallback() {
@Override
public void phase(String message) {
Agent.this.updateStatus(message);
}
@Override
public void saveState(State newState) {
state.replace(newState);
try {
Agent.this.saveState(newState);
} catch (IOException e) {
LOGGER.warn("Error storing agent state", e);
}
}
@Override
public void provisionList(Set<Resource> resources) {
Agent.this.provisionList(resources);
}
@Override
public void restoreConfigAdminIfNeeded() {
if (configInstaller != null) {
configInstaller.restoreConfigAdminIfNeeded();
}
}
@Override
public boolean done(boolean agentStarted, List<String> urls) {
return Agent.this.done(agentStarted, urls);
}
};
// FABRIC-790, FABRIC-981 - wait for ProfileUrlHandler before attempting to load bundles (in subsystem.resolve())
// (which may be the case with bundle.xxx=blueprint:profile:xxx URLs in io.fabric8.agent PID)
// https://developer.jboss.org/message/920681 - 30 seconds is too low sometimes
// there was "url.handler.timeouts" option for agent, but it was removed during migration to karaf 4.x resolver
// LOGGER.debug("Waiting for ProfileUrlHandler");
// awaitService(URLStreamHandlerService.class, "(url.handler.protocol=profile)", 30, TimeUnit.SECONDS);
// LOGGER.debug("Waiting for ProfileUrlHandler finished");
Deployer deployer = new Deployer(manager, callback);
deployer.setDeploymentAgentId(deploymentAgentId);
deployer.deploy(dstate, request);
break;
} catch (Deployer.PartialDeploymentException e) {
if (!prereqs.containsAll(e.getMissing())) {
prereqs.addAll(e.getMissing());
} else {
throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
}
}
}
}
use of io.fabric8.support.api.Resource in project fabric8 by jboss-fuse.
the class Deployer method getBundleInputStream.
protected InputStream getBundleInputStream(Resource resource, Map<String, StreamProvider> providers) throws IOException {
String uri = getUri(resource);
if (uri == null) {
throw new IllegalStateException("Resource has no uri");
}
StreamProvider provider = providers.get(uri);
if (provider == null) {
throw new IllegalStateException("Resource " + uri + " has no StreamProvider");
}
return new FileInputStream(provider.getFile());
}
use of io.fabric8.support.api.Resource in project fabric8 by jboss-fuse.
the class Deployer method computeBundlesToRefresh.
private void computeBundlesToRefresh(Map<Bundle, String> toRefresh, Collection<Bundle> bundles, Map<Resource, Bundle> resources, Map<Resource, List<Wire>> resolution) {
// Compute the new list of fragments
Map<Bundle, Set<Resource>> newFragments = new HashMap<>();
for (Bundle bundle : bundles) {
newFragments.put(bundle, new HashSet<Resource>());
}
if (resolution != null) {
for (Resource res : resolution.keySet()) {
for (Wire wire : resolution.get(res)) {
if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
Bundle bundle = resources.get(wire.getProvider());
if (bundle != null) {
Bundle b = resources.get(wire.getRequirer());
Resource r = b != null ? b.adapt(BundleRevision.class) : wire.getRequirer();
newFragments.get(bundle).add(r);
}
}
}
}
}
// Main loop
int size;
Map<Bundle, Resource> bndToRes = new HashMap<>();
for (Map.Entry<Resource, Bundle> entry : resources.entrySet()) {
bndToRes.put(entry.getValue(), entry.getKey());
}
do {
size = toRefresh.size();
main: for (Bundle bundle : bundles) {
Resource resource = bndToRes.get(bundle);
// This bundle is not managed
if (resource == null) {
continue;
}
// Continue if we already know about this bundle
if (toRefresh.containsKey(bundle)) {
continue;
}
// Ignore non resolved bundle
BundleWiring wiring = bundle.adapt(BundleWiring.class);
if (wiring == null) {
continue;
}
// Ignore bundles that won't be wired
List<Wire> newWires = resolution.get(resource);
if (newWires == null) {
continue;
}
// Check if this bundle is a host and its fragments changed
Set<Resource> oldFragments = new HashSet<>();
for (BundleWire wire : wiring.getProvidedWires(null)) {
if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
oldFragments.add(wire.getRequirer());
}
}
if (!oldFragments.equals(newFragments.get(bundle))) {
toRefresh.put(bundle, "Attached fragments changed: " + new ArrayList<>(newFragments.get(bundle)));
break;
}
// Compare the old and new resolutions
Set<Resource> wiredBundles = new HashSet<>();
for (BundleWire wire : wiring.getRequiredWires(null)) {
BundleRevision rev = wire.getProvider();
Bundle provider = rev.getBundle();
if (toRefresh.containsKey(provider)) {
// The bundle is wired to a bundle being refreshed,
// so we need to refresh it too
toRefresh.put(bundle, "Wired to " + provider.getSymbolicName() + "/" + provider.getVersion() + " which is being refreshed");
continue main;
}
Resource res = bndToRes.get(provider);
wiredBundles.add(res != null ? res : rev);
}
Map<Resource, Requirement> wiredResources = new HashMap<>();
for (Wire wire : newWires) {
// Handle only packages, hosts, and required bundles
String namespace = wire.getRequirement().getNamespace();
if (!namespace.equals(BundleNamespace.BUNDLE_NAMESPACE) && !namespace.equals(PackageNamespace.PACKAGE_NAMESPACE) && !namespace.equals(HostNamespace.HOST_NAMESPACE)) {
continue;
}
// Ignore non-resolution time requirements
String effective = wire.getRequirement().getDirectives().get(Namespace.CAPABILITY_EFFECTIVE_DIRECTIVE);
if (effective != null && !Namespace.EFFECTIVE_RESOLVE.equals(effective)) {
continue;
}
// Ignore non bundle resources
if (!isBundle(wire.getProvider())) {
continue;
}
if (!wiredResources.containsKey(wire.getProvider())) {
wiredResources.put(wire.getProvider(), wire.getRequirement());
}
}
if (!wiredBundles.containsAll(wiredResources.keySet())) {
Map<Resource, Requirement> newResources = new HashMap<>(wiredResources);
newResources.keySet().removeAll(wiredBundles);
StringBuilder sb = new StringBuilder();
sb.append("Should be wired to: ");
boolean first = true;
for (Map.Entry<Resource, Requirement> entry : newResources.entrySet()) {
if (!first) {
sb.append(", ");
} else {
first = false;
}
Resource res = entry.getKey();
Requirement req = entry.getValue();
sb.append(getSymbolicName(res)).append("/").append(getVersion(res));
sb.append(" (through ");
sb.append(req);
sb.append(")");
}
toRefresh.put(bundle, sb.toString());
}
}
} while (toRefresh.size() > size);
}
Aggregations