Code Search for Developers
 
 
  

ThinkcapPlugin.java from ThinkCap Collaborative Knowledge Portal at Krugle


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

ThinkCap Collaborative Knowledge Portal

A portal to explore and edit the knowledge contained in a set of ontologies in intuitive ways. Presents a Dictionary view (a Google-like interface), a Thesaurus view (a graphical display with simplified relationships) and a full graphical Concept view.

Project homepage: http://sourceforge.net/projects/thinkcap
Programming language(s): Java,JavaScript,XML
License: other

  basecommands/
    StreamResource.java
    StreamTemplate.java
    ThinkcapMain.java
  exceptions/
    ThinkcapException.java
    ThinkcapWidgetException.java
  portfolio/
    ThinkcapPortfolio.java
  widget/
    HtmlLink.java
    IItemDecorator.java
    IThinkcapWidget.java
    IWidgetPublisher.java
    ImageDecorator.java
    PopupMenu.java
    ThinkcapInputWidget.java
    ThinkcapWidgetManager.java
  PageSpecs.java
  ResultContainer.java
  TemplateProcessor.java
  ThinkCap.java
  ThinkcapAction.java
  ThinkcapAnswer.java
  ThinkcapCommand.java
  ThinkcapManager.java
  ThinkcapPlugin.java
  ThinkcapSession.java
  ThinkcapSessionListener.java