下面列出了javax.naming.InitialContext#doLookup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private synchronized void getKeyStoreInfo() throws IOException {
if (signingKey != null)
return;
try {
// load up the keystore
keyStore = InitialContext.doLookup("jwtKeyStore");
keyStorePW = InitialContext.doLookup("jwtKeyStorePassword");
keyStoreAlias = InitialContext.doLookup("jwtKeyStoreAlias");
FileInputStream is = new FileInputStream(keyStore);
KeyStore signingKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
signingKeystore.load(is, keyStorePW.toCharArray());
signingKey = signingKeystore.getKey(keyStoreAlias, keyStorePW.toCharArray());
} catch (Exception e) {
throw new IOException(e);
}
}
/**
* Default constructor
*
* @param conf
* namespace for metrics
*/
public ExecutorFacade(SessionConfiguration conf) {
ThreadFactory tf;
count = new AtomicLong(0);
this.namespace = conf.getNamespace();
String threadFactoryJndi = conf.getAttribute(THREAD_JNDI, "java:comp/DefaultManagedThreadFactory");
try {
// Try to get JEE 6 managed thread factory,
// and if not available, fall back to built-in default one.
tf = InitialContext.doLookup(threadFactoryJndi);
} catch (NamingException e) {
logger.warn("Unable to use ManagedThreadFactory from JNDI {}, using built-in thread pool. Cause: '{}'. "
+ "Activate debug tracing for more information.", threadFactoryJndi, e.getMessage());
logger.debug("Unable to use ManagedThreadFactory from JNDI, stack trace follows. ", e);
tf = Executors.defaultThreadFactory();
}
baseThreadFactory = tf;
int queueSize = Integer.parseInt(conf.getAttribute(WORK_QUEUE_SIZE, MAXIMUM_WORK_QUEUE_SIZE));
ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(queueSize);
executor = new ThreadPoolExecutor(CORE_THREADS_IN_POOL, MAXIMUM_THREADS_IN_POOL, THREAD_KEEPALIVE_TIME, SECONDS,
workQueue, this, new ThreadPoolExecutor.CallerRunsPolicy());
scheduledExecutor = new ScheduledThreadPoolExecutor(SCHEDULER_THREADS_IN_POOL, this, new DiscardAndLog());
}
public URL lookupBpmPlatformXmlLocationFromJndi() {
String jndi = "java:comp/env/" + BPM_PLATFORM_XML_LOCATION;
try {
String bpmPlatformXmlLocation = InitialContext.doLookup(jndi);
URL fileLocation = checkValidBpmPlatformXmlResourceLocation(bpmPlatformXmlLocation);
if (fileLocation != null) {
LOG.foundConfigJndi(jndi, fileLocation.toString());
}
return fileLocation;
}
catch (NamingException e) {
LOG.debugExceptionWhileGettingConfigFromJndi(jndi, e);
return null;
}
}
private static DataSource lookupDataSource(String dataSourceName) {
try {
return (DataSource) InitialContext.doLookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
@Test
public void testJndiServerName() throws Exception
{
addAndStartEngine();
String s = (String) InitialContext.doLookup("serverName");
Assert.assertEquals("localhost", s);
}
public ExecuteSqlWorkItemHandler(String dataSourceName) {
try {
this.ds = InitialContext.doLookup(dataSourceName);
} catch (NamingException e) {
throw new RuntimeException("Unable to look up data source: " + dataSourceName + " - " + e.getMessage());
}
}
private UserTransaction getUserTransaction() {
if (userTransaction == null) {
try {
userTransaction = InitialContext.doLookup("java:comp/UserTransaction");
} catch (NamingException e) {
throw new TransactionException(e);
}
}
return userTransaction;
}
private ManagedScheduledExecutorService executor() {
try {
return InitialContext.doLookup("java:comp/DefaultManagedScheduledExecutorService");
} catch (NamingException e) {
log("Unable to obtain ManagedScheduledExecutorService");
e.printStackTrace();
return null;
}
}
/**
* @param map -1=random, 0=empty, >0=specific map
*/
public static GameMap create(int map) {
try {
int mapOverride = InitialContext.doLookup("round/map");
if (mapOverride >= 0 && mapOverride <= NUM_MAPS) {
map = mapOverride;
System.out.println("Overriding map selection to map #" + map);
}
} catch (Exception ignore) {
}
switch (map) {
case -1:
return create(r.nextInt(NUM_MAPS) + 1);
case 0:
return new EmptyMap();
case 1:
return new OriginalMap();
case 2:
return new CrossSlice();
case 3:
return new FakeBlock();
case 4:
return new Smile();
case 5:
return new HulkSmash();
default:
throw new IllegalArgumentException("Illegal map number: " + map);
}
}
@GET
public String hello(@QueryParam("name") String name, @Context HttpServletRequest request) throws NamingException {
HttpSession session = request.getSession();
GreeterService greeter = (GreeterService) session.getAttribute(GreeterService.class.getName());
if (greeter == null) {
greeter = InitialContext.doLookup("java:module/" + GreeterService.class.getSimpleName());
session.setAttribute(GreeterService.class.getName(), greeter);
}
return simpleService.yay() + greeter.hello(name);
}
public static DataSource getProarcSource() throws NamingException {
DataSource source = InitialContext.doLookup("java:/comp/env/jdbc/proarc");
if (source == null) {
throw new IllegalStateException("Cannot find 'jdbc/proarc' resource!");
}
return source;
}
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.doLookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
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.doLookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
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);
}
}
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);
}
}
private static void initializeDatabase(String configFilePath) throws IOException, XMLStreamException, NamingException {
InputStream in;
in = FileUtils.openInputStream(new File(configFilePath));
StAXOMBuilder builder = new StAXOMBuilder(in);
String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
.getText();
OMElement databaseElement = builder.getDocumentElement()
.getFirstChildWithName(new QName("Database"));
String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(databaseDriver);
basicDataSource.setUrl(databaseURL);
basicDataSource.setUsername(databaseUser);
basicDataSource.setPassword(databasePass);
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES,
"org.apache.naming");
try {
InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
} catch (NamingException e) {
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");
ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
}
}
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.doLookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
public PersistentPlayerDB() throws NamingException {
ds = InitialContext.doLookup("java:comp/DefaultDataSource");
}
/**
* Protected method for unit test purposes.
*/
protected APPTemplateService getTemplateService() throws NamingException {
return InitialContext.doLookup(APPTemplateService.JNDI_NAME);
}
@Test
public void testDefaultProcessEngineBindingCreated() {
try {
ProcessEngine processEngine = InitialContext.doLookup("java:global/camunda-bpm-platform/process-engine/default");
Assert.assertNotNull("Process engine must not be null", processEngine);
} catch(Exception e) {
Assert.fail("Process Engine not bound in JNDI.");
}
}