View Javadoc

1   /**
2    * Copyright 2005-2013 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 java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.xml.namespace.QName;
27  
28  import org.apache.log4j.Logger;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.apache.struts.action.ActionMessages;
33  import org.kuali.rice.core.api.CoreConstants;
34  import org.kuali.rice.core.api.config.CoreConfigHelper;
35  import org.kuali.rice.core.api.config.property.ConfigContext;
36  import org.kuali.rice.core.api.util.RiceUtilities;
37  import org.kuali.rice.ksb.api.KsbApiConstants;
38  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
39  import org.kuali.rice.ksb.api.bus.Endpoint;
40  import org.kuali.rice.ksb.api.bus.ServiceBus;
41  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
42  import org.kuali.rice.ksb.api.bus.ServiceBusAdminService;
43  
44  
45  /**
46   * Struts action for interacting with the queue of messages.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class ServiceBusAction extends KSBAction {
51  
52      private static final QName SERVICE_BUS_ADMIN_SERVICE_QUEUE = new QName(
53              KsbApiConstants.Namespaces.KSB_NAMESPACE_2_0, "serviceBusAdminService");
54      private static final Logger LOG = Logger.getLogger(ServiceBusAction.class);
55  
56      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request,
57  			HttpServletResponse response) throws IOException, ServletException {
58  		return mapping.findForward("basic");
59  	}
60  
61  	public ActionForward refreshServiceBus(ActionMapping mapping, ActionForm form, HttpServletRequest request,
62  			HttpServletResponse response) throws IOException, ServletException {
63  
64          String applicationId = ConfigContext.getCurrentContextConfig().getProperty(
65                  CoreConstants.Config.APPLICATION_ID);
66  
67          List<Endpoint> endpoints = KsbApiServiceLocator.getServiceBus().getEndpoints(SERVICE_BUS_ADMIN_SERVICE_QUEUE, applicationId);
68          if (endpoints.isEmpty()) {
69              KsbApiServiceLocator.getServiceBus().synchronize();
70          } else {
71              for (Endpoint endpoint : endpoints) {
72                  ServiceBusAdminService serviceBusAdminService = (ServiceBusAdminService) endpoint.getService();
73                  LOG.info("Calling " + endpoint.getServiceConfiguration().getEndpointUrl() + " on " +
74                      endpoint.getServiceConfiguration().getInstanceId());
75  
76                  serviceBusAdminService.clearServiceBusCache();
77              }
78          }
79  		return mapping.findForward("basic");
80  	}
81  
82  	public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm actionForm) throws Exception {
83  		ServiceBusForm form = (ServiceBusForm)actionForm;
84  		form.setMyIpAddress(RiceUtilities.getIpNumber());
85  		form.setMyApplicationId(CoreConfigHelper.getApplicationId());
86  		form.setDevMode(ConfigContext.getCurrentContextConfig().getDevMode());
87  		ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
88  		form.setMyInstanceId(serviceBus.getInstanceId());
89  		form.setPublishedServices(getPublishedServices(serviceBus));
90  		form.setGlobalServices(getGlobalServices(serviceBus));
91  
92  		return null;
93  	}
94  
95  	private List<ServiceConfiguration> getPublishedServices(ServiceBus serviceBus) {
96  		Map<QName, Endpoint> localEndpoints = serviceBus.getLocalEndpoints();
97  		List<ServiceConfiguration> publishedServices = new ArrayList<ServiceConfiguration>();
98  		for (Endpoint endpoint : localEndpoints.values()) {
99  			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 }