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.geronimo.console.jmsmanager.server;
018    
019    import java.io.IOException;
020    import java.util.Arrays;
021    import java.util.List;
022    import java.util.ArrayList;
023    import java.net.URI;
024    
025    import javax.portlet.PortletRequestDispatcher;
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletException;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    import javax.portlet.WindowState;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletContext;
034    import org.apache.geronimo.console.util.PortletManager;
035    import org.apache.geronimo.kernel.proxy.GeronimoManagedBean;
036    import org.apache.geronimo.management.geronimo.JMSConnector;
037    import org.apache.geronimo.management.geronimo.JMSManager;
038    import org.apache.geronimo.management.geronimo.JMSBroker;
039    import org.apache.geronimo.gbean.AbstractName;
040    import org.apache.commons.logging.Log;
041    import org.apache.commons.logging.LogFactory;
042    
043    /**
044     * List, edit, add, remove JMS network connectors
045     *
046     * @version $Rev: 560996 $ $Date: 2007-07-30 10:27:07 -0400 (Mon, 30 Jul 2007) $
047     */
048    public class JMSConnectorPortlet extends BaseJMSPortlet {
049        private final static Log log = LogFactory.getLog(JMSConnectorPortlet.class);
050    
051        private PortletRequestDispatcher normalView;
052    
053        private PortletRequestDispatcher maximizedView;
054    
055        private PortletRequestDispatcher helpView;
056    
057        protected PortletRequestDispatcher editView;
058    
059        public void processAction(ActionRequest actionRequest,
060                                  ActionResponse actionResponse) throws PortletException, IOException {
061            try {
062                String mode = actionRequest.getParameter("mode");
063                String connectorURI = actionRequest.getParameter("connectorURI");
064                String brokerURI = actionRequest.getParameter("brokerURI");
065                JMSManager manager = PortletManager.getCurrentServer(actionRequest).getJMSManagers()[0];  //todo: handle multiple
066                if(mode.equals("new")) {
067                    // User selected to add a new connector, need to show criteria portlet
068                    actionResponse.setRenderParameter("mode", "new");
069                    String protocol = actionRequest.getParameter("protocol");
070                    actionResponse.setRenderParameter("protocol", protocol);
071                    actionResponse.setRenderParameter("brokerURI", brokerURI);
072                } else if(mode.equals("add")) { // User just submitted the form to add a new connector
073                    // Get submitted values
074                    //todo: lots of validation
075                    String protocol = actionRequest.getParameter("protocol");
076                    String host = actionRequest.getParameter("host");
077                    int port = Integer.parseInt(actionRequest.getParameter("port"));
078                    String name = actionRequest.getParameter("name");
079                    AbstractName brokerAbstractName = new AbstractName(URI.create(brokerURI));
080                    // Create and configure the connector
081                    JMSConnector connector = PortletManager.createJMSConnector(actionRequest, manager, brokerAbstractName, name, protocol, host, port);
082                    // Start the connector
083                    try {
084                        ((GeronimoManagedBean)connector).startRecursive();
085                    } catch (Exception e) {
086                        log.error("Unable to start connector", e); //todo: get into rendered page somehow?
087                    }
088                    actionResponse.setRenderParameter("mode", "list");
089                } else if(mode.equals("save")) { // User just submitted the form to update a connector
090                    // Get submitted values
091                    //todo: lots of validation
092                    String host = actionRequest.getParameter("host");
093                    int port = Integer.parseInt(actionRequest.getParameter("port"));
094                    // Identify and update the connector
095                    AbstractName connectorAbstractName = new AbstractName(URI.create(connectorURI));
096                    JMSConnector connector = (JMSConnector)PortletManager.getManagedBean(actionRequest, connectorAbstractName);
097                    if(connector != null) {
098                        connector.setHost(host);
099                        connector.setPort(port);
100                    }
101                    actionResponse.setRenderParameter("mode", "list");
102                } else if(mode.equals("start")) {
103                    AbstractName connectorAbstractName = new AbstractName(URI.create(connectorURI));
104                    try {
105                        PortletManager.getManagedBean(actionRequest, connectorAbstractName).startRecursive();
106                    } catch (Exception e) {
107                        throw new PortletException(e);
108                    }
109                    actionResponse.setRenderParameter("mode", "list");
110                } else if(mode.equals("stop")) {
111                    AbstractName connectorAbstractName = new AbstractName(URI.create(connectorURI));
112                    try {
113                        PortletManager.getManagedBean(actionRequest, connectorAbstractName).stop();
114                    } catch (Exception e) {
115                        throw new PortletException(e);
116                    }
117                    actionResponse.setRenderParameter("mode", "list");
118                } else if(mode.equals("edit")) {
119                    actionResponse.setRenderParameter("connectorURI", connectorURI);
120                    actionResponse.setRenderParameter("brokerURI", brokerURI);
121                    actionResponse.setRenderParameter("mode", "edit");
122                } else if(mode.equals("delete")) {
123                    AbstractName connectorAbstractName = new AbstractName(URI.create(connectorURI));
124                    manager.removeConnector(connectorAbstractName);
125                    actionResponse.setRenderParameter("mode", "list");
126                }
127            } catch (Throwable e) {
128                log.error("Unable to process portlet action", e);
129                if(e instanceof PortletException) {
130                    throw (PortletException)e;
131                }
132            }
133        }
134    
135        protected void doView(RenderRequest renderRequest,
136                              RenderResponse renderResponse) throws IOException, PortletException {
137            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
138                return;
139            }
140    
141            try {
142                String mode = renderRequest.getParameter("mode");
143                if(mode == null || mode.equals("")) {
144                    mode = "list";
145                }
146                JMSManager manager = PortletManager.getCurrentServer(renderRequest).getJMSManagers()[0];  //todo: handle multiple
147    
148                if(mode.equals("new")) {
149                    String brokerURI = renderRequest.getParameter("brokerURI");
150                    String protocol = renderRequest.getParameter("protocol");
151                    renderRequest.setAttribute("protocol", protocol);
152                    renderRequest.setAttribute("brokerURI", brokerURI);
153                    renderRequest.setAttribute("brokerName", new AbstractName(URI.create(brokerURI)).getName().get("name").toString());
154                    renderRequest.setAttribute("mode", "add");
155                    editView.include(renderRequest, renderResponse);
156                } else if(mode.equals("edit")) {
157                    String brokerURI = renderRequest.getParameter("brokerURI");
158                    String connectorURI = renderRequest.getParameter("connectorURI");
159                    JMSConnector connector = (JMSConnector)PortletManager.getManagedBean(renderRequest, new AbstractName(URI.create(connectorURI)));
160                    if(connector == null) {
161                        doList(renderRequest, manager, renderResponse);
162                    } else {
163                        renderRequest.setAttribute("connectorURI", connectorURI);
164                        renderRequest.setAttribute("brokerName", new AbstractName(URI.create(brokerURI)).getName().get("name").toString());
165                        renderRequest.setAttribute("connectorName", new AbstractName(URI.create(connectorURI)).getName().get("name").toString());
166                        renderRequest.setAttribute("protocol", connector.getProtocol());
167                        renderRequest.setAttribute("port", new Integer(connector.getPort()));
168                        renderRequest.setAttribute("host", connector.getHost());
169                        renderRequest.setAttribute("mode", "save");
170                        editView.include(renderRequest, renderResponse);
171                    }
172                } else if(mode.equals("list")) {
173                    doList(renderRequest, manager, renderResponse);
174                }
175            } catch (Throwable e) {
176                log.error("Unable to render portlet", e);
177            }
178        }
179    
180        private void doList(RenderRequest renderRequest, JMSManager manager, RenderResponse renderResponse) throws PortletException, IOException {
181            List beans = new ArrayList();
182            JMSBroker[] brokers = (JMSBroker[]) manager.getContainers();
183            for (int i = 0; i < brokers.length; i++) {
184                JMSBroker broker = brokers[i];
185                AbstractName brokerAbstractName = PortletManager.getNameFor(renderRequest, broker);
186                JMSConnector[] connectors = (JMSConnector[]) manager.getConnectorsForContainer(broker);
187                for (int j = 0; j < connectors.length; j++) {
188                    JMSConnector connector = connectors[j];
189                    AbstractName connectorAbstractName = PortletManager.getNameFor(renderRequest,connector);
190                    String brokerName = brokerAbstractName.getName().get("name").toString();
191                    String connectorName = connectorAbstractName.getName().get("name").toString();
192                    ConnectorWrapper info = new ConnectorWrapper(brokerName, brokerAbstractName.toString(),
193                                                                         connectorName, connectorAbstractName.toString(),
194                                                                         connector);
195                    beans.add(info);
196                }
197            }
198            renderRequest.setAttribute("brokers", getBrokerList(renderRequest, manager));
199            renderRequest.setAttribute("connectors", beans);
200            ArrayList protocols = new ArrayList(Arrays.asList(manager.getSupportedProtocols()));
201            protocols.remove("peer"); // add operation not supported for peer protocol
202            protocols.remove("failover"); // add operation not supported for failover protocol
203            renderRequest.setAttribute("protocols", protocols);
204    
205            if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
206                normalView.include(renderRequest, renderResponse);
207            } else {
208                maximizedView.include(renderRequest, renderResponse);
209            }
210        }
211    
212        protected void doHelp(RenderRequest renderRequest,
213                              RenderResponse renderResponse) throws PortletException, IOException {
214            helpView.include(renderRequest, renderResponse);
215        }
216    
217        public void init(PortletConfig portletConfig) throws PortletException {
218            super.init(portletConfig);
219            PortletContext pc = portletConfig.getPortletContext();
220            normalView = pc.getRequestDispatcher("/WEB-INF/view/jmsmanager/server/connector/normal.jsp");
221            maximizedView = pc.getRequestDispatcher("/WEB-INF/view/jmsmanager/server/connector/maximized.jsp");
222            helpView = pc.getRequestDispatcher("/WEB-INF/view/jmsmanager/server/connector/help.jsp");
223            editView = pc.getRequestDispatcher("/WEB-INF/view/jmsmanager/server/connector/editGeneric.jsp");
224        }
225    
226        public void destroy() {
227            helpView = null;
228            editView = null;
229            normalView = null;
230            maximizedView = null;
231            super.destroy();
232        }
233    
234        public static boolean isValid(String s) {
235            return s != null && !s.equals("");
236        }
237    
238        public static class ConnectorWrapper {
239            private String brokerName;
240            private String brokerURI;
241            private String connectorName;
242            private String connectorURI;
243            private JMSConnector connector;
244    
245            public ConnectorWrapper(String brokerName, String brokerURI, String connectorName, String connectorURI, JMSConnector connector) {
246                this.brokerName = brokerName;
247                this.brokerURI = brokerURI;
248                this.connectorName = connectorName;
249                this.connectorURI = connectorURI;
250                this.connector = connector;
251            }
252    
253            public String getBrokerName() {
254                return brokerName;
255            }
256    
257            public String getConnectorName() {
258                return connectorName;
259            }
260    
261            public JMSConnector getConnector() {
262                return connector;
263            }
264    
265            public String getBrokerURI() {
266                return brokerURI;
267            }
268            public String getConnectorURI() {
269                return connectorURI;
270            }
271        }
272    }