File Source: Install.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.core;
    20  
    21  import java.io.PrintWriter;
    22  import java.io.StringWriter;
    23  import java.sql.Connection;
    24  import java.util.List;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.planet.business.GuicePlanetProvider;
    28  import org.apache.roller.planet.business.PlanetFactory;
    29  import org.apache.roller.planet.business.PlanetProvider;
    30  import org.apache.roller.planet.business.startup.PlanetStartup;
    31  import org.apache.roller.weblogger.WebloggerException;
    32  import org.apache.roller.weblogger.business.WebloggerFactory;
    33  import org.apache.roller.weblogger.business.startup.StartupException;
    34  import org.apache.roller.weblogger.business.startup.WebloggerStartup;
    35  import org.apache.roller.weblogger.config.WebloggerConfig;
    36  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    37  import org.springframework.beans.factory.access.BootstrapException;
    38  
    39  
    40  /**
    41   * Walk user through install process.
    42   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.struts2.core.Install()
          * 
          *  Postconditions:
          *    this.databaseName == &"Unknown"
          *    this.error == 0
          *    this.success == 0
          *    this.messages == null
          *    this.rootCauseException == null
          */
    43  public class Install extends UIAction {
    44      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.core.Install__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    45      private static Log log = LogFactory.getLog(Install.class);
    46      
    47      private static final String DATABASE_ERROR = "database_error";
    48      private static final String CREATE_DATABASE = "create_database";
    49      private static final String UPGRADE_DATABASE = "upgrade_database";
    50      private static final String BOOTSTRAP = "bootstrap";
    51      
    52      private Throwable rootCauseException = null;
    53      private boolean error = false;
    54      private boolean success = false;
    55      private List<String> messages = null;
    56      private String databaseName = "Unknown";
    57      
    58      
    59      public boolean isUserRequired() {
                 /* 
    P/P           *  Method: bool isUserRequired()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
    60          return false;
    61      }
    62      
    63      public boolean isWeblogRequired() {
                 /* 
    P/P           *  Method: bool isWeblogRequired()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
    64          return false;
    65      }
    66      
    67      
    68      public String execute() {
    69          
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.startup.WebloggerStartup:getDatabaseProviderException(...)@75 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"bootstrap",&"upgrade_database",&"create_database",&"database_error",&"success"}
                  *    possibly_updated(this.messages)
                  *    possibly_updated(this.rootCauseException)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.business.WebloggerFactory:isBootstrapped(...)@70: {0}, {1}
                  *    org.apache.roller.weblogger.business.startup.StartupException:getRootCause(...)@76: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.business.startup.WebloggerStartup:getDatabaseProviderException(...)@74: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.business.startup.WebloggerStartup:isDatabaseCreationRequired(...)@88: {0}, {1}
                  *    org.apache.roller.weblogger.business.startup.WebloggerStartup:isDatabaseUpgradeRequired(...)@93: {0}, {1}
                  */
    70          if (WebloggerFactory.isBootstrapped()) {
    71              return SUCCESS;
    72          }
    73          
    74          if (WebloggerStartup.getDatabaseProviderException() != null) {
    75              StartupException se = WebloggerStartup.getDatabaseProviderException();
    76              if (se.getRootCause() != null) {
    77                  rootCauseException = se.getRootCause();
    78              } else {
    79                  rootCauseException = se;
    80              }
    81              messages = se.getStartupLog();
    82              
    83              log.debug("Forwarding to database error page");
    84              setPageTitle("installer.error.connection.pageTitle");
    85              return DATABASE_ERROR;
    86          }   
    87          
    88          if (WebloggerStartup.isDatabaseCreationRequired()) {
    89              log.debug("Forwarding to database table creation page");
    90              setPageTitle("installer.database.creation.pageTitle");
    91              return CREATE_DATABASE;
    92          }
    93          if (WebloggerStartup.isDatabaseUpgradeRequired()) {
    94              log.debug("Forwarding to database table upgrade page");
    95              setPageTitle("installer.database.upgrade.pageTitle");
    96              return UPGRADE_DATABASE;
    97          }
    98          setPageTitle("installer.error.unknown.pageTitle");        
    99          return BOOTSTRAP;
   100      }
   101      
   102      
   103      public String create() {
   104          
                 /* 
    P/P           *  Method: String create()
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"create_database",&"success"}
                  *    possibly_updated(this.error)
                  *    possibly_updated(this.messages)
                  *    possibly_updated(this.success)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.business.WebloggerFactory:isBootstrapped(...)@105: {0}, {1}
                  */
   105          if(WebloggerFactory.isBootstrapped()) {
   106              return SUCCESS;
   107          }
   108          
   109          try {
   110              messages = WebloggerStartup.createDatabase();
   111              
   112              success = true;
   113          } catch (StartupException se) {
   114              error = true;
   115              messages = se.getStartupLog();
   116          }
   117          
   118          setPageTitle("installer.database.creation.pageTitle");
   119          return CREATE_DATABASE;
   120      }
   121      
   122      
   123      public String upgrade() {
   124          
                 /* 
    P/P           *  Method: String upgrade()
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"upgrade_database",&"success"}
                  *    possibly_updated(this.error)
                  *    possibly_updated(this.messages)
                  *    possibly_updated(this.success)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.business.WebloggerFactory:isBootstrapped(...)@125: {0}, {1}
                  */
   125          if(WebloggerFactory.isBootstrapped()) {
   126              return SUCCESS;
   127          }
   128          
   129          try {
   130              messages = WebloggerStartup.upgradeDatabase(true);
   131              
   132              success = true;
   133          } catch (StartupException se) {
   134              error = true;
   135              messages = se.getStartupLog();
   136          }
   137          
   138          setPageTitle("installer.database.upgrade.pageTitle");
   139          return UPGRADE_DATABASE;
   140      }
   141      
   142      
   143      public String bootstrap() {
   144          
                 /* 
    P/P           *  Method: String bootstrap()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@174 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@154 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"success",&"bootstrap"}
                  *    possibly_updated(this.rootCauseException)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.business.WebloggerFactory:isBootstrapped(...)@145: {0}, {1}
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@157: {0}, {1}
                  */
   145          if(WebloggerFactory.isBootstrapped()) {
   146              return SUCCESS;
   147          }
   148          
   149          try {
   150              // trigger bootstrapping process
   151              WebloggerFactory.bootstrap();
   152              
   153              // trigger initialization process
   154              WebloggerFactory.getWeblogger().initialize();
   155              
   156              // also need to do planet if it's configured
   157              if (WebloggerConfig.getBooleanProperty("planet.aggregator.enabled")) {
   158                  
   159                  // Now prepare the core services of planet so we can bootstrap it
   160                  try {
   161                      PlanetStartup.prepare();
   162                  } catch (Throwable ex) {
   163                      log.fatal("Roller Planet startup failed during app preparation", ex);
   164                  }
   165                  
   166                  try {
   167                      // trigger planet bootstrapping process
   168                      // we planet to use our own planet provider for integration
   169                      String guiceModule = WebloggerConfig.getProperty("planet.aggregator.guice.module");
   170                      PlanetProvider provider = new GuicePlanetProvider(guiceModule);
   171                      PlanetFactory.bootstrap(provider);
   172                      
   173                      // and now initialize planet
   174                      PlanetFactory.getPlanet().initialize();
   175                      
   176                  } catch (Throwable t) {
   177                      log.fatal("Roller Planet bootstrapping failed", t);
   178                  }
   179              }
   180  
   181              return SUCCESS;
   182              
   183          } catch (BootstrapException ex) {
   184              rootCauseException = ex;
   185          } catch (WebloggerException ex) {
   186              rootCauseException = ex;
   187          }
   188          
   189          setPageTitle("installer.error.unknown.pageTitle");                
   190          return BOOTSTRAP;
   191      }
   192      
   193      
   194      public String getDatabaseProductName() {
                 /* 
    P/P           *  Method: String getDatabaseProductName()
                  * 
                  *  Presumptions:
                  *    java.sql.Connection:getMetaData(...)@200 != null
                  *    org.apache.roller.weblogger.business.DatabaseProvider:getConnection(...)@199 != null
                  *    org.apache.roller.weblogger.business.startup.WebloggerStartup:getDatabaseProvider(...)@199 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   195          String name = "unknown";
   196          
   197          Connection con = null;
   198          try {
   199              con = WebloggerStartup.getDatabaseProvider().getConnection();
   200              name = con.getMetaData().getDatabaseProductName();
+  201          } catch (Exception intentionallyIgnored) {
   202              // ignored
   203          } finally {
   204              if(con != null) try {
   205                  con.close();
   206              } catch(Exception ex) {}
   207          }
   208          
   209          return name;
   210      }
   211      
   212      public String getProp(String key) {
   213          // Static config only, we don't have database yet
                 /* 
    P/P           *  Method: String getProp(String)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   214          String value = WebloggerConfig.getProperty(key);
   215          return (value == null) ? key : value;
   216      }
   217      
   218      
   219      public Throwable getRootCauseException() {
                 /* 
    P/P           *  Method: Throwable getRootCauseException()
                  * 
                  *  Preconditions:
                  *    init'ed(this.rootCauseException)
                  * 
                  *  Postconditions:
                  *    return_value == this.rootCauseException
                  *    init'ed(return_value)
                  */
   220          return rootCauseException;
   221      }
   222      
   223      public String getRootCauseStackTrace() {
                 /* 
    P/P           *  Method: String getRootCauseStackTrace()
                  * 
                  *  Preconditions:
                  *    init'ed(this.rootCauseException)
                  * 
                  *  Postconditions:
                  *    return_value != null
                  * 
                  *  Test Vectors:
                  *    this.rootCauseException: Addr_Set{null}, Inverse{null}
                  */
   224          String stackTrace = "";
   225          if (getRootCauseException() != null) {
   226              StringWriter sw = new StringWriter();
   227              getRootCauseException().printStackTrace(new PrintWriter(sw));
   228              stackTrace = sw.toString().trim();
   229          }
   230          return stackTrace;
   231      }
   232  
   233      public boolean isUpgradeRequired() {
                 /* 
    P/P           *  Method: bool isUpgradeRequired()
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   234          return WebloggerStartup.isDatabaseUpgradeRequired();
   235      }
   236          
   237      public boolean isError() {
                 /* 
    P/P           *  Method: bool isError()
                  * 
                  *  Preconditions:
                  *    init'ed(this.error)
                  * 
                  *  Postconditions:
                  *    return_value == this.error
                  *    init'ed(return_value)
                  */
   238          return error;
   239      }
   240  
   241      public List<String> getMessages() {
                 /* 
    P/P           *  Method: List getMessages()
                  * 
                  *  Preconditions:
                  *    init'ed(this.messages)
                  * 
                  *  Postconditions:
                  *    return_value == this.messages
                  *    init'ed(return_value)
                  */
   242          return messages;
   243      }
   244  
   245      public String getDatabaseName() {
                 /* 
    P/P           *  Method: String getDatabaseName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.databaseName)
                  * 
                  *  Postconditions:
                  *    return_value == this.databaseName
                  *    init'ed(return_value)
                  */
   246          return databaseName;
   247      }
   248  
   249      public boolean isSuccess() {
                 /* 
    P/P           *  Method: bool isSuccess()
                  * 
                  *  Preconditions:
                  *    init'ed(this.success)
                  * 
                  *  Postconditions:
                  *    return_value == this.success
                  *    init'ed(return_value)
                  */
   250          return success;
   251      }
   252      
   253  }








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