blob: b79323e2ca1713b06f6a03451dcb178f0c83a558 [file] [log] [blame]
Carsten Ziegeler395a7822013-01-28 19:01:22 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.status.impl.webconsole;
20
21
22import java.io.IOException;
23import java.net.URL;
24import java.util.Enumeration;
25import java.util.HashMap;
26import java.util.Locale;
27import java.util.Map;
28import java.util.PropertyResourceBundle;
29import java.util.ResourceBundle;
30
31import org.osgi.framework.Bundle;
32import org.osgi.framework.BundleContext;
33import org.osgi.framework.BundleEvent;
34import org.osgi.framework.BundleListener;
35import org.osgi.framework.Constants;
36
37
38/**
39 * The ResourceBundleManager manages resource bundle instance per OSGi Bundle.
40 * It contains a local cache, for bundles, but when a bundle is being unistalled,
41 * its resources stored in the cache are cleaned up.
42 */
43public class ResourceBundleManager implements BundleListener
44{
45
46 private final BundleContext bundleContext;
47
48 private final Map<Long, ResourceBundle> resourceBundleCaches;
49
50
51 /**
52 * Creates a new object and adds self as a bundle listener
53 *
54 * @param bundleContext the bundle context of the Web Console.
55 */
56 public ResourceBundleManager( final BundleContext bundleContext )
57 {
58 this.bundleContext = bundleContext;
59 this.resourceBundleCaches = new HashMap<Long, ResourceBundle>();
60
61 bundleContext.addBundleListener( this );
62 }
63
64
65 /**
66 * Removes the bundle lister.
67 */
68 public void dispose()
69 {
70 bundleContext.removeBundleListener( this );
71 }
72
73
74 /**
75 * This method is used to retrieve a /cached/ instance of the i18n resource associated
76 * with a given bundle.
77 *
78 * @param provider the bundle, provider of the resources
79 * @param locale the requested locale.
80 */
81 public ResourceBundle getResourceBundle( final Bundle provider ) {
82 ResourceBundle cache;
83 final Long key = new Long( provider.getBundleId() );
84 synchronized ( resourceBundleCaches ) {
85 cache = resourceBundleCaches.get( key );
86 if ( cache == null && !resourceBundleCaches.containsKey(key)) {
87 cache = this.loadResourceBundle(provider);
88 resourceBundleCaches.put( key, cache );
89 }
90 }
91
92 return cache;
93 }
94
95
96 // ---------- BundleListener
97
98 /**
99 * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
100 */
101 public final void bundleChanged( BundleEvent event )
102 {
103 if ( event.getType() == BundleEvent.STOPPED )
104 {
105 final Long key = new Long( event.getBundle().getBundleId() );
106 synchronized ( resourceBundleCaches )
107 {
108 resourceBundleCaches.remove( key );
109 }
110 }
111 }
112
113 private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
114
115 private ResourceBundle loadResourceBundle(final Bundle bundle) {
116 final String path = "_" + DEFAULT_LOCALE.toString(); //$NON-NLS-1$
117 final URL source = ( URL ) getResourceBundleEntries(bundle).get( path );
118 if ( source != null ) {
119 try {
120 return new PropertyResourceBundle( source.openStream() );
121 } catch ( final IOException ignore ) {
122 // ignore
123 }
124 }
125 return null;
126 }
127
128 // TODO : Instead of getting all property files, we could just get the one for the default locale
129 private synchronized Map getResourceBundleEntries(final Bundle bundle)
130 {
131 String file = ( String ) bundle.getHeaders().get( Constants.BUNDLE_LOCALIZATION );
132 if ( file == null )
133 {
134 file = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
135 }
136
137 // remove leading slash
138 if ( file.startsWith( "/" ) ) //$NON-NLS-1$
139 {
140 file = file.substring( 1 );
141 }
142
143 // split path and base name
144 int slash = file.lastIndexOf( '/' );
145 String fileName = file.substring( slash + 1 );
146 String path = ( slash <= 0 ) ? "/" : file.substring( 0, slash ); //$NON-NLS-1$
147
148 HashMap resourceBundleEntries = new HashMap();
149
150 Enumeration locales = bundle.findEntries( path, fileName + "*.properties", false ); //$NON-NLS-1$
151 if ( locales != null )
152 {
153 while ( locales.hasMoreElements() )
154 {
155 URL entry = ( URL ) locales.nextElement();
156
157 // calculate the key
158 String entryPath = entry.getPath();
159 final int start = entryPath.lastIndexOf( '/' ) + 1 + fileName.length(); // path, slash and base name
160 final int end = entryPath.length() - 11; // .properties suffix
161 entryPath = entryPath.substring( start, end );
162
163 // the default language is "name.properties" thus the entry
164 // path is empty and must default to "_"+DEFAULT_LOCALE
165 if (entryPath.length() == 0) {
166 entryPath = "_" + DEFAULT_LOCALE; //$NON-NLS-1$
167 }
168
169 // only add this entry, if the "language" is not provided
170 // by the main bundle or an earlier bound fragment
171 if (!resourceBundleEntries.containsKey( entryPath )) {
172 resourceBundleEntries.put( entryPath, entry );
173 }
174 }
175 }
176
177 return resourceBundleEntries;
178 }
179}