1 11 package org.eclipse.osgi.framework.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.*; 17 import java.util.ArrayList ; 18 import java.util.Locale ; 19 import org.eclipse.osgi.framework.msg.MessageFormat; 20 import org.eclipse.osgi.service.systembundle.EntryLocator; 21 import org.osgi.framework.*; 22 23 public class FrameworkMessageFormat extends MessageFormat { 24 static private BundleContext context; 25 static private ArrayList msgList = new ArrayList (); 26 static private ServiceListener listener = null; 27 static private EntryLocator systemEntryLocator = null; 28 29 private Class clazz; 30 private String bundleName; 31 32 private FrameworkMessageFormat(String bundleName, Locale locale, Class clazz) { 33 super(bundleName, locale, clazz); 34 this.bundleName = bundleName; 35 this.clazz = clazz; 36 } 37 38 protected void init(final String bundleName, final Locale locale, final Class clazz) { 39 if (systemEntryLocator == null) { 40 super.init(bundleName, locale, clazz); 41 return; 42 } 43 44 URL resourceURL = systemEntryLocator.getProperties(bundleName, locale); 45 if (resourceURL != null) { 46 InputStream resourceStream = null; 47 try { 48 resourceStream = resourceURL.openStream(); 49 bundle = new PropertyResourceBundle(resourceStream); 50 this.locale = locale; 51 return; 52 } catch (IOException e) { 53 } finally { 55 if (resourceStream != null) { 56 try { 57 resourceStream.close(); 58 } catch (IOException e) { 59 } 61 } 62 } 63 } 64 super.init(bundleName, locale, clazz); 66 } 67 68 private void init() { 69 init(bundleName, locale, clazz); 70 } 71 72 static public synchronized void setContext(BundleContext context) { 73 if (context == null) { 74 if (FrameworkMessageFormat.context != null && listener != null) 75 FrameworkMessageFormat.context.removeServiceListener(listener); 76 FrameworkMessageFormat.context = null; 77 } else if (FrameworkMessageFormat.context == null) { 78 FrameworkMessageFormat.context = context; 79 listener = new ResourceFinderListener(); 80 try { 81 FrameworkMessageFormat.context.addServiceListener(listener, "(objectclass=org.eclipse.osgi.service.systembundle.EntryLocator)"); } catch (InvalidSyntaxException e) { 83 } 85 } 86 87 } 88 89 static public synchronized MessageFormat getMessageFormat(String bundleName) { 90 FrameworkMessageFormat msgFormat = new FrameworkMessageFormat(bundleName, Locale.getDefault(), FrameworkMessageFormat.class); 91 msgList.add(msgFormat); 92 return msgFormat; 93 } 94 95 static private synchronized void initMessages() { 96 int size = msgList.size(); 97 for (int i = 0; i < size; i++) { 98 ((FrameworkMessageFormat) msgList.get(i)).init(); 99 } 100 } 101 102 static private class ResourceFinderListener implements ServiceListener { 103 private ServiceReference resourceFinderRef = null; 104 105 public void serviceChanged(ServiceEvent event) { 106 synchronized (FrameworkMessageFormat.class) { 107 int eventType = event.getType(); 108 ServiceReference sr = event.getServiceReference(); 109 switch (eventType) { 110 case ServiceEvent.REGISTERED : 111 if (systemEntryLocator == null) { 112 resourceFinderRef = sr; 113 systemEntryLocator = (EntryLocator) context.getService(sr); 114 initMessages(); 115 } 116 break; 117 case ServiceEvent.UNREGISTERING : 118 if (sr.equals(resourceFinderRef)) { 119 systemEntryLocator = null; 120 initMessages(); 121 context.ungetService(resourceFinderRef); 122 resourceFinderRef = null; 123 } 124 break; 125 } 126 } 127 } 128 } 129 130 } | Popular Tags |