001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.jcr;
020
021import java.lang.reflect.Method;
022import java.util.Map;
023import javax.jcr.Repository;
024
025public class ExoPlugin extends JCRPlugin<ExoPlugin> {
026
027  @Override
028  public String getName() {
029    return "exo";
030  }
031
032  @Override
033  public String getDisplayName() {
034    return "Exo JCR plugin";
035  }
036
037  @Override
038  public String getUsage() {
039    return "You can use a container bound repository: 'repo use container=portal'";
040  }
041
042  @Override
043  public ExoPlugin getImplementation() {
044    return this;
045  }
046
047  @Override
048  public Repository getRepository(Map<String, String> properties) throws Exception {
049    ClassLoader cl = Thread.currentThread().getContextClassLoader();
050
051    // Get top container
052    Class<?> eXoContainerContextClass = cl.loadClass("org.exoplatform.container.ExoContainerContext");
053    Method getTopContainerMethod = eXoContainerContextClass.getMethod("getTopContainer");
054    Object topContainer = getTopContainerMethod.invoke(null);
055
056    //
057    if (topContainer != null) {
058      String containerName = properties != null ? properties.get("container") : null;
059      Object container;
060      if (containerName != null) {
061        Method getPortalContainerMethod = topContainer.getClass().getMethod("getPortalContainer",
062            String.class);
063        container = getPortalContainerMethod.invoke(topContainer, containerName);
064      } else {
065        container = topContainer;
066      }
067
068      //
069      if (container != null) {
070        Method getComponentInstanceOfTypeMethod = container.getClass().getMethod(
071            "getComponentInstanceOfType", Class.class);
072        Class<?> repositoryServiceClass = Thread.currentThread().getContextClassLoader().loadClass(
073            "org.exoplatform.services.jcr.RepositoryService");
074        Object repositoryService = getComponentInstanceOfTypeMethod.invoke(container,
075            repositoryServiceClass);
076
077        //
078        if (repositoryService != null) {
079          Method getCurrentRepositoryMethod = repositoryService.getClass().getMethod(
080              "getCurrentRepository");
081          return (Repository) getCurrentRepositoryMethod.invoke(repositoryService);
082        }
083      }
084    }
085
086    //
087    return null;
088  }
089}