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     */
016    package org.kuali.rice.ksb.messaging.web;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.apache.struts.action.ActionForm;
021    import org.apache.struts.action.ActionForward;
022    import org.apache.struts.action.ActionMapping;
023    import org.apache.struts.action.ActionMessages;
024    import org.kuali.rice.core.api.config.CoreConfigHelper;
025    import org.kuali.rice.core.api.config.property.ConfigContext;
026    import org.kuali.rice.core.api.util.RiceUtilities;
027    import org.kuali.rice.ksb.api.KsbApiServiceLocator;
028    import org.kuali.rice.ksb.api.bus.Endpoint;
029    import org.kuali.rice.ksb.api.bus.ServiceBus;
030    import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
031    import org.kuali.rice.ksb.api.registry.ServiceInfo;
032    import org.kuali.rice.ksb.api.registry.ServiceRegistry;
033    
034    import javax.servlet.ServletException;
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    import javax.xml.namespace.QName;
038    import java.io.IOException;
039    import java.util.ArrayList;
040    import java.util.List;
041    import java.util.Map;
042    
043    
044    /**
045     * Struts action for interacting with the queue of messages.
046     *
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     */
049    public class ServiceRegistryAction extends KSBAction {
050    
051            private static final String REMOVED_APPLICATION_ID_PARAM = "removedApplicationId";
052        private static final Logger LOG = Logger.getLogger(ServiceRegistryAction.class);
053    
054        public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
055                HttpServletResponse response) throws IOException, ServletException {
056            return mapping.findForward("basic");
057        }
058    
059        public ActionForward refreshServiceRegistry(ActionMapping mapping, ActionForm form, HttpServletRequest request,
060            HttpServletResponse response) throws IOException, ServletException {
061            // TODO is this what really constitutes a "refresh" of the service registry?
062            KsbApiServiceLocator.getServiceBus().synchronize();
063            return mapping.findForward("basic");
064        }
065        
066            /**
067         * Enable deletion of localhost service registry entries.
068         */
069        public ActionForward deleteLocalhostEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
070                    HttpServletResponse response) throws IOException, ServletException {
071            ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
072            List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
073            List<String> serviceEndpointsToDelete = new ArrayList<String>();
074            for (ServiceInfo serviceInfo : serviceInfos) {
075                    if (serviceInfo.getServerIpAddress().equals("localhost") ||
076                    serviceInfo.getEndpointUrl().contains("localhost")) {
077                            serviceEndpointsToDelete.add(serviceInfo.getServiceId());
078                    }
079            }
080            serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
081            KsbApiServiceLocator.getServiceBus().synchronize();
082                    return mapping.findForward("basic");
083        }
084    
085        public ActionForward deleteApplicationIdEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
086                HttpServletResponse response) throws IOException, ServletException {
087            
088            final String applicationId = request.getParameter(REMOVED_APPLICATION_ID_PARAM);
089            if(StringUtils.isNotBlank(applicationId)) {
090                ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
091                List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
092                List<String> serviceEndpointsToDelete = new ArrayList<String>();
093                for (ServiceInfo serviceInfo : serviceInfos) {
094                    if (serviceInfo.getApplicationId().equals(applicationId)) {
095                        serviceEndpointsToDelete.add(serviceInfo.getServiceId());
096                    }
097                }
098                serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
099                KsbApiServiceLocator.getServiceBus().synchronize();
100            } else {
101                LOG.info("No rows were deleted from the KRSB_SVC_DEF_T table because the application ID was null or blank.");
102            }
103    
104            return mapping.findForward("basic");
105        }
106    
107        public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm actionForm) throws Exception {
108            ServiceRegistryForm form = (ServiceRegistryForm)actionForm;
109            form.setMyIpAddress(RiceUtilities.getIpNumber());
110            form.setMyApplicationId(CoreConfigHelper.getApplicationId());
111            form.setDevMode(ConfigContext.getCurrentContextConfig().getDevMode());
112            ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
113            form.setMyInstanceId(serviceBus.getInstanceId());
114            form.setPublishedServices(getPublishedServices(serviceBus));
115            ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
116            form.setGlobalRegistryServices(getGlobalRegistryServices(serviceRegistry));
117    
118            return null;
119        }
120    
121        private List<ServiceConfiguration> getPublishedServices(ServiceBus serviceBus) {
122            Map<QName, Endpoint> localEndpoints = serviceBus.getLocalEndpoints();
123            List<ServiceConfiguration> publishedServices = new ArrayList<ServiceConfiguration>();
124            for (Endpoint endpoint : localEndpoints.values()) {
125                    publishedServices.add(endpoint.getServiceConfiguration());
126            }
127            return publishedServices;
128        }
129    
130        private List<ServiceInfo> getGlobalRegistryServices(ServiceRegistry serviceRegistry) {
131            return serviceRegistry.getAllServices();
132        }
133    
134    }