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.wizard;
018    
019    import java.io.IOException;
020    import java.io.Serializable;
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    import javax.management.MalformedObjectNameException;
025    import javax.management.ObjectName;
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletException;
029    import javax.portlet.PortletRequest;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.geronimo.console.MultiPageModel;
034    import org.apache.geronimo.console.util.PortletManager;
035    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
036    import org.apache.geronimo.kernel.management.State;
037    import org.apache.geronimo.kernel.proxy.GeronimoManagedBean;
038    import org.apache.geronimo.management.geronimo.JCAAdminObject;
039    import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
040    import org.apache.geronimo.management.geronimo.JCAResource;
041    import org.apache.geronimo.management.geronimo.ResourceAdapter;
042    import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
043    import org.apache.commons.logging.Log;
044    import org.apache.commons.logging.LogFactory;
045    
046    /**
047     * A handles for the front page that lists available resources.
048     *
049     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
050     */
051    public class ListScreenHandler extends AbstractHandler {
052        private static final Log log = LogFactory.getLog(ListScreenHandler.class);
053    
054        public ListScreenHandler() {
055            super(LIST_MODE, "/WEB-INF/view/jmswizard/list.jsp");
056        }
057    
058        public String actionBeforeView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
059            return getMode();
060        }
061    
062        public void renderView(RenderRequest request, RenderResponse response, MultiPageModel model) throws PortletException, IOException {
063            populateExistingList(request);
064        }
065    
066        public String actionAfterView(ActionRequest request, ActionResponse response, MultiPageModel model) throws PortletException, IOException {
067            JMSResourceData data = (JMSResourceData) model;
068            String provider = request.getParameter(PROVIDER_PARAMETER);
069            if (isEmpty(provider)) {
070                return SELECT_PROVIDER_MODE + BEFORE_ACTION;
071            }
072            JMSProviderData providerData = JMSProviderData.getProviderByName(provider);
073            data.setRarURI(providerData.getRaURI());
074            data.setDependency(providerData.getDependency());
075            return CONFIGURE_RA_MODE + BEFORE_ACTION;
076        }
077    
078    
079        private void populateExistingList(PortletRequest renderRequest) {
080            // Prepare a list of JMS configurations
081            List resources = new ArrayList();
082    
083            // Get the list of connection factories
084            ResourceAdapterModule[] modules = PortletManager.getOutboundRAModules(renderRequest, new String[]{
085                    "javax.jms.ConnectionFactory", "javax.jms.QueueConnectionFactory", "javax.jms.TopicConnectionFactory",});
086            try {
087                for (int i = 0; i < modules.length; i++) {
088                    ResourceAdapterModule module = modules[i];
089    
090                    JMSResourceSummary target = null;
091                    for (int j = 0; j < resources.size(); j++) {
092                        JMSResourceSummary data = (JMSResourceSummary) resources.get(j);
093                        if (data.adapterObjectName.equals(module.getObjectName())) {
094                            target = data;
095                            break;
096                        }
097                    }
098                    if (target == null) {
099                        ResourceAdapter[] adapters = PortletManager.getResourceAdapters(renderRequest, module);
100                        String name = null;
101                        if (adapters.length == 1 && adapters[0].getJCAResources().length == 1) {
102                            JCAResource[] resource = PortletManager.getJCAResources(renderRequest, adapters[0]);
103                            if (resource.length == 1 && resource[0].getResourceAdapterInstances().length == 1) {
104                                name = ObjectName.getInstance(resource[0].getResourceAdapterInstanceNames()[0]).getKeyProperty(NameFactory.J2EE_NAME);
105                            }
106                        }
107                        if (name == null) {
108                            name = ObjectName.getInstance(module.getObjectName()).getKeyProperty(NameFactory.J2EE_NAME);
109                        }
110                        target = new JMSResourceSummary(PortletManager.getConfigurationFor(renderRequest, PortletManager.getNameFor(renderRequest, module)).toString(),
111                                module.getObjectName(), name, ((GeronimoManagedBean) module).getState());
112                        resources.add(target);
113                    }
114    
115                    JCAManagedConnectionFactory[] factories = PortletManager.getOutboundFactoriesForRA(renderRequest, module, new String[]{
116                            "javax.jms.ConnectionFactory", "javax.jms.QueueConnectionFactory", "javax.jms.TopicConnectionFactory",});
117                    for (int j = 0; j < factories.length; j++) {
118                        JCAManagedConnectionFactory factory = factories[j];
119                        ObjectName name = ObjectName.getInstance(factory.getObjectName());
120                        target.getConnectionFactories().add(new ConnectionFactorySummary(factory.getObjectName(), name.getKeyProperty(NameFactory.J2EE_NAME),
121                                ((GeronimoManagedBean) factory).getState()));
122                    }
123                }
124    
125                // Get the list of admin objects
126                modules = PortletManager.getAdminObjectModules(renderRequest, new String[]{"javax.jms.Queue", "javax.jms.Topic"});
127                for (int i = 0; i < modules.length; i++) {
128                    ResourceAdapterModule module = modules[i];
129    
130                    JMSResourceSummary target = null;
131                    for (int j = 0; j < resources.size(); j++) {
132                        JMSResourceSummary data = (JMSResourceSummary) resources.get(j);
133                        if (data.adapterObjectName.equals(module.getObjectName())) {
134                            target = data;
135                            break;
136                        }
137                    }
138                    if (target == null) {
139                        ResourceAdapter[] adapters = PortletManager.getResourceAdapters(renderRequest, module);
140                        String name = null;
141                        if (adapters.length == 1 && adapters[0].getJCAResources().length == 1) {
142                            JCAResource[] resource = PortletManager.getJCAResources(renderRequest, adapters[0]);
143                            if (resource.length == 1 && resource[0].getResourceAdapterInstances().length == 1) {
144                                name = ObjectName.getInstance(resource[0].getResourceAdapterInstanceNames()[0]).getKeyProperty(NameFactory.J2EE_NAME);
145                            }
146                        }
147                        if (name == null) {
148                            name = ObjectName.getInstance(module.getObjectName()).getKeyProperty(NameFactory.J2EE_NAME);
149                        }
150                        target = new JMSResourceSummary(PortletManager.getConfigurationFor(renderRequest, PortletManager.getNameFor(renderRequest, module)).toString(),
151                                module.getObjectName(), name, ((GeronimoManagedBean) module).getState());
152                        resources.add(target);
153                    }
154    
155                    JCAAdminObject[] admins = PortletManager.getAdminObjectsForRA(renderRequest, module, new String[]{"javax.jms.Queue", "javax.jms.Topic"});
156                    for (int j = 0; j < admins.length; j++) {
157                        GeronimoManagedBean bean = (GeronimoManagedBean) admins[j];
158                        ObjectName name = ObjectName.getInstance(bean.getObjectName());
159                        target.getAdminObjects().add(new AdminObjectSummary(bean.getObjectName(), name.getKeyProperty(NameFactory.J2EE_NAME),
160                                admins[j].getAdminObjectInterface().indexOf("Queue") > -1 ? "Queue" : "Topic",
161                                bean.getState()));
162                    }
163                }
164            } catch (MalformedObjectNameException e) {
165                log.error(e.getMessage(), e);
166            }
167    
168            Collections.sort(resources);
169            renderRequest.setAttribute("resources", resources);
170            // Get the list of JMS providers
171            renderRequest.setAttribute("providers", JMSProviderData.getAllProviders());
172        }
173    
174    
175        public static class JMSResourceSummary implements Serializable, Comparable {
176            private static final long serialVersionUID = -2788803234448047035L;
177            private final String configurationName;
178            private final String adapterObjectName;
179            private final String name;
180            private final String parentName;
181            private final int state;
182            private final List connectionFactories = new ArrayList();
183            private final List adminObjects = new ArrayList();
184    
185            public JMSResourceSummary(String configurationName, String adapterObjectName, String name, int state) {
186                this.configurationName = configurationName;
187                this.adapterObjectName = adapterObjectName;
188                this.state = state;
189                try {
190                    ObjectName objectName = ObjectName.getInstance(adapterObjectName);
191                    String parent = objectName.getKeyProperty(NameFactory.J2EE_APPLICATION);
192                    if (parent != null && parent.equals("null")) {
193                        parent = null;
194                    }
195                    parentName = parent;
196                    this.name = name;
197                } catch (MalformedObjectNameException e) {
198                    throw new RuntimeException("Can't parse ObjectName", e);
199                }
200            }
201    
202            public String getConfigurationName() {
203                return configurationName;
204            }
205    
206            public String getAdapterObjectName() {
207                return adapterObjectName;
208            }
209    
210            public String getName() {
211                return name;
212            }
213    
214            public String getParentName() {
215                return parentName;
216            }
217    
218            public int getState() {
219                return state;
220            }
221    
222            public List getConnectionFactories() {
223                return connectionFactories;
224            }
225    
226            public List getAdminObjects() {
227                return adminObjects;
228            }
229    
230            public String getStateName() {
231                return State.toString(state);
232            }
233    
234            public int compareTo(Object o) {
235                final JMSResourceSummary pool = (JMSResourceSummary) o;
236                int names = name.toLowerCase().compareTo(pool.name.toLowerCase());
237                if (parentName == null) {
238                    if (pool.parentName == null) {
239                        return names;
240                    } else {
241                        return -1;
242                    }
243                } else {
244                    if (pool.parentName == null) {
245                        return 1;
246                    } else {
247                        int test = parentName.compareTo(pool.parentName);
248                        if (test != 0) {
249                            return test;
250                        } else {
251                            return names;
252                        }
253                    }
254                }
255            }
256        }
257    
258        public static class ConnectionFactorySummary implements Serializable, Comparable {
259            private static final long serialVersionUID = 5777507920880039759L;
260            private final String factoryObjectName;
261            private final String name;
262            private final int state;
263    
264            public ConnectionFactorySummary(String factoryObjectName, String name, int state) {
265                this.factoryObjectName = factoryObjectName;
266                this.name = name;
267                this.state = state;
268            }
269    
270            public String getFactoryObjectName() {
271                return factoryObjectName;
272            }
273    
274            public String getName() {
275                return name;
276            }
277    
278            public int getState() {
279                return state;
280            }
281    
282            public String getStateName() {
283                return State.toString(state);
284            }
285    
286            public int compareTo(Object o) {
287                final ConnectionFactorySummary pool = (ConnectionFactorySummary) o;
288                return name.compareTo(pool.name);
289            }
290        }
291    
292    
293        public static class AdminObjectSummary implements Serializable, Comparable {
294            private static final long serialVersionUID = 3941332145785485903L;
295            private final String adminObjectName;
296            private final String name;
297            private final String type;
298            private final int state;
299    
300            public AdminObjectSummary(String adminObjectName, String name, String type, int state) {
301                this.adminObjectName = adminObjectName;
302                this.name = name;
303                this.type = type;
304                this.state = state;
305            }
306    
307            public String getAdminObjectName() {
308                return adminObjectName;
309            }
310    
311            public String getName() {
312                return name;
313            }
314    
315            public String getType() {
316                return type;
317            }
318    
319            public int getState() {
320                return state;
321            }
322    
323            public String getStateName() {
324                return State.toString(state);
325            }
326    
327            public int compareTo(Object o) {
328                final AdminObjectSummary pool = (AdminObjectSummary) o;
329                int result = name.compareTo(pool.name);
330                return result == 0 ? type.compareTo(pool.type) : result;
331            }
332        }
333    }