LocalCommand.java

  1. /**
  2.  *
  3.  * Copyright 2005-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.commands;

  18. import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
  19. import org.jxmpp.jid.Jid;

  20. /**
  21.  * Represents a command that can be executed locally from a remote location. This
  22.  * class must be extended to implement an specific ad-hoc command. This class
  23.  * provides some useful tools:<ul>
  24.  *      <li>Node</li>
  25.  *      <li>Name</li>
  26.  *      <li>Session ID</li>
  27.  *      <li>Current Stage</li>
  28.  *      <li>Available actions</li>
  29.  *      <li>Default action</li>
  30.  * </ul><p/>
  31.  * To implement a new command extend this class and implement all the abstract
  32.  * methods. When implementing the actions remember that they could be invoked
  33.  * several times, and that you must use the current stage number to know what to
  34.  * do.
  35.  *
  36.  * @author Gabriel Guardincerri
  37.  */
  38. public abstract class LocalCommand extends AdHocCommand {

  39.     /**
  40.      * The time stamp of first invokation of the command. Used to implement the session timeout.
  41.      */
  42.     private long creationDate;

  43.     /**
  44.      * The unique ID of the execution of the command.
  45.      */
  46.     private String sessionID;

  47.     /**
  48.      * The full JID of the host of the command.
  49.      */
  50.     private Jid ownerJID;

  51.     /**
  52.      * The number of the current stage.
  53.      */
  54.     private int currenStage;

  55.     public LocalCommand() {
  56.         super();
  57.         this.creationDate = System.currentTimeMillis();
  58.         currenStage = -1;
  59.     }

  60.     /**
  61.      * The sessionID is an unique identifier of an execution request. This is
  62.      * automatically handled and should not be called.
  63.      *
  64.      * @param sessionID the unique session id of this execution
  65.      */
  66.     public void setSessionID(String sessionID) {
  67.         this.sessionID = sessionID;
  68.         getData().setSessionID(sessionID);
  69.     }

  70.     /**
  71.      * Returns the session ID of this execution.
  72.      *
  73.      * @return the unique session id of this execution
  74.      */
  75.     public String getSessionID() {
  76.         return sessionID;
  77.     }

  78.     /**
  79.      * Sets the JID of the command host. This is automatically handled and should
  80.      * not be called.
  81.      *
  82.      * @param ownerJID the JID of the owner.
  83.      */
  84.     public void setOwnerJID(Jid ownerJID) {
  85.         this.ownerJID = ownerJID;
  86.     }

  87.     @Override
  88.     public Jid getOwnerJID() {
  89.         return ownerJID;
  90.     }

  91.     /**
  92.      * Returns the date the command was created.
  93.      *
  94.      * @return the date the command was created.
  95.      */
  96.     public long getCreationDate() {
  97.         return creationDate;
  98.     }

  99.     /**
  100.      * Returns true if the current stage is the last one. If it is then the
  101.      * execution of some action will complete the execution of the command.
  102.      * Commands that don't have multiple stages can always return <tt>true</tt>.
  103.      *
  104.      * @return true if the command is in the last stage.
  105.      */
  106.     public abstract boolean isLastStage();

  107.     /**
  108.      * Returns true if the specified requester has permission to execute all the
  109.      * stages of this action. This is checked when the first request is received,
  110.      * if the permission is grant then the requester will be able to execute
  111.      * all the stages of the command. It is not checked again during the
  112.      * execution.
  113.      *
  114.      * @param jid the JID to check permissions on.
  115.      * @return true if the user has permission to execute this action.
  116.      */
  117.     public abstract boolean hasPermission(Jid jid);

  118.     /**
  119.      * Returns the currently executing stage number. The first stage number is
  120.      * 0. During the execution of the first action this method will answer 0.
  121.      *
  122.      * @return the current stage number.
  123.      */
  124.     public int getCurrentStage() {
  125.         return currenStage;
  126.     }

  127.     @Override
  128.     void setData(AdHocCommandData data) {
  129.         data.setSessionID(sessionID);
  130.         super.setData(data);
  131.     }

  132.     /**
  133.      * Increase the current stage number. This is automatically handled and should
  134.      * not be called.
  135.      *
  136.      */
  137.     void incrementStage() {
  138.         currenStage++;
  139.     }

  140.     /**
  141.      * Decrease the current stage number. This is automatically handled and should
  142.      * not be called.
  143.      *
  144.      */
  145.     void decrementStage() {
  146.         currenStage--;
  147.     }
  148. }