View Javadoc

1   /**
2    * Copyright 2005-2012 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      			serviceEndpointsToDelete.add(serviceInfo.getServiceId());
77      		}
78      	}
79      	serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
80      	KsbApiServiceLocator.getServiceBus().synchronize();
81  		return mapping.findForward("basic");
82      }
83  
84      public ActionForward deleteApplicationIdEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
85              HttpServletResponse response) throws IOException, ServletException {
86          
87          final String applicationId = request.getParameter(REMOVED_APPLICATION_ID_PARAM);
88          if(StringUtils.isNotBlank(applicationId)) {
89              ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
90              List<ServiceInfo> serviceInfos = serviceRegistry.getAllOnlineServices();
91              List<String> serviceEndpointsToDelete = new ArrayList<String>();
92              for (ServiceInfo serviceInfo : serviceInfos) {
93                  if (serviceInfo.getApplicationId().equals(applicationId)) {
94                      serviceEndpointsToDelete.add(serviceInfo.getServiceId());
95                  }
96              }
97              serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
98              KsbApiServiceLocator.getServiceBus().synchronize();
99          } else {
100             LOG.info("No rows were deleted from the KRSB_SVC_DEF_T table because the application ID was null or blank.");
101         }
102 
103         return mapping.findForward("basic");
104     }
105 
106     public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm actionForm) throws Exception {
107 	ServiceRegistryForm form = (ServiceRegistryForm)actionForm;
108 	form.setMyIpAddress(RiceUtilities.getIpNumber());
109 	form.setMyApplicationId(CoreConfigHelper.getApplicationId());
110 	form.setDevMode(ConfigContext.getCurrentContextConfig().getDevMode());
111 	ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
112 	form.setMyInstanceId(serviceBus.getInstanceId());
113 	form.setPublishedServices(getPublishedServices(serviceBus));
114 	ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
115 	form.setGlobalRegistryServices(getGlobalRegistryServices(serviceRegistry));
116 
117 	return null;
118     }
119 
120     private List<ServiceConfiguration> getPublishedServices(ServiceBus serviceBus) {
121     	Map<QName, Endpoint> localEndpoints = serviceBus.getLocalEndpoints();
122     	List<ServiceConfiguration> publishedServices = new ArrayList<ServiceConfiguration>();
123     	for (Endpoint endpoint : localEndpoints.values()) {
124     		publishedServices.add(endpoint.getServiceConfiguration());
125     	}
126     	return publishedServices;
127     }
128 
129     private List<ServiceInfo> getGlobalRegistryServices(ServiceRegistry serviceRegistry) {
130     	return serviceRegistry.getAllServices();
131     }
132 
133 }