001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.seam;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022import java.util.Set;
023
024import javax.servlet.http.HttpServletRequest;
025
026import org.jboss.seam.Component;
027import org.jboss.seam.contexts.Contexts;
028import org.jboss.seam.contexts.ServletLifecycle;
029import org.jboss.seam.web.ServletContexts;
030import org.nuxeo.apidoc.api.SeamComponentInfo;
031import org.nuxeo.apidoc.introspection.SeamComponentInfoImpl;
032
033public class SeamRuntimeIntrospector {
034
035    protected static List<String> listAllComponentsNames() {
036        List<String> names = new ArrayList<String>();
037        if (Contexts.isApplicationContextActive()) {
038            for (String name : Contexts.getApplicationContext().getNames()) {
039                if (name.endsWith(".component")) {
040                    names.add(name.replace(".component", ""));
041                }
042            }
043        }
044        return names;
045    }
046
047    public static List<SeamComponentInfo> listNuxeoComponents(HttpServletRequest request) {
048
049        ServletLifecycle.beginRequest(request);
050        ServletContexts.instance().setRequest(request);
051        // ConversationPropagation.instance().setConversationId( conversationId
052        // );
053        // Manager.instance().restoreConversation();
054        // ServletLifecycle.resumeConversation(request);
055
056        try {
057            return listNuxeoComponents();
058        } finally {
059            ServletLifecycle.endRequest(request);
060        }
061
062    }
063
064    protected static List<SeamComponentInfo> components = null;
065
066    protected static synchronized List<SeamComponentInfo> listNuxeoComponents() {
067        if (components == null) {
068            components = new ArrayList<SeamComponentInfo>();
069            for (String cName : listAllComponentsNames()) {
070                SeamComponentInfoImpl desc = new SeamComponentInfoImpl();
071                Component comp = Component.forName(cName);
072                String className = comp.getBeanClass().getName();
073                // if (className.startsWith("org.nuxeo")) {
074                if (!className.startsWith("org.jboss")) {
075                    desc.setName(cName);
076                    desc.setScope(comp.getScope().toString());
077                    desc.setClassName(className);
078
079                    @SuppressWarnings("rawtypes")
080                    Set<Class> ifaces = comp.getBusinessInterfaces();
081                    if (ifaces != null && ifaces.size() > 0) {
082                        for (Class<?> iface : ifaces) {
083                            desc.addInterfaceName(iface.getName());
084                        }
085                    }
086                    desc.addInterfaceName(comp.getBeanClass().getName());
087                    components.add(desc);
088                }
089            }
090            Collections.sort(components);
091        }
092        return components;
093    }
094}