package org.eclipse.core.internal.runtime;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
public class DataArea {
private static final String OPTION_DEBUG = "org.eclipse.equinox.common/debug";
private static final String REQUIRES_EXPLICIT_INIT = "osgi.dataAreaRequiresExplicitInit";
static final String F_META_AREA = ".metadata";
static final String F_PLUGIN_DATA = ".plugins";
static final String F_LOG = ".log";
static final String F_TRACE = "trace.log";
static final String PREFERENCES_FILE_NAME = "pref_store.ini";
private IPath location;
private boolean initialized = false;
protected synchronized void assertLocationInitialized() throws IllegalStateException {
if (location != null && initialized)
return;
Activator activator = Activator.getDefault();
if (activator == null)
throw new IllegalStateException(CommonMessages.activator_not_available);
Location service = activator.getInstanceLocation();
if (service == null)
throw new IllegalStateException(CommonMessages.meta_noDataModeSpecified);
boolean explicitInitRequired = Boolean.valueOf(Activator.getContext().getProperty(REQUIRES_EXPLICIT_INIT));
if (explicitInitRequired && !service.isSet()) {
throw new IllegalStateException(CommonMessages.meta_instanceDataUnspecified);
}
try {
URL url = service.getURL();
if (url == null)
throw new IllegalStateException(CommonMessages.meta_instanceDataUnspecified);
location = new Path(new File(url.getFile()).toString());
initializeLocation();
} catch (CoreException e) {
throw new IllegalStateException(e.getMessage());
}
}
public IPath getMetadataLocation() throws IllegalStateException {
assertLocationInitialized();
return location.append(F_META_AREA);
}
public IPath getInstanceDataLocation() throws IllegalStateException {
assertLocationInitialized();
return location;
}
public IPath getLogLocation() throws IllegalStateException {
if (isInstanceLocationSet())
assertLocationInitialized();
FrameworkLog log = Activator.getDefault().getFrameworkLog();
if (log != null) {
java.io.File file = log.getFile();
if (file != null)
return new Path(file.getAbsolutePath());
}
if (location == null)
throw new IllegalStateException(CommonMessages.meta_instanceDataUnspecified);
return location.append(F_META_AREA).append(F_LOG);
}
public IPath getTraceLocation() throws IllegalStateException {
DebugOptions debugOptions = Activator.getDefault().getDebugOptions();
if (debugOptions == null) {
return null;
}
File file = debugOptions.getFile();
if (file == null) {
return null;
}
return new Path(file.getAbsolutePath());
}
private boolean isInstanceLocationSet() {
Activator activator = Activator.getDefault();
if (activator == null)
return false;
Location service = activator.getInstanceLocation();
if (service == null)
return false;
return service.isSet();
}
public IPath getStateLocation(Bundle bundle) throws IllegalStateException {
assertLocationInitialized();
return getStateLocation(bundle.getSymbolicName());
}
public IPath getStateLocation(String bundleName) throws IllegalStateException {
assertLocationInitialized();
return getMetadataLocation().append(F_PLUGIN_DATA).append(bundleName);
}
public IPath getPreferenceLocation(String bundleName, boolean create) throws IllegalStateException {
IPath result = getStateLocation(bundleName);
if (create)
result.toFile().mkdirs();
return result.append(PREFERENCES_FILE_NAME);
}
private void initializeLocation() throws CoreException {
if (location.toFile().exists()) {
if (!location.toFile().isDirectory()) {
String message = NLS.bind(CommonMessages.meta_notDir, location);
throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, null));
}
}
if (location.getDevice() == null)
location = new Path(location.toFile().getAbsolutePath());
createLocation();
initialized = true;
}
private void createLocation() throws CoreException {
File file = location.append(F_META_AREA).toFile();
try {
file.mkdirs();
} catch (Exception e) {
String message = NLS.bind(CommonMessages.meta_couldNotCreate, file.getAbsolutePath());
throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, e));
}
if (!file.canWrite()) {
String message = NLS.bind(CommonMessages.meta_readonly, file.getAbsolutePath());
throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, null));
}
IPath logPath = location.append(F_META_AREA).append(F_LOG);
try {
Activator activator = Activator.getDefault();
if (activator != null) {
FrameworkLog log = activator.getFrameworkLog();
if (log != null)
log.setFile(logPath.toFile(), true);
else if (debug())
System.out.println("ERROR: Unable to acquire log service. Application will proceed, but logging will be disabled.");
}
} catch (IOException e) {
e.printStackTrace();
}
IPath tracePath = location.append(F_META_AREA).append(F_TRACE);
Activator activator = Activator.getDefault();
if (activator != null) {
DebugOptions debugOptions = activator.getDebugOptions();
if (debugOptions != null) {
debugOptions.setFile(tracePath.toFile());
} else {
System.out.println("ERROR: Unable to acquire debug service. Application will proceed, but debugging will be disabled.");
}
}
}
private boolean debug() {
Activator activator = Activator.getDefault();
if (activator == null)
return false;
DebugOptions debugOptions = activator.getDebugOptions();
if (debugOptions == null)
return false;
return debugOptions.getBooleanOption(OPTION_DEBUG, false);
}
}