Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

In this page you can find the example usage for javax.naming InitialContext lookup.

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

From source file:org.apache.openejb.config.Deploy.java

public static void main(final String... args) throws SystemExitException {

    final CommandLineParser parser = new PosixParser();

    // create the Options
    final Options options = new Options();
    options.addOption(option("v", "version", "cmd.deploy.opt.version"));
    options.addOption(option("h", "help", "cmd.deploy.opt.help"));
    options.addOption(option("o", "offline", "cmd.deploy.opt.offline"));
    options.addOption(option("s", "server-url", "url", "cmd.deploy.opt.server"));
    options.addOption(option("d", "debug", "cmd.deploy.opt.debug"));
    options.addOption(option("q", "quiet", "cmd.deploy.opt.quiet"));
    options.addOption(option("u", "undeploy", "cmd.deploy.opt.undeploy"));
    options.addOption(option(null, "dir", "cmd.deploy.opt.dir"));

    final CommandLine line;
    try {/*  w  w w. j a va2  s.c o  m*/
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }

    if (line.hasOption("help")) {
        help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    }

    if (line.getArgList().size() == 0) {
        System.out.println("Must specify an archive to deploy.");
        help(options);
        return;
    }

    // make sure that the modules given on the command line are accessible
    final List<?> modules = line.getArgList();
    for (final Object module : modules) {
        final String path = (String) module;
        final File file = new File(path);
        try {
            checkSource(file);
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            // TODO: What is it for?
            throw new SystemExitException(-100);
        }
    }

    final boolean offline = line.hasOption("offline");

    final File apps;
    try {
        final String dir = line.getOptionValue("dir", "apps");
        apps = SystemInstance.get().getBase().getDirectory(dir);
    } catch (final IOException e) {
        throw new SystemExitException(-1);
    }

    if (!apps.exists()) {
        System.out.println("Directory does not exist: " + apps.getAbsolutePath());
    }

    Deployer deployer = null;
    if (!offline) {
        final Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");

        final String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
        p.put(Context.PROVIDER_URL, serverUrl);

        try {
            final InitialContext ctx = new InitialContext(p);
            deployer = (Deployer) ctx.lookup("openejb/DeployerBusinessRemote");
        } catch (final ServiceUnavailableException e) {
            System.out.println(e.getCause().getMessage());
            System.out.println(messages.format("cmd.deploy.serverOffline"));
            throw new SystemExitException(-1);
        } catch (final NamingException e) {
            System.out.println("openejb/DeployerBusinessRemote does not exist in server '" + serverUrl
                    + "', check the server logs to ensure it exists and has not been removed.");
            throw new SystemExitException(-2);
        }
    }

    final boolean undeploy = line.hasOption("undeploy");

    // We increment the exit code once for every failed deploy
    int exitCode = 0;
    for (final Object obj : line.getArgList()) {
        final String path = (String) obj;

        final File file = new File(path);

        File destFile = new File(apps, file.getName());

        try {
            if (shouldUnpack(file)) {
                final File unpacked = unpackedLocation(file, apps);
                if (undeploy) {
                    undeploy(offline, unpacked, path, deployer);
                }
                destFile = unpack(file, unpacked);
            } else {
                if (undeploy) {
                    undeploy(offline, destFile, path, deployer);
                }
                checkDest(destFile, file);
                copyFile(file, destFile);
            }

            if (offline) {
                System.out.println(messages.format("cmd.deploy.offline", path, apps.getAbsolutePath()));
                continue;
            }

            final String location;
            try {
                location = destFile.getCanonicalPath();
            } catch (final IOException e) {
                throw new OpenEJBException(messages.format("cmd.deploy.fileNotFound", path));
            }
            final AppInfo appInfo = deployer.deploy(location);

            System.out.println(messages.format("cmd.deploy.successful", path, appInfo.path));

            if (line.hasOption("quiet")) {
                continue;
            }

            print(appInfo);

        } catch (final UndeployException e) {
            System.out.println(messages.format("cmd.undeploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            exitCode++;
        } catch (final ValidationFailedException e) {
            System.out.println(messages.format("cmd.deploy.validationFailed", path));
            int level = 2;
            if (line.hasOption("debug")) {
                level = 3;
            }
            final AppValidator appValidator = new AppValidator(level, false, true, false);
            appValidator.printResults(e);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        } catch (final Throwable e) {
            System.out.println(messages.format("cmd.deploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        }
    }

    if (exitCode != 0) {
        throw new SystemExitException(exitCode);
    }
}

From source file:org.nuxeo.ecm.platform.audit.ws.delegate.EJBFactory.java

public static WSAudit getWSAuditRemote() throws NamingException {
    String beanRemoteLocation = JNDILocations.nxauditWsAuditRemoteLocation;
    if (log.isDebugEnabled()) {
        log.debug("Trying to get the remote EJB with JNDI location :" + beanRemoteLocation);
    }/*w  ww.  java 2 s . c  o m*/
    InitialContext ctx = getInitialContext();
    return (WSAudit) ctx.lookup(beanRemoteLocation);
}

From source file:org.jbpm.util.JndiUtil.java

public static Object lookup(String jndiName, Class type) {
    Object object = null;/*from www  . j  a  v  a 2  s.co m*/
    try {
        InitialContext initialContext = new InitialContext();
        object = initialContext.lookup(jndiName);
        object = PortableRemoteObject.narrow(object, type);
        // fetch from JNDI
        log.debug("fetched '" + object + "' from JNDI location '" + jndiName + "'");
    } catch (Exception e) {
        throw new JndiLookupException("couldn't fetch '" + jndiName + "' from jndi", e);
    }
    return object;
}

From source file:Main.java

private static Connection getConnection() throws NamingException, SQLException {
    InitialContext initCtx = createContext();
    String jndiName = "HrDS";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    return pooledConnection.getConnection(); // Obtain connection from pool
}

From source file:org.jboss.dashboard.commons.cdi.CDIBeanLocator.java

private static BeanManager lookupBeanManager(String jndiName) {
    try {/* w  ww . ja v  a2  s.c o  m*/
        InitialContext initialContext = new InitialContext();
        return (BeanManager) initialContext.lookup(jndiName);
    } catch (NamingException e) {
        // Ignore and return null.
        return null;
    }
}

From source file:com.esa.infocontrol.data.jdbc.BaseDataJDBC.java

public static DataSource getDataSourceByName(String name) {
    DataSource ds = null;//from  w  ww.  ja  v  a 2s. c  o m
    try {
        InitialContext ic = new InitialContext();
        ds = (DataSource) ic.lookup("jdbc/" + name);
    } catch (NamingException ex) {
        java.util.logging.Logger.getLogger(BaseDataJDBC.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ds;
}

From source file:com.stratelia.silverpeas.silverStatisticsPeas.control.PerfVolumeTest.java

@AfterClass
public static void teardownDataSource() throws NamingException, SQLException {
    InitialContext ic = new InitialContext();
    BasicDataSource ds = (BasicDataSource) ic.lookup("java:/datasources/silverpeas-jdbc");
    ds.close();/*ww  w  .j a  v  a  2  s.c  o  m*/
    SimpleMemoryContextFactory.tearDownAsInitialContext();
}

From source file:PooledConnectionExample.java

private static Connection getConnection() throws NamingException, SQLException {
    InitialContext initCtx = createContext();
    String jndiName = "HrDS";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    return pooledConnection.getConnection();
}

From source file:org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil.java

/**
 * Lookup datasource using name and jndi properties
 *
 * @param dataSourceName Name of datasource to lookup
 * @param jndiProperties Hash table of JNDI Properties
 * @return datasource looked//from   ww  w .j  a v  a  2 s  .  c  o m
 */
public static DataSource lookupDataSource(String dataSourceName,
        final Hashtable<Object, Object> jndiProperties) {
    try {
        if (jndiProperties == null || jndiProperties.isEmpty()) {
            return (DataSource) InitialContext.doLookup(dataSourceName);
        }
        final InitialContext context = new InitialContext(jndiProperties);
        return (DataSource) context.lookup(dataSourceName);
    } catch (Exception e) {
        throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
    }
}

From source file:be.fedict.hsm.model.security.AdministratorSecurityBean.java

public static AdministratorSecurityBean getInstance() {
    try {/*from w  ww  . ja va 2 s . c o m*/
        InitialContext initialContext = new InitialContext();
        return (AdministratorSecurityBean) initialContext.lookup(JNDI_NAME);
    } catch (NamingException e) {
        throw new RuntimeException("JNDI error: " + e.getMessage(), e);
    }
}