Show ThinkcapPlugin.java syntax highlighted
package org.integratedmodelling.thinkcap;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.activation.MimetypesFileTypeMap;
import org.integratedmodelling.thinklab.KnowledgeManager;
import org.integratedmodelling.thinklab.exception.ThinklabException;
import org.integratedmodelling.thinklab.exception.ThinklabIOException;
import org.integratedmodelling.thinklab.exception.ThinklabPluginException;
import org.integratedmodelling.thinklab.plugin.Plugin;
import org.integratedmodelling.utils.CopyURL;
/**
* A specialized plugin that puts any Velocity template and other web resources notified to it where they
* belong. It also implements a do-nothing unload, just because.
*
* Create plugins for Thinkcap from here, and just put .vm files in the jar to make them
* available to commands.
*
* Resource files are scanned and any file recognized as something that could be streamed directly
* in http is cached and made available in a static hash identified by pluginname/filename.ext. The
* StreamResource command can be used in a template to stream such resources. Images, style sheets, js,
* and html files can be anywhere in the plugin hierarchy (the full path will be lost). Applets must be
* in an applet/ subtree in the plugin jar.
*
* @author Ferdinando Villa
*
*/
public abstract class ThinkcapPlugin extends Plugin {
static HashMap<String, File> resourceFiles = new HashMap<String, File>();
static TemplateProcessor templateProcessor = new TemplateProcessor();
String mainCommand = null;
public ThinkcapPlugin() {
super();
}
@Override
public abstract void load(KnowledgeManager arg0, File arg1, File arg2)
throws ThinklabPluginException;
@Override
public void unload(KnowledgeManager arg0) throws ThinklabPluginException {
// TODO Auto-generated method stub
}
/**
* Retrieve resource file from plugin area. Resource file is prefixed by plugin ID as in pluginID/file.ext.
*
* @param imageName
* @return
* @throws IMAPluginException
*/
public static File getResource(String fileName) throws ThinklabPluginException {
File f = resourceFiles.get(fileName);
if (f == null)
throw new ThinklabPluginException("resource file " + f + " not found in plugin area");
return f;
}
/**
* Use this one to retrieve the MIME type of a file. Provided as convenience to plugins.
* @param f
* @return
*/
static public String getMimeType(File f) {
return new MimetypesFileTypeMap().getContentType(f);
}
/**
* If the plugin defines a main template, return it.
* @return anything set with setMainTemplate
*/
public String getMainCommand() {
return mainCommand;
}
/**
* Call this in the load() method, passing the name of the main template desired. If no plugins defines a main template,
* ThinkCap will start with the one in the base package. Just need to pass the template name, making
* sure it's in some plugin.
* @param s
*/
public void setMainCommand(String s) {
mainCommand = s;
}
/**
* just for simplicity
* @return the Thinkcap
*/
public ThinkCap getThinkcap() {
return ThinkcapManager.getThinkcapManager().getThinkcap();
}
public void copyJarToContainerClasspath() throws ThinklabIOException {
getThinkcap().copyJarToClasspath(this.getJarFile());
}
@Override
public void notifyResource(String name, long arg1, long arg2) throws ThinklabException {
if (name.endsWith(".vm")) {
URL tfile = this.exportResourceCached(name);
File f = new File(tfile.getFile());
TemplateProcessor.get().parseTemplate(f);
getThinkcap().checkTemplatePath(f.getParent());
if (name.contains("vmacros/"))
getThinkcap().addMacroFile(name);
/* TODO associate templates with other plugin-wide settings besides style */
if (this.getProperties().containsKey("style"))
getThinkcap().setTemplateStyle(
name,
this.getProperties().getProperty("style"));
} else if (
name.endsWith(".png") ||
name.endsWith(".jpg") ||
name.endsWith(".gif") ||
name.endsWith(".css") ||
name.endsWith(".html") ||
name.endsWith(".mp3") ||
name.endsWith(".zul") ||
name.endsWith(".zhtml") ||
name.endsWith(".dsp") ||
name.endsWith(".js") ||
name.endsWith(".swf") ||
(name.endsWith(".jar") && name.contains("applets/"))
) {
URL uur = exportResourceCached(name);
/* copy in web area under plugin directory so we don't have to stream */
File newf = new File(getThinkcap().getWebFilePath() + "/" + getID() + "/" + name);
/* compare date of resource with cached if existing */
if (!newf.exists() || newf.lastModified() < getResourceTime(name)) {
if (newf.getParentFile() != null)
newf.getParentFile().mkdirs();
CopyURL.copy(uur, newf);
}
/* put away type icon if in system directory. FIXME make this configurable,
* if we really want to.
*/
if ((name.endsWith(".gif") || name.endsWith(".png")) && name.contains("type_icons/")) {
getThinkcap().addTypeIcon(getID() + "/" + name);
}
resourceFiles.put(getID() + "/" + name, new File(uur.getFile()));
}
}
}
See more files for this project here