001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.web;
018    
019    import javax.servlet.http.HttpServletRequest;
020    
021    import org.apache.activemq.broker.jmx.BrokerViewMBean;
022    import org.apache.activemq.broker.jmx.QueueViewMBean;
023    import org.apache.activemq.command.ActiveMQDestination;
024    import org.springframework.web.servlet.ModelAndView;
025    
026    /**
027     * @version $Revision: 915387 $
028     */
029    public class DestinationFacade {
030    
031        private String jmsDestination;
032        private String jmsDestinationType;
033        private BrokerFacade brokerFacade;
034    
035        public DestinationFacade(BrokerFacade brokerFacade) {
036            this.brokerFacade = brokerFacade;
037        }
038    
039        public String toString() {
040            return super.toString() + "[destination:" + jmsDestination + "; type=" + jmsDestinationType + "]";
041        }
042    
043        // Operations
044        // -------------------------------------------------------------------------
045        public void removeDestination() throws Exception {
046            getValidDestination();
047            if (isQueue()) {
048                getBrokerAdmin().removeQueue(getJMSDestination());
049            } else {
050                getBrokerAdmin().removeTopic(getJMSDestination());
051            }
052        }
053    
054        public void addDestination() throws Exception {
055            if (isQueue()) {
056                getBrokerAdmin().addQueue(getValidDestination());
057            } else {
058                getBrokerAdmin().addTopic(getValidDestination());
059            }
060        }
061    
062        // Properties
063        // -------------------------------------------------------------------------
064        public BrokerViewMBean getBrokerAdmin() throws Exception {
065            if (brokerFacade == null) {
066                throw new IllegalArgumentException("No brokerFacade injected!");
067            }
068            BrokerViewMBean answer = brokerFacade.getBrokerAdmin();
069            if (answer == null) {
070                throw new IllegalArgumentException("No brokerAdmin on the injected brokerFacade: " + brokerFacade);
071            }
072            return answer;
073        }
074    
075        public BrokerFacade getBrokerFacade() {
076            return brokerFacade;
077        }
078    
079        public boolean isQueue() {
080            if (jmsDestinationType != null && jmsDestinationType.equalsIgnoreCase("topic")) {
081                return false;
082            }
083            return true;
084        }
085    
086        public String getJMSDestination() {
087            return jmsDestination;
088        }
089    
090        public void setJMSDestination(String destination) {
091            this.jmsDestination = destination;
092        }
093    
094        public String getJMSDestinationType() {
095            return jmsDestinationType;
096        }
097    
098        public void setJMSDestinationType(String type) {
099            this.jmsDestinationType = type;
100        }
101    
102        protected ActiveMQDestination createDestination() {
103            byte destinationType = isQueue() ? ActiveMQDestination.QUEUE_TYPE : ActiveMQDestination.TOPIC_TYPE;
104            return ActiveMQDestination.createDestination(getValidDestination(), destinationType);
105        }
106    
107        protected String getValidDestination() {
108            if (jmsDestination == null) {
109                throw new IllegalArgumentException("No JMSDestination parameter specified");
110            }
111            return jmsDestination;
112        }
113        
114        protected QueueViewMBean getQueueView() throws Exception {
115            String name = getPhysicalDestinationName();
116            return getBrokerFacade().getQueue(name);
117        }    
118    
119        protected ModelAndView redirectToRequest(HttpServletRequest request) {
120            String view = "redirect:" + request.getRequestURI();
121            return new ModelAndView(view);
122        }
123    
124        protected ModelAndView redirectToBrowseView() {
125            return new ModelAndView("redirect:" + (isQueue() ? "queues.jsp" : "topics.jsp"));
126        }
127    
128        protected String getPhysicalDestinationName() {
129            return createDestination().getPhysicalName();
130        }
131        
132        public String[] getSupportedHttpMethods() {
133            return new String[]{"GET", "POST"};
134        }
135    }