001/*
002 * (C) Copyright 2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.comment.service;
023
024import static org.nuxeo.ecm.platform.comment.api.CommentConstants.MIGRATION_ID;
025import static org.nuxeo.ecm.platform.comment.api.CommentConstants.MIGRATION_STATE_PROPERTY;
026import static org.nuxeo.ecm.platform.comment.api.CommentConstants.MIGRATION_STATE_RELATION;
027import static org.nuxeo.ecm.platform.comment.api.CommentConstants.MIGRATION_STEP_RELATION_TO_PROPERTY;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.platform.comment.api.CommentManager;
032import org.nuxeo.ecm.platform.comment.impl.BridgeCommentManager;
033import org.nuxeo.ecm.platform.comment.impl.CommentManagerImpl;
034import org.nuxeo.ecm.platform.comment.impl.PropertyCommentManager;
035import org.nuxeo.runtime.api.Framework;
036import org.nuxeo.runtime.migration.MigrationService;
037import org.nuxeo.runtime.model.ComponentInstance;
038import org.nuxeo.runtime.model.ComponentName;
039import org.nuxeo.runtime.model.DefaultComponent;
040
041/**
042 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
043 */
044public class CommentService extends DefaultComponent {
045
046    public static final String ID = "org.nuxeo.ecm.platform.comment.service.CommentService";
047
048    /** @since 10.3 */
049    public static final ComponentName NAME = new ComponentName(ID);
050
051    public static final String VERSIONING_EXTENSION_POINT_RULES = "rules";
052
053    private static final Log log = LogFactory.getLog(CommentService.class);
054
055    // @GuardedBy("this")
056    protected volatile CommentManager commentManager;
057
058    private CommentServiceConfig config;
059
060    @Override
061    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
062        if ("config".equals(extensionPoint)) {
063            config = (CommentServiceConfig) contribution;
064            log.debug("registered service config: " + config);
065        } else {
066            log.warn("unknown extension point: " + extensionPoint);
067        }
068    }
069
070    @Override
071    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
072        // do nothing
073    }
074
075    public CommentManager getCommentManager() {
076        log.debug("getCommentManager");
077        if (commentManager == null) {
078            commentManager = new CommentManagerImpl(config);
079        }
080        return commentManager;
081    }
082
083    public CommentServiceConfig getConfig() {
084        return config;
085    }
086
087    @Override
088    @SuppressWarnings("unchecked")
089    public <T> T getAdapter(Class<T> adapter) {
090        if (commentManager == null) {
091            synchronized (this) {
092                if (commentManager == null) {
093                    commentManager = recomputeCommentManager();
094                }
095            }
096        }
097        return (T) commentManager;
098    }
099
100    // called under synchronized (this)
101    protected CommentManager recomputeCommentManager() {
102        MigrationService migrationService = Framework.getService(MigrationService.class);
103        MigrationService.MigrationStatus status = migrationService.getStatus(MIGRATION_ID);
104        if (status == null) {
105            throw new IllegalStateException("Unknown migration status for: " + MIGRATION_ID);
106        }
107        if (status.isRunning()) {
108            String step = status.getStep();
109            if (MIGRATION_STEP_RELATION_TO_PROPERTY.equals(step)) {
110                return new BridgeCommentManager(new CommentManagerImpl(config), new PropertyCommentManager());
111            } else {
112                throw new IllegalStateException("Unknown migration step: " + step);
113            }
114        } else {
115            String state = status.getState();
116            if (MIGRATION_STATE_RELATION.equals(state)) {
117                return new CommentManagerImpl(config);
118            } else if (MIGRATION_STATE_PROPERTY.equals(state)) {
119                return new PropertyCommentManager();
120            } else {
121                throw new IllegalStateException("Unknown migration state: " + state);
122            }
123        }
124    }
125
126    /**
127     * Called when the migration status changes, to recompute the new service.
128     *
129     * @since 10.3
130     */
131    public void invalidateCommentManagerImplementation() {
132        synchronized (this) {
133            commentManager = recomputeCommentManager();
134        }
135    }
136
137}