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