1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URL ; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.framework.log.FrameworkLog; 18 import org.eclipse.osgi.service.datalocation.Location; 19 import org.eclipse.osgi.service.debug.DebugOptions; 20 import org.eclipse.osgi.util.NLS; 21 import org.osgi.framework.Bundle; 22 23 26 public class DataArea { 27 private static final String OPTION_DEBUG = "org.eclipse.equinox.common/debug"; 29 static final String F_META_AREA = ".metadata"; static final String F_PLUGIN_DATA = ".plugins"; static final String F_LOG = ".log"; 35 static final String PREFERENCES_FILE_NAME = "pref_store.ini"; 37 private IPath location; private boolean initialized = false; 39 40 protected void assertLocationInitialized() throws IllegalStateException { 41 if (location != null && initialized) 42 return; 43 Activator activator = Activator.getDefault(); 44 if (activator == null) 45 throw new IllegalStateException (CommonMessages.activator_not_available); 46 Location service = activator.getInstanceLocation(); 47 if (service == null) 48 throw new IllegalStateException (CommonMessages.meta_noDataModeSpecified); 49 try { 50 URL url = service.getURL(); 51 if (url == null) 52 throw new IllegalStateException (CommonMessages.meta_instanceDataUnspecified); 53 location = new Path(new File (url.getFile()).toString()); 57 initializeLocation(); 58 } catch (CoreException e) { 59 throw new IllegalStateException (e.getMessage()); 60 } 61 } 62 63 public IPath getMetadataLocation() throws IllegalStateException { 64 assertLocationInitialized(); 65 return location.append(F_META_AREA); 66 } 67 68 public IPath getInstanceDataLocation() throws IllegalStateException { 69 assertLocationInitialized(); 70 return location; 71 } 72 73 public IPath getLogLocation() throws IllegalStateException { 74 FrameworkLog log = Activator.getDefault().getFrameworkLog(); 75 if (log == null) 76 return null; 77 return new Path(log.getFile().getAbsolutePath()); 78 } 79 80 83 public IPath getStateLocation(Bundle bundle) throws IllegalStateException { 84 assertLocationInitialized(); 85 return getStateLocation(bundle.getSymbolicName()); 86 } 87 88 public IPath getStateLocation(String bundleName) throws IllegalStateException { 89 assertLocationInitialized(); 90 return getMetadataLocation().append(F_PLUGIN_DATA).append(bundleName); 91 } 92 93 public IPath getPreferenceLocation(String bundleName, boolean create) throws IllegalStateException { 94 IPath result = getStateLocation(bundleName); 95 if (create) 96 result.toFile().mkdirs(); 97 return result.append(PREFERENCES_FILE_NAME); 98 } 99 100 private void initializeLocation() throws CoreException { 101 if (location.toFile().exists()) { 103 if (!location.toFile().isDirectory()) { 104 String message = NLS.bind(CommonMessages.meta_notDir, location); 105 throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, null)); 106 } 107 } 108 if (location.getDevice() == null) 110 location = new Path(location.toFile().getAbsolutePath()); 111 createLocation(); 112 initialized = true; 113 } 114 115 private void createLocation() throws CoreException { 116 File file = location.append(F_META_AREA).toFile(); 118 try { 119 file.mkdirs(); 120 } catch (Exception e) { 121 String message = NLS.bind(CommonMessages.meta_couldNotCreate, file.getAbsolutePath()); 122 throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, e)); 123 } 124 if (!file.canWrite()) { 125 String message = NLS.bind(CommonMessages.meta_readonly, file.getAbsolutePath()); 126 throw new CoreException(new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IRuntimeConstants.FAILED_WRITE_METADATA, message, null)); 127 } 128 IPath path = location.append(F_META_AREA).append(F_LOG); 130 try { 131 Activator activator = Activator.getDefault(); 132 if (activator != null) { 133 FrameworkLog log = activator.getFrameworkLog(); 134 if (log != null) 135 log.setFile(path.toFile(), true); 136 else if (debug()) 137 System.out.println("ERROR: Unable to acquire log service. Application will proceed, but logging will be disabled."); 138 } 139 } catch (IOException e) { 140 e.printStackTrace(); 141 } 142 } 143 144 private boolean debug() { 145 Activator activator = Activator.getDefault(); 146 if (activator == null) 147 return false; 148 DebugOptions debugOptions = activator.getDebugOptions(); 149 if (debugOptions == null) 150 return false; 151 return debugOptions.getBooleanOption(OPTION_DEBUG, false); 152 } 153 } 154 | Popular Tags |