<commit_before>/*******************************************************************************
  * Copyright (c) 2017 DocDoku.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
  *    DocDoku - initial API and implementation
  *******************************************************************************/
package org.polarsys.eplmp.server;

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utility class that looks-up EJBs implementing a given business interface from JNDI
 * naming service.
 *
 * @author <NAME>
 */
public class BeanLocator {

    private static final Logger LOGGER = Logger.getLogger(BeanLocator.class.getName());

    /**
     * Search for EJBs implementing a given business interface within the naming
     * Context "java:global".
     *
     * @param type Bean's Business Interface.
     * @return the list of EJBs implementing the given interface.
     * @throws NamingException
     */
    public <T> List<T> search(Class<T> type) {

        List<T> result = new ArrayList<>();

        try {
            InitialContext context = new InitialContext();
            Context ctx = (Context) context.lookup("java:global");
            result.addAll(search(type, ctx));
        } catch (NamingException e) {
            LOGGER.log(Level.SEVERE, null, e);
        }


        return result;
    }

    /**
     * Search for EJBs implementing a given business interface within the given
     * Naming Context.
     *
     * @param type EJB's Business Interface
     * @param ctx  Current Naming Context
     * @return the list of EJBs implementing the given interface within the
     * context.
     * @throws NamingException
     */
    @SuppressWarnings("unchecked")
    // Cause : Generic Type Erasure
    <T> List<T> search(Class<T> type, Context ctx) {
        List<T> result = new ArrayList<T>();
        try {
            NamingEnumeration<NameClassPair> ncps = ctx.list("");
            while (ncps.hasMoreElements()) {
                try {
                    NameClassPair ncp = ncps.next();
                    Object o = ctx.lookup(ncp.getName());
                    if (ncp.getName().contains("!" + type.getCanonicalName())) {
                        // bean reference
                        LOGGER.info("EJB found: " + ncp.getName());
                        result.add((T) PortableRemoteObject.narrow(o, type));
                    } else if (Context.class.isAssignableFrom(o.getClass())) {
                        // sub-context
                        result.addAll(search(type, (Context) o));
                    }
                    // else ignore this object
                } catch (NamingException e) {
                    LOGGER.log(Level.INFO, "Ignoring JNDI entry: " + e.getMessage());
                }
            }
        } catch (NamingException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
        return result;
    }
}<commit_msg>Catch generic exception in BeanLocator - some other exceptions can be thrown<commit_after>/*******************************************************************************
 * Copyright (c) 2017 DocDoku.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>
 * Contributors:
 * DocDoku - initial API and implementation
 *******************************************************************************/
package org.polarsys.eplmp.server;

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utility class that looks-up EJBs implementing a given business interface from JNDI
 * naming service.
 *
 * @author <NAME>
 */
public class BeanLocator {

    private static final Logger LOGGER = Logger.getLogger(BeanLocator.class.getName());

    /**
     * Search for EJBs implementing a given business interface within the naming
     * Context "java:global".
     *
     * @param type Bean's Business Interface.
     * @return the list of EJBs implementing the given interface.
     * @throws NamingException
     */
    public <T> List<T> search(Class<T> type) {

        List<T> result = new ArrayList<>();

        try {
            InitialContext context = new InitialContext();
            Context ctx = (Context) context.lookup("java:global");
            result.addAll(search(type, ctx));
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, null, e);
        }


        return result;
    }

    /**
     * Search for EJBs implementing a given business interface within the given
     * Naming Context.
     *
     * @param type EJB's Business Interface
     * @param ctx  Current Naming Context
     * @return the list of EJBs implementing the given interface within the
     * context.
     * @throws NamingException
     */
    @SuppressWarnings("unchecked")
    // Cause : Generic Type Erasure
    <T> List<T> search(Class<T> type, Context ctx) {
        List<T> result = new ArrayList<T>();
        try {
            NamingEnumeration<NameClassPair> ncps = ctx.list("");
            while (ncps.hasMoreElements()) {
                try {
                    NameClassPair ncp = ncps.next();
                    Object o = ctx.lookup(ncp.getName());
                    if (ncp.getName().contains("!" + type.getCanonicalName())) {
                        // bean reference
                        LOGGER.info("EJB found: " + ncp.getName());
                        result.add((T) PortableRemoteObject.narrow(o, type));
                    } else if (Context.class.isAssignableFrom(o.getClass())) {
                        // sub-context
                        result.addAll(search(type, (Context) o));
                    }
                    // else ignore this object
                } catch (Exception e) {
                    LOGGER.log(Level.INFO, "Ignoring JNDI entry: " + e.getMessage());
                }
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }
        return result;
    }
}
{'fraction_non_alphanumeric': 0.14892971003491726, 'fraction_numerical': 0.0024290268711097615, 'mean_word_length': 2.1856866537717603, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6724561', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1691, 'n_words': 491}