KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > BasicLocation


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime.internal.adaptor;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
17 import org.eclipse.osgi.service.datalocation.Location;
18
19 /**
20  * Internal class.
21  */

22 public class BasicLocation implements Location {
23     private static class MockLocker implements Locker {
24         public boolean lock() throws IOException JavaDoc {
25             // locking always successful
26
return true;
27         }
28         public void release() {
29             // nothing to release
30
}
31
32     }
33     private boolean isReadOnly;
34     private URL JavaDoc location = null;
35     private Location parent;
36     private URL JavaDoc defaultValue;
37     private String JavaDoc property;
38
39     // locking related fields
40
private File JavaDoc lockFile;
41     private Locker locker;
42     public static final String JavaDoc PROP_OSGI_LOCKING = "osgi.locking"; //$NON-NLS-1$
43
private static String JavaDoc LOCK_FILENAME = ".metadata/.lock"; //$NON-NLS-1$
44
public static boolean DEBUG;
45
46     private static boolean isRunningWithNio() {
47         try {
48              Class.forName("java.nio.channels.FileLock"); //$NON-NLS-1$
49
} catch (ClassNotFoundException JavaDoc e) {
50             return false;
51         }
52         return true;
53     }
54
55     public static Locker createLocker(File JavaDoc lock, String JavaDoc lockMode) {
56         if (lockMode == null)
57             lockMode = FrameworkProperties.getProperty(PROP_OSGI_LOCKING);
58         
59         if ("none".equals(lockMode)) //$NON-NLS-1$
60
return new MockLocker();
61         
62         if ("java.io".equals(lockMode)) //$NON-NLS-1$
63
return new Locker_JavaIo(lock);
64         
65         if ("java.nio".equals(lockMode)) { //$NON-NLS-1$
66
if (isRunningWithNio()) {
67                 return new Locker_JavaNio(lock);
68             } else {
69                 // TODO should we return null here. NIO was requested but we could not do it...
70
return new Locker_JavaIo(lock);
71             }
72         }
73         
74         // Backup case if an invalid value has been specified
75
if (isRunningWithNio()) {
76             return new Locker_JavaNio(lock);
77         } else {
78             return new Locker_JavaIo(lock);
79         }
80     }
81
82     public BasicLocation(String JavaDoc property, URL JavaDoc 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 JavaDoc getDefault() {
94         return defaultValue;
95     }
96
97     public Location getParentLocation() {
98         return parent;
99     }
100
101     public synchronized URL JavaDoc 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 JavaDoc value, boolean lock) throws IllegalStateException JavaDoc {
116         if (location != null)
117             throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_CANNOT_CHANGE_LOCATION);
118         File JavaDoc file = null;
119         if (value.getProtocol().equalsIgnoreCase("file")) { //$NON-NLS-1$
120
try {
121                 String JavaDoc basePath = new File JavaDoc(value.getFile()).getCanonicalPath();
122                 value = new URL JavaDoc("file:" + basePath); //$NON-NLS-1$
123
} catch (IOException JavaDoc e) {
124                 // do nothing just use the original value
125
}
126             file = new File JavaDoc(value.getFile(), LOCK_FILENAME);
127         }
128         lock = lock && !isReadOnly;
129         if (lock) {
130             try {
131                 if (!lock(file))
132                     return false;
133             } catch (IOException JavaDoc 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 JavaDoc {
149         if (!isSet())
150             return false;
151         return lock(lockFile);
152     }
153
154     private boolean lock(File JavaDoc lock) throws IOException JavaDoc {
155         if (lock == null || isReadOnly)
156             return false;
157
158         File JavaDoc parentFile = new File JavaDoc(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 JavaDoc lock) {
177         if (locker != null)
178             return;
179         String JavaDoc 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