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