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.migration.MigrationService.StatusChangeNotifier; 026import org.nuxeo.runtime.model.ComponentName; 027import org.nuxeo.runtime.model.DefaultComponent; 028 029public class TrashServiceImpl extends DefaultComponent { 030 031 /** @since 10.2 */ 032 public static final ComponentName NAME = new ComponentName("org.nuxeo.ecm.core.trash.TrashService"); 033 034 /** @since 10.1 */ 035 public static final String MIGRATION_ID = "trash-storage"; // also in XML 036 037 /** @since 10.1 */ 038 public static final String MIGRATION_STATE_LIFECYCLE = "lifecycle"; // also in XML 039 040 /** @since 10.1 */ 041 public static final String MIGRATION_STATE_PROPERTY = "property"; // also in XML 042 043 /** @since 10.1 */ 044 public static final String MIGRATION_STEP_LIFECYCLE_TO_PROPERTY = "lifecycle-to-property"; // also in XML 045 046 protected volatile TrashService trashService; 047 048 // called under synchronized (this) 049 @SuppressWarnings("deprecation") 050 protected TrashService recomputeTrashService() { 051 MigrationService migrationService = Framework.getService(MigrationService.class); 052 MigrationStatus status = migrationService.getStatus(MIGRATION_ID); 053 if (status == null) { 054 throw new IllegalStateException("Unknown migration status for: " + MIGRATION_ID); 055 } 056 if (status.isRunning()) { 057 String step = status.getStep(); 058 if (MIGRATION_STEP_LIFECYCLE_TO_PROPERTY.equals(step)) { 059 return new BridgeTrashService(new LifeCycleTrashService(), new PropertyTrashService()); 060 } else { 061 throw new IllegalStateException("Unknown migration step: " + step); 062 } 063 } else { 064 String state = status.getState(); 065 if (MIGRATION_STATE_LIFECYCLE.equals(state)) { 066 return new LifeCycleTrashService(); 067 } else if (MIGRATION_STATE_PROPERTY.equals(state)) { 068 return new PropertyTrashService(); 069 } else { 070 throw new IllegalStateException("Unknown migration state: " + state); 071 } 072 } 073 } 074 075 @Override 076 @SuppressWarnings("unchecked") 077 public <T> T getAdapter(Class<T> adapter) { 078 if (trashService == null) { 079 synchronized (this) { 080 if (trashService == null) { 081 trashService = recomputeTrashService(); 082 } 083 } 084 } 085 return (T) trashService; 086 } 087 088 /** 089 * Callback class to notify of migration status changes. 090 * 091 * @since 10.2 092 */ 093 public static class TrashServiceStatusChangeNotifier implements StatusChangeNotifier { 094 095 @Override 096 public void notifyStatusChange() { 097 TrashServiceImpl trashService = (TrashServiceImpl) Framework.getRuntime() 098 .getComponent(TrashServiceImpl.NAME); 099 trashService.invalidateTrashServiceImplementation(); 100 } 101 } 102 103 /** 104 * Called when the migration status changes, to recompute the new service. 105 * 106 * @since 10.2 107 */ 108 public void invalidateTrashServiceImplementation() { 109 synchronized (this) { 110 trashService = recomputeTrashService(); 111 } 112 } 113 114}