001/* 002 * Copyright 2010 Anyware Services 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 */ 016package org.ametys.cms.content.consistency; 017 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.core.ui.StaticClientSideElement; 032 033/** 034 * Consistency client side element 035 */ 036public class ConsistencyClientSideElement extends StaticClientSideElement implements Contextualizable 037{ 038 /** The component configuration. */ 039 protected Configuration _configuration; 040 041 /** The Avalon context. */ 042 protected Context _context; 043 044 /** The service manager */ 045 protected ServiceManager _manager; 046 047 @Override 048 public void service(ServiceManager smanager) throws ServiceException 049 { 050 super.service(smanager); 051 _manager = smanager; 052 } 053 054 @Override 055 public void contextualize(Context context) throws ContextException 056 { 057 _context = context; 058 } 059 060 @Override 061 public void configure(Configuration configuration) throws ConfigurationException 062 { 063 _configuration = configuration; 064 super.configure(configuration); 065 } 066 067 /** 068 * Get the engine state 069 * @return The engine state 070 */ 071 @Callable 072 public Map<String, Object> getState() 073 { 074 Map<String, Object> params = new HashMap<>(); 075 076 params.put("engine-running", ContentConsistencyEngine.isRunning()); 077 078 return params; 079 } 080 081 /** 082 * This method gathers information in order to start the consistency report. 083 * @return results the server's response in JSON 084 */ 085 @Callable 086 public Map<String, String> startReport() 087 { 088 Map<String, String> results = new HashMap<> (); 089 if (ContentConsistencyEngine.isRunning()) 090 { 091 results = Collections.singletonMap("error", "already-running"); 092 } 093 else 094 { 095 ContentConsistencyEngine contentConsistencyEngine = new ContentConsistencyEngine(); 096 097 try 098 { 099 // Initialize and configure the engine. 100 contentConsistencyEngine.initialize(_manager, _context); 101 contentConsistencyEngine.configure(_configuration); 102 103 // Create the thread, mark it as daemon and start it. 104 Thread engineThread = new Thread(contentConsistencyEngine, "ContentConsistencyEngine"); 105 engineThread.setDaemon(true); 106 engineThread.start(); 107 108 return Collections.emptyMap(); 109 } 110 catch (Exception e) 111 { 112 throw new RuntimeException("Unable to initialize the content consistency engine", e); 113 } 114 } 115 116 return results; 117 } 118}