001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.messaging.web;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026import javax.xml.namespace.QName;
027
028import org.apache.log4j.Logger;
029import org.apache.struts.action.ActionForm;
030import org.apache.struts.action.ActionForward;
031import org.apache.struts.action.ActionMapping;
032import org.apache.struts.action.ActionMessages;
033import org.kuali.rice.core.api.CoreConstants;
034import org.kuali.rice.core.api.config.CoreConfigHelper;
035import org.kuali.rice.core.api.config.property.ConfigContext;
036import org.kuali.rice.core.api.util.RiceUtilities;
037import org.kuali.rice.ksb.api.KsbApiConstants;
038import org.kuali.rice.ksb.api.KsbApiServiceLocator;
039import org.kuali.rice.ksb.api.bus.Endpoint;
040import org.kuali.rice.ksb.api.bus.ServiceBus;
041import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
042import org.kuali.rice.ksb.api.bus.ServiceBusAdminService;
043
044
045/**
046 * Struts action for interacting with the queue of messages.
047 *
048 * @author Kuali Rice Team (rice.collab@kuali.org)
049 */
050public class ServiceBusAction extends KSBAction {
051
052    private static final QName SERVICE_BUS_ADMIN_SERVICE_QUEUE = new QName(
053            KsbApiConstants.Namespaces.KSB_NAMESPACE_2_0, "serviceBusAdminService");
054    private static final Logger LOG = Logger.getLogger(ServiceBusAction.class);
055
056    public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
057                        HttpServletResponse response) throws IOException, ServletException {
058                return mapping.findForward("basic");
059        }
060
061        public ActionForward refreshServiceBus(ActionMapping mapping, ActionForm form, HttpServletRequest request,
062                        HttpServletResponse response) throws IOException, ServletException {
063
064        String applicationId = ConfigContext.getCurrentContextConfig().getProperty(
065                CoreConstants.Config.APPLICATION_ID);
066
067        List<Endpoint> endpoints = KsbApiServiceLocator.getServiceBus().getEndpoints(SERVICE_BUS_ADMIN_SERVICE_QUEUE, applicationId);
068        if (endpoints.isEmpty()) {
069            KsbApiServiceLocator.getServiceBus().synchronize();
070        } else {
071            for (Endpoint endpoint : endpoints) {
072                ServiceBusAdminService serviceBusAdminService = (ServiceBusAdminService) endpoint.getService();
073                LOG.info("Calling " + endpoint.getServiceConfiguration().getEndpointUrl() + " on " +
074                    endpoint.getServiceConfiguration().getInstanceId());
075
076                serviceBusAdminService.clearServiceBusCache();
077            }
078        }
079                return mapping.findForward("basic");
080        }
081
082        public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm actionForm) throws Exception {
083                ServiceBusForm form = (ServiceBusForm)actionForm;
084                form.setMyIpAddress(RiceUtilities.getIpNumber());
085                form.setMyApplicationId(CoreConfigHelper.getApplicationId());
086                form.setDevMode(ConfigContext.getCurrentContextConfig().getDevMode());
087                ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
088                form.setMyInstanceId(serviceBus.getInstanceId());
089                form.setPublishedServices(getPublishedServices(serviceBus));
090                form.setGlobalServices(getGlobalServices(serviceBus));
091
092                return null;
093        }
094
095        private List<ServiceConfiguration> getPublishedServices(ServiceBus serviceBus) {
096                Map<QName, Endpoint> localEndpoints = serviceBus.getLocalEndpoints();
097                List<ServiceConfiguration> publishedServices = new ArrayList<ServiceConfiguration>();
098                for (Endpoint endpoint : localEndpoints.values()) {
099                        publishedServices.add(endpoint.getServiceConfiguration());
100                }
101                return publishedServices;
102        }
103
104        private List<ServiceConfiguration> getGlobalServices(ServiceBus serviceBus) {
105                List<ServiceConfiguration> allServices = new ArrayList<ServiceConfiguration>();
106                for(Endpoint endpoint : serviceBus.getAllEndpoints()) {
107                        allServices.add(endpoint.getServiceConfiguration());
108                }
109                return allServices;
110        }
111
112}