File Source: Pings.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.struts2.editor;
    20  
    21  import org.apache.commons.logging.Log;
    22  import org.apache.commons.logging.LogFactory;
    23  import org.apache.roller.weblogger.WebloggerException;
    24  import org.apache.roller.weblogger.config.PingConfig;
    25  import org.apache.roller.weblogger.business.pings.AutoPingManager;
    26  import org.apache.roller.weblogger.business.pings.PingTargetManager;
    27  import org.apache.roller.weblogger.business.WebloggerFactory;
    28  import org.apache.roller.weblogger.pojos.AutoPing;
    29  import org.apache.roller.weblogger.pojos.PingTarget;
    30  import org.apache.roller.weblogger.business.pings.WeblogUpdatePinger;
    31  import org.apache.xmlrpc.XmlRpcException;
    32  import java.io.IOException;
    33  import java.net.SocketException;
    34  import java.net.UnknownHostException;
    35  import java.util.Collections;
    36  import java.util.HashMap;
    37  import java.util.Iterator;
    38  import java.util.List;
    39  import java.util.Map;
    40  import org.apache.roller.weblogger.pojos.WeblogPermission;
    41  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    42  
    43  
    44  /**
    45   * Actions for setting up automatic ping configuration for a weblog.
    46   */
    47  public class Pings extends UIAction {
    48      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.editor.Pings__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    49      private static Log log = LogFactory.getLog(Pings.class);
    50      
    51      // ping target id to work on
    52      private String pingTargetId = null;
    53      
    54      // ping target object we are working on, if available
    55      private PingTarget pingTarget = null;
    56      
    57      // commong ping targets list
    58      private List commonPingTargets = Collections.EMPTY_LIST;
    59      
    60      // custom ping targets list for weblog
    61      private List customPingTargets = Collections.EMPTY_LIST;
    62      
    63      // track the enabled/disabled status for pings
    64      private Map pingStatus = Collections.EMPTY_MAP;
    65      
    66      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.editor.Pings()
              * 
              *  Presumptions:
              *    init'ed(java.util.Collections.EMPTY_LIST)
              *    init'ed(java.util.Collections.EMPTY_MAP)
              * 
              *  Postconditions:
              *    this.actionName == &"pings"
              *    this.commonPingTargets == java.util.Collections.EMPTY_LIST
              *    (soft) init'ed(this.commonPingTargets)
              *    this.customPingTargets == this.commonPingTargets
              *    this.desiredMenu == &"editor"
              *    this.pageTitle == &"pings.title"
              *    this.pingStatus == java.util.Collections.EMPTY_MAP
              *    (soft) init'ed(this.pingStatus)
              *    this.pingTarget == null
              *    this.pingTargetId == null
              */
    67      public Pings() {
    68          this.actionName = "pings";
    69          this.desiredMenu = "editor";
    70          this.pageTitle = "pings.title";
    71      }
    72      
    73      
    74      // admin perms required
    75      public short requireWeblogPermissions() {
                 /* 
    P/P           *  Method: short requireWeblogPermissions()
                  * 
                  *  Presumptions:
                  *    init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
                  * 
                  *  Postconditions:
                  *    return_value == org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN
                  *    (soft) init'ed(return_value)
                  */
    76          return WeblogPermission.ADMIN;
    77      }
    78      
    79      
    80      public void myPrepare() {
    81          
                 /* 
    P/P           *  Method: void myPrepare()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pingTargetId)
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getPingTargetManager(...)@82 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@82 != null
                  *    org.apache.roller.weblogger.ui.struts2.editor.Pings:getActionWeblog(...)@101 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.commonPingTargets)
                  *    possibly_updated(this.customPingTargets)
                  *    possibly_updated(this.pingTarget)
                  * 
                  *  Test Vectors:
                  *    this.pingTargetId: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.config.PingConfig:getDisallowCustomTargets(...)@96: {1}, {0}
                  */
    82          PingTargetManager pingTargetMgr = WebloggerFactory.getWeblogger().getPingTargetManager();
    83          
    84          // load selected ping target, if possible
    85          if(getPingTargetId() != null) try {
    86              setPingTarget(pingTargetMgr.getPingTarget(getPingTargetId()));
    87          } catch (WebloggerException ex) {
    88              log.error("Error looking up ping target - "+getPingTargetId(), ex);
    89          }
    90          
    91          try {
    92              // load common ping targets list
    93              setCommonPingTargets(pingTargetMgr.getCommonPingTargets());
    94              
    95              // load custom ping targets list for weblog, if applicable
    96              if(!PingConfig.getDisallowCustomTargets()) {
    97                  setCustomPingTargets(pingTargetMgr.getCustomPingTargets(getActionWeblog()));
    98              }
    99              
   100          } catch (WebloggerException ex) {
   101              log.error("Error loading ping target lists for weblog - "+getActionWeblog().getHandle(), ex);
   102              // TODO: i18n
   103              addError("Error loading ping targets");
   104          }
   105      }
   106      
   107      
   108      /*
   109       * Display the common ping targets with page
   110       */
   111      public String execute() {
   112          
   113          // load map of enabled auto pings
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    this.commonPingTargets != null
                  *    this.customPingTargets != null
                  *    (soft) log != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.pingStatus == One-of{old this.pingStatus, &new HashMap(buildIsEnabledMap#1)}
                  *    new HashMap(buildIsEnabledMap#1) num objects == 1
                  */
   114          buildIsEnabledMap();
   115          
   116          return LIST;
   117      }
   118      
   119      
   120      /**
   121       * Enable a ping target.
   122       */
   123      public String enable() {
   124          
                 /* 
    P/P           *  Method: String enable()
                  * 
                  *  Preconditions:
                  *    this.commonPingTargets != null
                  *    this.customPingTargets != null
                  *    init'ed(this.pingTarget)
                  *    (soft) log != null
                  *    (soft) init'ed(this.pingTargetId)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getAutopingManager(...)@126 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@126 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@129 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.pingStatus == One-of{old this.pingStatus, &new HashMap(buildIsEnabledMap#1)}
                  *    new HashMap(buildIsEnabledMap#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    this.pingTarget: Addr_Set{null}, Inverse{null}
                  */
   125          if(getPingTarget() != null) try {
   126              AutoPingManager autoPingMgr = WebloggerFactory.getWeblogger().getAutopingManager();
   127              AutoPing autoPing = new AutoPing(null, getPingTarget(), getActionWeblog());
   128              autoPingMgr.saveAutoPing(autoPing);
   129              WebloggerFactory.getWeblogger().flush();
   130          } catch(Exception ex) {
   131              log.error("Error saving auto ping for target - "+getPingTargetId(), ex);
   132              // TODO: i18n
   133              addError("Error enabling auto ping");
   134          }
   135          
   136          return execute();
   137      }
   138      
   139      
   140      /**
   141       * Disable a ping target.
   142       */
   143      public String disable() {
   144          
                 /* 
    P/P           *  Method: String disable()
                  * 
                  *  Preconditions:
                  *    this.commonPingTargets != null
                  *    this.customPingTargets != null
                  *    init'ed(this.pingTarget)
                  *    (soft) log != null
                  *    (soft) init'ed(this.pingTargetId)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getAutopingManager(...)@146 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@146 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@148 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.pingStatus == One-of{old this.pingStatus, &new HashMap(buildIsEnabledMap#1)}
                  *    new HashMap(buildIsEnabledMap#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    this.pingTarget: Addr_Set{null}, Inverse{null}
                  */
   145          if(getPingTarget() != null) try {
   146              AutoPingManager autoPingMgr = WebloggerFactory.getWeblogger().getAutopingManager();
   147              autoPingMgr.removeAutoPing(getPingTarget(), getActionWeblog());
   148              WebloggerFactory.getWeblogger().flush();
   149          } catch (Exception ex) {
   150              log.error("Error removing auto ping for target - "+getPingTargetId(), ex);
   151              // TODO: i18n
   152              addError("Error disabling auto ping");
   153          }
   154          
   155          return execute();
   156      }
   157      
   158      
   159      /**
   160       * Ping the selected target now.
   161       */
   162      public String pingNow() {
   163          
                 /* 
    P/P           *  Method: String pingNow()
                  * 
                  *  Preconditions:
                  *    this.commonPingTargets != null
                  *    this.customPingTargets != null
                  *    init'ed(this.pingTarget)
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.pings.WeblogUpdatePinger:sendPing(...)@169 != null
                  *    org.apache.roller.weblogger.business.pings.WeblogUpdatePinger_PingResult:getMessage(...)@172 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    this.pingStatus == One-of{old this.pingStatus, &new HashMap(buildIsEnabledMap#1)}
                  *    new HashMap(buildIsEnabledMap#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    this.pingTarget: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@172: {0}, {1..232-1}
                  *    org.apache.roller.weblogger.business.pings.WeblogUpdatePinger_PingResult:getMessage(...)@172: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.business.pings.WeblogUpdatePinger_PingResult:isError(...)@170: {0}, {1}
                  *    org.apache.roller.weblogger.config.PingConfig:getSuspendPingProcessing(...)@165: {0}, {1}
                  */
   164          if(getPingTarget() != null) try {
   165              if (PingConfig.getSuspendPingProcessing()) {
   166                  log.debug("Ping processing is disabled.");
   167                  addError("ping.pingProcessingIsSuspended");
   168              } else {
   169                  WeblogUpdatePinger.PingResult pingResult = WeblogUpdatePinger.sendPing(getPingTarget(), getActionWeblog());
   170                  if (pingResult.isError()) {
   171                      log.debug("Ping Result: " + pingResult);
   172                      if (pingResult.getMessage() != null && pingResult.getMessage().trim().length() > 0) {
   173                          addError("ping.transmittedButError");
   174                          addError(pingResult.getMessage());
   175                      } else {
   176                          addError("ping.transmissionFailed");
   177                      }
   178                  } else {
   179                      addMessage("ping.successful");
   180                  }
   181              }
   182          } catch (IOException ex) {
   183              log.debug(ex);
   184              addError("ping.transmissionFailed");
   185              addSpecificMessages(ex);
   186          } catch (XmlRpcException ex) {
   187              log.debug(ex);
   188              addError("ping.transmissionFailed");
   189              addSpecificMessages(ex);
   190          }
   191          
   192          return execute();
   193      }
   194      
   195      
   196      // some extra error messaging
   197      private void addSpecificMessages(Exception ex) {
                 /* 
    P/P           *  Method: void addSpecificMessages(Exception)
                  */
   198          if (ex instanceof UnknownHostException) {
   199              addError("ping.unknownHost");
   200          } else if (ex instanceof SocketException) {
   201              addError("ping.networkConnectionFailed");
   202          }
   203      }
   204  
   205      
   206      /**
   207       * Private helper to build a map indexed by ping target id with values Boolean.TRUE and Boolean.FALSE
   208       * based on whether the ping target is enabled (has a corresponding auto ping configuration).
   209       */
   210      private void buildIsEnabledMap() {
   211          
                 /* 
    P/P           *  Method: void buildIsEnabledMap()
                  * 
                  *  Preconditions:
                  *    this.commonPingTargets != null
                  *    this.customPingTargets != null
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.FALSE)
                  *    init'ed(java.lang.Boolean.TRUE)
                  *    java.util.Collections.EMPTY_LIST != null
                  *    java.util.Iterator:next(...)@226 != null
                  *    java.util.Iterator:next(...)@233 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    this.pingStatus == One-of{old this.pingStatus, &new HashMap(buildIsEnabledMap#1)}
                  *    new HashMap(buildIsEnabledMap#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@225: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@232: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@240: {0}, {1}
                  *    java.util.Map:get(...)@234: Inverse{null}, Addr_Set{null}
                  *    java.util.Map:get(...)@242: Inverse{null}, Addr_Set{null}
                  *    java.util.Map:size(...)@247: {-231..0}, {1..232-1}
                  */
   212          AutoPingManager autoPingMgr = WebloggerFactory.getWeblogger().getAutopingManager();
   213          
   214          // Build isEnabled map (keyed by ping target id and values Boolean.TRUE/Boolean.FALSE)
   215          Map isEnabled = new HashMap();
   216          
   217          List autopings = Collections.EMPTY_LIST;
   218          try {
   219              autopings = autoPingMgr.getAutoPingsByWebsite(getActionWeblog());
   220          } catch (WebloggerException ex) {
   221              log.error("Error looking up auto pings for weblog - "+getActionWeblog().getHandle(), ex);
   222          }
   223          
   224          // Add the enabled auto ping configs with TRUE
   225          for (Iterator i = autopings.iterator(); i.hasNext();) {
   226              AutoPing autoPing = (AutoPing) i.next();
   227              isEnabled.put(autoPing.getPingTarget().getId(), Boolean.TRUE);
   228          }
   229          
   230          // Somewhat awkward, but the two loops save building a separate combined list.
   231          // Add disabled common ones with FALSE
   232          for (Iterator i = getCommonPingTargets().iterator(); i.hasNext();) {
   233              PingTarget pingTarget = (PingTarget) i.next();
   234              if (isEnabled.get(pingTarget.getId()) == null) {
   235                  isEnabled.put(pingTarget.getId(), Boolean.FALSE);
   236              }
   237          }
   238          
   239          // Add disabled custom ones with FALSE
   240          for (Iterator i = getCustomPingTargets().iterator(); i.hasNext();) {
   241              PingTarget pingTarget = (PingTarget) i.next();
   242              if (isEnabled.get(pingTarget.getId()) == null) {
   243                  isEnabled.put(pingTarget.getId(), Boolean.FALSE);
   244              }
   245          }
   246          
   247          if(isEnabled.size() > 0) {
   248              setPingStatus(isEnabled);
   249          }
   250      }
   251      
   252      
   253      public String getPingTargetId() {
                 /* 
    P/P           *  Method: String getPingTargetId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pingTargetId)
                  * 
                  *  Postconditions:
                  *    return_value == this.pingTargetId
                  *    init'ed(return_value)
                  */
   254          return pingTargetId;
   255      }
   256  
   257      public void setPingTargetId(String pingTargetId) {
                 /* 
    P/P           *  Method: void setPingTargetId(String)
                  * 
                  *  Postconditions:
                  *    this.pingTargetId == pingTargetId
                  *    init'ed(this.pingTargetId)
                  */
   258          this.pingTargetId = pingTargetId;
   259      }
   260  
   261      public PingTarget getPingTarget() {
                 /* 
    P/P           *  Method: PingTarget getPingTarget()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pingTarget)
                  * 
                  *  Postconditions:
                  *    return_value == this.pingTarget
                  *    init'ed(return_value)
                  */
   262          return pingTarget;
   263      }
   264  
   265      public void setPingTarget(PingTarget pingTarget) {
                 /* 
    P/P           *  Method: void setPingTarget(PingTarget)
                  * 
                  *  Postconditions:
                  *    this.pingTarget == pingTarget
                  *    init'ed(this.pingTarget)
                  */
   266          this.pingTarget = pingTarget;
   267      }
   268  
   269      public List getCommonPingTargets() {
                 /* 
    P/P           *  Method: List getCommonPingTargets()
                  * 
                  *  Preconditions:
                  *    init'ed(this.commonPingTargets)
                  * 
                  *  Postconditions:
                  *    return_value == this.commonPingTargets
                  *    init'ed(return_value)
                  */
   270          return commonPingTargets;
   271      }
   272  
   273      public void setCommonPingTargets(List commonPingTargets) {
                 /* 
    P/P           *  Method: void setCommonPingTargets(List)
                  * 
                  *  Postconditions:
                  *    this.commonPingTargets == commonPingTargets
                  *    init'ed(this.commonPingTargets)
                  */
   274          this.commonPingTargets = commonPingTargets;
   275      }
   276  
   277      public List getCustomPingTargets() {
                 /* 
    P/P           *  Method: List getCustomPingTargets()
                  * 
                  *  Preconditions:
                  *    init'ed(this.customPingTargets)
                  * 
                  *  Postconditions:
                  *    return_value == this.customPingTargets
                  *    init'ed(return_value)
                  */
   278          return customPingTargets;
   279      }
   280  
   281      public void setCustomPingTargets(List customPingTargets) {
                 /* 
    P/P           *  Method: void setCustomPingTargets(List)
                  * 
                  *  Postconditions:
                  *    this.customPingTargets == customPingTargets
                  *    init'ed(this.customPingTargets)
                  */
   282          this.customPingTargets = customPingTargets;
   283      }
   284  
   285      public Map getPingStatus() {
                 /* 
    P/P           *  Method: Map getPingStatus()
                  * 
                  *  Preconditions:
                  *    init'ed(this.pingStatus)
                  * 
                  *  Postconditions:
                  *    return_value == this.pingStatus
                  *    init'ed(return_value)
                  */
   286          return pingStatus;
   287      }
   288  
   289      public void setPingStatus(Map pingStatus) {
                 /* 
    P/P           *  Method: void setPingStatus(Map)
                  * 
                  *  Postconditions:
                  *    this.pingStatus == pingStatus
                  *    init'ed(this.pingStatus)
                  */
   290          this.pingStatus = pingStatus;
   291      }
   292  }








SofCheck Inspector Build Version : 2.18479
Pings.java 2009-Jan-02 14:25:20
Pings.class 2009-Sep-04 03:12:45