001/*
002 * (C) Copyright 2010 Nuxeo SA (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 *     mcedica
018 */
019package org.nuxeo.ecm.webengine.management.statuses;
020
021import java.util.Collection;
022import java.util.Iterator;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.PUT;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029
030import org.nuxeo.ecm.core.management.api.ProbeInfo;
031import org.nuxeo.ecm.core.management.api.ProbeManager;
032import org.nuxeo.ecm.webengine.management.ManagementObject;
033import org.nuxeo.ecm.webengine.model.WebObject;
034import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
035import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * List and run probes
040 *
041 * @author matic
042 */
043@WebObject(type = "Probes")
044@Produces("text/html; charset=UTF-8")
045public class ProbesObject extends ManagementObject {
046
047    protected ProbeManager probeMgr;
048
049    protected Collection<ProbeInfo> infos;
050
051    public static ProbesObject newProbes(DefaultObject parent) {
052        return (ProbesObject) parent.newObject("Probes");
053    }
054
055    @Override
056    protected void initialize(Object... args) {
057        assert args != null && args.length == 2;
058        probeMgr = Framework.getLocalService(ProbeManager.class);
059        infos = probeMgr.getAllProbeInfos();
060    }
061
062    @GET
063    public Object doGet() {
064        return getView("index");
065    }
066
067    public Collection<ProbeInfo> getInfos() {
068        return infos;
069    }
070
071    @GET
072    @Path("availability")
073    @Produces("text/plain")
074    public Object doGetAvailability() {
075        probeMgr.runAllProbes();
076        return getView("availability").arg("isAvailable", probeMgr.getProbesInError().isEmpty());
077    }
078
079    @Path("{probe}")
080    public ProbeObject doGetProbe(@PathParam("probe") String name) {
081        Iterator<ProbeInfo> it = infos.iterator();
082        while (it.hasNext()) {
083            ProbeInfo info = it.next();
084            if (info.getShortcutName().equals(name)) {
085                return ProbeObject.newProbe(this, info);
086            }
087        }
088        throw new WebResourceNotFoundException("No such probe " + name);
089    }
090
091    @PUT
092    public Object doPut() {
093        return doRun();
094    }
095
096    @GET
097    @Path("/@run")
098    public Object doRun() {
099        probeMgr.runAllProbes();
100        return redirect(getPath());
101    }
102}