All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.crsh.util.AbstractClassCache Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
/*
 * Copyright (C) 2012 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.crsh.util;

import org.crsh.command.CommandCreationException;
import org.crsh.shell.ErrorType;
import org.crsh.vfs.Resource;

import java.io.UnsupportedEncodingException;

public abstract class AbstractClassCache {

  /** . */
  private final ClassFactory classFactory;

  protected AbstractClassCache(ClassFactory classFactory) {
    this.classFactory = classFactory;
  }
  
  protected abstract TimestampedObject> loadClass(String name);

  protected abstract void saveClass(String name, TimestampedObject> clazz);

  protected abstract Resource getResource(String name);

  public TimestampedObject> getClass(String name) throws CommandCreationException, NullPointerException {
    if (name == null) {
      throw new NullPointerException("No null argument allowed");
    }

    TimestampedObject> providerRef = loadClass(name);

    //
    Resource script = getResource(name);

    //
    if (script != null) {
      if (providerRef != null) {
        if (script.getTimestamp() != providerRef.getTimestamp()) {
          providerRef = null;
        }
      }

      //
      if (providerRef == null) {

        //
        String source;
        try {
          source = new String(script.getContent(), "UTF-8");
        }
        catch (UnsupportedEncodingException e) {
          throw new CommandCreationException(name, ErrorType.INTERNAL, "Could not compile command script " + name, e);
        }

        //
        Class clazz = classFactory.parse(name, source);
        providerRef = new TimestampedObject>(script.getTimestamp(), clazz);
        saveClass(name, providerRef);
      }
    }

    //
    if (providerRef == null) {
      return null;
    }

    //
    return providerRef;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy