Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
Installer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import java.util.logging.Level;
22 import org.openide.modules.ModuleInstall;
23 import org.openide.util.NbBundle;
24 import org.openide.windows.WindowManager;
30 
38 class Installer extends ModuleInstall {
39 
40  private static final Logger logger = Logger.getLogger(Installer.class.getName());
41  private final static int SERVER_START_RETRIES = 5;
42 
43  @Override
44  public void restored() {
45  //Setup the default KeywordSearch configuration files
46  KeywordSearchSettings.setDefaults();
47 
48  Case.addPropertyChangeListener(new KeywordSearch.CaseChangeListener());
49 
50  final Server server = KeywordSearch.getServer();
51  int retries = SERVER_START_RETRIES;
52 
53  //TODO revise this logic, handle other server types, move some logic to Server class
54  try {
55  //check if running from previous application instance and try to shut down
56  logger.log(Level.INFO, "Checking if server is running"); //NON-NLS
57  if (server.isRunning()) {
58  //TODO this could hang if other type of server is running
59  logger.log(Level.WARNING, "Already a server running on " + server.getCurrentSolrServerPort() //NON-NLS
60  + " port, maybe leftover from a previous run. Trying to shut it down."); //NON-NLS
61  //stop gracefully
62  server.stop();
63  logger.log(Level.INFO, "Re-checking if server is running"); //NON-NLS
64  if (server.isRunning()) {
65  int serverPort = server.getCurrentSolrServerPort();
66  int serverStopPort = server.getCurrentSolrStopPort();
67  logger.log(Level.SEVERE, "There's already a server running on " //NON-NLS
68  + serverPort + " port that can't be shutdown."); //NON-NLS
69  if (!Server.isPortAvailable(serverPort)) {
70  reportPortError(serverPort);
71  } else if (!Server.isPortAvailable(serverStopPort)) {
72  reportStopPortError(serverStopPort);
73  } else {
74  //some other reason
75  reportInitError();
76  }
77 
78  //in this case give up
79 
80  } else {
81  logger.log(Level.INFO, "Old Solr server shutdown successfully."); //NON-NLS
82  //make sure there really isn't a hang Solr process, in case isRunning() reported false
83  server.killSolr();
84  }
85  }
86  } catch (KeywordSearchModuleException e) {
87  logger.log(Level.SEVERE, "Starting server failed, will try to kill. ", e); //NON-NLS
88  server.killSolr();
89  }
90 
91 
92  try {
93  //Ensure no other process is still bound to that port, even if we think solr is not running
94  //Try to bind to the port 4 times at 1 second intervals.
95  //TODO move some of this logic to Server class
96  for (int i = 0; i <= 3; i++) {
97  logger.log(Level.INFO, "Checking if port available."); //NON-NLS
98  if (Server.isPortAvailable(server.getCurrentSolrServerPort())) {
99  logger.log(Level.INFO, "Port available, trying to start server."); //NON-NLS
100  server.start();
101  break;
102  } else if (i == 3) {
103  logger.log(Level.INFO, "No port available, done retrying."); //NON-NLS
104  reportPortError(server.getCurrentSolrServerPort());
105  retries = 0;
106  break;
107  } else {
108  try {
109  Thread.sleep(1000);
110  } catch (InterruptedException iex) {
111  logger.log(Level.WARNING, "Timer interrupted"); //NON-NLS
112  }
113  }
114  }
115  } catch (SolrServerNoPortException npe) {
116  logger.log(Level.SEVERE, "Starting server failed due to no port available. ", npe); //NON-NLS
117  //try to kill it
118 
119  } catch (KeywordSearchModuleException e) {
120  logger.log(Level.SEVERE, "Starting server failed. ", e); //NON-NLS
121  }
122 
123 
124  //retry if needed
125  //TODO this loop may be now redundant
126  while (retries-- > 0) {
127  try {
128  Thread.sleep(1000);
129  } catch (InterruptedException ex) {
130  logger.log(Level.WARNING, "Timer interrupted."); //NON-NLS
131  }
132 
133  try {
134  logger.log(Level.INFO, "Ensuring the server is running, retries remaining: " + retries); //NON-NLS
135  if (!server.isRunning()) {
136  logger.log(Level.WARNING, "Server still not running"); //NON-NLS
137  try {
138  logger.log(Level.WARNING, "Trying to start the server. "); //NON-NLS
139  server.start();
140  } catch (SolrServerNoPortException npe) {
141  logger.log(Level.SEVERE, "Starting server failed due to no port available. ", npe); //NON-NLS
142  }
143  } else {
144  logger.log(Level.INFO, "Server appears now running. "); //NON-NLS
145  break;
146  }
147  } catch (KeywordSearchModuleException ex) {
148  logger.log(Level.SEVERE, "Starting server failed. ", ex); //NON-NLS
149  //retry if has retries
150  }
151 
152  } //end of retry while loop
153 
154 
155  //last check if still not running to report errors
156  try {
157  Thread.sleep(1000);
158  } catch (InterruptedException ex) {
159  logger.log(Level.WARNING, "Timer interrupted."); //NON-NLS
160  }
161  try {
162  logger.log(Level.INFO, "Last check if server is running. "); //NON-NLS
163  if (!server.isRunning()) {
164  logger.log(Level.SEVERE, "Server is still not running. "); //NON-NLS
165  //check if port is taken or some other reason
166  int serverPort = server.getCurrentSolrServerPort();
167  int serverStopPort = server.getCurrentSolrStopPort();
168  if (!Server.isPortAvailable(serverPort)) {
169  reportPortError(serverPort);
170  } else if (!Server.isPortAvailable(serverStopPort)) {
171  reportStopPortError(serverStopPort);
172  } else {
173  //some other reason
174  reportInitError();
175  }
176  }
177  } catch (KeywordSearchModuleException ex) {
178  logger.log(Level.SEVERE, "Starting server failed. ", ex); //NON-NLS
179  reportInitError();
180  }
181 
182 
183  }
184 
185  @Override
186  public boolean closing() {
187  //platform about to close
188 
189  KeywordSearch.getServer().stop();
190 
191  return true;
192  }
193 
194  @Override
195  public void uninstalled() {
196  //module is being unloaded
197  KeywordSearch.getServer().stop();
198 
199  }
200 
201  private void reportPortError(final int curFailPort) {
202  WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
203  @Override
204  public void run() {
205  final String msg = NbBundle.getMessage(this.getClass(), "Installer.reportPortError", curFailPort, Version.getName(), Server.PROPERTIES_CURRENT_SERVER_PORT, Server.PROPERTIES_FILE);
206  MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "Installer.errorInitKsmMsg"), msg);
207  }
208  });
209  }
210 
211  private void reportStopPortError(final int curFailPort) {
212  WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
213  @Override
214  public void run() {
215  final String msg = NbBundle.getMessage(this.getClass(), "Installer.reportStopPortError", curFailPort, Server.PROPERTIES_CURRENT_STOP_PORT, Server.PROPERTIES_FILE);
216  MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "Installer.errorInitKsmMsg"), msg);
217  }
218  });
219  }
220 
221  private void reportInitError() {
222  WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
223  @Override
224  public void run() {
225  final String msg = NbBundle.getMessage(this.getClass(), "Installer.reportInitError", KeywordSearch.getServer().getCurrentSolrServerPort(), Version.getName(), Server.PROPERTIES_CURRENT_SERVER_PORT, Server.PROPERTIES_FILE);
226  MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "Installer.errorInitKsmMsg"), msg);
227 
228  MessageNotifyUtil.Notify.error(NbBundle.getMessage(this.getClass(), "Installer.errorInitKsmMsg"), msg);
229  }
230  });
231  }
232 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.