001    /*
002     * Copyright (C) 2010 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     */
019    
020    package org.crsh.jcr;
021    
022    import groovy.lang.GroovySystem;
023    import groovy.lang.MetaClassRegistry;
024    import org.crsh.plugin.CRaSHPlugin;
025    import org.crsh.plugin.Service;
026    
027    import javax.jcr.Node;
028    import java.beans.IntrospectionException;
029    
030    /**
031     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
032     * @version $Revision$
033     */
034    public class JCRPlugin extends CRaSHPlugin<JCRPlugin> implements Service
035    {
036    
037       /** . */
038       private static final Object LOCK = new Object();
039    
040       /** . */
041       private static boolean integrated = false;
042    
043      @Override
044      public JCRPlugin getImplementation() {
045        return this;
046      }
047    
048      @Override
049       public void init()
050       {
051          // Force integration of node meta class
052          NodeMetaClass.setup();
053    
054          //
055          synchronized (LOCK) {
056            if (!integrated) {
057              try {
058                MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
059                Class<? extends Node> eXoNode = (Class<Node>)Thread.currentThread().getContextClassLoader().loadClass("org.exoplatform.services.jcr.impl.core.NodeImpl");
060                NodeMetaClass mc2 = new NodeMetaClass(registry, eXoNode);
061                mc2.initialize();
062                registry.setMetaClass(eXoNode, mc2);
063              }
064              catch (ClassNotFoundException e) {
065                throw new RuntimeException(e);
066              }
067              catch (IntrospectionException e) {
068                throw new RuntimeException(e);
069              }
070            }
071            integrated = true;
072          }
073       }
074    }