001/*
002 * (C) Copyright 2006-2008 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.webapp.seam;
020
021import java.util.Set;
022
023import org.nuxeo.ecm.platform.ui.web.restAPI.BaseStatelessNuxeoRestlet;
024import org.restlet.data.MediaType;
025import org.restlet.data.Request;
026import org.restlet.data.Response;
027
028/**
029 * Restlet to trigger the reloading. (can not be done directly from a Seam bean without messing up JSF scopes).
030 *
031 * @author tiry
032 */
033public class NuxeoSeamHotReloadRestTrigger extends BaseStatelessNuxeoRestlet {
034
035    @Override
036    protected void doHandleStatelessRequest(Request req, Response res) {
037
038        StringBuffer sb = new StringBuffer();
039
040        if (!SeamHotReloadHelper.isHotReloadEnabled()) {
041            sb.append("This operation is not permitted");
042        } else {
043
044            long t0 = System.currentTimeMillis();
045            Set<String> reloadedComponents = SeamHotReloadHelper.reloadSeamComponents(getHttpRequest(req));
046            long t1 = System.currentTimeMillis();
047
048            if (reloadedComponents != null) {
049                sb.append("Reloaded ");
050                sb.append(reloadedComponents.size());
051                sb.append(" Seam components in ");
052                sb.append(t1 - t0);
053                sb.append("ms");
054                sb.append("\n");
055
056                for (String cn : reloadedComponents) {
057                    sb.append("  ");
058                    sb.append(cn);
059                    sb.append("\n");
060                }
061            } else {
062                Set<String> reloadableComponents = SeamHotReloadHelper.getHotDeployableComponents(getHttpRequest(req));
063                if (reloadableComponents == null || reloadableComponents.size() == 0) {
064                    sb.append("Nothing to reload");
065                } else {
066                    sb.append(reloadableComponents.size());
067                    sb.append(" reloadable Seam Components\n");
068                    sb.append("But nothing to reload (classes are up to date)");
069                }
070            }
071        }
072        res.setEntity(sb.toString(), MediaType.TEXT_PLAIN);
073    }
074
075}