KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > UserLibraryManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
23 import org.eclipse.jdt.core.IClasspathContainer;
24 import org.eclipse.jdt.core.IClasspathEntry;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.internal.core.util.Util;
29 import org.osgi.service.prefs.BackingStoreException;
30
31 /**
32  *
33  */

34 public class UserLibraryManager implements IEclipsePreferences.IPreferenceChangeListener {
35     
36     public final static String JavaDoc CP_USERLIBRARY_PREFERENCES_PREFIX = JavaCore.PLUGIN_ID+".userLibrary."; //$NON-NLS-1$
37

38     private Map JavaDoc userLibraries;
39
40     public UserLibraryManager() {
41         initialize();
42     }
43         
44     /*
45      * Gets the library for a given name or <code>null</code> if no such library exists.
46      */

47     public synchronized UserLibrary getUserLibrary(String JavaDoc libName) {
48         return (UserLibrary) this.userLibraries.get(libName);
49     }
50     
51     /*
52      * Returns the names of all defined user libraries. The corresponding classpath container path
53      * is the name appended to the CONTAINER_ID.
54      */

55     public synchronized String JavaDoc[] getUserLibraryNames() {
56         Set JavaDoc set = this.userLibraries.keySet();
57         return (String JavaDoc[]) set.toArray(new String JavaDoc[set.size()]);
58     }
59     
60     private void initialize() {
61         this.userLibraries = new HashMap JavaDoc();
62         IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
63         String JavaDoc[] propertyNames;
64         try {
65             propertyNames = instancePreferences.keys();
66         } catch (BackingStoreException e) {
67             Util.log(e, "Exception while initializing user libraries"); //$NON-NLS-1$
68
return;
69         }
70
71         boolean preferencesNeedFlush = false;
72         for (int i = 0, length = propertyNames.length; i < length; i++) {
73             String JavaDoc propertyName = propertyNames[i];
74             if (propertyName.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
75                 String JavaDoc propertyValue = instancePreferences.get(propertyName, null);
76                 if (propertyValue != null) {
77                     String JavaDoc libName= propertyName.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length());
78                     StringReader JavaDoc reader = new StringReader JavaDoc(propertyValue);
79                     UserLibrary library;
80                     try {
81                         library = UserLibrary.createFromString(reader);
82                     } catch (IOException JavaDoc e) {
83                         Util.log(e, "Exception while initializing user library " + libName); //$NON-NLS-1$
84
instancePreferences.remove(propertyName);
85                         preferencesNeedFlush = true;
86                         continue;
87                     }
88                     this.userLibraries.put(libName, library);
89                 }
90             }
91         }
92         if (preferencesNeedFlush) {
93             try {
94                 instancePreferences.flush();
95             } catch (BackingStoreException e) {
96                 Util.log(e, "Exception while flusing instance preferences"); //$NON-NLS-1$
97
}
98         }
99     }
100
101     public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) {
102         String JavaDoc key = event.getKey();
103         if (key.startsWith(CP_USERLIBRARY_PREFERENCES_PREFIX)) {
104             String JavaDoc libName = key.substring(CP_USERLIBRARY_PREFERENCES_PREFIX.length());
105             try {
106                 // find affected projects
107
IPath containerPath = new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(libName);
108                 IJavaProject[] allJavaProjects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
109                 ArrayList JavaDoc affectedProjects = new ArrayList JavaDoc();
110                 for (int i= 0; i < allJavaProjects.length; i++) {
111                     IJavaProject javaProject = allJavaProjects[i];
112                     IClasspathEntry[] entries= javaProject.getRawClasspath();
113                     for (int j= 0; j < entries.length; j++) {
114                         IClasspathEntry entry = entries[j];
115                         if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
116                             if (containerPath.equals(entry.getPath())) {
117                                 affectedProjects.add(javaProject);
118                                 break;
119                             }
120                         }
121                     }
122                 }
123                 
124                 // decode user library
125
String JavaDoc encodedUserLibrary = (String JavaDoc) event.getNewValue();
126                 UserLibrary userLibrary = encodedUserLibrary == null ? null : UserLibrary.createFromString(new StringReader JavaDoc(encodedUserLibrary));
127                 
128                 // update user libraries map
129
if (userLibrary != null) {
130                     this.userLibraries.put(libName, userLibrary);
131                 } else {
132                     this.userLibraries.remove(libName);
133                 }
134                 
135                 // update affected projects
136
int length = affectedProjects.size();
137                 if (length == 0)
138                     return;
139                 IJavaProject[] projects = new IJavaProject[length];
140                 affectedProjects.toArray(projects);
141                 IClasspathContainer[] containers = new IClasspathContainer[length];
142                 if (userLibrary != null) {
143                     UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(libName);
144                     for (int i = 0; i < length; i++) {
145                         containers[i] = container;
146                     }
147                 }
148                 JavaCore.setClasspathContainer(containerPath, projects, containers, null);
149             } catch (IOException JavaDoc e) {
150                 Util.log(e, "Exception while decoding user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$
151
} catch (JavaModelException e) {
152                 Util.log(e, "Exception while setting user library '"+ libName +"'."); //$NON-NLS-1$ //$NON-NLS-2$
153
}
154         }
155     }
156     
157     public synchronized void removeUserLibrary(String JavaDoc libName) {
158         IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
159         String JavaDoc propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName;
160         instancePreferences.remove(propertyName);
161         try {
162             instancePreferences.flush();
163         } catch (BackingStoreException e) {
164             Util.log(e, "Exception while removing user library " + libName); //$NON-NLS-1$
165
}
166         // this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...))
167
}
168     
169     public synchronized void setUserLibrary(String JavaDoc libName, IClasspathEntry[] entries, boolean isSystemLibrary) {
170         IEclipsePreferences instancePreferences = JavaModelManager.getJavaModelManager().getInstancePreferences();
171         String JavaDoc propertyName = CP_USERLIBRARY_PREFERENCES_PREFIX+libName;
172         try {
173             String JavaDoc propertyValue = UserLibrary.serialize(entries, isSystemLibrary);
174             instancePreferences.put(propertyName, propertyValue); // sends out a PreferenceChangeEvent (see preferenceChange(...))
175
} catch (IOException JavaDoc e) {
176             Util.log(e, "Exception while serializing user library " + libName); //$NON-NLS-1$
177
return;
178         }
179         try {
180             instancePreferences.flush();
181         } catch (BackingStoreException e) {
182             Util.log(e, "Exception while saving user library " + libName); //$NON-NLS-1$
183
}
184         // this.userLibraries was updated during the PreferenceChangeEvent (see preferenceChange(...))
185
}
186     
187 }
188
Popular Tags