001/*
002 * (C) Copyright 2013 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.operations.test;
020
021import java.io.IOException;
022import java.net.URL;
023
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026import org.nuxeo.drive.adapter.FileSystemItem;
027import org.nuxeo.drive.service.FileSystemItemAdapterService;
028import org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Activates / deactivates the {@link FileSystemItem} factories of the given profile.
037 *
038 * @author Antoine Taillefer
039 */
040@Operation(id = NuxeoDriveSetActiveFactories.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Activate or deactivate file system item factories")
041public class NuxeoDriveSetActiveFactories {
042
043    public static final String ID = "NuxeoDrive.SetActiveFactories";
044
045    private static final Logger log = LogManager.getLogger(NuxeoDriveSetActiveFactories.class);
046
047    @Param(name = "profile")
048    protected String profile;
049
050    @Param(name = "enable", required = false)
051    protected boolean enable = true;
052
053    @OperationMethod
054    public boolean run() throws IOException {
055        NuxeoDriveIntegrationTestsHelper.checkOperationAllowed();
056        String contrib;
057        if ("userworkspace".equals(profile)) {
058            contrib = "/OSGI-INF/nuxeodrive-hierarchy-userworkspace-contrib.xml";
059        } else if ("permission".equals(profile)) {
060            contrib = "/OSGI-INF/nuxeodrive-hierarchy-permission-contrib.xml";
061        } else {
062            log.warn("No active file system item factory contribution for profile '{}'.", profile);
063            return false;
064        }
065        URL url = NuxeoDriveSetActiveFactories.class.getResource(contrib);
066        try {
067            if (enable) {
068                Framework.getRuntime().getContext().deploy(url);
069            } else {
070                Framework.getRuntime().getContext().undeploy(url);
071            }
072        } finally {
073            Framework.getRuntime().getComponentManager().unstash();
074        }
075        FileSystemItemAdapterServiceImpl fileSystemItemAdapterService = (FileSystemItemAdapterServiceImpl) Framework.getService(
076                FileSystemItemAdapterService.class);
077        fileSystemItemAdapterService.setActiveFactories();
078        return true;
079    }
080
081}