001/* 002 * (C) Copyright 2006-2018 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 * Thierry Delprat 018 * Florent Guillaume 019 */ 020package org.nuxeo.ecm.core.trash; 021 022import org.nuxeo.runtime.api.Framework; 023import org.nuxeo.runtime.migration.MigrationService; 024import org.nuxeo.runtime.migration.MigrationService.MigrationStatus; 025import org.nuxeo.runtime.model.ComponentName; 026import org.nuxeo.runtime.model.DefaultComponent; 027 028public class TrashServiceImpl extends DefaultComponent { 029 030 /** @since 10.2 */ 031 public static final ComponentName NAME = new ComponentName("org.nuxeo.ecm.core.trash.TrashService"); 032 033 /** @since 10.1 */ 034 public static final String MIGRATION_ID = "trash-storage"; // also in XML 035 036 /** @since 10.1 */ 037 public static final String MIGRATION_STATE_LIFECYCLE = "lifecycle"; // also in XML 038 039 /** @since 10.1 */ 040 public static final String MIGRATION_STATE_PROPERTY = "property"; // also in XML 041 042 /** @since 10.1 */ 043 public static final String MIGRATION_STEP_LIFECYCLE_TO_PROPERTY = "lifecycle-to-property"; // also in XML 044 045 protected volatile TrashService trashService; 046 047 // called under synchronized (this) 048 @SuppressWarnings("deprecation") 049 protected TrashService recomputeTrashService() { 050 MigrationService migrationService = Framework.getService(MigrationService.class); 051 MigrationStatus status = migrationService.getStatus(MIGRATION_ID); 052 if (status == null) { 053 throw new IllegalStateException("Unknown migration status for: " + MIGRATION_ID); 054 } 055 if (status.isRunning()) { 056 String step = status.getStep(); 057 if (MIGRATION_STEP_LIFECYCLE_TO_PROPERTY.equals(step)) { 058 return new BridgeTrashService(new LifeCycleTrashService(), new PropertyTrashService()); 059 } else { 060 throw new IllegalStateException("Unknown migration step: " + step); 061 } 062 } else { 063 String state = status.getState(); 064 if (MIGRATION_STATE_LIFECYCLE.equals(state)) { 065 return new LifeCycleTrashService(); 066 } else if (MIGRATION_STATE_PROPERTY.equals(state)) { 067 return new PropertyTrashService(); 068 } else { 069 throw new IllegalStateException("Unknown migration state: " + state); 070 } 071 } 072 } 073 074 @Override 075 @SuppressWarnings("unchecked") 076 public <T> T getAdapter(Class<T> adapter) { 077 if (trashService == null) { 078 synchronized (this) { 079 if (trashService == null) { 080 trashService = recomputeTrashService(); 081 } 082 } 083 } 084 return (T) trashService; 085 } 086 087 /** 088 * Called when the migration status changes, to recompute the new service. 089 * 090 * @since 10.2 091 */ 092 public void invalidateTrashServiceImplementation() { 093 synchronized (this) { 094 trashService = recomputeTrashService(); 095 } 096 } 097 098}