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: */package org.apache.geronimo.console.jmsmanager;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.jms.Connection;
029: import javax.jms.ConnectionFactory;
030: import javax.management.MalformedObjectNameException;
031: import javax.management.ObjectName;
032: import javax.portlet.ActionRequest;
033: import javax.portlet.ActionResponse;
034: import javax.portlet.PortletConfig;
035: import javax.portlet.PortletContext;
036: import javax.portlet.PortletException;
037: import javax.portlet.PortletRequestDispatcher;
038: import javax.portlet.RenderRequest;
039: import javax.portlet.RenderResponse;
040: import javax.portlet.WindowState;
041:
042: import org.apache.geronimo.console.BasePortlet;
043: import org.apache.geronimo.console.jmsmanager.DataSourceInfo;
044: import org.apache.geronimo.console.jmsmanager.activemqCF.ActiveMQConnectorHelper;
045: import org.apache.geronimo.gbean.GAttributeInfo;
046: import org.apache.geronimo.gbean.GBeanInfo;
047: import org.apache.geronimo.kernel.Kernel;
048: import org.apache.geronimo.kernel.KernelRegistry;
049: import org.apache.geronimo.kernel.ObjectNameUtil;
050:
051: public class JMSConnectionFactoryManagerPortlet extends BasePortlet {
052:
053: private final static ActiveMQConnectorHelper helper = new ActiveMQConnectorHelper();
054:
055: private final static String PARENT_ID = "geronimo/activemq-broker/"
056: + org.apache.geronimo.system.serverinfo.ServerConstants
057: .getVersion() + "/car";
058:
059: private final static String ADD_MODE = "addACF";
060:
061: private final static String SUBMIT_CREATE = "Create";
062:
063: private final ObjectName DATABASE_QUERY = ObjectNameUtil
064: .getObjectName("*:j2eeType=JCAManagedConnectionFactory,*");
065:
066: protected final String NORMAL_VIEW = "/WEB-INF/view/jmsmanager/activemq/normal.jsp";;
067:
068: protected final String DETAIL_VIEW = "/WEB-INF/view/jmsmanager/activemq/detail.jsp";
069:
070: protected final String HELP_VIEW = "/WEB-INF/view/jmsmanager/activemq/help.jsp";;
071:
072: protected final String ADD_VIEW = "/WEB-INF/view/jmsmanager/activemq/addACF.jsp";
073:
074: protected Kernel kernel;
075:
076: private PortletRequestDispatcher normalView;
077:
078: private PortletRequestDispatcher detailView;
079:
080: private PortletRequestDispatcher helpView;
081:
082: private PortletRequestDispatcher addView;
083:
084: private static final Set HIDDEN_ATTRIBUTES;
085:
086: static {
087: HIDDEN_ATTRIBUTES = new HashSet();
088: HIDDEN_ATTRIBUTES.add("kernel");
089: HIDDEN_ATTRIBUTES.add("connectionImplClass");
090: HIDDEN_ATTRIBUTES.add("connectionInterface");
091: HIDDEN_ATTRIBUTES.add("connectionFactoryInterface");
092: HIDDEN_ATTRIBUTES.add("connectionFactoryImplClass");
093: HIDDEN_ATTRIBUTES.add("implementedInterfaces");
094: HIDDEN_ATTRIBUTES.add("managedConnectionFactoryClass");
095: HIDDEN_ATTRIBUTES.add("recoveryXAResources");
096: }
097:
098: public void init(PortletConfig portletConfig)
099: throws PortletException {
100: super .init(portletConfig);
101: kernel = KernelRegistry.getSingleKernel();
102: PortletContext context = portletConfig.getPortletContext();
103: normalView = context.getRequestDispatcher(NORMAL_VIEW);
104: detailView = context.getRequestDispatcher(DETAIL_VIEW);
105: helpView = context.getRequestDispatcher(HELP_VIEW);
106: addView = context.getRequestDispatcher(ADD_VIEW);
107: }
108:
109: public void processAction(ActionRequest actionRequest,
110: ActionResponse actionResponse) throws PortletException,
111: IOException {
112: String mode = actionRequest.getParameter("mode");
113: String submit = actionRequest.getParameter("submit");
114:
115: if (mode == null) {
116: return;
117: } else if (ADD_MODE.equals(mode)) {
118: actionResponse.setRenderParameter("mode", mode);
119: if (SUBMIT_CREATE.equals(submit)) {
120: String acfName = actionRequest.getParameter("acfName");
121: String serverURL = actionRequest
122: .getParameter("serverURL");
123: String userName = actionRequest
124: .getParameter("userName");
125: String pword = actionRequest.getParameter("pword");
126: String poolMaxSize = actionRequest
127: .getParameter("poolMaxSize");
128: String blocking = actionRequest
129: .getParameter("blocking");
130:
131: String[] args = { trimStr(acfName), trimStr(PARENT_ID),
132: trimStr(acfName), trimStr(serverURL),
133: trimStr(userName), pword, trimStr(acfName),
134: trimStr(poolMaxSize), trimStr(blocking) };
135: helper.deployPlan(actionRequest, args);
136: // Set mode to list after creating the new ConnectionFactories
137: actionResponse.setRenderParameter("mode", "list");
138: }
139: return;
140: }
141:
142: String name = actionRequest.getParameter("name");
143: if (name != null) {
144: actionResponse.setRenderParameter("mode", "list");
145: return;
146: }
147: }
148:
149: protected void doView(RenderRequest renderRequest,
150: RenderResponse renderResponse) throws IOException,
151: PortletException {
152: if (WindowState.MINIMIZED
153: .equals(renderRequest.getWindowState())) {
154: return;
155: }
156:
157: List dependencies = helper.getDependencies(renderRequest);
158: // pass them to the render request
159: renderRequest.setAttribute("dependencies",
160: (String[]) dependencies.toArray(new String[dependencies
161: .size()]));
162:
163: String name = renderRequest.getParameter("name");
164: String mode = renderRequest.getParameter("mode");
165: String check = renderRequest.getParameter("check");
166:
167: if (ADD_MODE.equals(mode)) {
168: addView.include(renderRequest, renderResponse);
169: return;
170: }
171:
172: boolean test = false;
173: if (name == null || mode == null) {
174: mode = "list";
175: }
176: if ("true".equals(check)) {
177: test = true;
178: }
179: if ("detail".equals(mode) || "config".equals(mode)) {
180: renderDetail(renderRequest, renderResponse, name);
181: } else {
182: renderList(renderRequest, renderResponse, name, test);
183: }
184: }
185:
186: private void renderList(RenderRequest renderRequest,
187: RenderResponse renderResponse, String name, boolean check)
188: throws PortletException, IOException {
189:
190: Set gbeanNames = kernel.listGBeans(DATABASE_QUERY);
191: List connectionFactories = new ArrayList(gbeanNames.size());
192: for (Iterator i = gbeanNames.iterator(); i.hasNext();) {
193: ObjectName gbeanName = (ObjectName) i.next();
194:
195: // check that this connector is a DataSource
196: try {
197: Class cfInterface = Class.forName((String) kernel
198: .getAttribute(gbeanName,
199: "connectionFactoryInterface"));
200: if (!(ConnectionFactory.class)
201: .isAssignableFrom(cfInterface)) {
202: continue;
203: }
204: } catch (Exception e) {
205: throw new PortletException(e);
206: }
207:
208: DataSourceInfo info = new DataSourceInfo();
209: info.setObjectName(gbeanName);
210: info.setName(gbeanName.getKeyProperty("name"));
211: try {
212: info.setState(new Integer(kernel
213: .getGBeanState(gbeanName)));
214: //check if user asked this connection to be tested
215: if ((gbeanName.toString().equals(name)) && (check)) {
216: info.setWorking(true);
217: try {
218: Object cf = kernel.invoke(gbeanName,
219: "$getResource");
220: testConnection(cf);
221: info.setMessage("Connected");
222: } catch (Exception e) {
223: Throwable t = e;
224: String message = "Failed: ";
225: if (t.getMessage() != null) {
226: message = message + t.getMessage();
227: } else {
228: while (t.getMessage() == null) {
229: t = t.getCause();
230: if (t != null) {
231: message = message + t.getMessage();
232: } else {
233: message = message
234: + "Unknown reason";
235: }
236: }
237: }
238: info.setMessage(message);
239: }
240: } else {
241: info.setWorking(false);
242: }
243:
244: } catch (Exception e) {
245: throw new PortletException(e);
246: }
247: connectionFactories.add(info);
248: }
249: Collections.sort(connectionFactories);
250: renderRequest.setAttribute("cFactories", connectionFactories);
251:
252: normalView.include(renderRequest, renderResponse);
253: }
254:
255: private void renderDetail(RenderRequest renderRequest,
256: RenderResponse renderResponse, String name)
257: throws PortletException, IOException {
258: ObjectName gbeanName;
259:
260: try {
261: gbeanName = new ObjectName(name);
262: } catch (MalformedObjectNameException e) {
263: throw new PortletException("Malformed parameter name: "
264: + renderRequest.getParameter("name"));
265: }
266:
267: try {
268: GBeanInfo gbeanInfo = kernel.getGBeanInfo(gbeanName);
269: Set attributes = gbeanInfo.getAttributes();
270: Map values = new HashMap(attributes.size());
271: for (Iterator i = attributes.iterator(); i.hasNext();) {
272: GAttributeInfo attribute = (GAttributeInfo) i.next();
273: String gname = attribute.getName();
274: if (HIDDEN_ATTRIBUTES.contains(gname)) {
275: continue;
276: }
277: Object value = kernel.getAttribute(gbeanName, gname);
278: values.put(gname, value);
279: }
280: renderRequest.setAttribute("attributeMap", values);
281: } catch (Exception e) {
282: throw new PortletException(e);
283: }
284:
285: detailView.include(renderRequest, renderResponse);
286: }
287:
288: protected void doHelp(RenderRequest renderRequest,
289: RenderResponse renderResponse) throws PortletException,
290: IOException {
291: helpView.include(renderRequest, renderResponse);
292: }
293:
294: public void destroy() {
295: normalView = null;
296: helpView = null;
297: addView = null;
298: kernel = null;
299: super .destroy();
300: }
301:
302: private String trimStr(String str) {
303: if (str != null) {
304: return str.trim();
305: }
306: return "";
307: }
308:
309: protected void testConnection(Object cf) throws Exception {
310: ConnectionFactory jmscf = (ConnectionFactory) cf;
311: Connection c = jmscf.createConnection();
312: c.close();
313: }
314:
315: }
|