View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.startup.healthcheck;
21  
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.util.Properties;
25  
26  import org.neo4j.server.configuration.Configurator;
27  
28  public class Neo4jPropertiesMustExistRule implements StartupHealthCheckRule
29  {
30      private static final String EMPTY_STRING = "";
31      private boolean passed = false;
32      private boolean ran = false;
33      protected String failureMessage = EMPTY_STRING;
34  
35      public boolean execute(Properties properties) {
36          ran = true;
37  
38          String configFilename = properties.getProperty( Configurator.NEO_SERVER_CONFIG_FILE_KEY);
39  
40          Properties configProperties = new Properties();
41          FileInputStream inputStream = null;
42          try
43          {
44              inputStream = new FileInputStream( configFilename );
45              configProperties.load( inputStream );
46          }
47          catch ( IOException e )
48          {
49              failureMessage = String.format("Failed to load configuration properties from [%s]", configFilename);
50              return false;
51          }
52          finally
53          {
54              if ( inputStream != null )
55              {
56                  try
57                  {
58                      inputStream.close();
59                  }
60                  catch ( IOException e )
61                  {   // Couldn't close it
62                  }
63              }
64          }
65  
66          passed = validateProperties( configProperties );
67          return passed;
68      }
69  
70      protected boolean validateProperties( Properties configProperties )
71      {
72          // default implementation: all OK
73          return true;
74      }
75  
76      public String getFailureMessage() {
77          if(passed) {
78              return EMPTY_STRING;
79          }
80  
81          if(!ran) {
82              return String.format("%s has not been run", getClass().getName());
83          } else {
84              return failureMessage;
85          }
86      }
87  }