KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.core.runtime.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.service.datalocation.Location;
17
18 /**
19  * Internal class.
20  */

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