use of javax.naming.InitialContext in project jackrabbit by apache.
the class JNDIRepositoryStub method getRepository.
/**
* Returns a reference to the <code>Repository</code> provided by this
* <code>RepositoryStub</code>.
* @return
*/
public synchronized Repository getRepository() throws RepositoryStubException {
if (repository == null) {
try {
String lookupName = environment.getProperty(REPOSITORY_LOOKUP_PROP);
if (lookupName == null) {
throw new RepositoryStubException("Property " + REPOSITORY_LOOKUP_PROP + " not defined!");
}
InitialContext initial = new InitialContext(environment);
Object obj = initial.lookup(lookupName);
repository = (Repository) PortableRemoteObject.narrow(obj, Repository.class);
} catch (ClassCastException e) {
// ClassCastException may be thrown by ProtableRemoteObject.narrow()
throw new RepositoryStubException("Object cannot be narrowed to javax.jcr.Repository", e);
} catch (NamingException e) {
throw new RepositoryStubException(e);
}
}
return repository;
}
use of javax.naming.InitialContext in project jackrabbit by apache.
the class JNDIBindingServlet method init.
/**
* Binds a repository from the servlet context in the configured
* JNDI location.
*
* @throws ServletException if the repository could not be bound in JNDI
*/
public void init() throws ServletException {
try {
Hashtable environment = new Hashtable();
Enumeration names = getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (name.equals("location")) {
location = getInitParameter(name);
} else if (!name.equals(Repository.class.getName())) {
environment.put(name, getInitParameter(name));
}
}
context = new InitialContext(environment);
context.bind(location, new ServletRepository(this));
} catch (NamingException e) {
throw new ServletException("Failed to bind repository to JNDI: " + location, e);
}
}
use of javax.naming.InitialContext in project jackrabbit-oak by apache.
the class RepositoryAccessServlet method getRepositoryByJNDI.
/**
* Checks if the repository is available via JNDI and returns it.
* @return the repository or <code>null</code>
* @throws ServletException if this servlet is not properly configured.
*/
private Repository getRepositoryByJNDI() throws ServletException {
BootstrapConfig config = getConfig();
if (!config.getJndiConfig().isValid() || !config.getJndiConfig().enabled()) {
return null;
}
// acquire via JNDI
String repositoryName = config.getRepositoryName();
InitialContext ctx = getInitialContext();
if (ctx == null) {
return null;
}
try {
Repository r = (Repository) ctx.lookup(repositoryName);
log.info("Acquired repository via JNDI.");
return r;
} catch (NamingException e) {
log.error("Error while retrieving repository using JNDI (name={})", repositoryName, e);
return null;
}
}
use of javax.naming.InitialContext in project jackrabbit-oak by apache.
the class RepositoryStartupServlet method registerJNDI.
/**
* Binds the repository to the JNDI context
* @throws ServletException if an error occurs.
*/
private void registerJNDI() throws ServletException {
JNDIConfig jc = config.getJndiConfig();
if (jc.isValid() && jc.enabled()) {
try {
jndiContext = new InitialContext(jc.getJndiEnv());
jndiContext.bind(jc.getJndiName(), repository);
log.info("Repository bound to JNDI with name: " + jc.getJndiName());
} catch (NamingException e) {
throw new ServletExceptionWithCause("Unable to bind repository using JNDI: " + jc.getJndiName(), e);
}
}
}
use of javax.naming.InitialContext in project jmeter by apache.
the class JMSSampler method getInitialContext.
private Context getInitialContext() throws NamingException {
Hashtable<String, String> table = new Hashtable<>();
if (getInitialContextFactory() != null && getInitialContextFactory().trim().length() > 0) {
LOGGER.debug("Using InitialContext [{}]", getInitialContextFactory());
table.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
}
if (getContextProvider() != null && getContextProvider().trim().length() > 0) {
LOGGER.debug("Using Provider [{}]", getContextProvider());
table.put(Context.PROVIDER_URL, getContextProvider());
}
Map<String, String> map = getArguments(JMSSampler.JNDI_PROPERTIES).getArgumentsAsMap();
if (LOGGER.isDebugEnabled()) {
if (map.isEmpty()) {
LOGGER.debug("Empty JNDI properties");
} else {
LOGGER.debug("Number of JNDI properties: {}", map.size());
}
}
for (Map.Entry<String, String> me : map.entrySet()) {
table.put(me.getKey(), me.getValue());
}
Context context = new InitialContext(table);
if (LOGGER.isDebugEnabled()) {
printEnvironment(context);
}
return context;
}
Aggregations