001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.management.web.action;
022
023 import java.io.IOException;
024 import java.util.ArrayList;
025 import java.util.LinkedList;
026 import java.util.List;
027
028 import javax.servlet.RequestDispatcher;
029 import javax.servlet.ServletException;
030 import javax.servlet.http.HttpServlet;
031 import javax.servlet.http.HttpServletRequest;
032 import javax.servlet.http.HttpServletResponse;
033
034 import org.apache.qpid.management.Names;
035 import org.apache.qpid.management.configuration.BrokerAlreadyConnectedException;
036 import org.apache.qpid.management.configuration.BrokerConnectionData;
037 import org.apache.qpid.management.configuration.BrokerConnectionException;
038 import org.apache.qpid.management.domain.services.ManagementClient;
039 import org.apache.qpid.management.domain.services.QMan;
040
041 /**
042 * This controller is responsible to :
043 *
044 * <ul>
045 * <li> prepare data for the page that is showing all connected brokers.</li>.
046 * </li> connect QMan with a broker on demand.
047 * </ul>
048 * @author Andrea Gazzarini
049 */
050 public class BrokersManagementAction extends HttpServlet
051 {
052 private static final long serialVersionUID = -2411413147821629363L;
053
054 /**
055 * Retrieves all connected brokers (their connection data) and prepare the model that
056 * is then forwarded to the appropriate view page.
057 */
058 @SuppressWarnings("unchecked")
059 @Override
060 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
061 {
062 try
063 {
064 QMan qman = (QMan)getServletContext().getAttribute(Names.APPLICATION_NAME);
065 List<ManagementClient> managementClients = qman.getManagementClients();
066
067 List<BrokerConnectionData> brokers = new ArrayList<BrokerConnectionData>(managementClients.size());
068
069 if (!managementClients.isEmpty())
070 {
071 for (ManagementClient managementClient : managementClients)
072 {
073 brokers.add(managementClient.getBrokerConnectionData());
074 }
075 request.setAttribute("model", brokers);
076 }
077
078 RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
079 dispatcher.forward(request,response);
080 } catch(Exception exception)
081 {
082 request.setAttribute("errorMessage","Unable to detect the exact cause Please look at the reported stack trace below.");
083 request.setAttribute("exception",exception);
084 RequestDispatcher dispatcher = request.getRequestDispatcher("/error_page.jsp");
085 dispatcher.forward(request,response);
086 }
087 }
088
089 /**
090 * Connects QMan with a new broker.
091 */
092 @SuppressWarnings("unchecked")
093 @Override
094 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
095 {
096 try
097 {
098 QMan qman = (QMan)getServletContext().getAttribute(Names.APPLICATION_NAME);
099
100 String host = request.getParameter("host");
101 String portString = request.getParameter("port");
102 String virtualHost = request.getParameter("virtualHost");
103 String username = request.getParameter("username");
104 String password = request.getParameter("password");
105
106 String initialCapacityString = request.getParameter("initialCapacity");
107 String maxCapacityString = request.getParameter("maxCapacity");
108 String maxWaitTimeoutString = request.getParameter("maxWaitTimeout");
109
110 List<String> errors = new LinkedList<String>();
111 int port = 0;
112 int initialPoolCapacity = 0;
113 int maxPoolCapacity = 0;
114 long maxWaitTimeout = 0;
115
116 if(host== null || host.trim().length()==0)
117 {
118 errors.add("Invalid value for \"host\" attribute. Must be not null.");
119 }
120
121 if(virtualHost == null || virtualHost.trim().length()==0)
122 {
123 errors.add("Invalid value for \"virtualHost\" attribute. Must be not null.");
124 }
125
126 try{
127 port = Integer.parseInt(portString);
128 } catch(Exception exception)
129 {
130 errors.add("Invalid value for \"port\" attribute. Must be not null and must be a number.");
131 }
132
133 try{
134 initialPoolCapacity = Integer.parseInt(initialCapacityString);
135 } catch(Exception exception)
136 {
137 errors.add("Invalid value for \"Initial Pool Capacity\" attribute. Must be not null and must be a number.");
138 }
139
140 try{
141 maxPoolCapacity = Integer.parseInt(maxCapacityString);
142 } catch(Exception exception)
143 {
144 errors.add("Invalid value for \"Max Pool Capacity\" attribute. Must be not null and must be a number.");
145 }
146
147 try{
148 maxWaitTimeout = Long.parseLong(maxWaitTimeoutString);
149 } catch(Exception exception)
150 {
151 errors.add("Invalid value for \"Max Wait Timeout\" attribute. Must be not null and must be a number.");
152 }
153
154 request.setAttribute("errors", errors);
155
156 if (errors.isEmpty())
157 {
158 qman.addBroker(
159 host,
160 port,
161 username,
162 password,
163 virtualHost,
164 initialPoolCapacity,
165 maxPoolCapacity,
166 maxWaitTimeout);
167 }
168 doGet(request, response);
169 }catch(BrokerAlreadyConnectedException exception)
170 {
171 request.setAttribute("errorMessage","Supplied data refers to an already connected broker...");
172 RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
173 dispatcher.forward(request,response);
174 }
175 catch(BrokerConnectionException exception)
176 {
177 request.setAttribute("errorMessage","Unable to connect with the requested broker...");
178 RequestDispatcher dispatcher = request.getRequestDispatcher("/brokers_management.jsp");
179 dispatcher.forward(request,response);
180 } catch(Exception exception)
181 {
182 request.setAttribute("errorMessage","Unable to detect the exact cause Please look at the reported stack trace below.");
183 request.setAttribute("exception",exception);
184 RequestDispatcher dispatcher = request.getRequestDispatcher("/error_page.jsp");
185 dispatcher.forward(request,response);
186 }
187 }
188 }
|