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 */
019
020 package org.crsh.shell.impl.command;
021
022 import groovy.lang.GroovyClassLoader;
023 import groovy.lang.GroovyCodeSource;
024 import groovy.lang.Script;
025 import org.codehaus.groovy.control.CompilationFailedException;
026 import org.codehaus.groovy.control.CompilerConfiguration;
027 import org.crsh.command.CommandInvoker;
028 import org.crsh.command.GroovyScriptCommand;
029 import org.crsh.command.NoSuchCommandException;
030 import org.crsh.plugin.PluginContext;
031 import org.crsh.plugin.ResourceKind;
032 import org.crsh.shell.ErrorType;
033 import org.crsh.util.TimestampedObject;
034 import org.crsh.vfs.Resource;
035
036 import java.io.UnsupportedEncodingException;
037 import java.util.Map;
038 import java.util.concurrent.ConcurrentHashMap;
039
040 class ClassManager<T> {
041
042 /** . */
043 private final Map<String, TimestampedObject<Class<? extends T>>> classes = new ConcurrentHashMap<String, TimestampedObject<Class<? extends T>>>();
044
045 /** . */
046 private final PluginContext context;
047
048 /** . */
049 private final Class<? extends Script> baseScriptClass;
050
051 /** . */
052 private final CompilerConfiguration config;
053
054 /** . */
055 private final Class<T> baseClass;
056
057 /** . */
058 private final ResourceKind kind;
059
060 ClassManager(PluginContext context, ResourceKind kind, Class<T> baseClass, Class<? extends Script> baseScriptClass) {
061 CompilerConfiguration config = new CompilerConfiguration();
062 config.setRecompileGroovySource(true);
063 config.setScriptBaseClass(GroovyScriptCommand.class.getName());
064
065 //
066 this.context = context;
067 this.baseScriptClass = baseScriptClass;
068 this.config = config;
069 this.baseClass = baseClass;
070 this.kind = kind;
071 }
072
073 Class<? extends T> getClass(String name) throws NoSuchCommandException, NullPointerException {
074 if (name == null) {
075 throw new NullPointerException("No null argument allowed");
076 }
077
078 TimestampedObject<Class<? extends T>> providerRef = classes.get(name);
079
080 //
081 Resource script = context.loadResource(name, kind);
082
083 //
084 if (script != null) {
085 if (providerRef != null) {
086 if (script.getTimestamp() != providerRef.getTimestamp()) {
087 providerRef = null;
088 }
089 }
090
091 //
092 if (providerRef == null) {
093
094 Class<?> clazz;
095 try {
096 GroovyCodeSource gcs = new GroovyCodeSource(new String(script.getContent(), "UTF-8"), name, "/groovy/shell");
097 GroovyClassLoader gcl = new GroovyClassLoader(context.getLoader(), config);
098 clazz = gcl.parseClass(gcs, false);
099 }
100 catch (UnsupportedEncodingException e) {
101 throw new NoSuchCommandException(name, ErrorType.INTERNAL, "Could not compile command script " + name, e);
102 }
103 catch (CompilationFailedException e) {
104 throw new NoSuchCommandException(name, ErrorType.INTERNAL, "Could not compile command script " + name, e);
105 }
106
107 //
108 if (baseClass.isAssignableFrom(clazz)) {
109 Class<? extends T> providerClass = clazz.asSubclass(baseClass);
110 providerRef = new TimestampedObject<Class<? extends T>>(script.getTimestamp(), providerClass);
111 classes.put(name, providerRef);
112 } else {
113 throw new NoSuchCommandException(name, ErrorType.INTERNAL, "Parsed script " + clazz.getName() +
114 " does not implements " + CommandInvoker.class.getName());
115 }
116 }
117 }
118
119 //
120 if (providerRef == null) {
121 return null;
122 }
123
124 //
125 return providerRef.getObject();
126 }
127
128 T getInstance(String name) throws NoSuchCommandException, NullPointerException {
129 Class<? extends T> clazz = getClass(name);
130 if (clazz == null) {
131 return null;
132 }
133
134 //
135 try {
136 return clazz.newInstance();
137 }
138 catch (Exception e) {
139 throw new NoSuchCommandException(name, ErrorType.INTERNAL, "Could not create command " + name + " instance", e);
140 }
141 }
142 }