Skip to main content
summaryrefslogtreecommitdiffstats
blob: c819690d39dd9307c758ae97feb6cdccdc82df6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *
 ********************************************************************************/
package org.eclipse.jst.jsf.designtime.internal.symbols;

import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.jsf.common.internal.resource.IResourceLifecycleListener;
import org.eclipse.jst.jsf.common.internal.resource.LifecycleListener;
import org.eclipse.jst.jsf.common.internal.resource.ResourceLifecycleEvent;
import org.eclipse.jst.jsf.common.internal.resource.ResourceLifecycleEvent.EventType;
import org.eclipse.jst.jsf.context.symbol.internal.impl.IMapSourceInfo;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.tld.LoadBundleUtil;

class ResourceBundleMapSource extends AbstractMap implements IMapSourceInfo
{
    private static final String   PROPERTY_QUALIFIER =
    	"org.eclipse.jst.jsf.designtime.internal.jsp"; //$NON-NLS-1$
    private static final String   SESSION_PROPERTY_NAME_PROJECT =
    	"ResourceBundleMapSource"; //$NON-NLS-1$
    private static final QualifiedName  SESSION_PROPERTY_KEY_PROJECT =
        new QualifiedName(PROPERTY_QUALIFIER, SESSION_PROPERTY_NAME_PROJECT);
    private static final Object  STATIC_LOCK = new Object();

    private static IFile    getCachedBundleFile(final IProject project, final String baseName)
    {
        if (project != null)
        {
            final Map<String, BundleFileCacheInfo> bundleFileCache = getBundleFileCache(project);

            if (bundleFileCache != null)
            {
                final BundleFileCacheInfo info = bundleFileCache.get(baseName);
                if (info != null)
                {
                    return info.getFile();
                }
            }
        }

        return null;
    }

    private static class BundleFileCacheInfo
    {
        private final IFile                 _file;
        private final Map<Object,CachedDataItem>    _cachedData;
        public BundleFileCacheInfo(final IFile file)
        {
            super();
            _file = file;
            _cachedData =
                Collections.synchronizedMap(new HashMap<Object, CachedDataItem>());
        }
        public IFile getFile()
        {
            return _file;
        }
        public Object getCachedData(final Object key)
        {
            CachedDataItem item = _cachedData.get(key);
            
            if (item != null)
            {
                return item.getData();
            }
            return null;
        }

        public Object putCachedData(final Object key, final Object value, final long timestamp)
        {
            CachedDataItem item = new CachedDataItem(value, timestamp);
            CachedDataItem oldItem = _cachedData.put(key, item);
            if (oldItem != null)
            {
                return oldItem.getData();
            }
            return null;
        }

        public boolean hasChanged(final Object key, final long timestamp)
        {
            CachedDataItem item = _cachedData.get(key);
            if (item != null)
            {
                return item.getTimestamp() != timestamp;
            }
            return false;
        }

        private static class CachedDataItem
        {
            private final Object        _data;
            private final long          _timestamp;
            public CachedDataItem(Object data, long timestamp)
            {
                super();
                _data = data;
                _timestamp = timestamp;
            }
            public final Object getData()
            {
                return _data;
            }
            public final long getTimestamp()
            {
                return _timestamp;
            }
        }
    }

    private static Map<String, BundleFileCacheInfo> getBundleFileCache(final IProject project)
    {
        synchronized(STATIC_LOCK)
        {
            Map<String, BundleFileCacheInfo> bundleFileCache = null;

            try
            {
                bundleFileCache =
                    (Map<String, BundleFileCacheInfo>) project.getSessionProperty(SESSION_PROPERTY_KEY_PROJECT);

                if (bundleFileCache == null)
                {
                    bundleFileCache = new HashMap<String, BundleFileCacheInfo>();
                    final LifecycleListener listener = new LifecycleListener(project, ResourcesPlugin.getWorkspace());
                    listener.addListener(new IResourceLifecycleListener()
                    {
                        public EventResult acceptEvent(final ResourceLifecycleEvent event)
                        {
                            EventResult result = EventResult.getDefaultEventResult();

                            if (event.getEventType() == EventType.RESOURCE_INACCESSIBLE)
                            {
                                try
                                {
                                	//Bug 283764: this listener may be called more than once - check project not already inaccessible
                                	if (project.isAccessible())
                                	{
	                                    final Map<String, BundleFileCacheInfo> bundleCache =
	                                        (Map<String, BundleFileCacheInfo>) project.getSessionProperty(SESSION_PROPERTY_KEY_PROJECT);
	                                    //This listener may be called more than once - check session property not already nulled
	                                    if (bundleCache != null)
	                                    {
	                                    	bundleCache.clear();
	                                        project.setSessionProperty(SESSION_PROPERTY_KEY_PROJECT, null);
	                                    }
                                	}
                                }
                                catch (final CoreException ce)
                                {
                                    JSFCorePlugin.log("Error clearing bundle file cache", ce); //$NON-NLS-1$
                                }
                                result = EventResult.getDisposeAfterEventResult();
                            }

                            return result;
                        }
                    }
                    );

                    project.setSessionProperty(SESSION_PROPERTY_KEY_PROJECT, bundleFileCache);
                }
            }
            catch (final CoreException ce)
            {
                JSFCorePlugin.log("Error creating bundle file cache", ce); //$NON-NLS-1$
            }

            return bundleFileCache;
        }
    }

    private static IFile createCachedBundleFile(final IProject project,
                                                   final String  resourcePathStr)
                      throws IOException, CoreException
    {
        final IStorage storage =
            LoadBundleUtil.getLoadBundleResource(project, resourcePathStr);

        IFile bundleRes = null;

        if (storage != null
                && storage.getAdapter(IFile.class) != null)
        {
            bundleRes = (IFile) storage.getAdapter(IFile.class);
            // if file is removed, clear the bundle from the store.
            final LifecycleListener listener = new LifecycleListener(bundleRes, ResourcesPlugin.getWorkspace());
            listener.addListener(new IResourceLifecycleListener()
            {
                public EventResult acceptEvent(final ResourceLifecycleEvent event)
                {
                    EventResult result = EventResult.getDefaultEventResult();

                    if (event.getEventType() == EventType.RESOURCE_INACCESSIBLE)
                    {
                        Map<String, BundleFileCacheInfo> bundleFileCache = getBundleFileCache(project);
                        bundleFileCache.remove(resourcePathStr);
                        result = EventResult.getDisposeAfterEventResult();
                    }
                    return result;
                }
            }
            );

            getBundleFileCache(project).put(resourcePathStr, new BundleFileCacheInfo(bundleRes));
            return bundleRes;
        }

        throw new IOException("Bundle "+resourcePathStr+" not found in classpath for project: "+project.getName()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    private Properties                  _resourceBundle; // = null; set on first access or changes
    private final IFile                 _bundleFile;   // the resource
    private final String                _resourcePathStr; // the key used in the file cache
    // as returned by IResource.getModificationStamp()
    // the last time _resourceBundle was loaded
    private long                        _lastModificationStamp;

    ResourceBundleMapSource(final IProject context,
                            final String  resourcePathStr)
                                throws IOException, JavaModelException, CoreException
    {
        IFile cachedBundleFile = getCachedBundleFile(context, resourcePathStr);

        if (cachedBundleFile == null)
        {
            cachedBundleFile = createCachedBundleFile(context, resourcePathStr);
        }

        _bundleFile = cachedBundleFile;
        _resourcePathStr = resourcePathStr;
    }

    private void checkAndRefreshBundle()
    {
        if (_bundleFile.isAccessible())
        {
            if (_resourceBundle == null  // doesn't exist yet
                    // exists but ws is out of sync
                    || !_bundleFile.isSynchronized(IResource.DEPTH_ZERO)
                    // exists but user has changed in workspace
                    || _bundleFile.getModificationStamp() 
                            != _lastModificationStamp)
            {
                InputStream  bundleStream = null;
                try
                {
                    // force refresh if out of sync
                    bundleStream = _bundleFile.getContents(true);
                    _resourceBundle = new Properties();
                    _resourceBundle.load(bundleStream);
                    _lastModificationStamp = _bundleFile.getModificationStamp();
                }
                catch (final CoreException ce)
                {
                    JSFCorePlugin.log("Error refreshing bundle", ce); //$NON-NLS-1$
                }
                catch (final IOException ioe)
                {
                    JSFCorePlugin.log("Error refreshing bundle", ioe); //$NON-NLS-1$
                }
                finally
                {
                    if (bundleStream != null)
                    {
                        try
                        {
                            bundleStream.close();
                        }
                        catch (final IOException ioe)
                        {
                            JSFCorePlugin.log("Error closing bundle", ioe); //$NON-NLS-1$
                        }
                    }
                }
            }
        }
        else
        {
            // bundle no longer exists so remove it
            final Map<String, BundleFileCacheInfo> bundleFileCache = getBundleFileCache(_bundleFile.getProject());

            if (bundleFileCache != null &&
                    bundleFileCache.containsKey(_resourcePathStr))
            {
                bundleFileCache.remove(_resourcePathStr);
            }
            // in either case, clear the bundle entry
            if (_resourceBundle != null)
            {
                _resourceBundle.clear();
                _resourceBundle = null;
            }
        }
    }

    @Override
    public Set entrySet()
    {
        checkAndRefreshBundle();

        if (_resourceBundle == null)
        {
        	return Collections.EMPTY_SET;
        }
        return _resourceBundle.entrySet();
    }

    /**
     * @param key
     * @return the value
     * @see java.util.AbstractMap#get(java.lang.Object)
     * @overrride to optimize for the fact that we are doing a hash get
     */
    //
    @Override
    public Object get(final Object key)
    {
        checkAndRefreshBundle();

        if (_resourceBundle == null)
        {
        	return null;
        }
        return _resourceBundle.get(key);
    }

    public final boolean hasChanged(final Object key)
    {
        final BundleFileCacheInfo cache = getBundleFileCache(
                _bundleFile.getProject()).get(_resourcePathStr);
        if (cache != null)
        {
            return cache.hasChanged(key, _bundleFile.getModificationStamp());
        }
        // return true since if there is nothing cached, the caller will want 
        // to react.
        return true;
    }

    public Object getCachedValue(final Object key)
    {
        final BundleFileCacheInfo cache = getBundleFileCache(
                _bundleFile.getProject()).get(_resourcePathStr);
        if (cache != null)
        {
            return cache.getCachedData(key);
        }
        return null;
    }

    public void putCachedValue(final Object key, final Object value)
    {
        final BundleFileCacheInfo cache = getBundleFileCache(
                _bundleFile.getProject()).get(_resourcePathStr);
        if (cache != null)
        {
            cache.putCachedData(key, value, _lastModificationStamp);
        }
    }
}

Back to the top