001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.seam;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024import java.util.Set;
025
026import javax.servlet.http.HttpServletRequest;
027
028import org.jboss.seam.Component;
029import org.jboss.seam.contexts.Contexts;
030import org.jboss.seam.contexts.ServletLifecycle;
031import org.jboss.seam.web.ServletContexts;
032import org.nuxeo.apidoc.api.SeamComponentInfo;
033import org.nuxeo.apidoc.introspection.SeamComponentInfoImpl;
034
035public class SeamRuntimeIntrospector {
036
037    protected static List<String> listAllComponentsNames() {
038        List<String> names = new ArrayList<>();
039        if (Contexts.isApplicationContextActive()) {
040            for (String name : Contexts.getApplicationContext().getNames()) {
041                if (name.endsWith(".component")) {
042                    names.add(name.replace(".component", ""));
043                }
044            }
045        }
046        return names;
047    }
048
049    public static List<SeamComponentInfo> listNuxeoComponents(HttpServletRequest request) {
050
051        ServletLifecycle.beginRequest(request);
052        ServletContexts.instance().setRequest(request);
053        // ConversationPropagation.instance().setConversationId( conversationId
054        // );
055        // Manager.instance().restoreConversation();
056        // ServletLifecycle.resumeConversation(request);
057
058        try {
059            return listNuxeoComponents();
060        } finally {
061            ServletLifecycle.endRequest(request);
062        }
063
064    }
065
066    protected static List<SeamComponentInfo> components = null;
067
068    protected static synchronized List<SeamComponentInfo> listNuxeoComponents() {
069        if (components == null) {
070            components = new ArrayList<>();
071            for (String cName : listAllComponentsNames()) {
072                SeamComponentInfoImpl desc = new SeamComponentInfoImpl();
073                Component comp = Component.forName(cName);
074                String className = comp.getBeanClass().getName();
075                // if (className.startsWith("org.nuxeo")) {
076                if (!className.startsWith("org.jboss")) {
077                    desc.setName(cName);
078                    desc.setScope(comp.getScope().toString());
079                    desc.setClassName(className);
080
081                    @SuppressWarnings("rawtypes")
082                    Set<Class> ifaces = comp.getBusinessInterfaces();
083                    if (ifaces != null && ifaces.size() > 0) {
084                        for (Class<?> iface : ifaces) {
085                            desc.addInterfaceName(iface.getName());
086                        }
087                    }
088                    desc.addInterfaceName(comp.getBeanClass().getName());
089                    components.add(desc);
090                }
091            }
092            Collections.sort(components);
093        }
094        return components;
095    }
096}