- /**
- * Import declared node type definitions and register namespaces. Tries to
- * update the node definitions if they have changed. In case of failures an
- * error will be logged but no exception will be thrown.
- */
- protected void prepareDataModel() {
- // importing node def on remote si currently not supported
- if (isRemote())
- return;
-
- Runnable action = new Runnable() {
- public void run() {
- Session session = null;
- try {
- session = login();
- // register namespaces
- if (namespaces.size() > 0) {
- NamespaceHelper namespaceHelper = new NamespaceHelper(
- session);
- namespaceHelper.registerNamespaces(namespaces);
- }
- // load CND files from classpath or as URL
- for (String resUrl : cndFiles) {
- boolean classpath;
- if (resUrl.startsWith("classpath:")) {
- resUrl = resUrl.substring("classpath:".length());
- classpath = true;
- } else if (resUrl.indexOf(':') < 0) {
- classpath = true;
- } else {
- classpath = false;
- }
-
- URL url = classpath ? getClass().getClassLoader()
- .getResource(resUrl) : new URL(resUrl);
-
- Reader reader = null;
- try {
- reader = new InputStreamReader(url.openStream());
- CndImporter
- .registerNodeTypes(reader, session, true);
- } finally {
- IOUtils.closeQuietly(reader);
- }
- }
- } catch (Exception e) {
- log.error(
- "Cannot import node type definitions " + cndFiles,
- e);
- JcrUtils.discardQuietly(session);
- } finally {
- JcrUtils.logoutQuietly(session);
- }
- }
- };
-
- if (systemExecutor != null)
- systemExecutor.execute(action);
- else
- action.run();
- }
-
- /*
- * DELEGATED JCR REPOSITORY METHODS
- */
-
- public String getDescriptor(String key) {
- return getRepository().getDescriptor(key);
- }
-
- public String[] getDescriptorKeys() {
- return getRepository().getDescriptorKeys();
- }
-
- /** Central login method */
- public Session login(Credentials credentials, String workspaceName)
- throws LoginException, NoSuchWorkspaceException,
- RepositoryException {
-
- // retrieve credentials for remote
- if (credentials == null && isRemote()) {
- Authentication authentication = SecurityContextHolder.getContext()
- .getAuthentication();
- if (authentication != null) {
- if (authentication instanceof UsernamePasswordAuthenticationToken) {
- UsernamePasswordAuthenticationToken upat = (UsernamePasswordAuthenticationToken) authentication;
- credentials = new SimpleCredentials(upat.getName(), upat
- .getCredentials().toString().toCharArray());
- } else if ((authentication instanceof SystemAuthentication)
- && remoteSystemCredentials != null) {
- credentials = remoteSystemCredentials;
- }
- }
- }
-
- Session session;
- try {
- session = getRepository().login(credentials, workspaceName);
- } catch (NoSuchWorkspaceException e) {
- if (autocreateWorkspaces && workspaceName != null)
- session = createWorkspaceAndLogsIn(credentials, workspaceName);
- else
- throw e;
- }
- processNewSession(session);
- return session;
- }
-
- public Session login() throws LoginException, RepositoryException {
- return login(null, null);
- }
-
- public Session login(Credentials credentials) throws LoginException,
- RepositoryException {
- return login(credentials, null);
- }
-
- public Session login(String workspaceName) throws LoginException,
- NoSuchWorkspaceException, RepositoryException {
- return login(null, workspaceName);
- }
-
- /** Called after a session has been created, does nothing by default. */
- protected void processNewSession(Session session) {
- }
-
- public Boolean isRemote() {
- return uri != null;
- }
-
- /** Wraps access to the repository, making sure it is available. */
- protected Repository getRepository() {
- if (repository == null) {
- throw new ArgeoException(
- "No repository initialized."
- + " Was the init() method called?"
- + " The dispose() method should also be called on shutdown.");
- }
- return repository;
- }
-
- /**
- * Logs in to the default workspace, creates the required workspace, logs
- * out, logs in to the required workspace.
- */
- protected Session createWorkspaceAndLogsIn(Credentials credentials,
- String workspaceName) throws RepositoryException {
- if (workspaceName == null)
- throw new ArgeoException("No workspace specified.");
- Session session = getRepository().login(credentials);
- session.getWorkspace().createWorkspace(workspaceName);
- session.logout();
- return getRepository().login(credentials, workspaceName);
- }
-
- public boolean isStandardDescriptor(String key) {
- return getRepository().isStandardDescriptor(key);
- }
-
- public boolean isSingleValueDescriptor(String key) {
- return getRepository().isSingleValueDescriptor(key);
- }
-
- public Value getDescriptorValue(String key) {
- return getRepository().getDescriptorValue(key);
- }
-
- public Value[] getDescriptorValues(String key) {
- return getRepository().getDescriptorValues(key);
- }
-