1 11 package org.eclipse.core.runtime.internal.adaptor; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URL ; 16 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 17 import org.eclipse.osgi.service.datalocation.Location; 18 19 22 public class BasicLocation implements Location { 23 private static class MockLocker implements Locker { 24 public boolean lock() throws IOException { 25 return true; 27 } 28 public void release() { 29 } 31 32 } 33 private boolean isReadOnly; 34 private URL location = null; 35 private Location parent; 36 private URL defaultValue; 37 private String property; 38 39 private File lockFile; 41 private Locker locker; 42 public static final String PROP_OSGI_LOCKING = "osgi.locking"; private static String LOCK_FILENAME = ".metadata/.lock"; public static boolean DEBUG; 45 46 private static boolean isRunningWithNio() { 47 try { 48 Class.forName("java.nio.channels.FileLock"); } catch (ClassNotFoundException e) { 50 return false; 51 } 52 return true; 53 } 54 55 public static Locker createLocker(File lock, String lockMode) { 56 if (lockMode == null) 57 lockMode = FrameworkProperties.getProperty(PROP_OSGI_LOCKING); 58 59 if ("none".equals(lockMode)) return new MockLocker(); 61 62 if ("java.io".equals(lockMode)) return new Locker_JavaIo(lock); 64 65 if ("java.nio".equals(lockMode)) { if (isRunningWithNio()) { 67 return new Locker_JavaNio(lock); 68 } else { 69 return new Locker_JavaIo(lock); 71 } 72 } 73 74 if (isRunningWithNio()) { 76 return new Locker_JavaNio(lock); 77 } else { 78 return new Locker_JavaIo(lock); 79 } 80 } 81 82 public BasicLocation(String property, URL defaultValue, boolean isReadOnly) { 83 super(); 84 this.property = property; 85 this.defaultValue = defaultValue; 86 this.isReadOnly = isReadOnly; 87 } 88 89 public boolean allowsDefault() { 90 return defaultValue != null; 91 } 92 93 public URL getDefault() { 94 return defaultValue; 95 } 96 97 public Location getParentLocation() { 98 return parent; 99 } 100 101 public synchronized URL getURL() { 102 if (location == null && defaultValue != null) 103 setURL(defaultValue, false); 104 return location; 105 } 106 107 public synchronized boolean isSet() { 108 return location != null; 109 } 110 111 public boolean isReadOnly() { 112 return isReadOnly; 113 } 114 115 public synchronized boolean setURL(URL value, boolean lock) throws IllegalStateException { 116 if (location != null) 117 throw new IllegalStateException (EclipseAdaptorMsg.ECLIPSE_CANNOT_CHANGE_LOCATION); 118 File file = null; 119 if (value.getProtocol().equalsIgnoreCase("file")) { try { 121 String basePath = new File (value.getFile()).getCanonicalPath(); 122 value = new URL ("file:" + basePath); } catch (IOException e) { 124 } 126 file = new File (value.getFile(), LOCK_FILENAME); 127 } 128 lock = lock && !isReadOnly; 129 if (lock) { 130 try { 131 if (!lock(file)) 132 return false; 133 } catch (IOException e) { 134 return false; 135 } 136 } 137 lockFile = file; 138 location = LocationHelper.buildURL(value.toExternalForm(), true); 139 if (property != null) 140 FrameworkProperties.setProperty(property, location.toExternalForm()); 141 return lock; 142 } 143 144 public synchronized void setParent(Location value) { 145 parent = value; 146 } 147 148 public synchronized boolean lock() throws IOException { 149 if (!isSet()) 150 return false; 151 return lock(lockFile); 152 } 153 154 private boolean lock(File lock) throws IOException { 155 if (lock == null || isReadOnly) 156 return false; 157 158 File parentFile = new File (lock.getParent()); 159 if (!parentFile.exists()) 160 if (!parentFile.mkdirs()) 161 return false; 162 163 setLocker(lock); 164 if (locker == null) 165 return true; 166 boolean locked = false; 167 try { 168 locked = locker.lock(); 169 return locked; 170 } finally { 171 if (!locked) 172 locker = null; 173 } 174 } 175 176 private void setLocker(File lock) { 177 if (locker != null) 178 return; 179 String lockMode = FrameworkProperties.getProperty(PROP_OSGI_LOCKING); 180 locker = createLocker(lock, lockMode); 181 } 182 183 public synchronized void release() { 184 if (locker != null) 185 locker.release(); 186 } 187 } 188 | Popular Tags |