it.tidalwave.netbeans.workspacemanager.impl.WorkspaceManagerImpl Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://openbluesky.java.net
* SCM: https://bitbucket.org/tidalwave/openbluesky-src
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.workspacemanager.impl;
import javax.annotation.Nonnull;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.loggerconfiguration.LoggerConfiguration;
import it.tidalwave.netbeans.workspacemanager.WorkspaceManager;
/***********************************************************************************************************************
*
* The implementation of WorkspaceManager.
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class WorkspaceManagerImpl implements WorkspaceManager
{
private static final String CLASS = WorkspaceManagerImpl.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private static final String DEBUG_WORKSPACE_PROPERTY = "it.tidalwave.netbeans.workspacemanager.workspace";
private static final String SCRATCH_WORKSPACE_PROPERTY = "it.tidalwave.netbeans.workspacemanager.scratchWorkspace";
private final String appName = getDefaultApplicationName();
/** The preferences file name. */
private final String mainPropertyFileName = appName + ".properties.xml";
private static final String CACHES_FOLDER_NAME = "Caches";
private static final String CONFIG_FOLDER_NAME = "Config";
/** The folder where the current workspace is stored. */
private final File workspaceFolder;
/** The preference file. */
private File propertyFile;
/** The current properties. */
private final Properties properties = new Properties();
/*******************************************************************************************************************
*
* Creates a new instance and tries to retrieve a workspace path from the
* system preferences and to initialize it.
*
******************************************************************************************************************/
public WorkspaceManagerImpl()
{
String workspaceFolderPath = System.getProperty(DEBUG_WORKSPACE_PROPERTY); // used for debugging
logger.info("Working folder from system properties: %s", workspaceFolderPath);
if (workspaceFolderPath == null)
{
final String userHome = System.getProperty("user.home");
String template = null;
if (Utilities.isMac())
{
template = userHome + "/Library/Application Support/%s";
}
else if (Utilities.isWindows())
{
template = userHome + "/Local Settings/Application Data/%s";
}
else
{
template = userHome + "/.%s";
}
workspaceFolderPath = String.format(template, appName);
}
workspaceFolder = new File(workspaceFolderPath);
logger.info("Working folder: %s", workspaceFolderPath);
if (Boolean.getBoolean(SCRATCH_WORKSPACE_PROPERTY) && (System.getProperty(DEBUG_WORKSPACE_PROPERTY) != null))
{
deleteRecursively(workspaceFolder);
}
createDirectory(workspaceFolder);
initializeWorkspace(workspaceFolder);
// FIXME to force loading, should do by itself
final LoggerConfiguration loggerConfiguration = Lookup.getDefault().lookup(LoggerConfiguration.class);
final File logConfigFile = getConfigFile("log.properties");
loggerConfiguration.initialize(logConfigFile, appName, new File(workspaceFolder, "Logs"));
System.setProperty("netbeans.buildnumber", " [" + workspaceFolder.getName() + "]"); // important for the initial window title
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public File getWorkspaceFolder()
{
return workspaceFolder;
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public File getCacheFolder (final @Nonnull String name)
{
final File caches = new File(workspaceFolder, CACHES_FOLDER_NAME);
createDirectory(caches);
final File folder = new File(caches, name);
createDirectory(folder);
return folder;
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public File getConfigFile (final @Nonnull String name)
{
final File configFolder = new File(workspaceFolder, CONFIG_FOLDER_NAME);
createDirectory(configFolder);
return new File(configFolder, name);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
@Nonnull
public Properties getBootProperties()
{
return properties;
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public void commitBootProperties()
{
saveProperties();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected String getDefaultApplicationName()
{
return NbBundle.getMessage(WorkspaceManagerImpl.class, "ApplicationName");
}
/*******************************************************************************************************************
*
* Initializes the workspace by reading all the properties.
*
******************************************************************************************************************/
private void initializeWorkspace (final @Nonnull File workspaceFolder)
{
createDirectory(workspaceFolder);
propertyFile = new File(workspaceFolder, mainPropertyFileName);
if (propertyFile.exists())
{
loadProperties();
}
saveProperties();
// final String title = "blueMarine - " + workspaceFolder;
// SwingUtilities.invokeLater(new Runnable()
// {
// public void run()
// {
// WindowManager.getDefault().getMainWindow().setTitle(title);
// }
// });
}
/*******************************************************************************************************************
*
* Loads the workspace properties from the property file.
*
******************************************************************************************************************/
private void loadProperties()
{
InputStream is = null;
try
{
is = new FileInputStream(propertyFile);
properties.loadFromXML(is);
}
catch (IOException e)
{
logger.severe("Could not save properties");
logger.throwing(CLASS, "saveProperties()", e);
}
finally
{
IOUtils.closeQuietly(is);
}
}
/*******************************************************************************************************************
*
* Saves the workspace properties in the property file.
*
******************************************************************************************************************/
private void saveProperties()
{
OutputStream os = null;
try
{
os = new FileOutputStream(propertyFile);
properties.storeToXML(os, "");
}
catch (IOException e)
{
logger.severe("Could not save properties");
logger.throwing(CLASS, "saveProperties()", e);
}
finally
{
IOUtils.closeQuietly(os);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private static void deleteRecursively (final @Nonnull File file) // FIXME: use Commons IO instead
{
if (!file.exists())
{
return;
}
if (file.isDirectory())
{
for (File child : file.listFiles())
{
deleteRecursively(child);
}
}
file.delete();
if (file.exists())
{
logger.warning("Could not delete %s", file);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private static void createDirectory (final @Nonnull File folder)
{
if (!folder.mkdirs() && !folder.exists())
{
throw new RuntimeException("Cannot create " + folder);
}
}
}