List of usage examples for javax.naming InitialContext lookup
public Object lookup(Name name) throws NamingException
From source file:Main.java
public static void main(String[] argv) throws Exception { InitialContext init = new InitialContext(); DataSource source = (DataSource) init.lookup("dsn"); Connection connection = source.getConnection(); System.out.println("Connect to " + connection.getCatalog() + " a success!"); }
From source file:Main.java
public static void main(String[] args) throws Exception { String dsn = args[0];/*from www . j a v a2s. c o m*/ System.out.println("Attempting to connect to " + dsn); try { System.out.println("Initializing the naming context..."); InitialContext init = new InitialContext(); System.out.println("Looking up " + dsn); DataSource source = (DataSource) init.lookup(dsn); System.out.println("Establishing a connection..."); Connection connection = source.getConnection(); System.out.println("Connect to " + connection.getCatalog() + " a success!"); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.pepstock.jem.junit.test.antutils.java.DeleteJunitDataSet.java
/** * /*from w ww . jav a 2s . c om*/ * @param args * @throws Exception */ public static void main(String[] args) throws Exception { InitialContext ic = ContextUtils.getContext(); // loads datapath container DataPathsContainer dc = (DataPathsContainer) ic.lookup(AntKeys.ANT_DATAPATHS_BIND_NAME); for (String path : dc.getDataPaths()) { File dirToDelete = new File(path, DATA_SET_JUNIT_FOLDER); System.out.println("Deliting folder:" + dirToDelete); if (dirToDelete.exists()) { FileUtils.deleteDirectory(dirToDelete); System.out.println("folder:" + dirToDelete + " deleted"); } else { System.out.println("folder:" + dirToDelete + " does not exists"); } } }
From source file:org.pepstock.jem.junit.test.common.java.DeleteJunitDataSet.java
/** * /*from w w w. j a va2 s. c o m*/ * @param args * @param args[0] is the folder on the jem.dataPaths to delete * @throws Exception */ public static void main(String[] args) throws Exception { if (args == null || args[0].length() == 0) { throw new Exception("Need argument for the folder to delete !"); } String folder = args[0]; InitialContext ic = ContextUtils.getContext(); // loads datapath container DataPathsContainer dc = (DataPathsContainer) ic.lookup(AntKeys.ANT_DATAPATHS_BIND_NAME); for (String path : dc.getDataPaths()) { File dirToDelete = new File(path, folder); System.out.println("Deliting folder:" + dirToDelete); if (dirToDelete.exists()) { FileUtils.deleteDirectory(dirToDelete); System.out.println("folder:" + dirToDelete + " deleted"); } else { System.out.println("folder:" + dirToDelete + " does not exists"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); // Set up the environment for creating the initial context Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:/jdbc"); Context context = new InitialContext(env); NamingEnumeration list = context.list("jdbc"); while (list.hasMore()) { NameClassPair nc = (NameClassPair) list.next(); System.out.println(nc);//from ww w . j a v a 2s . co m } OracleDataSource ods = new OracleDataSource(); ods.setDriverType("thin"); ods.setServerName("localhost"); ods.setNetworkProtocol("tcp"); ods.setDatabaseName("databaseName"); ods.setPortNumber(1521); ods.setUser("userName"); ods.setPassword("Password"); Context ctx = new InitialContext(); ctx.bind("file:/jdbc/mydb", ods); // Get the initial context of JNDI and lookup the datasource. InitialContext ic = new InitialContext(); javax.sql.DataSource ds = (javax.sql.DataSource) ic.lookup("file:/jdbc/mydb"); // Set the optional printwriter where the trace log is to be directed. ds.setLogWriter(new PrintWriter(new FileOutputStream("c:/datasource.log"))); Connection con1 = ds.getConnection(); Connection con2 = ds.getConnection("userName", "password"); conn.close(); }
From source file:org.pepstock.jem.junit.test.http.java.HttpConsumeAnt.java
/** * Get http resource "jem-http" from JCL and read the content of * http connection.//from www.j av a 2 s . co m * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Hashtable<String, String> env = createEnvironment(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.pepstock.jem.node.tasks.jndi.JemContextFactory"); InitialContext context = new InitialContext(env); // get http resource, note that jem-http is the name of the resource // present in the JCL InputStream httpStream = (InputStream) context.lookup("jem-http"); StringWriter writer = new StringWriter(); IOUtils.copy(httpStream, writer); String theString = writer.toString(); System.out.println("Read Http: " + theString); httpStream.close(); }
From source file:org.pepstock.jem.junit.test.jppf.java.CreateJemJdbcResource.java
/** * /*www. j a v a 2 s . c o m*/ * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String persistencePath = System.getProperty(ConfigKeys.JEM_PERSISTENCE_PATH_NAME); // String dataPath = System.getProperty(ConfigKeys.JEM_DATA_PATH_NAME); File file = new File(persistencePath); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); // get first directory that should be one of the JEM environment String jemEnv = directories[0]; File configFile = new File(persistencePath + "/" + jemEnv + "/" + JEM_ENV_CONFIG); String xmlConfig = FileUtils.readFileToString(configFile, CharSet.DEFAULT_CHARSET_NAME); Configuration jemEnvConf = Configuration.unmarshall(xmlConfig); // create dataSource with JEM Dabase info read from configuration file Resource resource = new Resource(); resource.setType("jdbc"); resource.setName("JUNIT_JPPF_JDBC_JEM"); resource.setUser("root"); resource.setLastModified(new Date()); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DRIVER_CLASS_NAME, jemEnvConf.getDatabase().getDriver(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.URL, jemEnvConf.getDatabase().getUrl(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.USERID, jemEnvConf.getDatabase().getUser(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.PASSWORD, jemEnvConf.getDatabase().getPassword(), false, false); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DEFAULT_READONLY, "true", true, false); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DEFAULT_AUTOCOMMIT, "true", true, false); InitialContext ic = ContextUtils.getContext(); // loads datapath container DataPathsContainer dc = (DataPathsContainer) ic.lookup(AntKeys.ANT_DATAPATHS_BIND_NAME); PathsContainer container = dc.getPaths(DATA_SET_NAME); String dataPath = container.getCurrent().getContent(); XStream xStream = XmlUtil.getXStream(); // write the resource to a file that will than be use to import the // resource on JEM String xmlResource = xStream.toXML(resource); File dataSet = new File(dataPath, DATA_SET_NAME); System.out.println("Generated reousrce:"); System.out.println(xmlResource); System.out.println(""); System.out.println("Writing resource to dataset:" + dataSet); FileUtils.writeStringToFile(dataSet, xmlResource); }
From source file:org.pepstock.jem.junit.test.antutils.java.CreateJemJdbcResource.java
/** * /*from w ww .ja va 2 s. co m*/ * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String persistencePath = System.getProperty(ConfigKeys.JEM_PERSISTENCE_PATH_NAME); // String dataPath = System.getProperty(ConfigKeys.JEM_DATA_PATH_NAME); File file = new File(persistencePath); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); // get first directory that should be one of the JEM environment String jemEnv = directories[0]; File configFile = new File(persistencePath + "/" + jemEnv + "/" + JEM_ENV_CONFIG); String xmlConfig = FileUtils.readFileToString(configFile, CharSet.DEFAULT_CHARSET_NAME); Configuration jemEnvConf = Configuration.unmarshall(xmlConfig); // create dataSource with JEM Dabase info read from configuration file Resource resource = new Resource(); resource.setType("jdbc"); resource.setName("JUNIT_JDBC_JEM"); resource.setUser("root"); resource.setLastModified(new Date()); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DRIVER_CLASS_NAME, jemEnvConf.getDatabase().getDriver(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.URL, jemEnvConf.getDatabase().getUrl(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.USERID, jemEnvConf.getDatabase().getUser(), true, false); ResourcePropertiesUtil.addProperty(resource, CommonKeys.PASSWORD, jemEnvConf.getDatabase().getPassword(), false, false); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DEFAULT_READONLY, "true", true, false); ResourcePropertiesUtil.addProperty(resource, JdbcResourceKeys.DEFAULT_AUTOCOMMIT, "true", true, false); Map<String, String> customProps = new HashMap<String, String>(); customProps.put("useCompression", "false"); InitialContext ic = ContextUtils.getContext(); // loads datapath container DataPathsContainer dc = (DataPathsContainer) ic.lookup(AntKeys.ANT_DATAPATHS_BIND_NAME); PathsContainer container = dc.getPaths(DATA_SET_NAME); String dataPath = container.getCurrent().getContent(); XStream xStream = XmlUtil.getXStream(); // write the resource to a file that will than be use to import the // resource on JEM String xmlResource = xStream.toXML(resource); File dataSet = new File(dataPath, DATA_SET_NAME); System.out.println("Generated reousrce:"); System.out.println(xmlResource); System.out.println(""); System.out.println("Writing resource to dataset:" + dataSet); FileUtils.writeStringToFile(dataSet, xmlResource); }
From source file:org.apache.openejb.config.Undeploy.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(Undeploy.option("v", "version", "cmd.deploy.opt.version")); options.addOption(Undeploy.option("h", "help", "cmd.undeploy.opt.help")); // TODO this message doesn't exist options.addOption(Undeploy.option("s", "server-url", "url", "cmd.deploy.opt.server")); CommandLine line = null;//from w ww . j a va2s. c om try { // parse the command line arguments line = parser.parse(options, args); } catch (final ParseException exp) { Undeploy.help(options); throw new SystemExitException(-1); } if (line.hasOption("help")) { Undeploy.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 module id."); help(options); return; } 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); Deployer deployer = null; 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(Undeploy.messages.format("cmd.deploy.serverOffline")); throw new SystemExitException(-1); } catch (final NamingException e) { System.out.println("DeployerEjb does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed."); throw new SystemExitException(-2); } int exitCode = 0; for (final Object obj : line.getArgList()) { final String moduleId = (String) obj; try { undeploy(moduleId, deployer); } catch (final DeploymentTerminatedException e) { System.out.println(e.getMessage()); exitCode++; } catch (final UndeployException e) { System.out.println(messages.format("cmd.undeploy.failed", moduleId)); e.printStackTrace(System.out); exitCode++; } catch (final NoSuchApplicationException e) { // TODO make this message System.out.println(messages.format("cmd.undeploy.noSuchModule", moduleId)); exitCode++; } } if (exitCode != 0) { throw new SystemExitException(exitCode); } }
From source file:org.apache.openejb.assembler.classic.cmd.Info2Properties.java
public static void main(final String[] args) { final CommandLineParser parser = new PosixParser(); // create the Options final Options options = new Options(); options.addOption(option("v", "version", "cmd.properties.opt.version")); options.addOption(option("h", "help", "cmd.properties.opt.help")); options.addOption(option("s", "server-url", "url", "cmd.properties.opt.server")); CommandLine line = null;//w ww. jav a 2 s. c o m try { // parse the command line arguments line = parser.parse(options, args); } catch (final ParseException exp) { help(options); System.exit(-1); } if (line.hasOption("help")) { help(options); System.exit(0); } else if (line.hasOption("version")) { OpenEjbVersion.get().print(System.out); System.exit(0); } 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); ConfigurationInfo configInfo = null; try { final InitialContext ctx = new InitialContext(p); configInfo = (ConfigurationInfo) ctx.lookup("openejb/ConfigurationInfoBusinessRemote"); } catch (final ServiceUnavailableException e) { System.out.println(e.getCause().getMessage()); System.out.println(messages.format("cmd.deploy.serverOffline")); System.exit(1); } catch (final NamingException e) { System.out.println("ConfigurationInfo does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed."); System.exit(2); } File tempFile = null; try { try { tempFile = File.createTempFile("configrequest", "txt"); } catch (final Throwable e) { final File tmp = new File("tmp"); if (!tmp.exists() && !tmp.mkdirs()) { throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath()); } tempFile = File.createTempFile("configrequest", "txt", tmp); } if (!tempFile.exists()) { throw new IllegalStateException("Failed to create tmp file: " + tempFile.getAbsolutePath()); } } catch (final Exception e) { System.err.println("Temp file creation failed."); e.printStackTrace(); System.exit(1); } OpenEjbConfiguration configuration = null; try { configuration = configInfo.getOpenEjbConfiguration(tempFile); } catch (final ConfigurationInfo.UnauthorizedException e) { System.err.println( "This tool is currently crippled to only work with server's on the same physical machine. See this JIRA issue for details: http://issues.apache.org/jira/browse/OPENEJB-621"); System.exit(10); } printConfig(configuration); }