001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     mcedica
016 */
017package org.nuxeo.ecm.webengine.management.statuses;
018
019import java.util.Collection;
020import java.util.Iterator;
021
022import javax.ws.rs.GET;
023import javax.ws.rs.PUT;
024import javax.ws.rs.Path;
025import javax.ws.rs.PathParam;
026import javax.ws.rs.Produces;
027
028import org.nuxeo.ecm.core.management.api.ProbeInfo;
029import org.nuxeo.ecm.core.management.api.ProbeManager;
030import org.nuxeo.ecm.webengine.management.ManagementObject;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * List and run probes
038 *
039 * @author matic
040 */
041@WebObject(type = "Probes")
042@Produces("text/html; charset=UTF-8")
043public class ProbesObject extends ManagementObject {
044
045    protected ProbeManager probeMgr;
046
047    protected Collection<ProbeInfo> infos;
048
049    public static ProbesObject newProbes(DefaultObject parent) {
050        return (ProbesObject) parent.newObject("Probes");
051    }
052
053    @Override
054    protected void initialize(Object... args) {
055        assert args != null && args.length == 2;
056        probeMgr = Framework.getLocalService(ProbeManager.class);
057        infos = probeMgr.getAllProbeInfos();
058    }
059
060    @GET
061    public Object doGet() {
062        return getView("index");
063    }
064
065    public Collection<ProbeInfo> getInfos() {
066        return infos;
067    }
068
069    @GET
070    @Path("availability")
071    @Produces("text/plain")
072    public Object doGetAvailability() {
073        probeMgr.runAllProbes();
074        return getView("availability").arg("isAvailable", probeMgr.getProbesInError().isEmpty());
075    }
076
077    @Path("{probe}")
078    public ProbeObject doGetProbe(@PathParam("probe") String name) {
079        Iterator<ProbeInfo> it = infos.iterator();
080        while (it.hasNext()) {
081            ProbeInfo info = it.next();
082            if (info.getShortcutName().equals(name)) {
083                return ProbeObject.newProbe(this, info);
084            }
085        }
086        throw new WebResourceNotFoundException("No such probe " + name);
087    }
088
089    @PUT
090    public Object doPut() {
091        return doRun();
092    }
093
094    @GET
095    @Path("/@run")
096    public Object doRun() {
097        probeMgr.runAllProbes();
098        return redirect(getPath());
099    }
100}