001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Kevin Leturc <kleturc@nuxeo.com>
018 *
019 */
020package org.nuxeo.runtime.reload;
021
022import java.io.File;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026
027import org.nuxeo.runtime.RuntimeServiceException;
028import org.nuxeo.runtime.api.Framework;
029import org.osgi.framework.Bundle;
030import org.osgi.framework.BundleException;
031
032/**
033 * This class is used from DevFrameworkBootstrap class by reflection. It is instantiated by reflection too, so keep the
034 * default constructor.
035 *
036 * @since 9.3
037 * @apiNote We need to keep common type for arguments and returned value.
038 */
039public class DevReloadBridge {
040
041    /**
042     * Keep this default constructor. The class is instantiated from ReloadServiceInvoker by reflection.
043     */
044    public DevReloadBridge() {
045        // nothing to do
046    }
047
048    /**
049     * @return the deployed bundles as a map associating the bundle symbolic name to its location.
050     */
051    public Map<String, String> reloadBundles(List<String> bundleNamesToUndeploy, List<File> bundlesToDeploy) {
052        ReloadService reloadService = Framework.getService(ReloadService.class);
053
054        ReloadContext context = new ReloadContext("dev-bundles").undeploy(bundleNamesToUndeploy)
055                                                                .deploy(bundlesToDeploy);
056
057        try {
058            // reload server
059            ReloadResult result = reloadService.reloadBundles(context);
060            // convert result to something usual
061            // furthermore, we just need the deployed bundles list in DevFrameworkBootstrap
062            return result.deployedBundles()
063                         .stream()
064                         .collect(Collectors.toMap(Bundle::getSymbolicName, Bundle::getLocation));
065        } catch (BundleException e) {
066            throw new RuntimeServiceException("Unable to reload server", e);
067        }
068    }
069
070}