]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/model/RepoElem.java
+ clean repo model
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / model / RepoElem.java
index 75de44e773820f28a62466737e4b3a0f485a013f..28841c8695aaf51195a6fe881e7af9756ba3dfd6 100644 (file)
 package org.argeo.slc.client.ui.dist.model;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.security.AccessControlException;
 
 import javax.jcr.Credentials;
 import javax.jcr.Node;
-import javax.jcr.Property;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
-import javax.jcr.nodetype.NodeType;
 
+import org.argeo.jcr.ArgeoJcrUtils;
+import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.repo.RepoConstants;
 import org.argeo.slc.repo.RepoUtils;
 import org.argeo.util.security.Keyring;
 
+/**
+ * Abstract a repository. It might be persisted by a node in the current user
+ * home Node or just an URI and a label if user is anonymous
+ */
 public class RepoElem extends DistParentElem {
-       private Node repoNode;
-       private Repository repository;
-       private Credentials credentials;
+       // private final static Log log = LogFactory.getLog(RepoElem.class);
 
        private RepositoryFactory repositoryFactory;
        private Keyring keyring;
+       private Credentials credentials;
+       private Session defaultSession = null;
 
-       @Deprecated
-       public RepoElem(Node repoNode, boolean inHome, boolean isReadOnly) {
-               super(inHome, isReadOnly);
-               this.repoNode = repoNode;
-       }
+       // Defines current repo
+       private Node repoNode = null;
+       private String label;
+       private String uri;
 
-       /** Inject repofactory and keyring to enable lazy init */
-       public RepoElem(Node repoNode, RepositoryFactory repoFactory,
-                       Keyring keyring, boolean inHome, boolean isReadOnly) {
-               super(inHome, isReadOnly);
-               this.repoNode = repoNode;
+       private Repository repository;
+
+       /**
+        * Creates a RepoElement for anonymous user. The {@code RepositoryFactory}
+        * is used to enable lazy initialisation
+        */
+       public RepoElem(RepositoryFactory repoFactory, String uri, String label) {
+               super(label);
                this.repositoryFactory = repoFactory;
-               this.keyring = keyring;
+               this.uri = uri;
+               this.label = label;
        }
 
-       /** Inject repofactory and keyring to enable lazy init */
-       public RepoElem(Node repoNode, RepositoryFactory repoFactory,
-                       Keyring keyring) {
+       /**
+        * Creates a RepoElement for an authenticated user. The
+        * {@code RepositoryFactory} and {@code Keyring} are used to enable lazy
+        * initialisation
+        * 
+        */
+       public RepoElem(RepositoryFactory repoFactory, Keyring keyring,
+                       Node repoNode, String alias) {
+               super(alias);
+               this.label = alias;
+               // label = repoNode.isNodeType(NodeType.MIX_TITLE) ? repoNode
+               // .getProperty(Property.JCR_TITLE).getString() : repoNode
+               // .getName();
                this.repoNode = repoNode;
                this.repositoryFactory = repoFactory;
                this.keyring = keyring;
+               try {
+                       // Initialize this repo information
+                       setInHome(RepoConstants.DEFAULT_JAVA_REPOSITORY_ALIAS
+                                       .equals(repoNode.getName()));
+                       if (inHome())
+                               // Directly log and retrieve children for local repository
+                               login();
+                       else
+                               setReadOnly(!repoNode.hasNode(ArgeoNames.ARGEO_PASSWORD));
+                       uri = JcrUtils.get(repoNode, ArgeoNames.ARGEO_URI);
+               } catch (RepositoryException e) {
+                       throw new SlcException("Unable to " + "initialize repo element", e);
+               }
        }
 
-       @Deprecated
-       public RepoElem(Node repoNode) {
-               this.repoNode = repoNode;
-       }
-
-       /** Lazily connects to repository */
-       protected void connect() {
-               if (repository != null)
+       /** Effective login. Does nothing if the session is already there. */
+       public void login() {
+               if (isConnected())
                        return;
-               repository = RepoUtils.getRepository(repositoryFactory, keyring,
-                               repoNode);
-               credentials = RepoUtils.getRepositoryCredentials(keyring, repoNode);
-       }
 
-       public String getLabel() {
-               try {
-                       if (repoNode.isNodeType(NodeType.MIX_TITLE)) {
-                               return repoNode.getProperty(Property.JCR_TITLE).getString();
-                       } else {
-                               return repoNode.getName();
+               if (repository == null)
+                       if (repoNode == null)
+                               // Anonymous
+                               repository = ArgeoJcrUtils.getRepositoryByUri(
+                                               repositoryFactory, uri);
+                       else {
+                               repository = RepoUtils.getRepository(repositoryFactory,
+                                               keyring, repoNode);
+                               credentials = RepoUtils.getRepositoryCredentials(keyring,
+                                               repoNode);
                        }
+
+               try {
+                       defaultSession = repository.login(credentials);
+                       refreshChildren();
                } catch (RepositoryException e) {
-                       throw new SlcException("Cannot read label of " + repoNode, e);
+                       throw new SlcException("Cannot login repository " + label
+                                       + " with credential " + credentials, e);
                }
        }
 
-       public String toString() {
-               return repoNode.toString();
-       }
-
-       public Object[] getChildren() {
-               connect();
-               Session session = null;
+       protected void refreshChildren() {
                try {
-                       session = repository.login(credentials);
-                       String[] workspaceNames = session.getWorkspace()
+                       // TODO also remove deleted children (only adds for the time being
+                       String[] workspaceNames = defaultSession.getWorkspace()
                                        .getAccessibleWorkspaceNames();
-                       // List<DistributionElem> distributionElems = new
-                       // ArrayList<DistributionElem>();
-                       Map<String, GroupElem> children = new HashMap<String, GroupElem>();
-                       for (String workspaceName : workspaceNames) {
-                               // filter technical workspaces
-                               // FIXME: rely on a more robust rule than just wksp name
-                               if (workspaceName.lastIndexOf('-') > 0) {
-                                       String prefix = workspaceName.substring(0,
-                                                       workspaceName.lastIndexOf('-'));
-                                       if (!repoNode.hasNode(workspaceName))
-                                               repoNode.addNode(workspaceName);
-                                       repoNode.getSession().save();
-                                       if (!children.containsKey(prefix)) {
-                                               children.put(prefix, new GroupElem(RepoElem.this,
-                                                               prefix));
-                                       }
-                                       // FIXME remove deleted workspaces
+                       buildWksp: for (String workspaceName : workspaceNames) {
+                               if (!isWorkspaceVisible(workspaceName))
+                                       continue buildWksp;
+
+                               String prefix = getPrefix(workspaceName);
+                               if (getChildByName(prefix) == null) {
+                                       WkspGroupElem wkspGpElem = new WkspGroupElem(RepoElem.this,
+                                                       prefix);
+                                       addChild(wkspGpElem);
                                }
                        }
-                       return children.values().toArray();
                } catch (RepositoryException e) {
                        throw new SlcException("Cannot list workspaces for " + repoNode, e);
-               } finally {
-                       JcrUtils.logoutQuietly(session);
                }
        }
 
-       public String getRepoPath() {
+       @Override
+       public synchronized void dispose() {
+               JcrUtils.logoutQuietly(defaultSession);
+               super.dispose();
+       }
+
+       private String getPrefix(String workspaceName) {
+               // Here is the tricks - we rely on a "hard coded" convention
+               // Workspace name should be like: name-major.minor
+               if (workspaceName.lastIndexOf(VERSION_SEP) > 0)
+                       return workspaceName.substring(0,
+                                       workspaceName.lastIndexOf(VERSION_SEP));
+               else
+                       return workspaceName;
+       }
+
+       /* Exposes this to the children workspace group */
+       protected boolean isWorkspaceVisible(String wkspName) {
+               Boolean result = true;
+               if (ARGEO_SYSTEM_WKSP.contains(wkspName))
+                       return false;
+               // Add a supplementary check to hide workspace that are not
+               // public to anonymous user
+               if (repoNode == null) {
+                       Session tmpSession = null;
+                       try {
+                               tmpSession = repository.login(wkspName);
+                               try {
+                                       tmpSession.checkPermission("/", "read");
+                               } catch (AccessControlException e) {
+                                       result = false;
+                               }
+                       } catch (RepositoryException e) {
+                               throw new SlcException(
+                                               "Cannot list workspaces for anonymous user", e);
+                       } finally {
+                               JcrUtils.logoutQuietly(tmpSession);
+                       }
+               }
+               return result;
+       }
+
+       /**
+        * Actual call to the
+        * {@link Repository#login(javax.jcr.Credentials, String)} method. To be
+        * overridden.
+        * 
+        * Creates a new session with correct credentials using the information
+        * contained in the corresponding repo node. It provides all UI children
+        * elements an unique entry point to retrieve a new Session. Caller must
+        * close the session when it is not in use anymore.
+        * 
+        */
+       protected Session repositoryLogin(String workspaceName) {
                try {
-                       return repoNode.getPath();
+                       return repository.login(credentials, workspaceName);
                } catch (RepositoryException e) {
-                       throw new SlcException("Cannot get path for " + repoNode, e);
+                       throw new SlcException("Cannot login repository " + label
+                                       + " with credential " + credentials, e);
                }
        }
 
-       public Repository getRepository() {
-               connect();
+       public Boolean isConnected() {
+               if (defaultSession != null && defaultSession.isLive())
+                       return true;
+               else
+                       return false;
+       }
+
+       /** Exposes URI to the current repository */
+       public String getUri() {
+               return uri;
+       }
+
+       public String getRepoNodePath() {
+               if (repoNode == null)
+                       return null;
+               else
+                       try {
+                               return repoNode.getPath();
+                       } catch (RepositoryException e) {
+                               throw new SlcException("Cannot get node path for repository "
+                                               + label, e);
+                       }
+       }
+
+       /**
+        * Exposes the local repoNode that completely define a connection to a
+        * repository (including a set of credentials). Might return null in case of
+        * an anonymous user
+        */
+       protected Node getRepoNode() {
+               return repoNode;
+       }
+
+       protected Repository getRepository() {
                return repository;
        }
 
-       public Credentials getCredentials() {
+       protected Credentials getCredentials() {
                return credentials;
        }
 
-       public Node getRepoNode() {
-               return repoNode;
+       // META INFO
+       public String getDescription() {
+               String desc = label;
+               if (repoNode != null)
+                       desc = label + " (" + uri + ")";
+               return desc;
+       }
+
+       public String getLabel() {
+               return label;
        }
 
-}
+       public String toString() {
+               return repoNode != null ? repoNode.toString() : label;
+       }
+}
\ No newline at end of file