use of javax.jcr.Repository in project sling by apache.
the class DescriptorHelper method setDescriptor.
public static void setDescriptor(ResourceResolverFactory factory, String key, String value) throws Exception {
ResourceResolver resourceResolver = factory.getServiceResourceResolver(null);
try {
Session session = resourceResolver.adaptTo(Session.class);
if (session == null) {
return;
}
Repository repo = session.getRepository();
//<hack>
// Method setDescriptorMethod = repo.getClass().
// getDeclaredMethod("setDescriptor", String.class, String.class);
// if (setDescriptorMethod!=null) {
// setDescriptorMethod.setAccessible(true);
// setDescriptorMethod.invoke(repo, key, value);
// } else {
// fail("could not get 'setDescriptor' method");
// }
Method getDescriptorsMethod = repo.getClass().getDeclaredMethod("getDescriptors");
if (getDescriptorsMethod == null) {
fail("could not get 'getDescriptors' method");
} else {
getDescriptorsMethod.setAccessible(true);
GenericDescriptors descriptors = (GenericDescriptors) getDescriptorsMethod.invoke(repo);
SimpleValueFactory valueFactory = new SimpleValueFactory();
descriptors.put(key, valueFactory.createValue(value), true, true);
}
//</hack>
//<verify-hack>
assertEquals(value, repo.getDescriptor(key));
//</verify-hack>
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of javax.jcr.Repository in project sling by apache.
the class AbstractSlingRepository2 method login.
/**
* Logs into the repository at the given {@code workspace} with the given
* {@code credentials} and returns the session returned from
* the repository.
* <p>
* This method logs in as a guest if {@code null} credentials are provided.
* The method may be overwritten to implement a different behaviour as
* indicated by the JCR specification for this method to use external
* mechanisms to login instead of leveraging provided credentials.
* <p>
* This method may be overwritten.
*
* @param credentials The {@code Credentials} to use to login. If this is
* {@code null} JCR {@code GuestCredentials} are used to login.
* @param workspace The workspace to access or {@code null} to access the
* {@link #getDefaultWorkspace() default workspace}
* @throws LoginException If login is not possible
* @throws NoSuchWorkspaceException if the desired workspace is not
* available
* @throws RepositoryException If another error occurrs during login
* @see #getNamespaceAwareSession(Session)
*/
@Override
public Session login(Credentials credentials, String workspace) throws LoginException, NoSuchWorkspaceException, RepositoryException {
if (credentials == null) {
credentials = new GuestCredentials();
}
// check the workspace
if (workspace == null) {
workspace = this.getDefaultWorkspace();
}
try {
logger.debug("login: Logging in to workspace '" + workspace + "'");
final Repository repository = this.getRepository();
if (this.getRepository() == null) {
throw new RepositoryException("Sling Repository not ready");
}
final Session session = repository.login(credentials, workspace);
return session;
} catch (final RuntimeException re) {
// repository has already been shut down ...
throw new RepositoryException(re.getMessage(), re);
}
}
use of javax.jcr.Repository in project sling by apache.
the class AbstractSlingRepository2 method getDescriptorValues.
@Override
public Value[] getDescriptorValues(String key) {
Repository repo = getRepository();
if (repo != null) {
return repo.getDescriptorValues(key);
}
logger.error("getDescriptorValues: Repository not available");
return null;
}
use of javax.jcr.Repository in project sling by apache.
the class RepositoryAccessor method getRepository.
/**
* First try to access the Repository via JNDI (unless jndiContext is null),
* and if not successful try RMI.
*
* @param repositoryName JNDI name or RMI URL (must start with "rmi://") of
* the Repository
* @param jndiContext if null, JNDI is not tried
* @return a Repository, or null if not found
*/
public Repository getRepository(String repositoryName, Hashtable<String, Object> jndiContext) {
Repository result = null;
String tried = "";
if (jndiContext == null || jndiContext.size() == 0) {
log.info("jndiContext is null or empty, not trying JNDI");
} else {
log.debug("Trying to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext);
tried += "JNDI ";
final ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
InitialContext initialContext = new InitialContext(jndiContext);
Object repoObject = initialContext.lookup(repositoryName);
if (repoObject instanceof Repository) {
result = (Repository) repoObject;
log.info("Acquired Repository '" + repositoryName + "' via JNDI");
} else if (repoObject instanceof RemoteRepository) {
RemoteRepository remoteRepo = (RemoteRepository) repoObject;
LocalAdapterFactory laf = getLocalAdapterFactory();
result = laf.getRepository(remoteRepo);
log.info("Acquired RemoteRepository '" + repositoryName + "' via JNDI");
} else {
log.info("Repository '" + repositoryName + "' acquired via JDNI " + "does not implement the required interfaces, class=" + repoObject.getClass().getName());
}
} catch (Throwable t) {
log.info("Unable to acquire Repository '" + repositoryName + "' via JNDI, context=" + jndiContext, t);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
if (result == null) {
if (repositoryName == null || !repositoryName.startsWith(RMI_PREFIX)) {
log.info("Repository name does not start with '" + RMI_PREFIX + "', not trying RMI");
} else {
try {
tried += "RMI ";
log.debug("Trying to acquire Repository '" + repositoryName + "' via RMI");
ClientRepositoryFactory crf = getClientRepositoryFactory();
result = crf.getRepository(repositoryName);
log.info("Acquired Repository '" + repositoryName + "' via RMI");
} catch (Throwable t) {
log.info("Unable to acquire Repository '" + repositoryName + "' via RMI", t);
}
}
}
if (result == null) {
log.info("Unable to acquire Repository '" + repositoryName + "', tried " + tried);
}
return result;
}
use of javax.jcr.Repository in project sling by apache.
the class AbstractSlingRepositoryManager method initializeAndRegisterRepositoryService.
private void initializeAndRegisterRepositoryService() {
Throwable t = null;
try {
log.debug("start: calling acquireRepository()");
Repository newRepo = this.acquireRepository();
if (newRepo != null) {
// ensure we really have the repository
log.debug("start: got a Repository");
this.repository = newRepo;
synchronized (this.repoInitLock) {
this.masterSlingRepository = this.create(this.bundleContext.getBundle());
log.debug("start: setting up Loader");
this.loader = new Loader(this.masterSlingRepository, this.bundleContext);
log.debug("start: calling SlingRepositoryInitializer");
try {
executeRepositoryInitializers(this.masterSlingRepository);
} catch (Throwable e) {
t = e;
log.error("Exception in a SlingRepositoryInitializer, SlingRepository service registration aborted", t);
}
log.debug("start: calling registerService()");
this.repositoryService = registerService();
log.debug("start: registerService() successful, registration=" + repositoryService);
}
}
} catch (Throwable e) {
// consider an uncaught problem an error
log.error("start: Uncaught Throwable trying to access Repository, calling stopRepository()", e);
t = e;
} finally {
if (t != null) {
// repository might be partially started, stop anything left
stop();
}
}
}
Aggregations