Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.profiler.service.management; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.management.openmbean.CompositeData; import javax.management.openmbean.CompositeDataSupport; import javax.management.openmbean.CompositeType; import javax.management.openmbean.OpenDataException; import javax.management.openmbean.OpenType; import javax.management.openmbean.SimpleType; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.seajas.search.bridge.jms.model.test.TestElement; import com.seajas.search.bridge.profiler.model.feed.Feed; import com.seajas.search.bridge.profiler.model.modifier.Modifier; import com.seajas.search.profiler.model.archive.Archive; import com.seajas.search.profiler.service.cache.CacheService; import com.seajas.search.profiler.service.profiler.ProfilerService; import com.seajas.search.profiler.service.task.TaskService; import com.seajas.search.profiler.service.testing.TestingException; import com.seajas.search.profiler.service.testing.TestingService; import com.seajas.search.utilities.logging.SearchLogger; /** * A simple JMX Management Service bean. * * @author Jasper van Veghel <jasper@seajas.com> */ public class ManagementService implements IManagementService { /** * The logger. */ private static final Logger logger = (Logger) LoggerFactory.getLogger(ManagementService.class); /** * The application logger. */ @Autowired @Qualifier("logger") private SearchLogger applicationLogger; /** * The profiler service. */ @Autowired private ProfilerService profilerService; /** * The cache service. */ @Autowired private CacheService cacheService; /** * The task service. */ @Autowired private TaskService taskService; /** * The testing service. */ @Autowired private TestingService testingService; /** * The management state. */ @Autowired private ManagementState managementState; /** * {@inheritDoc} */ @Override public CompositeData getActiveFeedModifiers() throws OpenDataException { List<Modifier> modifiers = profilerService.getFeedModifiers(); Map<Integer, Map<String, Boolean>> results = new HashMap<Integer, Map<String, Boolean>>(); for (Modifier modifier : modifiers) if (modifier.getIsEnabled() && modifier.getTestFeed() != null && modifier.getTestFeed().getIsEnabled()) { TestElement testFeed = testingService.getTestFeedByModifierId(modifier.getId()); if (testFeed != null) try { results.put(modifier.getId(), testingService.testFeedModifier(testFeed)); } catch (TestingException e) { Map<String, Boolean> exceptionResult = new HashMap<String, Boolean>(); exceptionResult.put("exception", false); results.put(modifier.getId(), exceptionResult); logger.error("Testing exception occurred", e); } } Integer successful = 0, total = 0; for (Entry<Integer, Map<String, Boolean>> modifierResult : results.entrySet()) for (Entry<String, Boolean> result : modifierResult.getValue().entrySet()) { total++; if (result.getValue()) successful++; } // Return the compound status as the absolute difference - where 0 means success CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { Math.abs(successful - total), successful + "/" + total + " results were successful" }); } /** * {@inheritDoc} */ @Override public CompositeData getActiveResultModifiers() throws OpenDataException { List<Modifier> modifiers = profilerService.getResultModifiers(); Map<Integer, Map<String, Boolean>> results = new HashMap<Integer, Map<String, Boolean>>(); for (Modifier modifier : modifiers) if (modifier.getIsEnabled() && modifier.getTestFeed() != null && modifier.getTestFeed().getIsEnabled()) { TestElement testFeed = testingService.getTestFeedByModifierId(modifier.getId()); if (testFeed != null) try { results.put(modifier.getId(), testingService.testResultModifier(testFeed)); } catch (TestingException e) { Map<String, Boolean> exceptionResult = new HashMap<String, Boolean>(); exceptionResult.put("exception", false); results.put(modifier.getId(), exceptionResult); logger.error("Testing exception occurred", e); } } Integer successful = 0, total = 0; for (Entry<Integer, Map<String, Boolean>> modifierResult : results.entrySet()) for (Entry<String, Boolean> result : modifierResult.getValue().entrySet()) { total++; if (result.getValue()) successful++; } // Return the compound status as the absolute difference - where 0 means success CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { Math.abs(successful - total), successful + "/" + total + " results were successful" }); } /** * {@inheritDoc} */ @Override public CompositeData getActiveFeedConnections() throws OpenDataException { List<Feed> feeds = profilerService.getEnabledFeeds(); Map<Integer, Map<String, Boolean>> results = new HashMap<Integer, Map<String, Boolean>>(); for (Feed feed : feeds) try { results.put(feed.getId(), testingService.testFeedConnection(feed)); } catch (TestingException e) { logger.error("Could not test feed with ID " + feed.getId() + " as no testing response was received", e); } Integer successful = 0, total = 0; for (Entry<Integer, Map<String, Boolean>> modifierResult : results.entrySet()) for (Entry<String, Boolean> result : modifierResult.getValue().entrySet()) { total++; if (result.getValue()) successful++; } // Return the compound status as the absolute difference - where 0 means success CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { Math.abs(successful - total), successful + "/" + total + " feeds could be successfully retrieved" }); } /** * {@inheritDoc} */ @Override public CompositeData getActiveArchiveConnections() throws OpenDataException { List<Archive> archives = profilerService.getEnabledArchives(); Map<Integer, Map<String, Boolean>> results = new HashMap<Integer, Map<String, Boolean>>(); for (Archive archive : archives) try { results.put(archive.getId(), testingService.testArchiveConnection(archive)); } catch (TestingException e) { logger.error( "Could not test feed with ID " + archive.getId() + " as no testing response was received", e); } Integer successful = 0, total = 0; for (Entry<Integer, Map<String, Boolean>> modifierResult : results.entrySet()) for (Entry<String, Boolean> result : modifierResult.getValue().entrySet()) { total++; if (result.getValue()) successful++; } // Return the compound status as the absolute difference - where 0 means success CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { Math.abs(successful - total), successful + "/" + total + " archives could successfully connect" }); } /** * {@inheritDoc} */ @Override public CompositeData getLoggingCount() throws OpenDataException { Integer count = profilerService.getLoggingCount(); // Return the compound status as the number of messages CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { count, "The logging table contains " + count + " entries" }); } /** * {@inheritDoc} */ @Override public CompositeData getElementCacheCount() throws OpenDataException { Integer count = cacheService.getCacheCounts().get("elementCache"); // Return the compound status as the number of messages CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { count, "The queue cache contains " + count + " entries" }); } /** * {@inheritDoc} */ @Override public CompositeData getDeleteCacheCount() throws OpenDataException { Integer count = cacheService.getCacheCounts().get("deleteCache"); // Return the compound status as the number of messages CompositeType compositeType = new CompositeType("managementResult", "Management result", new String[] { "compound", "status" }, new String[] { "Compound value", "Textual representation" }, new OpenType[] { SimpleType.INTEGER, SimpleType.STRING }); return new CompositeDataSupport(compositeType, new String[] { "compound", "status" }, new Object[] { count, "The delete cache contains " + count + " entries" }); } /** * {@inheritDoc} */ @Override public String getRootLoggerLevel() { return ((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).getLevel().toString(); } /** * {@inheritDoc} */ @Override public void setRootLoggerLevel(final String level) { ((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.toLevel(level)); } /** * {@inheritDoc} */ @Override public String getApplicationLoggerLevel() { return applicationLogger.getLogLevel(); } /** * {@inheritDoc} */ @Override public void setApplicationLoggerLevel(final String level) { applicationLogger.setLogLevel(level); } /** * {@inheritDoc} */ @Override public void startSchedulerJob(final String jobName, final String triggerName) { taskService.startSchedulerJob(jobName, triggerName); } /** * {@inheritDoc} */ @Override public Integer[] getManagementStatePopulation() { return new Integer[] { managementState.getTotalFeedsRequesting(), managementState.getTotalFeedsFinished(), managementState.getTotalPopulated() }; } /** * {@inheritDoc} */ @Override public Integer getManagementStateFlushing() { return managementState.getTotalFlushed(); } /** * {@inheritDoc} */ @Override public String getManagementStateCurrent() { return managementState.getCurrentState().name(); } }