001/* 002 * (C) Copyright 2009-2016 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 * Radu Darlea 018 * Catalin Baican 019 * Florent Guillaume 020 */ 021 022package org.nuxeo.ecm.platform.tag; 023 024import static org.nuxeo.ecm.platform.tag.TagConstants.MIGRATION_ID; 025import static org.nuxeo.ecm.platform.tag.TagConstants.MIGRATION_STATE_FACETS; 026import static org.nuxeo.ecm.platform.tag.TagConstants.MIGRATION_STATE_RELATIONS; 027import static org.nuxeo.ecm.platform.tag.TagConstants.MIGRATION_STEP_RELATIONS_TO_FACETS; 028 029import org.nuxeo.runtime.api.Framework; 030import org.nuxeo.runtime.migration.MigrationService; 031import org.nuxeo.runtime.migration.MigrationService.MigrationStatus; 032import org.nuxeo.runtime.migration.MigrationService.StatusChangeNotifier; 033import org.nuxeo.runtime.model.Component; 034import org.nuxeo.runtime.model.ComponentName; 035import org.nuxeo.runtime.model.DefaultComponent; 036 037/** 038 * the tag component. 039 */ 040public class TagServiceImpl extends DefaultComponent { 041 042 public static final ComponentName NAME = new ComponentName("org.nuxeo.ecm.platform.tag.TagService"); 043 044 // @GuardedBy("this") 045 protected volatile TagService tagService; 046 047 @Override 048 public int getApplicationStartedOrder() { 049 // should deploy before repository service because the tag service is indirectly used (through a listener) by 050 // the repository init handlers 051 Component component = (Component) Framework.getRuntime() 052 .getComponentInstance( 053 "org.nuxeo.ecm.core.repository.RepositoryServiceComponent") 054 .getInstance(); 055 return component.getApplicationStartedOrder() - 1; 056 } 057 058 // called under synchronized (this) 059 protected TagService recomputeTagService() { 060 MigrationService migrationService = Framework.getService(MigrationService.class); 061 MigrationStatus status = migrationService.getStatus(MIGRATION_ID); 062 if (status == null) { 063 throw new IllegalStateException("Unknown migration status for: " + MIGRATION_ID); 064 } 065 if (status.isRunning()) { 066 String step = status.getStep(); 067 if (MIGRATION_STEP_RELATIONS_TO_FACETS.equals(step)) { 068 return new BridgeTagService(new RelationTagService(), new FacetedTagService()); 069 } else { 070 throw new IllegalStateException("Unknown migration step: " + step); 071 } 072 } else { 073 String state = status.getState(); 074 if (MIGRATION_STATE_RELATIONS.equals(state)) { 075 return new RelationTagService(); 076 } else if (MIGRATION_STATE_FACETS.equals(state)) { 077 return new FacetedTagService(); 078 } else { 079 throw new IllegalStateException("Unknown migration state: " + state); 080 } 081 } 082 } 083 084 @Override 085 @SuppressWarnings("unchecked") 086 public <T> T getAdapter(Class<T> adapter) { 087 if (tagService == null) { 088 synchronized (this) { 089 if (tagService == null) { 090 tagService = recomputeTagService(); 091 } 092 } 093 } 094 return (T) tagService; 095 } 096 097 /** 098 * Callback class to notify of migration status changes. 099 * 100 * @since 9.3 101 */ 102 public static class TagServiceStatusChangeNotifier implements StatusChangeNotifier { 103 @Override 104 public void notifyStatusChange() { 105 TagServiceImpl tagService = (TagServiceImpl) Framework.getRuntime().getComponent(TagServiceImpl.NAME); 106 tagService.invalidateTagServiceImplementation(); 107 } 108 } 109 110 /** 111 * Called when the migration status changes, to recompute the new service. 112 * 113 * @since 9.3 114 */ 115 public void invalidateTagServiceImplementation() { 116 synchronized (this) { 117 tagService = recomputeTagService(); 118 } 119 } 120 121}