001/*
002 * (C) Copyright 2015 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.theme.migration.service;
020
021import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
022import org.nuxeo.ecm.web.resources.core.ResourceDescriptor;
023import org.nuxeo.runtime.api.Framework;
024import org.nuxeo.runtime.logging.DeprecationLogger;
025import org.nuxeo.runtime.model.ComponentInstance;
026import org.nuxeo.runtime.model.DefaultComponent;
027
028/**
029 * Compat service displaying migration information for old extension points.
030 *
031 * @since 7.4
032 */
033public class ThemeMigrationService extends DefaultComponent {
034
035    protected static final String XP = "org.nuxeo.theme.services.ThemeService";
036
037    protected static final String WR_XP = "org.nuxeo.ecm.platform.WebResources";
038
039    @Override
040    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
041        if ("resources".equals(extensionPoint)) {
042            if (contribution instanceof ResourceDescriptor) {
043                ResourceDescriptor r = (ResourceDescriptor) contribution;
044                String message = String.format("Resource '%s' on component %s should now be contributed to extension "
045                        + "point '%s': a compatibility registration was performed but it may not be " + "accurate.",
046                        r.getName(), contributor.getName(), WR_XP);
047                DeprecationLogger.log(message, "7.4");
048                Framework.getRuntime().getWarnings().add(message);
049                // ensure path is absolute, consider that resource is in the war, and if not, user will have to declare
050                // it directly to the WRM endpoint
051                String path = r.getPath();
052                if (path != null && !path.startsWith("/")) {
053                    r.setUri("/" + path);
054                }
055                WebResourceManager wrm = Framework.getService(WebResourceManager.class);
056                wrm.registerResource(r);
057            } else {
058                String message = String.format("Warning: unknown contribution to target extension point '%s' of '%s'. "
059                        + "Check your extension in component %s", extensionPoint, XP, contributor.getName());
060                DeprecationLogger.log(message, "7.4");
061                Framework.getRuntime().getWarnings().add(message);
062            }
063        } else {
064            String message = String.format("Warning: target extension point '%s' of '%s'"
065                    + " is unknown as it has been removed since 7.4. Check your extension in component %s",
066                    extensionPoint, XP, contributor.getName());
067            DeprecationLogger.log(message, "7.4");
068            Framework.getRuntime().getWarnings().add(message);
069        }
070    }
071
072    @Override
073    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
074        if ("resources".equals(extensionPoint) && contribution instanceof ResourceDescriptor) {
075            ResourceDescriptor r = (ResourceDescriptor) contribution;
076            WebResourceManager wrm = Framework.getService(WebResourceManager.class);
077            wrm.unregisterResource(r);
078        } else {
079            // NOOP
080        }
081    }
082
083}