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     *
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            if (destination != null) {
092                destination = destination.trim();
093            }
094            this.jmsDestination = destination;
095        }
096    
097        public String getJMSDestinationType() {
098            return jmsDestinationType;
099        }
100    
101        public void setJMSDestinationType(String type) {
102            this.jmsDestinationType = type;
103        }
104    
105        protected ActiveMQDestination createDestination() {
106            byte destinationType = isQueue() ? ActiveMQDestination.QUEUE_TYPE : ActiveMQDestination.TOPIC_TYPE;
107            return ActiveMQDestination.createDestination(getValidDestination(), destinationType);
108        }
109    
110        protected String getValidDestination() {
111            if (jmsDestination == null || jmsDestination.isEmpty()) {
112                throw new IllegalArgumentException("No JMSDestination parameter specified");
113            }
114            return jmsDestination;
115        }
116    
117        protected QueueViewMBean getQueueView() throws Exception {
118            String name = getPhysicalDestinationName();
119            return getBrokerFacade().getQueue(name);
120        }
121    
122        protected ModelAndView redirectToRequest(HttpServletRequest request) {
123            String view = "redirect:" + request.getRequestURI();
124            return new ModelAndView(view);
125        }
126    
127        protected ModelAndView redirectToBrowseView() {
128            return new ModelAndView("redirect:" + (isQueue() ? "queues.jsp" : "topics.jsp"));
129        }
130    
131        protected String getPhysicalDestinationName() {
132            return createDestination().getPhysicalName();
133        }
134    
135        public String[] getSupportedHttpMethods() {
136            return new String[]{"GET", "POST"};
137        }
138    }