View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  import org.apache.struts.action.ActionMessages;
24  import org.kuali.rice.core.api.config.CoreConfigHelper;
25  import org.kuali.rice.core.api.config.property.ConfigContext;
26  import org.kuali.rice.core.api.util.RiceUtilities;
27  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
28  import org.kuali.rice.ksb.api.bus.Endpoint;
29  import org.kuali.rice.ksb.api.bus.ServiceBus;
30  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
31  import org.kuali.rice.ksb.api.registry.ServiceInfo;
32  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import javax.xml.namespace.QName;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Map;
42  
43  
44  /**
45   * Struts action for interacting with the queue of messages.
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class ServiceRegistryAction extends KSBAction {
50  
51  	private static final String REMOVED_APPLICATION_ID_PARAM = "removedApplicationId";
52      private static final Logger LOG = Logger.getLogger(ServiceRegistryAction.class);
53  
54      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
55  	    HttpServletResponse response) throws IOException, ServletException {
56  	return mapping.findForward("basic");
57      }
58  
59      public ActionForward refreshServiceRegistry(ActionMapping mapping, ActionForm form, HttpServletRequest request,
60      	HttpServletResponse response) throws IOException, ServletException {
61      	// TODO is this what really constitutes a "refresh" of the service registry?
62      	KsbApiServiceLocator.getServiceBus().synchronize();
63      	return mapping.findForward("basic");
64      }
65      
66  	/**
67       * Enable deletion of localhost service registry entries.
68       */
69      public ActionForward deleteLocalhostEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
70          	HttpServletResponse response) throws IOException, ServletException {
71      	ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
72      	List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
73      	List<String> serviceEndpointsToDelete = new ArrayList<String>();
74      	for (ServiceInfo serviceInfo : serviceInfos) {
75      		if (serviceInfo.getServerIpAddress().equals("localhost") ||
76                  serviceInfo.getEndpointUrl().contains("localhost")) {
77      			serviceEndpointsToDelete.add(serviceInfo.getServiceId());
78      		}
79      	}
80      	serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
81      	KsbApiServiceLocator.getServiceBus().synchronize();
82  		return mapping.findForward("basic");
83      }
84  
85      public ActionForward deleteApplicationIdEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
86              HttpServletResponse response) throws IOException, ServletException {
87          
88          final String applicationId = request.getParameter(REMOVED_APPLICATION_ID_PARAM);
89          if(StringUtils.isNotBlank(applicationId)) {
90              ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
91              List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
92              List<String> serviceEndpointsToDelete = new ArrayList<String>();
93              for (ServiceInfo serviceInfo : serviceInfos) {
94                  if (serviceInfo.getApplicationId().equals(applicationId)) {
95                      serviceEndpointsToDelete.add(serviceInfo.getServiceId());
96                  }
97              }
98              serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
99              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 }