KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > DataArea


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.internal.runtime;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
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 /**
24  * This class can only be used if OSGi plugin is available
25  */

26 public class DataArea {
27     private static final String JavaDoc OPTION_DEBUG = "org.eclipse.equinox.common/debug"; //$NON-NLS-1$;
28

29     /* package */static final String JavaDoc F_META_AREA = ".metadata"; //$NON-NLS-1$
30
/* package */static final String JavaDoc F_PLUGIN_DATA = ".plugins"; //$NON-NLS-1$
31
/* package */static final String JavaDoc F_LOG = ".log"; //$NON-NLS-1$
32
/**
33      * Internal name of the preference storage file (value <code>"pref_store.ini"</code>) in this plug-in's (read-write) state area.
34      */

35     /* package */static final String JavaDoc PREFERENCES_FILE_NAME = "pref_store.ini"; //$NON-NLS-1$
36

37     private IPath location; //The location of the instance data
38
private boolean initialized = false;
39
40     protected void assertLocationInitialized() throws IllegalStateException JavaDoc {
41         if (location != null && initialized)
42             return;
43         Activator activator = Activator.getDefault();
44         if (activator == null)
45             throw new IllegalStateException JavaDoc(CommonMessages.activator_not_available);
46         Location service = activator.getInstanceLocation();
47         if (service == null)
48             throw new IllegalStateException JavaDoc(CommonMessages.meta_noDataModeSpecified);
49         try {
50             URL JavaDoc url = service.getURL();
51             if (url == null)
52                 throw new IllegalStateException JavaDoc(CommonMessages.meta_instanceDataUnspecified);
53             // TODO assume the URL is a file:
54
// Use the new File technique to ensure that the resultant string is
55
// in the right format (e.g., leading / removed from /c:/foo etc)
56
location = new Path(new File JavaDoc(url.getFile()).toString());
57             initializeLocation();
58         } catch (CoreException e) {
59             throw new IllegalStateException JavaDoc(e.getMessage());
60         }
61     }
62
63     public IPath getMetadataLocation() throws IllegalStateException JavaDoc {
64         assertLocationInitialized();
65         return location.append(F_META_AREA);
66     }
67
68     public IPath getInstanceDataLocation() throws IllegalStateException JavaDoc {
69         assertLocationInitialized();
70         return location;
71     }
72
73     public IPath getLogLocation() throws IllegalStateException JavaDoc {
74         FrameworkLog log = Activator.getDefault().getFrameworkLog();
75         if (log == null)
76             return null;
77         return new Path(log.getFile().getAbsolutePath());
78     }
79
80     /**
81      * Returns the read/write location in which the given bundle can manage private state.
82      */

83     public IPath getStateLocation(Bundle bundle) throws IllegalStateException JavaDoc {
84         assertLocationInitialized();
85         return getStateLocation(bundle.getSymbolicName());
86     }
87
88     public IPath getStateLocation(String JavaDoc bundleName) throws IllegalStateException JavaDoc {
89         assertLocationInitialized();
90         return getMetadataLocation().append(F_PLUGIN_DATA).append(bundleName);
91     }
92
93     public IPath getPreferenceLocation(String JavaDoc bundleName, boolean create) throws IllegalStateException JavaDoc {
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         // check if the location can be created
102
if (location.toFile().exists()) {
103             if (!location.toFile().isDirectory()) {
104                 String JavaDoc 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         //try infer the device if there isn't one (windows)
109
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         // append on the metadata location so that the whole structure is created.
117
File JavaDoc file = location.append(F_META_AREA).toFile();
118         try {
119             file.mkdirs();
120         } catch (Exception JavaDoc e) {
121             String JavaDoc 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 JavaDoc 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         // set the log file location now that we created the data area
129
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 JavaDoc 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