001    package org.crsh.shell.impl.command;
002    
003    import groovy.lang.Script;
004    import org.crsh.command.GroovyScriptCommand;
005    import org.crsh.command.NoSuchCommandException;
006    import org.crsh.command.ShellCommand;
007    import org.crsh.plugin.PluginContext;
008    import org.crsh.plugin.ResourceKind;
009    
010    import java.security.Principal;
011    
012    /** @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> */
013    public class CRaSH {
014    
015    
016      /** . */
017      final ClassManager<ShellCommand> commands;
018    
019      /** . */
020      final ClassManager<Script> lifecycles;
021    
022      /** . */
023      final PluginContext context;
024    
025      /**
026       * Create a new CRaSH.
027       *
028       * @param context the plugin context
029       * @throws NullPointerException if the context argument is null
030       */
031      public CRaSH(PluginContext context) throws NullPointerException {
032        this.context = context;
033        this.commands = new ClassManager<ShellCommand>(context, ResourceKind.COMMAND, ShellCommand.class, GroovyScriptCommand.class);
034        this.lifecycles = new ClassManager<Script>(context, ResourceKind.LIFECYCLE, Script.class, Script.class);
035      }
036    
037      public CRaSHSession createSession(Principal user) {
038        return new CRaSHSession(this, user);
039      }
040    
041      /**
042       * Returns the plugin context.
043       *
044       * @return the plugin context
045       */
046      public PluginContext getContext() {
047        return context;
048      }
049    
050      /**
051       * Attempt to obtain a command instance. Null is returned when such command does not exist.
052       *
053       * @param name the command name
054       * @return a command instance
055       * @throws org.crsh.command.NoSuchCommandException if an error occured preventing the command creation
056       * @throws NullPointerException if the name argument is null
057       */
058      public ShellCommand getCommand(String name) throws NoSuchCommandException, NullPointerException {
059        return commands.getInstance(name);
060      }
061    }