1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
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
62 KsbApiServiceLocator.getServiceBus().synchronize();
63 return mapping.findForward("basic");
64 }
65
66
67
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 serviceInfo.getServerIpAddress().equals("127.0.0.1") ||
78 serviceInfo.getEndpointUrl().contains("127.0.0.1")) {
79 serviceEndpointsToDelete.add(serviceInfo.getServiceId());
80 }
81 }
82 serviceRegistry.removeServiceEndpoints(serviceEndpointsToDelete);
83 KsbApiServiceLocator.getServiceBus().synchronize();
84 return mapping.findForward("basic");
85 }
86
87 public ActionForward deleteApplicationIdEntries(ActionMapping mapping, ActionForm form, HttpServletRequest request,
88 HttpServletResponse response) throws IOException, ServletException {
89
90 final String applicationId = request.getParameter(REMOVED_APPLICATION_ID_PARAM);
91 if(StringUtils.isNotBlank(applicationId)) {
92 ServiceRegistry serviceRegistry = KsbApiServiceLocator.getServiceRegistry();
93 List<ServiceInfo> serviceInfos = serviceRegistry.getAllServicesForApplication(applicationId);
94 List<String> serviceEndpointsToDelete = new ArrayList<String>();
95 for (ServiceInfo serviceInfo : serviceInfos) {
96 serviceEndpointsToDelete.add(serviceInfo.getServiceId());
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 }