private void postActionHook() {
      if (LOG.isTraceEnabled()) {
        LOG.trace("postActionHook()...  Task: " + mAction.getName() + "has completed");
      }

      persistActionRecord();

      // Check if the initiator of this action supports scoring...
      Object initiator = mContext.getMapObjectCollection().getObjectByGuid(mAction.getInitiator());
      if (initiator instanceof IScoringAbility) {
        ((IScoringAbility) initiator).score(mAction);
      }
    }
 public void process(WatchedEvent event) {
   if (event.getType() == Event.EventType.NodeChildrenChanged) {
     LOG.debug("Running children changed [" + event.getPath() + "]");
     try {
       getZkRunning();
     } catch (Exception e) {
       e.printStackTrace();
       LOG.error(e);
     }
   } else if (event.getType() == Event.EventType.NodeDeleted) {
     String znodePath = event.getPath();
     LOG.debug("Running znode deleted [" + znodePath + "]");
     try {
       restartServer(znodePath);
     } catch (Exception e) {
       e.printStackTrace();
       LOG.error(e);
     }
   }
 }
 ReadAccessToken(boolean explicit) {
   myExplicit = explicit;
   LOG.assertTrue(
       !Thread.holdsLock(PsiLock.LOCK),
       "Thread must not hold PsiLock while performing readAction");
   try {
     myActionsLock.readLock().acquire();
     if (myExplicit) acquired();
   } catch (InterruptedException e) {
     throw new RuntimeInterruptedException(e);
   }
 }
    @Override
    public void run() {
      try {
        if (myDisposed) {
          return;
        }
        synchronized (LOCK) {
          if (myTask == null) return;
        }

        final Runnable scheduledTask =
            new Runnable() {
              @Override
              public void run() {
                final Runnable task;
                synchronized (LOCK) {
                  task = myTask;
                  if (task == null) return;
                  myTask = null;

                  myRequests.remove(Request.this);
                }

                if (myThreadToUse == ThreadToUse.SWING_THREAD && !isEdt()) {
                  try {
                    SwingUtilities.invokeAndWait(task);
                  } catch (Exception e) {
                    LOG.error(e);
                  }
                } else {
                  try {
                    task.run();
                  } catch (Exception e) {
                    LOG.error(e);
                  }
                }
              }
            };

        if (myModalityState == null) {
          myFuture = myExecutorService.submit(scheduledTask);
        } else {
          final Application app = ApplicationManager.getApplication();
          if (app != null) {
            app.invokeLater(scheduledTask, myModalityState);
          } else {
            SwingUtilities.invokeLater(scheduledTask);
          }
        }
      } catch (Throwable e) {
        LOG.error(e);
      }
    }
    public WriteAccessToken(Class _clazz) {
      clazz = _clazz;
      assertCanRunWriteAction();

      ActivityTracker.getInstance().inc();
      fireBeforeWriteActionStart(_clazz);
      final AtomicBoolean stopped = new AtomicBoolean(false);

      if (ourDumpThreadsOnLongWriteActionWaiting > 0) {
        executeOnPooledThread(
            new Runnable() {
              @Override
              public void run() {
                while (!stopped.get()) {
                  try {
                    Thread.sleep(ourDumpThreadsOnLongWriteActionWaiting);
                    if (!stopped.get()) {
                      PerformanceWatcher.getInstance().dumpThreads(true);
                    }
                  } catch (InterruptedException ignored) {
                  }
                }
              }
            });
      }

      LOG.assertTrue(
          myActionsLock.isWriteLockAcquired(Thread.currentThread())
              || !Thread.holdsLock(PsiLock.LOCK),
          "Thread must not hold PsiLock while performing writeAction");
      try {
        myActionsLock.writeLock().acquire();
        acquired();
      } catch (InterruptedException e) {
        throw new RuntimeInterruptedException(e);
      }
      stopped.set(true);

      myWriteActionsStack.push(_clazz);

      fireWriteActionStarted(_clazz);
    }
    /**
     * The actual wrapped call. Note that all of the action's execution method is wrapped in a
     * try/catch block, so that we can contain any explosive exceptions that occur during operation.
     */
    public String call() throws Exception {
      /*
       * Perform pre-action checks to verify that the initiators and
       * receivers support the action's required IAbilities
       */

      if (!preActionHook()) {
        persistActionRecord();
        return mResult;
      }

      try {
        mAction.preActionHook();
        mResult = mAction.executeCallableAction();
        mAction.postActionHook();
      } catch (Exception e) {
        LOG.error(
            "Exception occured while attempting to perform PreActionHook for action:"
                + e.getLocalizedMessage());
        mResult = "FAILED: EXCEPTION " + e.getLocalizedMessage();
      }
      /*
       * If this is a repeating task, schedule the future task at the
       * appropriate period. Note that we are carrying forward the same
       * mAction, so that internal changes to the action object will have
       * continuity over their entire lifetime.
       *
       * Calling get() or any other waiting block will only work for the
       * first iteration of the task, because the caller would have a
       * reference to the first of many possible ScheduledFuture's... this
       * is alright, because the IAction's behavior is still the same as
       * if it were submitted normally, the Future that the caller got
       * still behaves as if the task were running periodically. No loss
       * for them...
       */
      if (mAction.getPeriod() > 0) {
        mAction.schedule(mAction.getPeriod(), mAction.getTimeUnit());
      }
      postActionHook();
      persistActionRecord();
      return mResult;
    }
    private boolean preActionHook() {
      if (LOG.isTraceEnabled()) {
        LOG.trace("preActionHook()...  Task: " + mAction.getName() + "starting");
      }

      /*
       * If this action was started by the SYSTEM_GUID, we don't do any
       * ability checking on the operation. We may want to expand this out
       * later to include different processes with different abilities,
       * but for now, let's just let the system do anything it wants.
       */
      if (!GuidManager.SYSTEM_GUID.equals(mAction.getInitiator())) {
        Object initiator =
            mContext.getMapObjectCollection().getObjectByGuid(mAction.getInitiator());
        if (initiator == null) {
          initiator = mContext.getRegionCollection().getRegion(mAction.getInitiator());
          if (null == initiator) {
            LOG.warn("Command action using the system guid...was this expected?");
          }
        }

        //  TODO:  This check may not be appropriate here.... How about AI objects?  Moving Zones?
        if (!(initiator instanceof IMapObject) && !(initiator instanceof IMapRegion)) {
          mResult = "FAIL: Initiator is not an IMapObject or IMapRegion";
          return false;
        }

        // Now verify that the initiator supports all of the IAction's
        // required abilities
        if (!supportsRequiredAbilities(
            mAction.getRequiredInitiatorAbilities(), ((IAbility) initiator).getObjectAbilities())) {
          mResult = "FAIL: Initiator doesn't support one of the required IAbilities";
          return false;
        }
      }

      /*
       * Now walk each of the receivers, verifying that they support all
       * of the required methods necessary to support the action. But,
       * recievers might not be in the trackableByGuidList. atm I am
       * trying to use a record that does implement the IAction And if it
       * is the system, just return.
       */
      Iterator<Long> itor = mAction.getReceiver().iterator();
      while (itor.hasNext()) {
        // Verify first that this receiver supports IAbility
        long next = itor.next();
        // if system, then continue to next itoration
        if (next == GuidManager.SYSTEM_GUID) {
          continue;
        }
        Object receiver = mContext.getMapObjectCollection().getObjectByGuid(next);

        // The recever might be found int the
        // m_context.getPlayerSignUpActionCollection.get(next);
        //                if (receiver == null)
        //                    receiver = m_context.getPlayerSignUpActionHashtable().get(next);

        if (!(receiver instanceof IAbility)) {
          mResult = "FAIL: Receiver doesn't support IAbility";
          return false;
        }

        // Now verify that the receiver supports all of the IAction's
        // required abilities
        if (!supportsRequiredAbilities(
            mAction.getRequiredReceiverAbilities(), ((IAbility) receiver).getObjectAbilities())) {
          mResult = "FAIL: Receiver doesn't support one of the required IAbilities";
          return false;
        }
      }
      return true;
    }
    @Override
    public ScriptContext call() throws Exception {
      try {
        Scanner scn = new Scanner(znodePath);
        scn.useDelimiter(":");
        String hostName = scn.next(); // host name
        String instance = scn.next(); // instance
        int infoPort = Integer.parseInt(scn.next()); // UI info port
        long serverStartTimestamp = Long.parseLong(scn.next());
        scn.close();

        // Get the --config property from classpath...it's always first
        // in the classpath
        String cp = System.getProperty("java.class.path");
        scn = new Scanner(cp);
        scn.useDelimiter(":");
        String confDir = scn.next();
        scn.close();
        LOG.debug("conf dir [" + confDir + "]");

        // Get -Dwms.home.dir
        String wmsHome = System.getProperty("wms.home.dir");

        // If stop-wms.sh is executed and WMS_MANAGES_ZK then zookeeper
        // is stopped abruptly.
        // Second scenario is when ZooKeeper fails for some reason
        // regardless of whether WMS
        // manages it. When either happens the WmsServer running znodes
        // still exist in ZooKeeper
        // and we see them at next startup. When they eventually timeout
        // we get node deleted events for a server that no longer
        // exists. So, only recognize
        // WmsServer running znodes that have timestamps after last
        // WmsMaster startup.
        if (serverStartTimestamp > startupTimestamp) {
          scriptContext.setHostName(hostName);
          scriptContext.setScriptName("sys_shell.py");
          if (hostName.equalsIgnoreCase(ia.getCanonicalHostName()))
            scriptContext.setCommand(
                "bin/wms-daemon.sh --config " + confDir + " start server " + instance);
          else
            scriptContext.setCommand(
                "pdsh -w "
                    + hostName
                    + " \"cd "
                    + wmsHome
                    + ";bin/wms-daemon.sh --config "
                    + confDir
                    + " start server "
                    + instance
                    + "\"");

          RetryCounter retryCounter = retryCounterFactory.create();
          while (true) {
            if (scriptContext.getStdOut().length() > 0)
              scriptContext.getStdOut().delete(0, scriptContext.getStdOut().length());
            if (scriptContext.getStdErr().length() > 0)
              scriptContext.getStdErr().delete(0, scriptContext.getStdErr().length());
            LOG.info(
                "Restarting WmsServer ["
                    + hostName
                    + ":"
                    + instance
                    + "], script [ "
                    + scriptContext.toString()
                    + " ]");
            ScriptManager.getInstance().runScript(scriptContext);

            if (scriptContext.getExitCode() == 0) {
              LOG.info("WmsServer [" + hostName + ":" + instance + "] restarted");
              break;
            } else {
              StringBuilder sb = new StringBuilder();
              sb.append("exit code [" + scriptContext.getExitCode() + "]");
              if (!scriptContext.getStdOut().toString().isEmpty())
                sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
              if (!scriptContext.getStdErr().toString().isEmpty())
                sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
              LOG.error(sb.toString());

              if (!retryCounter.shouldRetry()) {
                LOG.error(
                    "WmsServer ["
                        + hostName
                        + ":"
                        + instance
                        + "] restart failed after "
                        + retryCounter.getMaxRetries()
                        + " retries");
                break;
              } else {
                retryCounter.sleepUntilNextRetry();
                retryCounter.useRetry();
              }
            }
          }
        } else {
          LOG.debug(
              "No restart for "
                  + znodePath
                  + "\nbecause WmsServer start time ["
                  + DateFormat.getDateTimeInstance().format(new Date(serverStartTimestamp))
                  + "] was before WmsMaster start time ["
                  + DateFormat.getDateTimeInstance().format(new Date(startupTimestamp))
                  + "]");
        }
      } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
      }

      return scriptContext;
    }
 private void runAThread(ThreadedEntityProcessorWrapper epw, EntityRow rows, String currProcess)
     throws Exception {
   currentEntityProcWrapper.set(epw);
   epw.threadedInit(context);
   initEntity();
   try {
     epw.init(rows);
     DocWrapper docWrapper = this.docWrapper;
     Context.CURRENT_CONTEXT.set(context);
     for (; ; ) {
       if (DocBuilder.this.stop.get()) break;
       try {
         Map<String, Object> arow = epw.nextRow();
         if (arow == null) {
           break;
         } else {
           importStatistics.rowsCount.incrementAndGet();
           if (docWrapper == null && entity.isDocRoot) {
             docWrapper = new DocWrapper();
             context.setDoc(docWrapper);
             DataConfig.Entity e = entity.parentEntity;
             for (EntityRow row = rows;
                 row != null && e != null;
                 row = row.tail, e = e.parentEntity) {
               addFields(e, docWrapper, row.row, epw.resolver);
             }
           }
           if (docWrapper != null) {
             handleSpecialCommands(arow, docWrapper);
             addFields(entity, docWrapper, arow, epw.resolver);
           }
           if (entity.entities != null) {
             EntityRow nextRow = new EntityRow(arow, rows, entity.name);
             for (DataConfig.Entity e : entity.entities) {
               epw.children.get(e).run(docWrapper, currProcess, nextRow);
             }
           }
         }
         if (entity.isDocRoot) {
           LOG.info("a row on docroot" + docWrapper);
           if (!docWrapper.isEmpty()) {
             LOG.info("adding a doc " + docWrapper);
             boolean result = writer.upload(docWrapper);
             docWrapper = null;
             if (result) {
               importStatistics.docCount.incrementAndGet();
             } else {
               importStatistics.failedDocCount.incrementAndGet();
             }
           }
         }
       } catch (DataImportHandlerException dihe) {
         exception = dihe;
         if (dihe.getErrCode() == SKIP_ROW || dihe.getErrCode() == SKIP) {
           importStatistics.skipDocCount.getAndIncrement();
           exception = null; // should not propogate up
           continue;
         }
         if (entity.isDocRoot) {
           if (dihe.getErrCode() == DataImportHandlerException.SKIP) {
             importStatistics.skipDocCount.getAndIncrement();
             exception = null; // should not propogate up
           } else {
             LOG.error(
                 "Exception while processing: " + entity.name + " document : " + docWrapper,
                 dihe);
           }
           if (dihe.getErrCode() == DataImportHandlerException.SEVERE) throw dihe;
         } else {
           // if this is not the docRoot then the execution has happened in the same thread. so
           // propogate up,
           // it will be handled at the docroot
           entityEnded.set(true);
           throw dihe;
         }
         entityEnded.set(true);
       }
     }
   } finally {
     epw.destroy();
     currentEntityProcWrapper.remove();
     Context.CURRENT_CONTEXT.remove();
   }
 }